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     
00104     public function init()
00105     {
00106         $this->_filterDynData();
00107         parent::init();
00108     }
00109 
00121     public function render()
00122     {
00123         $myConfig  = $this->getConfig();
00124 
00125         if ($myConfig->getConfigParam( 'blPsBasketReservationEnabled' )) {
00126             $this->getSession()->getBasketReservations()->renewExpiration();
00127         }
00128 
00129         parent::render();
00130 
00131         //if it happens that you are not in SSL
00132         //then forcing to HTTPS
00133 
00134         //but first checking maybe there were redirection already to prevent infinite redirections due to possible buggy ssl detection on server
00135         $blAlreadyRedirected = oxConfig::getParameter( 'sslredirect' ) == 'forced';
00136 
00137         if ( $this->getIsOrderStep() ) {
00138 
00139             //additional check if we really really have a user now
00140             //and the basket is not empty
00141             $oBasket = $this->getSession()->getBasket();
00142             if ( $myConfig->getConfigParam( 'blPsBasketReservationEnabled' ) && (!$oBasket || ( $oBasket && !$oBasket->getProductsCount() )) ) {
00143                 oxUtils::getInstance()->redirect( $myConfig->getShopHomeURL() .'cl=basket', true, 302 );
00144             }
00145 
00146             $oUser = $this->getUser();
00147             if (!$oUser && ($oBasket && $oBasket->getProductsCount() > 0)) {
00148                 oxUtils::getInstance()->redirect( $myConfig->getShopHomeURL() .'cl=basket', false, 302 );
00149             } elseif ( !$oBasket || !$oUser || ( $oBasket && !$oBasket->getProductsCount() ) ) {
00150                 oxUtils::getInstance()->redirect( $myConfig->getShopHomeURL() .'cl=start', false, 302 );
00151             }
00152         }
00153 
00154         if ( $myConfig->getCurrentShopURL() != $myConfig->getSSLShopURL() && !$blAlreadyRedirected && !oxConfig::getParameter('fnc') ) {
00155             $sPayError = oxConfig::getParameter( 'payerror' )?'payerror='.oxConfig::getParameter( 'payerror' ):'';
00156             $sPayErrorText = oxConfig::getParameter('payerrortext')?'payerrortext='.oxConfig::getParameter( 'payerrortext' ):'';
00157             $sRedirectURL = $myConfig->getShopSecureHomeURL().'sslredirect=forced&cl=payment&'.$sPayError."&".$sPayErrorText;
00158             oxUtils::getInstance()->redirect( $sRedirectURL, true, 302 );
00159         }
00160 
00161         if ( !$this->getAllSetsCnt() ) {
00162             // no fitting shipping set found, setting default empty payment
00163             $this->_setDefaultEmptyPayment();
00164             oxSession::setVar( 'sShipSet', null );
00165         }
00166 
00167         $this->_unsetPaymentErrors();
00168 
00169         return $this->_sThisTemplate;
00170     }
00171 
00179     protected function _setDefaultEmptyPayment()
00180     {
00181         // no shipping method there !!
00182         if ( $this->getConfig()->getConfigParam( 'blOtherCountryOrder' ) ) {
00183             $oPayment = oxNew( 'oxpayment' );
00184             if ( $oPayment->load( 'oxempty' ) ) {
00185                 $this->_oEmptyPayment = $oPayment;
00186             } else {
00187                 // some error with setup ??
00188                 $this->_sPaymentError = -2;
00189             }
00190         } else {
00191             $this->_sPaymentError = -2;
00192         }
00193     }
00194 
00200     protected function _unsetPaymentErrors()
00201     {
00202         $iPayError     = oxConfig::getParameter( 'payerror' );
00203         $sPayErrorText = oxConfig::getParameter( 'payerrortext' );
00204 
00205         if (!($iPayError || $sPayErrorText)) {
00206             $iPayError     = oxSession::getVar( 'payerror' );
00207             $sPayErrorText = oxSession::getVar( 'payerrortext' );
00208         }
00209 
00210         if ( $iPayError ) {
00211             oxSession::deleteVar( 'payerror' );
00212             $this->_sPaymentError = $iPayError;
00213         }
00214         if ( $sPayErrorText ) {
00215             oxSession::deleteVar( 'payerrortext' );
00216             $this->_sPaymentErrorText = $sPayErrorText;
00217         }
00218     }
00219 
00226     public function changeshipping()
00227     {
00228         $mySession = $this->getSession();
00229 
00230         $oBasket = $mySession->getBasket();
00231         $oBasket->setShipping( null );
00232         $oBasket->onUpdate();
00233         oxSession::setVar( 'sShipSet', oxConfig::getParameter( 'sShipSet' ) );
00234     }
00235 
00247     public function validatePayment()
00248     {
00249         $myConfig  = $this->getConfig();
00250         $mySession = $this->getSession();
00251 
00252         //#1308C - check user. Function is executed before render(), and oUser is not set!
00253         // Set it manually for use in methods getPaymentList(), getShippingSetList()...
00254         $oUser = $this->getUser();
00255         if ( !$oUser ) {
00256             oxSession::setVar( 'payerror', 2 );
00257             return;
00258         }
00259 
00260         if (! ($sShipSetId = oxConfig::getParameter( 'sShipSet' ))) {
00261             $sShipSetId = oxSession::getVar('sShipSet');
00262         }
00263         if (! ($sPaymentId = oxConfig::getParameter( 'paymentid' ))) {
00264             $sPaymentId = oxSession::getVar('paymentid');
00265         }
00266         if (! ($aDynvalue = oxConfig::getParameter( 'dynvalue' ))) {
00267             $aDynvalue = oxSession::getVar('dynvalue');
00268         }
00269 
00270         // A. additional protection
00271         if ( !$myConfig->getConfigParam( 'blOtherCountryOrder' ) && $sPaymentId == 'oxempty' ) {
00272             $sPaymentId = '';
00273         }
00274 
00275         //#1308C - check if we have paymentID, and it really exists
00276         if ( !$sPaymentId ) {
00277             oxSession::setVar( 'payerror', 1 );
00278             return;
00279         }
00280         
00281         if ( $this->getDynDataFiltered() && $sPaymentId == 'oxidcreditcard' ) {
00282             oxSession::setVar( 'payerror', 7 );
00283             return;
00284         }
00285 
00286         $oBasket = $mySession->getBasket();
00287         $oBasket->setPayment(null);
00288         $oPayment = oxNew( 'oxpayment' );
00289         $oPayment->load( $sPaymentId );
00290 
00291         // getting basket price for payment calculation
00292         $dBasketPrice = $oBasket->getPriceForPayment();
00293 
00294         $blOK = $oPayment->isValidPayment( $aDynvalue, $myConfig->getShopId(), $oUser, $dBasketPrice, $sShipSetId );
00295   
00296         if ( $blOK ) {
00297             oxSession::setVar( 'paymentid', $sPaymentId );
00298             oxSession::setVar( 'dynvalue', $aDynvalue );
00299             if ( oxConfig::getParameter( 'bltsprotection' ) ) {
00300                 $sTsProductId = oxConfig::getParameter( 'stsprotection' );
00301                 $oBasket->setTsProductId($sTsProductId);
00302                 oxSession::setVar( 'stsprotection', $sTsProductId );
00303             } else {
00304                 oxSession::deleteVar( 'stsprotection' );
00305                 $oBasket->setTsProductId(null);
00306             }
00307             $oBasket->setShipping($sShipSetId);
00308             oxSession::deleteVar( '_selected_paymentid' );
00309             return 'order';
00310         } else {
00311             oxSession::setVar( 'payerror', $oPayment->getPaymentErrorNumber() );
00312 
00313             //#1308C - delete paymentid from session, and save selected it just for view
00314             oxSession::deleteVar( 'paymentid' );
00315             oxSession::setVar( '_selected_paymentid', $sPaymentId );
00316             oxSession::deleteVar( 'stsprotection' );
00317             $oBasket->setTsProductId(null);
00318             return;
00319         }
00320     }
00321 
00327     public function getPaymentList()
00328     {
00329         if ( $this->_oPaymentList === null ) {
00330             $this->_oPaymentList = false;
00331 
00332             $sActShipSet = oxConfig::getParameter( 'sShipSet' );
00333             if ( !$sActShipSet ) {
00334                  $sActShipSet = oxSession::getVar( 'sShipSet' );
00335             }
00336 
00337             $oBasket = $this->getSession()->getBasket();
00338 
00339             // load sets, active set, and active set payment list
00340             list( $aAllSets, $sActShipSet, $aPaymentList ) = oxDeliverySetList::getInstance()->getDeliverySetData( $sActShipSet, $this->getUser(), $oBasket );
00341 
00342             $oBasket->setShipping( $sActShipSet );
00343 
00344             // calculating payment expences for preview for each payment
00345             $this->_setDeprecatedValues( $aPaymentList, $oBasket );
00346             $this->_oPaymentList = $aPaymentList;
00347             $this->_aAllSets     = $aAllSets;
00348 
00349         }
00350         return $this->_oPaymentList;
00351     }
00352 
00358     public function getAllSets()
00359     {
00360         if ( $this->_aAllSets === null ) {
00361             $this->_aAllSets = false;
00362 
00363             if ($this->getPaymentList()) {
00364                 return $this->_aAllSets;
00365             }
00366         }
00367         return $this->_aAllSets;
00368     }
00369 
00375     public function getAllSetsCnt()
00376     {
00377         if ( $this->_iAllSetsCnt === null ) {
00378             $this->_iAllSetsCnt = 0;
00379 
00380             if ($this->getPaymentList()) {
00381                 $this->_iAllSetsCnt = count($this->_aAllSets);
00382             }
00383         }
00384         return $this->_iAllSetsCnt;
00385     }
00386 
00395     protected function _setDeprecatedValues( & $aPaymentList, $oBasket = null )
00396     {
00397         if ( is_array($aPaymentList) ) {
00398             $oLang = oxLang::getInstance();
00399             foreach ( $aPaymentList as $oPayment ) {
00400                 $oPrice = $oPayment->getPaymentPrice( $oBasket );
00401                 $oPayment->dAddPaymentSum = $oPrice->getBruttoPrice();
00402                 $oPayment->fAddPaymentSum = $oLang->formatCurrency( $oPayment->dAddPaymentSum, $oBasket->getBasketCurrency() );
00403                 $oPayment->aDynValues     = $oPayment->getDynValues();
00404                 if ( $oPayment->oxpayments__oxchecked->value ) {
00405                     $this->_sCheckedId = $oPayment->getId();
00406                 }
00407             }
00408         }
00409     }
00410 
00416     public function getEmptyPayment()
00417     {
00418         return $this->_oEmptyPayment;
00419     }
00420 
00426     public function getPaymentError()
00427     {
00428         return $this->_sPaymentError;
00429     }
00430 
00436     public function getDynDataFiltered()
00437     {
00438         return $this->_blDynDataFiltered;
00439     }
00440 
00446     public function getPaymentErrorText()
00447     {
00448         return $this->_sPaymentErrorText;
00449     }
00450 
00456     public function getDynValue()
00457     {
00458         if ( $this->_aDynValue === null ) {
00459             $this->_aDynValue = false;
00460 
00461             // flyspray#1217 (sarunas)
00462             if ( ( $aDynValue = oxSession::getVar( 'dynvalue' ) ) ) {
00463                 $this->_aDynValue  = $aDynValue;
00464             } else {
00465                 $this->_aDynValue  = oxConfig::getParameter( "dynvalue");
00466             }
00467 
00468             // #701A
00469             // assign debit note payment params to view data
00470             $aPaymentList = $this->getPaymentList();
00471             if ( isset( $aPaymentList['oxiddebitnote'] ) ) {
00472                 $this->_assignDebitNoteParams();
00473             }
00474         }
00475         return $this->_aDynValue;
00476     }
00477 
00484     protected function _assignDebitNoteParams()
00485     {
00486         // #701A
00487         $oUserPayment = oxNew( 'oxuserpayment');
00488         //such info available ?
00489         if ( $oUserPayment->getPaymentByPaymentType( $this->getUser(), 'oxiddebitnote' ) ) {
00490             $aAddPaymentData = oxUtils::getInstance()->assignValuesFromText( $oUserPayment->oxuserpayments__oxvalue->value );
00491 
00492             //checking if some of values is allready set in session - leave it
00493             foreach ( $aAddPaymentData as $oData ) {
00494                 if ( !isset( $this->_aDynValue[$oData->name] ) ||
00495                    (  isset( $this->_aDynValue[$oData->name] ) && !$this->_aDynValue[$oData->name] ) ) {
00496                     $this->_aDynValue[$oData->name] = $oData->value;
00497                 }
00498             }
00499         }
00500     }
00501 
00508     public function getCheckedPaymentId()
00509     {
00510         if ( $this->_sCheckedPaymentId === null ) {
00511             if (! ($sPaymentID = oxConfig::getParameter( 'paymentid' ))) {
00512                 $sPaymentID = oxSession::getVar('paymentid');
00513             }
00514             if ( $sPaymentID ) {
00515                 $sCheckedId = $sPaymentID;
00516             } elseif ( ( $sSelectedPaymentID = oxSession::getVar( '_selected_paymentid' ) ) ) {
00517                 $sCheckedId = $sSelectedPaymentID;
00518             } else {
00519                 // #1010A.
00520                 if ( $oUser = $this->getUser()) {
00521                     $oOrder = oxNew('oxorder');
00522                     if ( ( $sLastPaymentId = $oOrder->getLastUserPaymentType( $oUser->getId()) ) ) {
00523                         $sCheckedId = $sLastPaymentId;
00524                     }
00525                 }
00526             }
00527 
00528             // #M253 set to selected payment in db
00529             if ( !$sCheckedId && $this->_sCheckedId ) {
00530                 $sCheckedId = $this->_sCheckedId;
00531             }
00532 
00533             // #646
00534             $oPaymentList = $this->getPaymentList();
00535             if ( isset( $oPaymentList ) && $oPaymentList && !isset( $oPaymentList[$sCheckedId] ) ) {
00536                 end($oPaymentList);
00537                 $sCheckedId = key( $oPaymentList );
00538             }
00539             $this->_sCheckedPaymentId = $sCheckedId;
00540         }
00541 
00542         return $this->_sCheckedPaymentId;
00543     }
00544 
00550     public function getPaymentCnt()
00551     {
00552         if ( $this->_iPaymentCnt === null ) {
00553             $this->_iPaymentCnt = false;
00554 
00555             if ($oPaymentList = $this->getPaymentList()) {
00556                 $this->_iPaymentCnt = count($oPaymentList);
00557             }
00558         }
00559         return $this->_iPaymentCnt;
00560     }
00561 
00567     public function getCreditYears()
00568     {
00569         if ( $this->_aCreditYears === null ) {
00570             $this->_aCreditYears = false;
00571 
00572             $this->_aCreditYears = range( date('Y'), date('Y') + 10 );
00573         }
00574         return $this->_aCreditYears;
00575     }
00576 
00585     protected function _checkArrValuesEmpty( $aData, $aKeys ) 
00586     {
00587         if ( !is_array( $aKeys ) || count( $aKeys ) < 1 ) {
00588             return false;
00589         }
00590         
00591         foreach ( $aKeys as $sKey ) {
00592             if ( isset( $aData[$sKey] ) && !empty( $aData[$sKey] ) ) {
00593                 return false;
00594             }
00595         }
00596         
00597         return true;
00598     }
00599 
00600 
00611     protected function _filterDynData()
00612     {
00613         //in case we actually ARE allowed to store the data
00614         if (oxConfig::getInstance()->getConfigParam("blStoreCreditCardInfo")) {
00615             //then do nothing and reset _blDynDataFiltered
00616             $this->_blDynDataFiltered = false;
00617             return;
00618         }
00619             
00620         $aDynData = $this->getSession()->getVar("dynvalue");
00621         
00622         $aFields = array("kktype", "kknumber", "kkname", "kkmonth", "kkyear", "kkpruef");
00623   
00624         if ( $aDynData ) {
00625             if ( !$this->_checkArrValuesEmpty( $aDynData, $aFields ) ) {
00626                 $this->_blDynDataFiltered = true;
00627             }
00628             $aDynData["kktype"] = null;
00629             $aDynData["kknumber"] = null;
00630             $aDynData["kkname"] = null;
00631             $aDynData["kkmonth"] = null;
00632             $aDynData["kkyear"] = null;
00633             $aDynData["kkpruef"] = null;
00634             oxSession::setVar("dynvalue", $aDynData);
00635         }
00636 
00637         if (  !$this->_checkArrValuesEmpty( $_REQUEST["dynvalue"], $aFields ) ||
00638               !$this->_checkArrValuesEmpty( $_POST["dynvalue"], $aFields ) ||
00639               !$this->_checkArrValuesEmpty( $_GET["dynvalue"], $aFields ) ) {
00640             $this->_blDynDataFiltered = true;
00641         }
00642 
00643         unset($_REQUEST["dynvalue"]["kktype"]);
00644         unset($_REQUEST["dynvalue"]["kknumber"]);
00645         unset($_REQUEST["dynvalue"]["kkname"]);
00646         unset($_REQUEST["dynvalue"]["kkmonth"]);
00647         unset($_REQUEST["dynvalue"]["kkyear"]);
00648         unset($_REQUEST["dynvalue"]["kkpruef"]);
00649 
00650         unset($_POST["dynvalue"]["kktype"]);
00651         unset($_POST["dynvalue"]["kknumber"]);
00652         unset($_POST["dynvalue"]["kkname"]);
00653         unset($_POST["dynvalue"]["kkmonth"]);
00654         unset($_POST["dynvalue"]["kkyear"]);
00655         unset($_POST["dynvalue"]["kkpruef"]);
00656 
00657         unset($_GET["dynvalue"]["kktype"]);
00658         unset($_GET["dynvalue"]["kknumber"]);
00659         unset($_GET["dynvalue"]["kkname"]);
00660         unset($_GET["dynvalue"]["kkmonth"]);
00661         unset($_GET["dynvalue"]["kkyear"]);
00662         unset($_GET["dynvalue"]["kkpruef"]);
00663 
00664     }
00665 
00671     public function getTsProtections()
00672     {
00673         if ( $this->_aTsProducts === null ) {
00674             $oBasket = $this->getSession()->getBasket();
00675             if ( $dPrice = $oBasket->getPrice()->getBruttoPrice() ) {
00676                 $oTsProtection = oxNew('oxtsprotection');
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'] = oxLang::getInstance()->translateString( 'PAGE_CHECKOUT_PAY', oxLang::getInstance()->getBaseLanguage(), false );
00711         $aPath['link']  = $this->getLink();
00712 
00713         $aPaths[] = $aPath;
00714 
00715         return $aPaths;
00716     }
00717 
00718 }