payment.php

Go to the documentation of this file.
00001 <?php
00002 
00007 class Payment extends oxUBase
00008 {
00013     protected $_oPaymentList = null;
00014 
00019     protected $_iPaymentCnt = null;
00020 
00025     protected $_aAllSets = null;
00026 
00031     protected $_iAllSetsCnt = null;
00032 
00037     protected $_oEmptyPayment = null;
00038 
00043     protected $_sPaymentError = null;
00044 
00049     protected $_sPaymentErrorText = null;
00050 
00055     protected $_aDynValue = null;
00056 
00061     protected $_sCheckedId = null;
00062 
00067     protected $_sCheckedPaymentId = null;
00068 
00073     protected $_aCreditYears = null;
00074 
00079     protected $_sThisTemplate = 'payment.tpl';
00080 
00085     protected $_blIsOrderStep = true;
00086 
00092     public function init()
00093     {
00094         $this->_filterDynData();
00095 
00096         parent::init();
00097 
00098         if ( ( $soxAddressId = oxConfig::getParameter( 'oxaddressid' ) ) ) {
00099             oxSession::setVar( 'deladdrid', $soxAddressId );
00100         }
00101     }
00102 
00119     public function render()
00120     {
00121         parent::render();
00122 
00123         $myConfig  = $this->getConfig();
00124 
00125         //if it happens that you are not in SSL
00126         //then forcing to HTTPS
00127 
00128         //but first checking maybe there were redirection already to prevent infinite redirections due to possible buggy ssl detection on server
00129         $blAlreadyRedirected = oxConfig::getParameter( 'sslredirect' ) == 'forced';
00130 
00131         if ( $myConfig->getCurrentShopURL() != $myConfig->getSSLShopURL() && !$blAlreadyRedirected && !oxConfig::getParameter('fnc') ) {
00132             $sPayError = oxConfig::getParameter( 'payerror' )?'payerror='.oxConfig::getParameter( 'payerror' ):'';
00133             $sPayErrorText = oxConfig::getParameter('payerrortext')?'payerrortext='.oxConfig::getParameter( 'payerrortext' ):'';
00134             $sRedirectURL = $myConfig->getShopSecureHomeURL().'sslredirect=forced&cl=payment&'.$sPayError."&".$sPayErrorText;
00135             oxUtils::getInstance()->redirect( $sRedirectURL );
00136         }
00137 
00138         //additional check if we really really have a user now
00139         //and the basket is not empty
00140         $oUser = $this->getUser();
00141         $oBasket = $this->getSession()->getBasket();
00142         if ( !$oBasket || !$oUser || ( $oBasket && !$oBasket->getProductsCount() ) ) {
00143             oxUtils::getInstance()->redirect( $myConfig->getShopHomeURL() .'cl=start' );
00144         }
00145 
00146         // passing payments to view
00147         $this->_aViewData[ 'payments' ] = $this->getPaymentList();
00148 
00149         // #955A. must recalculate count
00150         $this->_aViewData['allShipsetsCnt'] = $this->getAllSetsCnt();
00151         $this->_aViewData['allShipsets']    = $this->getAllSets();
00152 
00153         if ( !$this->getAllSetsCnt() ) {
00154             // no fitting shipping set found, setting default empty payment
00155             $this->_setDefaultEmptyPayment();
00156             oxSession::setVar( 'sShipSet', null );
00157         }
00158 
00159         $this->_unsetPaymentErrors();
00160 
00161         $this->_aViewData['oxemptypayment'] = $this->getEmptyPayment();
00162         $this->_aViewData['payerror']       = $this->getPaymentError();
00163         $this->_aViewData['payerrortext']   = $this->getPaymentErrorText();
00164 
00165         $this->_aViewData['dynvalue']  = $this->getDynValue();
00166 
00167         // get checked payment ID
00168         $this->_aViewData['defpaymentid'] = $this->getCheckedPaymentId();
00169         $this->_aViewData['paymencnt']    = $this->getPaymentCnt();
00170 
00171         //add a array with current years for the credit card drop down box
00172         $this->_aViewData['creditYears'] = $this->getCreditYears();
00173 
00174         return $this->_sThisTemplate;
00175     }
00176 
00184     protected function _setDefaultEmptyPayment()
00185     {
00186         // no shipping method there !!
00187         if ( $this->getConfig()->getConfigParam( 'blOtherCountryOrder' ) ) {
00188             $oPayment = oxNew( 'oxpayment' );
00189             if ( $oPayment->load( 'oxempty' ) ) {
00190                 $this->_oEmptyPayment = $oPayment;
00191             } else {
00192                 // some error with setup ??
00193                 $this->_sPaymentError = -2;
00194             }
00195         } else {
00196             $this->_sPaymentError = -2;
00197         }
00198     }
00199 
00205     protected function _unsetPaymentErrors()
00206     {
00207         $iPayError     = oxConfig::getParameter( 'payerror' );
00208         $iPayErrorText = oxConfig::getParameter( 'payerrortext' );
00209 
00210         if ( $iPayError ) {
00211             oxSession::deleteVar( 'payerror' );
00212             $this->_sPaymentError = $iPayError;
00213             //QWERTY
00214         }
00215         if ( $iPayErrorText ) {
00216             oxSession::deleteVar( 'payerrortext' );
00217             $this->_sPaymentErrorText = $iPayErrorText;
00218             //QWERTY
00219         }
00220     }
00221 
00228     public function changeshipping()
00229     {
00230         $mySession = $this->getSession();
00231 
00232         oxSession::setVar( 'sShipSet', oxConfig::getParameter( 'sShipSet' ) );
00233         $oBasket = $mySession->getBasket();
00234         $oBasket->setShipping( null );
00235         $oBasket->onUpdate();
00236     }
00237 
00251     public function validatePayment()
00252     {
00253         $myConfig  = $this->getConfig();
00254         $mySession = $this->getSession();
00255 
00256         //#1308C - check user. Function is executed before render(), and oUser is not set!
00257         // Set it manually for use in methods getPaymentList(), getShippingSetList()...
00258         $oUser = $this->getUser();
00259         if ( !$oUser ) {
00260             oxSession::setVar( 'payerror', 2 );
00261             return;
00262         }
00263 
00264         if (! ($sShipSetId = oxConfig::getParameter( 'sShipSet' ))) {
00265             $sShipSetId = oxSession::getVar('sShipSet');
00266         }
00267         $sPaymentId = oxConfig::getParameter( 'paymentid' );
00268         $aDynvalue  = oxConfig::getParameter( 'dynvalue' );
00269 
00270         // A. additional protection
00271         if ( !$myConfig->getConfigParam( 'blOtherCountryOrder' ) && $sPaymentId == 'oxempty' ) {
00272             $sPaymentId = '';
00273         }
00274 
00275         //#1308C - check if we have paymentID, and it really exists
00276         if ( !$sPaymentId ) {
00277             oxSession::setVar( 'payerror', 1 );
00278             return;
00279         }
00280 
00281         $oBasket = $mySession->getBasket();
00282         $oBasket->setPayment(null);
00283         $oPayment = oxNew( 'oxpayment' );
00284         $oPayment->load( $sPaymentId );
00285 
00286         // getting basket price for payment calculation
00287         $dBasketPrice = $oBasket->getPriceForPayment();
00288 
00289         $blOK = $oPayment->isValidPayment( $aDynvalue, $myConfig->getShopId(), $oUser, $dBasketPrice, $sShipSetId );
00290 
00291         if ( $blOK ) {
00292             oxSession::setVar( 'paymentid', $sPaymentId );
00293             oxSession::setVar( 'dynvalue', $aDynvalue );
00294             $oBasket->setShipping($sShipSetId);
00295             oxSession::deleteVar( '_selected_paymentid' );
00296             return 'order';
00297         } else {
00298             oxSession::setVar( 'payerror', $oPayment->getPaymentErrorNumber() );
00299 
00300             //#1308C - delete paymentid from session, and save selected it just for view
00301             oxSession::deleteVar( 'paymentid' );
00302             oxSession::setVar( '_selected_paymentid', $sPaymentId );
00303             return;
00304         }
00305     }
00306 
00312     public function getPaymentList()
00313     {
00314         if ( $this->_oPaymentList === null ) {
00315             $this->_oPaymentList = false;
00316 
00317             $sActShipSet = oxConfig::getParameter( 'sShipSet' );
00318             $oBasket = $this->getSession()->getBasket();
00319 
00320             // load sets, active set, and active set payment list
00321             list( $aAllSets, $sActShipSet, $aPaymentList ) = oxDeliverySetList::getInstance()->getDeliverySetData( $sActShipSet, $this->getUser(), $oBasket );
00322 
00323             oxSession::setVar( 'sShipSet', $sActShipSet );
00324             $oBasket->setShipping( $sActShipSet );
00325 
00326             // calculating payment expences for preview for each payment
00327             $this->_setDeprecatedValues( $aPaymentList, $oBasket );
00328             $this->_oPaymentList = $aPaymentList;
00329             $this->_aAllSets     = $aAllSets;
00330 
00331         }
00332         return $this->_oPaymentList;
00333     }
00334 
00340     public function getAllSets()
00341     {
00342         if ( $this->_aAllSets === null ) {
00343             $this->_aAllSets = false;
00344 
00345             if ($this->getPaymentList()) {
00346                 return $this->_aAllSets;
00347             }
00348         }
00349         return $this->_aAllSets;
00350     }
00351 
00357     public function getAllSetsCnt()
00358     {
00359         if ( $this->_iAllSetsCnt === null ) {
00360             $this->_iAllSetsCnt = 0;
00361 
00362             if ($this->getPaymentList()) {
00363                 $this->_iAllSetsCnt = count($this->_aAllSets);
00364             }
00365         }
00366         return $this->_iAllSetsCnt;
00367     }
00368 
00377     protected function _setDeprecatedValues( & $aPaymentList, $oBasket = null )
00378     {
00379         if ( is_array($aPaymentList) ) {
00380             $oLang = oxLang::getInstance();
00381             foreach ( $aPaymentList as $oPayment ) {
00382                 $oPrice = $oPayment->getPaymentPrice( $oBasket );
00383                 $oPayment->dAddPaymentSum = $oPrice->getBruttoPrice();
00384                 $oPayment->fAddPaymentSum = $oLang->formatCurrency( $oPayment->dAddPaymentSum, $oBasket->getBasketCurrency() );
00385                 $oPayment->aDynValues     = $oPayment->getDynValues();
00386                 if ( $oPayment->oxpayments__oxchecked->value ) {
00387                     $this->_sCheckedId = $oPayment->getId();
00388                 }
00389             }
00390         }
00391     }
00392 
00398     public function getEmptyPayment()
00399     {
00400         return $this->_oEmptyPayment;
00401     }
00402 
00408     public function getPaymentError()
00409     {
00410         return $this->_sPaymentError;
00411     }
00412 
00418     public function getPaymentErrorText()
00419     {
00420         return $this->_sPaymentErrorText;
00421     }
00422 
00428     public function getDynValue()
00429     {
00430         if ( $this->_aDynValue === null ) {
00431             $this->_aDynValue = false;
00432 
00433             // #1217 R
00434             if ( ( $aDynValue = oxSession::getVar( 'dynvalue' ) ) ) {
00435                 $this->_aDynValue  = $aDynValue;
00436             } else {
00437                 $this->_aDynValue  = oxConfig::getParameter( "dynvalue");
00438             }
00439 
00440             // #701A
00441             // assign debit note payment params to view data
00442             $aPaymentList = $this->getPaymentList();
00443             if ( isset( $aPaymentList['oxiddebitnote'] ) ) {
00444                 $this->_assignDebitNoteParams();
00445             }
00446         }
00447         return $this->_aDynValue;
00448     }
00449 
00456     protected function _assignDebitNoteParams()
00457     {
00458         // #701A
00459         $oUserPayment = oxNew( 'oxuserpayment');
00460         //such info available ?
00461         if ( $oUserPayment->getPaymentByPaymentType( $this->getUser(), 'oxiddebitnote' ) ) {
00462             $aAddPaymentData = oxUtils::getInstance()->assignValuesFromText( $oUserPayment->oxuserpayments__oxvalue->value );
00463 
00464             //checking if some of values is allready set in session - leave it
00465             foreach ( $aAddPaymentData as $oData ) {
00466                 if ( !isset( $this->_aDynValue[$oData->name] ) ||
00467                    (  isset( $this->_aDynValue[$oData->name] ) && !$this->_aDynValue[$oData->name] ) ) {
00468                     $this->_aDynValue[$oData->name] = $oData->value;
00469                 }
00470             }
00471         }
00472     }
00473 
00480     public function getCheckedPaymentId()
00481     {
00482         if ( $this->_sCheckedPaymentId === null ) {
00483             if ( ( $sPaymentID = oxConfig::getParameter( 'paymentid' ) ) ) {
00484                 $sCheckedId = $sPaymentID;
00485             } elseif ( ( $sSelectedPaymentID = oxSession::getVar( '_selected_paymentid' ) ) ) {
00486                 $sCheckedId = $sSelectedPaymentID;
00487             } else {
00488                 // #1010A.
00489                 if ( $oUser = $this->getUser()) {
00490                     $oOrder = oxNew('oxorder');
00491                     if ( ( $sLastPaymentId = $oOrder->getLastUserPaymentType( $oUser->getId()) ) ) {
00492                         $sCheckedId = $sLastPaymentId;
00493                     }
00494                 }
00495             }
00496 
00497             // #M253 set to selected payment in db
00498             if ( !$sCheckedId && $this->_sCheckedId ) {
00499                 $sCheckedId = $this->_sCheckedId;
00500             }
00501 
00502             // #646
00503             $oPaymentList = $this->getPaymentList();
00504             if ( isset( $oPaymentList ) && $oPaymentList && !isset( $oPaymentList[$sCheckedId] ) ) {
00505                 end($oPaymentList);
00506                 $sCheckedId = key( $oPaymentList );
00507             }
00508             $this->_sCheckedPaymentId = $sCheckedId;
00509         }
00510 
00511         return $this->_sCheckedPaymentId;
00512     }
00513 
00519     public function getPaymentCnt()
00520     {
00521         if ( $this->_iPaymentCnt === null ) {
00522             $this->_iPaymentCnt = false;
00523 
00524             if ($oPaymentList = $this->getPaymentList()) {
00525                 $this->_iPaymentCnt = count($oPaymentList);
00526             }
00527         }
00528         return $this->_iPaymentCnt;
00529     }
00530 
00536     public function getCreditYears()
00537     {
00538         if ( $this->_aCreditYears === null ) {
00539             $this->_aCreditYears = false;
00540 
00541             $this->_aCreditYears = range( date('Y'), date('Y') + 10 );
00542         }
00543         return $this->_aCreditYears;
00544     }
00545 
00556     protected function _filterDynData()
00557     {
00558         //in case we actually ARE allowed to store the data
00559         if (oxConfig::getInstance()->getConfigParam("blStoreCreditCardInfo"))
00560             //then do nothing
00561             return;
00562 
00563         $aDynData = $this->getSession()->getVar("dynvalue");
00564 
00565         if ($aDynData) {
00566             $aDynData["kktype"] = null;
00567             $aDynData["kknumber"] = null;
00568             $aDynData["kkname"] = null;
00569             $aDynData["kkmonth"] = null;
00570             $aDynData["kkyear"] = null;
00571             $aDynData["kkpruef"] = null;
00572             $this->getSession()->setVar("dynvalue", $aDynData);
00573         }
00574 
00575 
00576         unset($_REQUEST["dynvalue"]["kktype"]);
00577         unset($_REQUEST["dynvalue"]["kknumber"]);
00578         unset($_REQUEST["dynvalue"]["kkname"]);
00579         unset($_REQUEST["dynvalue"]["kkmonth"]);
00580         unset($_REQUEST["dynvalue"]["kkyear"]);
00581         unset($_REQUEST["dynvalue"]["kkpruef"]);
00582 
00583         unset($_POST["dynvalue"]["kktype"]);
00584         unset($_POST["dynvalue"]["kknumber"]);
00585         unset($_POST["dynvalue"]["kkname"]);
00586         unset($_POST["dynvalue"]["kkmonth"]);
00587         unset($_POST["dynvalue"]["kkyear"]);
00588         unset($_POST["dynvalue"]["kkpruef"]);
00589 
00590         unset($_GET["dynvalue"]["kktype"]);
00591         unset($_GET["dynvalue"]["kknumber"]);
00592         unset($_GET["dynvalue"]["kkname"]);
00593         unset($_GET["dynvalue"]["kkmonth"]);
00594         unset($_GET["dynvalue"]["kkyear"]);
00595         unset($_GET["dynvalue"]["kkpruef"]);
00596 
00597     }
00598 
00599 }

Generated by  doxygen 1.6.2