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 
00098     public function init()
00099     {
00100         $this->_filterDynData();
00101         parent::init();
00102     }
00103 
00115     public function render()
00116     {
00117         $myConfig  = $this->getConfig();
00118 
00119         if ($myConfig->getConfigParam( 'blPsBasketReservationEnabled' )) {
00120             $this->getSession()->getBasketReservations()->renewExpiration();
00121         }
00122 
00123         parent::render();
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, true, 302 );
00136         }
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                 oxUtils::getInstance()->redirect( $myConfig->getShopHomeURL() .'cl=basket', true, 302 );
00145             }
00146 
00147             $oUser = $this->getUser();
00148             if ( !$oBasket || !$oUser || ( $oBasket && !$oBasket->getProductsCount() ) ) {
00149                 oxUtils::getInstance()->redirect( $myConfig->getShopHomeURL() .'cl=start', true, 302 );
00150             }
00151         }
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         return $this->_sThisTemplate;
00162     }
00163 
00171     protected function _setDefaultEmptyPayment()
00172     {
00173         // no shipping method there !!
00174         if ( $this->getConfig()->getConfigParam( 'blOtherCountryOrder' ) ) {
00175             $oPayment = oxNew( 'oxpayment' );
00176             if ( $oPayment->load( 'oxempty' ) ) {
00177                 $this->_oEmptyPayment = $oPayment;
00178             } else {
00179                 // some error with setup ??
00180                 $this->_sPaymentError = -2;
00181             }
00182         } else {
00183             $this->_sPaymentError = -2;
00184         }
00185     }
00186 
00192     protected function _unsetPaymentErrors()
00193     {
00194         $iPayError     = oxConfig::getParameter( 'payerror' );
00195         $sPayErrorText = oxConfig::getParameter( 'payerrortext' );
00196 
00197         if (!($iPayError || $sPayErrorText)) {
00198             $iPayError     = oxSession::getVar( 'payerror' );
00199             $sPayErrorText = oxSession::getVar( 'payerrortext' );
00200         }
00201 
00202         if ( $iPayError ) {
00203             oxSession::deleteVar( 'payerror' );
00204             $this->_sPaymentError = $iPayError;
00205         }
00206         if ( $sPayErrorText ) {
00207             oxSession::deleteVar( 'payerrortext' );
00208             $this->_sPaymentErrorText = $sPayErrorText;
00209         }
00210     }
00211 
00218     public function changeshipping()
00219     {
00220         $mySession = $this->getSession();
00221 
00222         $oBasket = $mySession->getBasket();
00223         $oBasket->setShipping( null );
00224         $oBasket->onUpdate();
00225         oxSession::setVar( 'sShipSet', oxConfig::getParameter( 'sShipSet' ) );
00226     }
00227 
00239     public function validatePayment()
00240     {
00241         $myConfig  = $this->getConfig();
00242         $mySession = $this->getSession();
00243 
00244         //#1308C - check user. Function is executed before render(), and oUser is not set!
00245         // Set it manually for use in methods getPaymentList(), getShippingSetList()...
00246         $oUser = $this->getUser();
00247         if ( !$oUser ) {
00248             oxSession::setVar( 'payerror', 2 );
00249             return;
00250         }
00251 
00252         if (! ($sShipSetId = oxConfig::getParameter( 'sShipSet' ))) {
00253             $sShipSetId = oxSession::getVar('sShipSet');
00254         }
00255         if (! ($sPaymentId = oxConfig::getParameter( 'paymentid' ))) {
00256             $sPaymentId = oxSession::getVar('paymentid');
00257         }
00258         if (! ($aDynvalue = oxConfig::getParameter( 'dynvalue' ))) {
00259             $aDynvalue = oxSession::getVar('dynvalue');
00260         }
00261 
00262         // A. additional protection
00263         if ( !$myConfig->getConfigParam( 'blOtherCountryOrder' ) && $sPaymentId == 'oxempty' ) {
00264             $sPaymentId = '';
00265         }
00266 
00267         //#1308C - check if we have paymentID, and it really exists
00268         if ( !$sPaymentId ) {
00269             oxSession::setVar( 'payerror', 1 );
00270             return;
00271         }
00272 
00273         $oBasket = $mySession->getBasket();
00274         $oBasket->setPayment(null);
00275         $oPayment = oxNew( 'oxpayment' );
00276         $oPayment->load( $sPaymentId );
00277 
00278         // getting basket price for payment calculation
00279         $dBasketPrice = $oBasket->getPriceForPayment();
00280 
00281         $blOK = $oPayment->isValidPayment( $aDynvalue, $myConfig->getShopId(), $oUser, $dBasketPrice, $sShipSetId );
00282 
00283         if ( $blOK ) {
00284             oxSession::setVar( 'paymentid', $sPaymentId );
00285             oxSession::setVar( 'dynvalue', $aDynvalue );
00286             if ( oxConfig::getParameter( 'bltsprotection' ) ) {
00287                 $sTsProductId = oxConfig::getParameter( 'stsprotection' );
00288                 $oBasket->setTsProductId($sTsProductId);
00289                 oxSession::setVar( 'stsprotection', $sTsProductId );
00290             } else {
00291                 oxSession::deleteVar( 'stsprotection' );
00292                 $oBasket->setTsProductId(null);
00293             }
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             oxSession::deleteVar( 'stsprotection' );
00304             $oBasket->setTsProductId(null);
00305             return;
00306         }
00307     }
00308 
00314     public function getPaymentList()
00315     {
00316         if ( $this->_oPaymentList === null ) {
00317             $this->_oPaymentList = false;
00318 
00319             $sActShipSet = oxConfig::getParameter( 'sShipSet' );
00320             if ( !$sActShipSet ) {
00321                  $sActShipSet = oxSession::getVar( 'sShipSet' );
00322             }
00323 
00324             $oBasket = $this->getSession()->getBasket();
00325 
00326             // load sets, active set, and active set payment list
00327             list( $aAllSets, $sActShipSet, $aPaymentList ) = oxDeliverySetList::getInstance()->getDeliverySetData( $sActShipSet, $this->getUser(), $oBasket );
00328 
00329             $oBasket->setShipping( $sActShipSet );
00330 
00331             // calculating payment expences for preview for each payment
00332             $this->_setDeprecatedValues( $aPaymentList, $oBasket );
00333             $this->_oPaymentList = $aPaymentList;
00334             $this->_aAllSets     = $aAllSets;
00335 
00336         }
00337         return $this->_oPaymentList;
00338     }
00339 
00345     public function getAllSets()
00346     {
00347         if ( $this->_aAllSets === null ) {
00348             $this->_aAllSets = false;
00349 
00350             if ($this->getPaymentList()) {
00351                 return $this->_aAllSets;
00352             }
00353         }
00354         return $this->_aAllSets;
00355     }
00356 
00362     public function getAllSetsCnt()
00363     {
00364         if ( $this->_iAllSetsCnt === null ) {
00365             $this->_iAllSetsCnt = 0;
00366 
00367             if ($this->getPaymentList()) {
00368                 $this->_iAllSetsCnt = count($this->_aAllSets);
00369             }
00370         }
00371         return $this->_iAllSetsCnt;
00372     }
00373 
00382     protected function _setDeprecatedValues( & $aPaymentList, $oBasket = null )
00383     {
00384         if ( is_array($aPaymentList) ) {
00385             $oLang = oxLang::getInstance();
00386             foreach ( $aPaymentList as $oPayment ) {
00387                 $oPrice = $oPayment->getPaymentPrice( $oBasket );
00388                 $oPayment->dAddPaymentSum = $oPrice->getBruttoPrice();
00389                 $oPayment->fAddPaymentSum = $oLang->formatCurrency( $oPayment->dAddPaymentSum, $oBasket->getBasketCurrency() );
00390                 $oPayment->aDynValues     = $oPayment->getDynValues();
00391                 if ( $oPayment->oxpayments__oxchecked->value ) {
00392                     $this->_sCheckedId = $oPayment->getId();
00393                 }
00394             }
00395         }
00396     }
00397 
00403     public function getEmptyPayment()
00404     {
00405         return $this->_oEmptyPayment;
00406     }
00407 
00413     public function getPaymentError()
00414     {
00415         return $this->_sPaymentError;
00416     }
00417 
00423     public function getPaymentErrorText()
00424     {
00425         return $this->_sPaymentErrorText;
00426     }
00427 
00433     public function getDynValue()
00434     {
00435         if ( $this->_aDynValue === null ) {
00436             $this->_aDynValue = false;
00437 
00438             // flyspray#1217 (sarunas)
00439             if ( ( $aDynValue = oxSession::getVar( 'dynvalue' ) ) ) {
00440                 $this->_aDynValue  = $aDynValue;
00441             } else {
00442                 $this->_aDynValue  = oxConfig::getParameter( "dynvalue");
00443             }
00444 
00445             // #701A
00446             // assign debit note payment params to view data
00447             $aPaymentList = $this->getPaymentList();
00448             if ( isset( $aPaymentList['oxiddebitnote'] ) ) {
00449                 $this->_assignDebitNoteParams();
00450             }
00451         }
00452         return $this->_aDynValue;
00453     }
00454 
00461     protected function _assignDebitNoteParams()
00462     {
00463         // #701A
00464         $oUserPayment = oxNew( 'oxuserpayment');
00465         //such info available ?
00466         if ( $oUserPayment->getPaymentByPaymentType( $this->getUser(), 'oxiddebitnote' ) ) {
00467             $aAddPaymentData = oxUtils::getInstance()->assignValuesFromText( $oUserPayment->oxuserpayments__oxvalue->value );
00468 
00469             //checking if some of values is allready set in session - leave it
00470             foreach ( $aAddPaymentData as $oData ) {
00471                 if ( !isset( $this->_aDynValue[$oData->name] ) ||
00472                    (  isset( $this->_aDynValue[$oData->name] ) && !$this->_aDynValue[$oData->name] ) ) {
00473                     $this->_aDynValue[$oData->name] = $oData->value;
00474                 }
00475             }
00476         }
00477     }
00478 
00485     public function getCheckedPaymentId()
00486     {
00487         if ( $this->_sCheckedPaymentId === null ) {
00488             if (! ($sPaymentID = oxConfig::getParameter( 'paymentid' ))) {
00489                 $sPaymentID = oxSession::getVar('paymentid');
00490             }
00491             if ( $sPaymentID ) {
00492                 $sCheckedId = $sPaymentID;
00493             } elseif ( ( $sSelectedPaymentID = oxSession::getVar( '_selected_paymentid' ) ) ) {
00494                 $sCheckedId = $sSelectedPaymentID;
00495             } else {
00496                 // #1010A.
00497                 if ( $oUser = $this->getUser()) {
00498                     $oOrder = oxNew('oxorder');
00499                     if ( ( $sLastPaymentId = $oOrder->getLastUserPaymentType( $oUser->getId()) ) ) {
00500                         $sCheckedId = $sLastPaymentId;
00501                     }
00502                 }
00503             }
00504 
00505             // #M253 set to selected payment in db
00506             if ( !$sCheckedId && $this->_sCheckedId ) {
00507                 $sCheckedId = $this->_sCheckedId;
00508             }
00509 
00510             // #646
00511             $oPaymentList = $this->getPaymentList();
00512             if ( isset( $oPaymentList ) && $oPaymentList && !isset( $oPaymentList[$sCheckedId] ) ) {
00513                 end($oPaymentList);
00514                 $sCheckedId = key( $oPaymentList );
00515             }
00516             $this->_sCheckedPaymentId = $sCheckedId;
00517         }
00518 
00519         return $this->_sCheckedPaymentId;
00520     }
00521 
00527     public function getPaymentCnt()
00528     {
00529         if ( $this->_iPaymentCnt === null ) {
00530             $this->_iPaymentCnt = false;
00531 
00532             if ($oPaymentList = $this->getPaymentList()) {
00533                 $this->_iPaymentCnt = count($oPaymentList);
00534             }
00535         }
00536         return $this->_iPaymentCnt;
00537     }
00538 
00544     public function getCreditYears()
00545     {
00546         if ( $this->_aCreditYears === null ) {
00547             $this->_aCreditYears = false;
00548 
00549             $this->_aCreditYears = range( date('Y'), date('Y') + 10 );
00550         }
00551         return $this->_aCreditYears;
00552     }
00553 
00564     protected function _filterDynData()
00565     {
00566         //in case we actually ARE allowed to store the data
00567         if (oxConfig::getInstance()->getConfigParam("blStoreCreditCardInfo"))
00568             //then do nothing
00569             return;
00570 
00571         $aDynData = $this->getSession()->getVar("dynvalue");
00572 
00573         if ($aDynData) {
00574             $aDynData["kktype"] = null;
00575             $aDynData["kknumber"] = null;
00576             $aDynData["kkname"] = null;
00577             $aDynData["kkmonth"] = null;
00578             $aDynData["kkyear"] = null;
00579             $aDynData["kkpruef"] = null;
00580             oxSession::setVar("dynvalue", $aDynData);
00581         }
00582 
00583 
00584         unset($_REQUEST["dynvalue"]["kktype"]);
00585         unset($_REQUEST["dynvalue"]["kknumber"]);
00586         unset($_REQUEST["dynvalue"]["kkname"]);
00587         unset($_REQUEST["dynvalue"]["kkmonth"]);
00588         unset($_REQUEST["dynvalue"]["kkyear"]);
00589         unset($_REQUEST["dynvalue"]["kkpruef"]);
00590 
00591         unset($_POST["dynvalue"]["kktype"]);
00592         unset($_POST["dynvalue"]["kknumber"]);
00593         unset($_POST["dynvalue"]["kkname"]);
00594         unset($_POST["dynvalue"]["kkmonth"]);
00595         unset($_POST["dynvalue"]["kkyear"]);
00596         unset($_POST["dynvalue"]["kkpruef"]);
00597 
00598         unset($_GET["dynvalue"]["kktype"]);
00599         unset($_GET["dynvalue"]["kknumber"]);
00600         unset($_GET["dynvalue"]["kkname"]);
00601         unset($_GET["dynvalue"]["kkmonth"]);
00602         unset($_GET["dynvalue"]["kkyear"]);
00603         unset($_GET["dynvalue"]["kkpruef"]);
00604 
00605     }
00606 
00612     public function getTsProtections()
00613     {
00614         if ( $this->_aTsProducts === null ) {
00615             $oBasket = $this->getSession()->getBasket();
00616             if ( $dPrice = $oBasket->getPrice()->getBruttoPrice() ) {
00617                 $oTsProtection = oxNew('oxtsprotection');
00618                 $this->_aTsProducts = $oTsProtection->getTsProducts($dPrice);
00619             }
00620         }
00621         return $this->_aTsProducts;
00622     }
00623 
00629     public function getCheckedTsProductId()
00630     {
00631         if ( $this->_sCheckedProductId === null ) {
00632             $this->_sCheckedProductId = false;
00633             if ( $sId = oxConfig::getParameter( 'stsprotection' ) ) {
00634                 $this->_sCheckedProductId = $sId;
00635             }
00636         }
00637         return $this->_sCheckedProductId;
00638     }
00639 
00645     public function getBreadCrumb()
00646     {
00647         $aPaths = array();
00648         $aPath = array();
00649 
00650 
00651         $aPath['title'] = oxLang::getInstance()->translateString( 'PAGE_CHECKOUT_PAY', oxLang::getInstance()->getBaseLanguage(), false );
00652         $aPath['link']  = $this->getLink();
00653 
00654         $aPaths[] = $aPath;
00655 
00656         return $aPaths;
00657     }
00658 
00659 }