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