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 ( $this->getIsOrderStep() ) {
00132 
00133             //additional check if we really really have a user now
00134             //and the basket is not empty
00135             $oBasket = $this->getSession()->getBasket();
00136             if ( $myConfig->getConfigParam( 'blPsBasketReservationEnabled' ) && (!$oBasket || ( $oBasket && !$oBasket->getProductsCount() )) ) {
00137                 oxUtils::getInstance()->redirect( $myConfig->getShopHomeURL() .'cl=basket', true, 302 );
00138             }
00139 
00140             $oUser = $this->getUser();
00141             if (!$oUser && ($oBasket && $oBasket->getProductsCount() > 0)) {
00142                 oxUtils::getInstance()->redirect( $myConfig->getShopHomeURL() .'cl=basket', false, 302 );
00143             } elseif ( !$oBasket || !$oUser || ( $oBasket && !$oBasket->getProductsCount() ) ) {
00144                 oxUtils::getInstance()->redirect( $myConfig->getShopHomeURL() .'cl=start', false, 302 );
00145             }
00146         }
00147 
00148         if ( $myConfig->getCurrentShopURL() != $myConfig->getSSLShopURL() && !$blAlreadyRedirected && !oxConfig::getParameter('fnc') ) {
00149             $sPayError = oxConfig::getParameter( 'payerror' )?'payerror='.oxConfig::getParameter( 'payerror' ):'';
00150             $sPayErrorText = oxConfig::getParameter('payerrortext')?'payerrortext='.oxConfig::getParameter( 'payerrortext' ):'';
00151             $sRedirectURL = $myConfig->getShopSecureHomeURL().'sslredirect=forced&cl=payment&'.$sPayError."&".$sPayErrorText;
00152             oxUtils::getInstance()->redirect( $sRedirectURL, true, 302 );
00153         }
00154 
00155         if ( !$this->getAllSetsCnt() ) {
00156             // no fitting shipping set found, setting default empty payment
00157             $this->_setDefaultEmptyPayment();
00158             oxSession::setVar( 'sShipSet', null );
00159         }
00160 
00161         $this->_unsetPaymentErrors();
00162 
00163         return $this->_sThisTemplate;
00164     }
00165 
00173     protected function _setDefaultEmptyPayment()
00174     {
00175         // no shipping method there !!
00176         if ( $this->getConfig()->getConfigParam( 'blOtherCountryOrder' ) ) {
00177             $oPayment = oxNew( 'oxpayment' );
00178             if ( $oPayment->load( 'oxempty' ) ) {
00179                 $this->_oEmptyPayment = $oPayment;
00180             } else {
00181                 // some error with setup ??
00182                 $this->_sPaymentError = -2;
00183             }
00184         } else {
00185             $this->_sPaymentError = -2;
00186         }
00187     }
00188 
00194     protected function _unsetPaymentErrors()
00195     {
00196         $iPayError     = oxConfig::getParameter( 'payerror' );
00197         $sPayErrorText = oxConfig::getParameter( 'payerrortext' );
00198 
00199         if (!($iPayError || $sPayErrorText)) {
00200             $iPayError     = oxSession::getVar( 'payerror' );
00201             $sPayErrorText = oxSession::getVar( 'payerrortext' );
00202         }
00203 
00204         if ( $iPayError ) {
00205             oxSession::deleteVar( 'payerror' );
00206             $this->_sPaymentError = $iPayError;
00207         }
00208         if ( $sPayErrorText ) {
00209             oxSession::deleteVar( 'payerrortext' );
00210             $this->_sPaymentErrorText = $sPayErrorText;
00211         }
00212     }
00213 
00220     public function changeshipping()
00221     {
00222         $mySession = $this->getSession();
00223 
00224         $oBasket = $mySession->getBasket();
00225         $oBasket->setShipping( null );
00226         $oBasket->onUpdate();
00227         oxSession::setVar( 'sShipSet', oxConfig::getParameter( 'sShipSet' ) );
00228     }
00229 
00241     public function validatePayment()
00242     {
00243         $myConfig  = $this->getConfig();
00244         $mySession = $this->getSession();
00245 
00246         //#1308C - check user. Function is executed before render(), and oUser is not set!
00247         // Set it manually for use in methods getPaymentList(), getShippingSetList()...
00248         $oUser = $this->getUser();
00249         if ( !$oUser ) {
00250             oxSession::setVar( 'payerror', 2 );
00251             return;
00252         }
00253 
00254         if (! ($sShipSetId = oxConfig::getParameter( 'sShipSet' ))) {
00255             $sShipSetId = oxSession::getVar('sShipSet');
00256         }
00257         if (! ($sPaymentId = oxConfig::getParameter( 'paymentid' ))) {
00258             $sPaymentId = oxSession::getVar('paymentid');
00259         }
00260         if (! ($aDynvalue = oxConfig::getParameter( 'dynvalue' ))) {
00261             $aDynvalue = oxSession::getVar('dynvalue');
00262         }
00263 
00264         // A. additional protection
00265         if ( !$myConfig->getConfigParam( 'blOtherCountryOrder' ) && $sPaymentId == 'oxempty' ) {
00266             $sPaymentId = '';
00267         }
00268 
00269         //#1308C - check if we have paymentID, and it really exists
00270         if ( !$sPaymentId ) {
00271             oxSession::setVar( 'payerror', 1 );
00272             return;
00273         }
00274 
00275         $oBasket = $mySession->getBasket();
00276         $oBasket->setPayment(null);
00277         $oPayment = oxNew( 'oxpayment' );
00278         $oPayment->load( $sPaymentId );
00279 
00280         // getting basket price for payment calculation
00281         $dBasketPrice = $oBasket->getPriceForPayment();
00282 
00283         $blOK = $oPayment->isValidPayment( $aDynvalue, $myConfig->getShopId(), $oUser, $dBasketPrice, $sShipSetId );
00284 
00285         if ( $blOK ) {
00286             oxSession::setVar( 'paymentid', $sPaymentId );
00287             oxSession::setVar( 'dynvalue', $aDynvalue );
00288             if ( oxConfig::getParameter( 'bltsprotection' ) ) {
00289                 $sTsProductId = oxConfig::getParameter( 'stsprotection' );
00290                 $oBasket->setTsProductId($sTsProductId);
00291                 oxSession::setVar( 'stsprotection', $sTsProductId );
00292             } else {
00293                 oxSession::deleteVar( 'stsprotection' );
00294                 $oBasket->setTsProductId(null);
00295             }
00296             $oBasket->setShipping($sShipSetId);
00297             oxSession::deleteVar( '_selected_paymentid' );
00298             return 'order';
00299         } else {
00300             oxSession::setVar( 'payerror', $oPayment->getPaymentErrorNumber() );
00301 
00302             //#1308C - delete paymentid from session, and save selected it just for view
00303             oxSession::deleteVar( 'paymentid' );
00304             oxSession::setVar( '_selected_paymentid', $sPaymentId );
00305             oxSession::deleteVar( 'stsprotection' );
00306             $oBasket->setTsProductId(null);
00307             return;
00308         }
00309     }
00310 
00316     public function getPaymentList()
00317     {
00318         if ( $this->_oPaymentList === null ) {
00319             $this->_oPaymentList = false;
00320 
00321             $sActShipSet = oxConfig::getParameter( 'sShipSet' );
00322             if ( !$sActShipSet ) {
00323                  $sActShipSet = oxSession::getVar( 'sShipSet' );
00324             }
00325 
00326             $oBasket = $this->getSession()->getBasket();
00327 
00328             // load sets, active set, and active set payment list
00329             list( $aAllSets, $sActShipSet, $aPaymentList ) = oxDeliverySetList::getInstance()->getDeliverySetData( $sActShipSet, $this->getUser(), $oBasket );
00330 
00331             $oBasket->setShipping( $sActShipSet );
00332 
00333             // calculating payment expences for preview for each payment
00334             $this->_setDeprecatedValues( $aPaymentList, $oBasket );
00335             $this->_oPaymentList = $aPaymentList;
00336             $this->_aAllSets     = $aAllSets;
00337 
00338         }
00339         return $this->_oPaymentList;
00340     }
00341 
00347     public function getAllSets()
00348     {
00349         if ( $this->_aAllSets === null ) {
00350             $this->_aAllSets = false;
00351 
00352             if ($this->getPaymentList()) {
00353                 return $this->_aAllSets;
00354             }
00355         }
00356         return $this->_aAllSets;
00357     }
00358 
00364     public function getAllSetsCnt()
00365     {
00366         if ( $this->_iAllSetsCnt === null ) {
00367             $this->_iAllSetsCnt = 0;
00368 
00369             if ($this->getPaymentList()) {
00370                 $this->_iAllSetsCnt = count($this->_aAllSets);
00371             }
00372         }
00373         return $this->_iAllSetsCnt;
00374     }
00375 
00384     protected function _setDeprecatedValues( & $aPaymentList, $oBasket = null )
00385     {
00386         if ( is_array($aPaymentList) ) {
00387             $oLang = oxLang::getInstance();
00388             foreach ( $aPaymentList as $oPayment ) {
00389                 $oPrice = $oPayment->getPaymentPrice( $oBasket );
00390                 $oPayment->dAddPaymentSum = $oPrice->getBruttoPrice();
00391                 $oPayment->fAddPaymentSum = $oLang->formatCurrency( $oPayment->dAddPaymentSum, $oBasket->getBasketCurrency() );
00392                 $oPayment->aDynValues     = $oPayment->getDynValues();
00393                 if ( $oPayment->oxpayments__oxchecked->value ) {
00394                     $this->_sCheckedId = $oPayment->getId();
00395                 }
00396             }
00397         }
00398     }
00399 
00405     public function getEmptyPayment()
00406     {
00407         return $this->_oEmptyPayment;
00408     }
00409 
00415     public function getPaymentError()
00416     {
00417         return $this->_sPaymentError;
00418     }
00419 
00425     public function getPaymentErrorText()
00426     {
00427         return $this->_sPaymentErrorText;
00428     }
00429 
00435     public function getDynValue()
00436     {
00437         if ( $this->_aDynValue === null ) {
00438             $this->_aDynValue = false;
00439 
00440             // flyspray#1217 (sarunas)
00441             if ( ( $aDynValue = oxSession::getVar( 'dynvalue' ) ) ) {
00442                 $this->_aDynValue  = $aDynValue;
00443             } else {
00444                 $this->_aDynValue  = oxConfig::getParameter( "dynvalue");
00445             }
00446 
00447             // #701A
00448             // assign debit note payment params to view data
00449             $aPaymentList = $this->getPaymentList();
00450             if ( isset( $aPaymentList['oxiddebitnote'] ) ) {
00451                 $this->_assignDebitNoteParams();
00452             }
00453         }
00454         return $this->_aDynValue;
00455     }
00456 
00463     protected function _assignDebitNoteParams()
00464     {
00465         // #701A
00466         $oUserPayment = oxNew( 'oxuserpayment');
00467         //such info available ?
00468         if ( $oUserPayment->getPaymentByPaymentType( $this->getUser(), 'oxiddebitnote' ) ) {
00469             $aAddPaymentData = oxUtils::getInstance()->assignValuesFromText( $oUserPayment->oxuserpayments__oxvalue->value );
00470 
00471             //checking if some of values is allready set in session - leave it
00472             foreach ( $aAddPaymentData as $oData ) {
00473                 if ( !isset( $this->_aDynValue[$oData->name] ) ||
00474                    (  isset( $this->_aDynValue[$oData->name] ) && !$this->_aDynValue[$oData->name] ) ) {
00475                     $this->_aDynValue[$oData->name] = $oData->value;
00476                 }
00477             }
00478         }
00479     }
00480 
00487     public function getCheckedPaymentId()
00488     {
00489         if ( $this->_sCheckedPaymentId === null ) {
00490             if (! ($sPaymentID = oxConfig::getParameter( 'paymentid' ))) {
00491                 $sPaymentID = oxSession::getVar('paymentid');
00492             }
00493             if ( $sPaymentID ) {
00494                 $sCheckedId = $sPaymentID;
00495             } elseif ( ( $sSelectedPaymentID = oxSession::getVar( '_selected_paymentid' ) ) ) {
00496                 $sCheckedId = $sSelectedPaymentID;
00497             } else {
00498                 // #1010A.
00499                 if ( $oUser = $this->getUser()) {
00500                     $oOrder = oxNew('oxorder');
00501                     if ( ( $sLastPaymentId = $oOrder->getLastUserPaymentType( $oUser->getId()) ) ) {
00502                         $sCheckedId = $sLastPaymentId;
00503                     }
00504                 }
00505             }
00506 
00507             // #M253 set to selected payment in db
00508             if ( !$sCheckedId && $this->_sCheckedId ) {
00509                 $sCheckedId = $this->_sCheckedId;
00510             }
00511 
00512             // #646
00513             $oPaymentList = $this->getPaymentList();
00514             if ( isset( $oPaymentList ) && $oPaymentList && !isset( $oPaymentList[$sCheckedId] ) ) {
00515                 end($oPaymentList);
00516                 $sCheckedId = key( $oPaymentList );
00517             }
00518             $this->_sCheckedPaymentId = $sCheckedId;
00519         }
00520 
00521         return $this->_sCheckedPaymentId;
00522     }
00523 
00529     public function getPaymentCnt()
00530     {
00531         if ( $this->_iPaymentCnt === null ) {
00532             $this->_iPaymentCnt = false;
00533 
00534             if ($oPaymentList = $this->getPaymentList()) {
00535                 $this->_iPaymentCnt = count($oPaymentList);
00536             }
00537         }
00538         return $this->_iPaymentCnt;
00539     }
00540 
00546     public function getCreditYears()
00547     {
00548         if ( $this->_aCreditYears === null ) {
00549             $this->_aCreditYears = false;
00550 
00551             $this->_aCreditYears = range( date('Y'), date('Y') + 10 );
00552         }
00553         return $this->_aCreditYears;
00554     }
00555 
00566     protected function _filterDynData()
00567     {
00568         //in case we actually ARE allowed to store the data
00569         if (oxConfig::getInstance()->getConfigParam("blStoreCreditCardInfo"))
00570             //then do nothing
00571             return;
00572 
00573         $aDynData = $this->getSession()->getVar("dynvalue");
00574 
00575         if ($aDynData) {
00576             $aDynData["kktype"] = null;
00577             $aDynData["kknumber"] = null;
00578             $aDynData["kkname"] = null;
00579             $aDynData["kkmonth"] = null;
00580             $aDynData["kkyear"] = null;
00581             $aDynData["kkpruef"] = null;
00582             oxSession::setVar("dynvalue", $aDynData);
00583         }
00584 
00585 
00586         unset($_REQUEST["dynvalue"]["kktype"]);
00587         unset($_REQUEST["dynvalue"]["kknumber"]);
00588         unset($_REQUEST["dynvalue"]["kkname"]);
00589         unset($_REQUEST["dynvalue"]["kkmonth"]);
00590         unset($_REQUEST["dynvalue"]["kkyear"]);
00591         unset($_REQUEST["dynvalue"]["kkpruef"]);
00592 
00593         unset($_POST["dynvalue"]["kktype"]);
00594         unset($_POST["dynvalue"]["kknumber"]);
00595         unset($_POST["dynvalue"]["kkname"]);
00596         unset($_POST["dynvalue"]["kkmonth"]);
00597         unset($_POST["dynvalue"]["kkyear"]);
00598         unset($_POST["dynvalue"]["kkpruef"]);
00599 
00600         unset($_GET["dynvalue"]["kktype"]);
00601         unset($_GET["dynvalue"]["kknumber"]);
00602         unset($_GET["dynvalue"]["kkname"]);
00603         unset($_GET["dynvalue"]["kkmonth"]);
00604         unset($_GET["dynvalue"]["kkyear"]);
00605         unset($_GET["dynvalue"]["kkpruef"]);
00606 
00607     }
00608 
00614     public function getTsProtections()
00615     {
00616         if ( $this->_aTsProducts === null ) {
00617             $oBasket = $this->getSession()->getBasket();
00618             if ( $dPrice = $oBasket->getPrice()->getBruttoPrice() ) {
00619                 $oTsProtection = oxNew('oxtsprotection');
00620                 $this->_aTsProducts = $oTsProtection->getTsProducts($dPrice);
00621             }
00622         }
00623         return $this->_aTsProducts;
00624     }
00625 
00631     public function getCheckedTsProductId()
00632     {
00633         if ( $this->_sCheckedProductId === null ) {
00634             $this->_sCheckedProductId = false;
00635             if ( $sId = oxConfig::getParameter( 'stsprotection' ) ) {
00636                 $this->_sCheckedProductId = $sId;
00637             }
00638         }
00639         return $this->_sCheckedProductId;
00640     }
00641 
00647     public function getBreadCrumb()
00648     {
00649         $aPaths = array();
00650         $aPath = array();
00651 
00652 
00653         $aPath['title'] = oxLang::getInstance()->translateString( 'PAGE_CHECKOUT_PAY', oxLang::getInstance()->getBaseLanguage(), false );
00654         $aPath['link']  = $this->getLink();
00655 
00656         $aPaths[] = $aPath;
00657 
00658         return $aPaths;
00659     }
00660 
00661 }