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 getDynValue()
00455     {
00456         if ( $this->_aDynValue === null ) {
00457             $this->_aDynValue = false;
00458 
00459             // flyspray#1217 (sarunas)
00460             if ( ( $aDynValue = oxSession::getVar( 'dynvalue' ) ) ) {
00461                 $this->_aDynValue  = $aDynValue;
00462             } else {
00463                 $this->_aDynValue  = oxConfig::getParameter( "dynvalue");
00464             }
00465 
00466             // #701A
00467             // assign debit note payment params to view data
00468             $aPaymentList = $this->getPaymentList();
00469             if ( isset( $aPaymentList['oxiddebitnote'] ) ) {
00470                 $this->_assignDebitNoteParams();
00471             }
00472         }
00473         return $this->_aDynValue;
00474     }
00475 
00482     protected function _assignDebitNoteParams()
00483     {
00484         // #701A
00485         $oUserPayment = oxNew( 'oxuserpayment');
00486         //such info available ?
00487         if ( $oUserPayment->getPaymentByPaymentType( $this->getUser(), 'oxiddebitnote' ) ) {
00488             $aAddPaymentData = oxRegistry::getUtils()->assignValuesFromText( $oUserPayment->oxuserpayments__oxvalue->value );
00489 
00490             //checking if some of values is allready set in session - leave it
00491             foreach ( $aAddPaymentData as $oData ) {
00492                 if ( !isset( $this->_aDynValue[$oData->name] ) ||
00493                    (  isset( $this->_aDynValue[$oData->name] ) && !$this->_aDynValue[$oData->name] ) ) {
00494                     $this->_aDynValue[$oData->name] = $oData->value;
00495                 }
00496             }
00497         }
00498     }
00499 
00506     public function getCheckedPaymentId()
00507     {
00508         if ( $this->_sCheckedPaymentId === null ) {
00509             if (! ($sPaymentID = oxConfig::getParameter( 'paymentid' ))) {
00510                 $sPaymentID = oxSession::getVar('paymentid');
00511             }
00512             if ( $sPaymentID ) {
00513                 $sCheckedId = $sPaymentID;
00514             } elseif ( ( $sSelectedPaymentID = oxSession::getVar( '_selected_paymentid' ) ) ) {
00515                 $sCheckedId = $sSelectedPaymentID;
00516             } else {
00517                 // #1010A.
00518                 if ( $oUser = $this->getUser()) {
00519                     $oOrder = oxNew('oxorder');
00520                     if ( ( $sLastPaymentId = $oOrder->getLastUserPaymentType( $oUser->getId()) ) ) {
00521                         $sCheckedId = $sLastPaymentId;
00522                     }
00523                 }
00524             }
00525 
00526             // #M253 set to selected payment in db
00527             if ( !$sCheckedId && $this->_sCheckedId ) {
00528                 $sCheckedId = $this->_sCheckedId;
00529             }
00530 
00531             // #646
00532             $oPaymentList = $this->getPaymentList();
00533             if ( isset( $oPaymentList ) && $oPaymentList && !isset( $oPaymentList[$sCheckedId] ) ) {
00534                 end($oPaymentList);
00535                 $sCheckedId = key( $oPaymentList );
00536             }
00537             $this->_sCheckedPaymentId = $sCheckedId;
00538         }
00539 
00540         return $this->_sCheckedPaymentId;
00541     }
00542 
00548     public function getPaymentCnt()
00549     {
00550         if ( $this->_iPaymentCnt === null ) {
00551             $this->_iPaymentCnt = false;
00552 
00553             if ($oPaymentList = $this->getPaymentList()) {
00554                 $this->_iPaymentCnt = count($oPaymentList);
00555             }
00556         }
00557         return $this->_iPaymentCnt;
00558     }
00559 
00565     public function getCreditYears()
00566     {
00567         if ( $this->_aCreditYears === null ) {
00568             $this->_aCreditYears = false;
00569 
00570             $this->_aCreditYears = range( date('Y'), date('Y') + 10 );
00571         }
00572         return $this->_aCreditYears;
00573     }
00574 
00583     protected function _checkArrValuesEmpty( $aData, $aKeys )
00584     {
00585         if ( !is_array( $aKeys ) || count( $aKeys ) < 1 ) {
00586             return false;
00587         }
00588 
00589         foreach ( $aKeys as $sKey ) {
00590             if ( isset( $aData[$sKey] ) && !empty( $aData[$sKey] ) ) {
00591                 return false;
00592             }
00593         }
00594 
00595         return true;
00596     }
00597 
00598 
00609     protected function _filterDynData()
00610     {
00611         //in case we actually ARE allowed to store the data
00612         if (oxRegistry::getConfig()->getConfigParam("blStoreCreditCardInfo")) {
00613             //then do nothing and reset _blDynDataFiltered
00614             $this->_blDynDataFiltered = false;
00615             return;
00616         }
00617 
00618         $aDynData = $this->getSession()->getVar("dynvalue");
00619 
00620         $aFields = array("kktype", "kknumber", "kkname", "kkmonth", "kkyear", "kkpruef");
00621 
00622         if ( $aDynData ) {
00623             if ( !$this->_checkArrValuesEmpty( $aDynData, $aFields ) ) {
00624                 $this->_blDynDataFiltered = true;
00625             }
00626             $aDynData["kktype"] = null;
00627             $aDynData["kknumber"] = null;
00628             $aDynData["kkname"] = null;
00629             $aDynData["kkmonth"] = null;
00630             $aDynData["kkyear"] = null;
00631             $aDynData["kkpruef"] = null;
00632             oxSession::setVar("dynvalue", $aDynData);
00633         }
00634 
00635         if (  !$this->_checkArrValuesEmpty( $_REQUEST["dynvalue"], $aFields ) ||
00636               !$this->_checkArrValuesEmpty( $_POST["dynvalue"], $aFields ) ||
00637               !$this->_checkArrValuesEmpty( $_GET["dynvalue"], $aFields ) ) {
00638             $this->_blDynDataFiltered = true;
00639         }
00640 
00641         unset($_REQUEST["dynvalue"]["kktype"]);
00642         unset($_REQUEST["dynvalue"]["kknumber"]);
00643         unset($_REQUEST["dynvalue"]["kkname"]);
00644         unset($_REQUEST["dynvalue"]["kkmonth"]);
00645         unset($_REQUEST["dynvalue"]["kkyear"]);
00646         unset($_REQUEST["dynvalue"]["kkpruef"]);
00647 
00648         unset($_POST["dynvalue"]["kktype"]);
00649         unset($_POST["dynvalue"]["kknumber"]);
00650         unset($_POST["dynvalue"]["kkname"]);
00651         unset($_POST["dynvalue"]["kkmonth"]);
00652         unset($_POST["dynvalue"]["kkyear"]);
00653         unset($_POST["dynvalue"]["kkpruef"]);
00654 
00655         unset($_GET["dynvalue"]["kktype"]);
00656         unset($_GET["dynvalue"]["kknumber"]);
00657         unset($_GET["dynvalue"]["kkname"]);
00658         unset($_GET["dynvalue"]["kkmonth"]);
00659         unset($_GET["dynvalue"]["kkyear"]);
00660         unset($_GET["dynvalue"]["kkpruef"]);
00661 
00662     }
00663 
00669     public function getTsProtections()
00670     {
00671         if ( $this->_aTsProducts === null ) {
00672             $oBasket = $this->getSession()->getBasket();
00673             $dVat = $oBasket->getAdditionalServicesVatPercent();
00674             if ( $dPrice = $oBasket->getPrice()->getBruttoPrice() ) {
00675                 $oTsProtection = oxNew('oxtsprotection');
00676                 $oTsProtection->setVat( $dVat );
00677                 $this->_aTsProducts = $oTsProtection->getTsProducts($dPrice);
00678             }
00679         }
00680         return $this->_aTsProducts;
00681     }
00682 
00688     public function getCheckedTsProductId()
00689     {
00690         if ( $this->_sCheckedProductId === null ) {
00691             $this->_sCheckedProductId = false;
00692             if ( $sId = oxConfig::getParameter( 'stsprotection' ) ) {
00693                 $this->_sCheckedProductId = $sId;
00694             }
00695         }
00696         return $this->_sCheckedProductId;
00697     }
00698 
00704     public function getBreadCrumb()
00705     {
00706         $aPaths = array();
00707         $aPath = array();
00708 
00709 
00710         $aPath['title'] = oxRegistry::getLang()->translateString( 'PAGE_CHECKOUT_PAY', oxRegistry::getLang()->getBaseLanguage(), false );
00711         $aPath['link']  = $this->getLink();
00712 
00713         $aPaths[] = $aPath;
00714 
00715         return $aPaths;
00716     }
00717 
00723     public function isPaymentVatSplitted()
00724     {
00725         return $this->getConfig()->getConfigParam('blShowVATForPayCharge');
00726     }
00727 
00728 }