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 = 'page/checkout/payment.tpl';
00080 
00085     protected $_blIsOrderStep = true;
00086 
00091     protected $_aTsProducts = null;
00092 
00097     protected $_blDynDataFiltered = false;
00098 
00099 
00105     public function init()
00106     {
00107         $this->_filterDynData();
00108         parent::init();
00109     }
00110 
00122     public function render()
00123     {
00124         $myConfig  = $this->getConfig();
00125 
00126         if ($myConfig->getConfigParam( 'blPsBasketReservationEnabled' )) {
00127             $this->getSession()->getBasketReservations()->renewExpiration();
00128         }
00129 
00130         parent::render();
00131 
00132         //if it happens that you are not in SSL
00133         //then forcing to HTTPS
00134 
00135         //but first checking maybe there were redirection already to prevent infinite redirections due to possible buggy ssl detection on server
00136         $blAlreadyRedirected = oxConfig::getParameter( 'sslredirect' ) == 'forced';
00137 
00138         if ( $this->getIsOrderStep() ) {
00139 
00140             //additional check if we really really have a user now
00141             //and the basket is not empty
00142             $oBasket = $this->getSession()->getBasket();
00143             if ( $myConfig->getConfigParam( 'blPsBasketReservationEnabled' ) && (!$oBasket || ( $oBasket && !$oBasket->getProductsCount() )) ) {
00144                 oxRegistry::getUtils()->redirect( $myConfig->getShopHomeURL() .'cl=basket', true, 302 );
00145             }
00146 
00147             $oUser = $this->getUser();
00148             if (!$oUser && ($oBasket && $oBasket->getProductsCount() > 0)) {
00149                 oxRegistry::getUtils()->redirect( $myConfig->getShopHomeURL() .'cl=basket', false, 302 );
00150             } elseif ( !$oBasket || !$oUser || ( $oBasket && !$oBasket->getProductsCount() ) ) {
00151                 oxRegistry::getUtils()->redirect( $myConfig->getShopHomeURL() .'cl=start', false, 302 );
00152             }
00153         }
00154 
00155         if ( $myConfig->getCurrentShopURL() != $myConfig->getSSLShopURL() && !$blAlreadyRedirected && !oxConfig::getParameter('fnc') ) {
00156             $sPayError = oxConfig::getParameter( 'payerror' )?'payerror='.oxConfig::getParameter( 'payerror' ):'';
00157             $sPayErrorText = oxConfig::getParameter('payerrortext')?'payerrortext='.oxConfig::getParameter( 'payerrortext' ):'';
00158             $sRedirectURL = $myConfig->getShopSecureHomeURL().'sslredirect=forced&cl=payment&'.$sPayError."&".$sPayErrorText;
00159             oxRegistry::getUtils()->redirect( $sRedirectURL, true, 302 );
00160         }
00161 
00162         if ( !$this->getAllSetsCnt() ) {
00163             // no fitting shipping set found, setting default empty payment
00164             $this->_setDefaultEmptyPayment();
00165             oxSession::setVar( 'sShipSet', null );
00166         }
00167 
00168         $this->_unsetPaymentErrors();
00169 
00170         return $this->_sThisTemplate;
00171     }
00172 
00180     protected function _setDefaultEmptyPayment()
00181     {
00182         // no shipping method there !!
00183         if ( $this->getConfig()->getConfigParam( 'blOtherCountryOrder' ) ) {
00184             $oPayment = oxNew( 'oxpayment' );
00185             if ( $oPayment->load( 'oxempty' ) ) {
00186                 $this->_oEmptyPayment = $oPayment;
00187             } else {
00188                 // some error with setup ??
00189                 $this->_sPaymentError = -2;
00190             }
00191         } else {
00192             $this->_sPaymentError = -2;
00193         }
00194     }
00195 
00201     protected function _unsetPaymentErrors()
00202     {
00203         $iPayError     = oxConfig::getParameter( 'payerror' );
00204         $sPayErrorText = oxConfig::getParameter( 'payerrortext' );
00205 
00206         if (!($iPayError || $sPayErrorText)) {
00207             $iPayError     = oxSession::getVar( 'payerror' );
00208             $sPayErrorText = oxSession::getVar( 'payerrortext' );
00209         }
00210 
00211         if ( $iPayError ) {
00212             oxSession::deleteVar( 'payerror' );
00213             $this->_sPaymentError = $iPayError;
00214         }
00215         if ( $sPayErrorText ) {
00216             oxSession::deleteVar( 'payerrortext' );
00217             $this->_sPaymentErrorText = $sPayErrorText;
00218         }
00219     }
00220 
00227     public function changeshipping()
00228     {
00229         $oSession = $this->getSession();
00230 
00231         $oBasket = $oSession->getBasket();
00232         $oBasket->setShipping( null );
00233         $oBasket->onUpdate();
00234         $oSession->setVariable( 'sShipSet', $this->getConfig()->getRequestParameter( 'sShipSet' ) );
00235     }
00236 
00248     public function validatePayment()
00249     {
00250         $myConfig  = $this->getConfig();
00251         $oSession = $this->getSession();
00252 
00253         //#1308C - check user. Function is executed before render(), and oUser is not set!
00254         // Set it manually for use in methods getPaymentList(), getShippingSetList()...
00255         $oUser = $this->getUser();
00256         if ( !$oUser ) {
00257             $oSession->setVariable( 'payerror', 2 );
00258             return;
00259         }
00260 
00261         if (! ($sShipSetId = oxConfig::getParameter( 'sShipSet' ))) {
00262             $sShipSetId = $oSession->getVariable('sShipSet');
00263         }
00264         if (! ($sPaymentId = oxConfig::getParameter( 'paymentid' ))) {
00265             $sPaymentId = $oSession->getVariable('paymentid');
00266         }
00267         if (! ($aDynvalue = oxConfig::getParameter( 'dynvalue' ))) {
00268             $aDynvalue = $oSession->getVariable('dynvalue');
00269         }
00270 
00271         // A. additional protection
00272         if ( !$myConfig->getConfigParam( 'blOtherCountryOrder' ) && $sPaymentId == 'oxempty' ) {
00273             $sPaymentId = '';
00274         }
00275 
00276         //#1308C - check if we have paymentID, and it really exists
00277         if ( !$sPaymentId ) {
00278             $oSession->setVariable( 'payerror', 1 );
00279             return;
00280         }
00281 
00282         if ( $this->getDynDataFiltered() && $sPaymentId == 'oxidcreditcard' ) {
00283             $oSession->setVariable( 'payerror', 7 );
00284             return;
00285         }
00286 
00287         $oBasket = $oSession->getBasket();
00288         $oBasket->setPayment(null);
00289         $oPayment = oxNew( 'oxpayment' );
00290         $oPayment->load( $sPaymentId );
00291 
00292         // getting basket price for payment calculation
00293         $dBasketPrice = $oBasket->getPriceForPayment();
00294 
00295         $blOK = $oPayment->isValidPayment( $aDynvalue, $myConfig->getShopId(), $oUser, $dBasketPrice, $sShipSetId );
00296 
00297         if ( $blOK ) {
00298             $oSession->setVariable( 'paymentid', $sPaymentId );
00299             $oSession->setVariable( 'dynvalue', $aDynvalue );
00300             if ( oxConfig::getParameter( 'bltsprotection' ) ) {
00301                 $sTsProductId = oxConfig::getParameter( 'stsprotection' );
00302                 $oBasket->setTsProductId($sTsProductId);
00303                 $oSession->setVariable( 'stsprotection', $sTsProductId );
00304             } else {
00305                 $oSession->deleteVariable( 'stsprotection' );
00306                 $oBasket->setTsProductId(null);
00307             }
00308             $oBasket->setShipping($sShipSetId);
00309             $oSession->deleteVariable( '_selected_paymentid' );
00310             return 'order';
00311         } else {
00312             $oSession->setVariable( 'payerror', $oPayment->getPaymentErrorNumber() );
00313 
00314             //#1308C - delete paymentid from session, and save selected it just for view
00315             $oSession->deleteVariable( 'paymentid' );
00316             $oSession->setVariable( '_selected_paymentid', $sPaymentId );
00317             $oSession->deleteVariable( 'stsprotection' );
00318             $oBasket->setTsProductId(null);
00319             return;
00320         }
00321     }
00322 
00328     public function getPaymentList()
00329     {
00330         if ( $this->_oPaymentList === null ) {
00331             $this->_oPaymentList = false;
00332 
00333             $sActShipSet = oxConfig::getParameter( 'sShipSet' );
00334             if ( !$sActShipSet ) {
00335                  $sActShipSet = oxSession::getVar( 'sShipSet' );
00336             }
00337 
00338             $oBasket = $this->getSession()->getBasket();
00339 
00340             // load sets, active set, and active set payment list
00341             list( $aAllSets, $sActShipSet, $aPaymentList ) = oxRegistry::get("oxDeliverySetList")->getDeliverySetData( $sActShipSet, $this->getUser(), $oBasket );
00342 
00343             $oBasket->setShipping( $sActShipSet );
00344 
00345             // calculating payment expences for preview for each payment
00346             $this->_setValues( $aPaymentList, $oBasket );
00347             $this->_oPaymentList = $aPaymentList;
00348             $this->_aAllSets     = $aAllSets;
00349 
00350         }
00351         return $this->_oPaymentList;
00352     }
00353 
00359     public function getAllSets()
00360     {
00361         if ( $this->_aAllSets === null ) {
00362             $this->_aAllSets = false;
00363 
00364             if ($this->getPaymentList()) {
00365                 return $this->_aAllSets;
00366             }
00367         }
00368         return $this->_aAllSets;
00369     }
00370 
00376     public function getAllSetsCnt()
00377     {
00378         if ( $this->_iAllSetsCnt === null ) {
00379             $this->_iAllSetsCnt = 0;
00380 
00381             if ($this->getPaymentList()) {
00382                 $this->_iAllSetsCnt = count($this->_aAllSets);
00383             }
00384         }
00385         return $this->_iAllSetsCnt;
00386     }
00387 
00396     protected function _setValues( & $aPaymentList, $oBasket = null )
00397     {
00398         if ( is_array($aPaymentList) ) {
00399             foreach ( $aPaymentList as $oPayment ) {
00400                 $oPayment->calculate( $oBasket );
00401                 $oPayment->aDynValues     = $oPayment->getDynValues();
00402                 if ( $oPayment->oxpayments__oxchecked->value ) {
00403                     $this->_sCheckedId = $oPayment->getId();
00404                 }
00405             }
00406         }
00407     }
00408 
00414     public function getEmptyPayment()
00415     {
00416         return $this->_oEmptyPayment;
00417     }
00418 
00424     public function getPaymentError()
00425     {
00426         return $this->_sPaymentError;
00427     }
00428 
00434     public function getDynDataFiltered()
00435     {
00436         return $this->_blDynDataFiltered;
00437     }
00438 
00444     public function getPaymentErrorText()
00445     {
00446         return $this->_sPaymentErrorText;
00447     }
00448 
00454     public function isOldDebitValidationEnabled()
00455     {
00456         return !$this->getConfig()->getConfigParam( 'blSkipDebitOldBankInfo' );
00457     }
00458 
00464     public function getDynValue()
00465     {
00466         if ( $this->_aDynValue === null ) {
00467             $this->_aDynValue = false;
00468 
00469             // flyspray#1217 (sarunas)
00470             if ( ( $aDynValue = oxSession::getVar( 'dynvalue' ) ) ) {
00471                 $this->_aDynValue  = $aDynValue;
00472             } else {
00473                 $this->_aDynValue  = oxConfig::getParameter( "dynvalue");
00474             }
00475 
00476             // #701A
00477             // assign debit note payment params to view data
00478             $aPaymentList = $this->getPaymentList();
00479             if ( isset( $aPaymentList['oxiddebitnote'] ) ) {
00480                 $this->_assignDebitNoteParams();
00481             }
00482         }
00483         return $this->_aDynValue;
00484     }
00485 
00492     protected function _assignDebitNoteParams()
00493     {
00494         // #701A
00495         $oUserPayment = oxNew( 'oxuserpayment');
00496         //such info available ?
00497         if ( $oUserPayment->getPaymentByPaymentType( $this->getUser(), 'oxiddebitnote' ) ) {
00498             $aAddPaymentData = oxRegistry::getUtils()->assignValuesFromText( $oUserPayment->oxuserpayments__oxvalue->value );
00499 
00500             //checking if some of values is allready set in session - leave it
00501             foreach ( $aAddPaymentData as $oData ) {
00502                 if ( !isset( $this->_aDynValue[$oData->name] ) ||
00503                    (  isset( $this->_aDynValue[$oData->name] ) && !$this->_aDynValue[$oData->name] ) ) {
00504                     $this->_aDynValue[$oData->name] = $oData->value;
00505                 }
00506             }
00507         }
00508     }
00509 
00516     public function getCheckedPaymentId()
00517     {
00518         if ( $this->_sCheckedPaymentId === null ) {
00519             if (! ($sPaymentID = oxConfig::getParameter( 'paymentid' ))) {
00520                 $sPaymentID = oxSession::getVar('paymentid');
00521             }
00522             if ( $sPaymentID ) {
00523                 $sCheckedId = $sPaymentID;
00524             } elseif ( ( $sSelectedPaymentID = oxSession::getVar( '_selected_paymentid' ) ) ) {
00525                 $sCheckedId = $sSelectedPaymentID;
00526             } else {
00527                 // #1010A.
00528                 if ( $oUser = $this->getUser()) {
00529                     $oOrder = oxNew('oxorder');
00530                     if ( ( $sLastPaymentId = $oOrder->getLastUserPaymentType( $oUser->getId()) ) ) {
00531                         $sCheckedId = $sLastPaymentId;
00532                     }
00533                 }
00534             }
00535 
00536             // #M253 set to selected payment in db
00537             if ( !$sCheckedId && $this->_sCheckedId ) {
00538                 $sCheckedId = $this->_sCheckedId;
00539             }
00540 
00541             // #646
00542             $oPaymentList = $this->getPaymentList();
00543             if ( isset( $oPaymentList ) && $oPaymentList && !isset( $oPaymentList[$sCheckedId] ) ) {
00544                 end($oPaymentList);
00545                 $sCheckedId = key( $oPaymentList );
00546             }
00547             $this->_sCheckedPaymentId = $sCheckedId;
00548         }
00549 
00550         return $this->_sCheckedPaymentId;
00551     }
00552 
00558     public function getPaymentCnt()
00559     {
00560         if ( $this->_iPaymentCnt === null ) {
00561             $this->_iPaymentCnt = false;
00562 
00563             if ($oPaymentList = $this->getPaymentList()) {
00564                 $this->_iPaymentCnt = count($oPaymentList);
00565             }
00566         }
00567         return $this->_iPaymentCnt;
00568     }
00569 
00575     public function getCreditYears()
00576     {
00577         if ( $this->_aCreditYears === null ) {
00578             $this->_aCreditYears = false;
00579 
00580             $this->_aCreditYears = range( date('Y'), date('Y') + 10 );
00581         }
00582         return $this->_aCreditYears;
00583     }
00584 
00593     protected function _checkArrValuesEmpty( $aData, $aKeys )
00594     {
00595         if ( !is_array( $aKeys ) || count( $aKeys ) < 1 ) {
00596             return false;
00597         }
00598 
00599         foreach ( $aKeys as $sKey ) {
00600             if ( isset( $aData[$sKey] ) && !empty( $aData[$sKey] ) ) {
00601                 return false;
00602             }
00603         }
00604 
00605         return true;
00606     }
00607 
00608 
00619     protected function _filterDynData()
00620     {
00621         //in case we actually ARE allowed to store the data
00622         if (oxRegistry::getConfig()->getConfigParam("blStoreCreditCardInfo")) {
00623             //then do nothing and reset _blDynDataFiltered
00624             $this->_blDynDataFiltered = false;
00625             return;
00626         }
00627 
00628         $aDynData = $this->getSession()->getVar("dynvalue");
00629 
00630         $aFields = array("kktype", "kknumber", "kkname", "kkmonth", "kkyear", "kkpruef");
00631 
00632         if ( $aDynData ) {
00633             if ( !$this->_checkArrValuesEmpty( $aDynData, $aFields ) ) {
00634                 $this->_blDynDataFiltered = true;
00635             }
00636             $aDynData["kktype"] = null;
00637             $aDynData["kknumber"] = null;
00638             $aDynData["kkname"] = null;
00639             $aDynData["kkmonth"] = null;
00640             $aDynData["kkyear"] = null;
00641             $aDynData["kkpruef"] = null;
00642             oxSession::setVar("dynvalue", $aDynData);
00643         }
00644 
00645         if (  !$this->_checkArrValuesEmpty( $_REQUEST["dynvalue"], $aFields ) ||
00646               !$this->_checkArrValuesEmpty( $_POST["dynvalue"], $aFields ) ||
00647               !$this->_checkArrValuesEmpty( $_GET["dynvalue"], $aFields ) ) {
00648             $this->_blDynDataFiltered = true;
00649         }
00650 
00651         unset($_REQUEST["dynvalue"]["kktype"]);
00652         unset($_REQUEST["dynvalue"]["kknumber"]);
00653         unset($_REQUEST["dynvalue"]["kkname"]);
00654         unset($_REQUEST["dynvalue"]["kkmonth"]);
00655         unset($_REQUEST["dynvalue"]["kkyear"]);
00656         unset($_REQUEST["dynvalue"]["kkpruef"]);
00657 
00658         unset($_POST["dynvalue"]["kktype"]);
00659         unset($_POST["dynvalue"]["kknumber"]);
00660         unset($_POST["dynvalue"]["kkname"]);
00661         unset($_POST["dynvalue"]["kkmonth"]);
00662         unset($_POST["dynvalue"]["kkyear"]);
00663         unset($_POST["dynvalue"]["kkpruef"]);
00664 
00665         unset($_GET["dynvalue"]["kktype"]);
00666         unset($_GET["dynvalue"]["kknumber"]);
00667         unset($_GET["dynvalue"]["kkname"]);
00668         unset($_GET["dynvalue"]["kkmonth"]);
00669         unset($_GET["dynvalue"]["kkyear"]);
00670         unset($_GET["dynvalue"]["kkpruef"]);
00671 
00672     }
00673 
00679     public function getTsProtections()
00680     {
00681         if ( $this->_aTsProducts === null ) {
00682             $oBasket = $this->getSession()->getBasket();
00683             $dVat = $oBasket->getAdditionalServicesVatPercent();
00684             if ( $dPrice = $oBasket->getPrice()->getBruttoPrice() ) {
00685                 $oTsProtection = oxNew('oxtsprotection');
00686                 $oTsProtection->setVat( $dVat );
00687                 $this->_aTsProducts = $oTsProtection->getTsProducts($dPrice);
00688             }
00689         }
00690         return $this->_aTsProducts;
00691     }
00692 
00698     public function getCheckedTsProductId()
00699     {
00700         if ( $this->_sCheckedProductId === null ) {
00701             $this->_sCheckedProductId = false;
00702             if ( $sId = oxConfig::getParameter( 'stsprotection' ) ) {
00703                 $this->_sCheckedProductId = $sId;
00704             }
00705         }
00706         return $this->_sCheckedProductId;
00707     }
00708 
00714     public function getBreadCrumb()
00715     {
00716         $aPaths = array();
00717         $aPath = array();
00718 
00719 
00720         $aPath['title'] = oxRegistry::getLang()->translateString( 'PAY', oxRegistry::getLang()->getBaseLanguage(), false );
00721         $aPath['link']  = $this->getLink();
00722 
00723         $aPaths[] = $aPath;
00724 
00725         return $aPaths;
00726     }
00727 
00733     public function isPaymentVatSplitted()
00734     {
00735         return $this->getConfig()->getConfigParam('blShowVATForPayCharge');
00736     }
00737 
00738 }