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 = '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 
00120     public function render()
00121     {
00122         $myConfig  = $this->getConfig();
00123 
00124         if ($myConfig->getConfigParam( 'blPsBasketReservationEnabled' )) {
00125             $this->getSession()->getBasketReservations()->renewExpiration();
00126         }
00127 
00128         parent::render();
00129 
00130         //if it happens that you are not in SSL
00131         //then forcing to HTTPS
00132 
00133         //but first checking maybe there were redirection already to prevent infinite redirections due to possible buggy ssl detection on server
00134         $blAlreadyRedirected = oxConfig::getParameter( 'sslredirect' ) == 'forced';
00135 
00136         if ( $myConfig->getCurrentShopURL() != $myConfig->getSSLShopURL() && !$blAlreadyRedirected && !oxConfig::getParameter('fnc') ) {
00137             $sPayError = oxConfig::getParameter( 'payerror' )?'payerror='.oxConfig::getParameter( 'payerror' ):'';
00138             $sPayErrorText = oxConfig::getParameter('payerrortext')?'payerrortext='.oxConfig::getParameter( 'payerrortext' ):'';
00139             $sRedirectURL = $myConfig->getShopSecureHomeURL().'sslredirect=forced&cl=payment&'.$sPayError."&".$sPayErrorText;
00140             oxUtils::getInstance()->redirect( $sRedirectURL );
00141         }
00142 
00143         if ( $this->getIsOrderStep() ) {
00144 
00145             //additional check if we really really have a user now
00146             //and the basket is not empty
00147             $oBasket = $this->getSession()->getBasket();
00148             if ( $myConfig->getConfigParam( 'blPsBasketReservationEnabled' ) && (!$oBasket || ( $oBasket && !$oBasket->getProductsCount() )) ) {
00149                 oxUtils::getInstance()->redirect( $myConfig->getShopHomeURL() .'cl=basket' );
00150             }
00151 
00152             $oUser = $this->getUser();
00153             if ( !$oBasket || !$oUser || ( $oBasket && !$oBasket->getProductsCount() ) ) {
00154                 oxUtils::getInstance()->redirect( $myConfig->getShopHomeURL() .'cl=start' );
00155             }
00156         }
00157 
00158         // passing payments to view
00159         $this->_aViewData[ 'payments' ] = $this->getPaymentList();
00160 
00161         // #955A. must recalculate count
00162         $this->_aViewData['allShipsetsCnt'] = $this->getAllSetsCnt();
00163         $this->_aViewData['allShipsets']    = $this->getAllSets();
00164 
00165         if ( !$this->getAllSetsCnt() ) {
00166             // no fitting shipping set found, setting default empty payment
00167             $this->_setDefaultEmptyPayment();
00168             oxSession::setVar( 'sShipSet', null );
00169         }
00170 
00171         $this->_unsetPaymentErrors();
00172 
00173         $this->_aViewData['oxemptypayment'] = $this->getEmptyPayment();
00174         $this->_aViewData['payerror']       = $this->getPaymentError();
00175         $this->_aViewData['payerrortext']   = $this->getPaymentErrorText();
00176 
00177         $this->_aViewData['dynvalue']  = $this->getDynValue();
00178 
00179         // get checked payment ID
00180         $this->_aViewData['defpaymentid'] = $this->getCheckedPaymentId();
00181         $this->_aViewData['paymencnt']    = $this->getPaymentCnt();
00182 
00183         //add a array with current years for the credit card drop down box
00184         $this->_aViewData['creditYears'] = $this->getCreditYears();
00185 
00186         return $this->_sThisTemplate;
00187     }
00188 
00196     protected function _setDefaultEmptyPayment()
00197     {
00198         // no shipping method there !!
00199         if ( $this->getConfig()->getConfigParam( 'blOtherCountryOrder' ) ) {
00200             $oPayment = oxNew( 'oxpayment' );
00201             if ( $oPayment->load( 'oxempty' ) ) {
00202                 $this->_oEmptyPayment = $oPayment;
00203             } else {
00204                 // some error with setup ??
00205                 $this->_sPaymentError = -2;
00206             }
00207         } else {
00208             $this->_sPaymentError = -2;
00209         }
00210     }
00211 
00217     protected function _unsetPaymentErrors()
00218     {
00219         $iPayError     = oxConfig::getParameter( 'payerror' );
00220         $iPayErrorText = oxConfig::getParameter( 'payerrortext' );
00221 
00222         if ( $iPayError ) {
00223             oxSession::deleteVar( 'payerror' );
00224             $this->_sPaymentError = $iPayError;
00225             //QWERTY
00226         }
00227         if ( $iPayErrorText ) {
00228             oxSession::deleteVar( 'payerrortext' );
00229             $this->_sPaymentErrorText = $iPayErrorText;
00230             //QWERTY
00231         }
00232     }
00233 
00240     public function changeshipping()
00241     {
00242         $mySession = $this->getSession();
00243 
00244         oxSession::setVar( 'sShipSet', oxConfig::getParameter( 'sShipSet' ) );
00245         $oBasket = $mySession->getBasket();
00246         $oBasket->setShipping( null );
00247         $oBasket->onUpdate();
00248     }
00249 
00261     public function validatePayment()
00262     {
00263         $myConfig  = $this->getConfig();
00264         $mySession = $this->getSession();
00265 
00266         //#1308C - check user. Function is executed before render(), and oUser is not set!
00267         // Set it manually for use in methods getPaymentList(), getShippingSetList()...
00268         $oUser = $this->getUser();
00269         if ( !$oUser ) {
00270             oxSession::setVar( 'payerror', 2 );
00271             return;
00272         }
00273 
00274         if (! ($sShipSetId = oxConfig::getParameter( 'sShipSet' ))) {
00275             $sShipSetId = oxSession::getVar('sShipSet');
00276         }
00277         $sPaymentId = oxConfig::getParameter( 'paymentid' );
00278         $aDynvalue  = oxConfig::getParameter( 'dynvalue' );
00279 
00280         // A. additional protection
00281         if ( !$myConfig->getConfigParam( 'blOtherCountryOrder' ) && $sPaymentId == 'oxempty' ) {
00282             $sPaymentId = '';
00283         }
00284 
00285         //#1308C - check if we have paymentID, and it really exists
00286         if ( !$sPaymentId ) {
00287             oxSession::setVar( 'payerror', 1 );
00288             return;
00289         }
00290 
00291         $oBasket = $mySession->getBasket();
00292         $oBasket->setPayment(null);
00293         $oPayment = oxNew( 'oxpayment' );
00294         $oPayment->load( $sPaymentId );
00295 
00296         // getting basket price for payment calculation
00297         $dBasketPrice = $oBasket->getPriceForPayment();
00298 
00299         $blOK = $oPayment->isValidPayment( $aDynvalue, $myConfig->getShopId(), $oUser, $dBasketPrice, $sShipSetId );
00300 
00301         if ( $blOK ) {
00302             oxSession::setVar( 'paymentid', $sPaymentId );
00303             oxSession::setVar( 'dynvalue', $aDynvalue );
00304             if ( oxConfig::getParameter( 'bltsprotection' ) ) {
00305                 $sTsProductId = oxConfig::getParameter( 'stsprotection' );
00306                 $oBasket->setTsProductId($sTsProductId);
00307                 oxSession::setVar( 'stsprotection', $sTsProductId );
00308             } else {
00309                 oxSession::deleteVar( 'stsprotection' );
00310                 $oBasket->setTsProductId(null);
00311             }
00312             $oBasket->setShipping($sShipSetId);
00313             oxSession::deleteVar( '_selected_paymentid' );
00314             return 'order';
00315         } else {
00316             oxSession::setVar( 'payerror', $oPayment->getPaymentErrorNumber() );
00317 
00318             //#1308C - delete paymentid from session, and save selected it just for view
00319             oxSession::deleteVar( 'paymentid' );
00320             oxSession::setVar( '_selected_paymentid', $sPaymentId );
00321             oxSession::deleteVar( 'stsprotection' );
00322             $oBasket->setTsProductId(null);
00323             return;
00324         }
00325     }
00326 
00332     public function getPaymentList()
00333     {
00334         if ( $this->_oPaymentList === null ) {
00335             $this->_oPaymentList = false;
00336 
00337             $sActShipSet = oxConfig::getParameter( 'sShipSet' );
00338             $oBasket = $this->getSession()->getBasket();
00339 
00340             // load sets, active set, and active set payment list
00341             list( $aAllSets, $sActShipSet, $aPaymentList ) = oxDeliverySetList::getInstance()->getDeliverySetData( $sActShipSet, $this->getUser(), $oBasket );
00342 
00343             oxSession::setVar( 'sShipSet', $sActShipSet );
00344             $oBasket->setShipping( $sActShipSet );
00345 
00346             // calculating payment expences for preview for each payment
00347             $this->_setDeprecatedValues( $aPaymentList, $oBasket );
00348             $this->_oPaymentList = $aPaymentList;
00349             $this->_aAllSets     = $aAllSets;
00350 
00351         }
00352         return $this->_oPaymentList;
00353     }
00354 
00360     public function getAllSets()
00361     {
00362         if ( $this->_aAllSets === null ) {
00363             $this->_aAllSets = false;
00364 
00365             if ($this->getPaymentList()) {
00366                 return $this->_aAllSets;
00367             }
00368         }
00369         return $this->_aAllSets;
00370     }
00371 
00377     public function getAllSetsCnt()
00378     {
00379         if ( $this->_iAllSetsCnt === null ) {
00380             $this->_iAllSetsCnt = 0;
00381 
00382             if ($this->getPaymentList()) {
00383                 $this->_iAllSetsCnt = count($this->_aAllSets);
00384             }
00385         }
00386         return $this->_iAllSetsCnt;
00387     }
00388 
00397     protected function _setDeprecatedValues( & $aPaymentList, $oBasket = null )
00398     {
00399         if ( is_array($aPaymentList) ) {
00400             $oLang = oxLang::getInstance();
00401             foreach ( $aPaymentList as $oPayment ) {
00402                 $oPrice = $oPayment->getPaymentPrice( $oBasket );
00403                 $oPayment->dAddPaymentSum = $oPrice->getBruttoPrice();
00404                 $oPayment->fAddPaymentSum = $oLang->formatCurrency( $oPayment->dAddPaymentSum, $oBasket->getBasketCurrency() );
00405                 $oPayment->aDynValues     = $oPayment->getDynValues();
00406                 if ( $oPayment->oxpayments__oxchecked->value ) {
00407                     $this->_sCheckedId = $oPayment->getId();
00408                 }
00409             }
00410         }
00411     }
00412 
00418     public function getEmptyPayment()
00419     {
00420         return $this->_oEmptyPayment;
00421     }
00422 
00428     public function getPaymentError()
00429     {
00430         return $this->_sPaymentError;
00431     }
00432 
00438     public function getPaymentErrorText()
00439     {
00440         return $this->_sPaymentErrorText;
00441     }
00442 
00448     public function getDynValue()
00449     {
00450         if ( $this->_aDynValue === null ) {
00451             $this->_aDynValue = false;
00452 
00453             // #1217 R
00454             if ( ( $aDynValue = oxSession::getVar( 'dynvalue' ) ) ) {
00455                 $this->_aDynValue  = $aDynValue;
00456             } else {
00457                 $this->_aDynValue  = oxConfig::getParameter( "dynvalue");
00458             }
00459 
00460             // #701A
00461             // assign debit note payment params to view data
00462             $aPaymentList = $this->getPaymentList();
00463             if ( isset( $aPaymentList['oxiddebitnote'] ) ) {
00464                 $this->_assignDebitNoteParams();
00465             }
00466         }
00467         return $this->_aDynValue;
00468     }
00469 
00476     protected function _assignDebitNoteParams()
00477     {
00478         // #701A
00479         $oUserPayment = oxNew( 'oxuserpayment');
00480         //such info available ?
00481         if ( $oUserPayment->getPaymentByPaymentType( $this->getUser(), 'oxiddebitnote' ) ) {
00482             $aAddPaymentData = oxUtils::getInstance()->assignValuesFromText( $oUserPayment->oxuserpayments__oxvalue->value );
00483 
00484             //checking if some of values is allready set in session - leave it
00485             foreach ( $aAddPaymentData as $oData ) {
00486                 if ( !isset( $this->_aDynValue[$oData->name] ) ||
00487                    (  isset( $this->_aDynValue[$oData->name] ) && !$this->_aDynValue[$oData->name] ) ) {
00488                     $this->_aDynValue[$oData->name] = $oData->value;
00489                 }
00490             }
00491         }
00492     }
00493 
00500     public function getCheckedPaymentId()
00501     {
00502         if ( $this->_sCheckedPaymentId === null ) {
00503             if ( ( $sPaymentID = oxConfig::getParameter( 'paymentid' ) ) ) {
00504                 $sCheckedId = $sPaymentID;
00505             } elseif ( ( $sSelectedPaymentID = oxSession::getVar( '_selected_paymentid' ) ) ) {
00506                 $sCheckedId = $sSelectedPaymentID;
00507             } else {
00508                 // #1010A.
00509                 if ( $oUser = $this->getUser()) {
00510                     $oOrder = oxNew('oxorder');
00511                     if ( ( $sLastPaymentId = $oOrder->getLastUserPaymentType( $oUser->getId()) ) ) {
00512                         $sCheckedId = $sLastPaymentId;
00513                     }
00514                 }
00515             }
00516 
00517             // #M253 set to selected payment in db
00518             if ( !$sCheckedId && $this->_sCheckedId ) {
00519                 $sCheckedId = $this->_sCheckedId;
00520             }
00521 
00522             // #646
00523             $oPaymentList = $this->getPaymentList();
00524             if ( isset( $oPaymentList ) && $oPaymentList && !isset( $oPaymentList[$sCheckedId] ) ) {
00525                 end($oPaymentList);
00526                 $sCheckedId = key( $oPaymentList );
00527             }
00528             $this->_sCheckedPaymentId = $sCheckedId;
00529         }
00530 
00531         return $this->_sCheckedPaymentId;
00532     }
00533 
00539     public function getPaymentCnt()
00540     {
00541         if ( $this->_iPaymentCnt === null ) {
00542             $this->_iPaymentCnt = false;
00543 
00544             if ($oPaymentList = $this->getPaymentList()) {
00545                 $this->_iPaymentCnt = count($oPaymentList);
00546             }
00547         }
00548         return $this->_iPaymentCnt;
00549     }
00550 
00556     public function getCreditYears()
00557     {
00558         if ( $this->_aCreditYears === null ) {
00559             $this->_aCreditYears = false;
00560 
00561             $this->_aCreditYears = range( date('Y'), date('Y') + 10 );
00562         }
00563         return $this->_aCreditYears;
00564     }
00565 
00576     protected function _filterDynData()
00577     {
00578         //in case we actually ARE allowed to store the data
00579         if (oxConfig::getInstance()->getConfigParam("blStoreCreditCardInfo"))
00580             //then do nothing
00581             return;
00582 
00583         $aDynData = $this->getSession()->getVar("dynvalue");
00584 
00585         if ($aDynData) {
00586             $aDynData["kktype"] = null;
00587             $aDynData["kknumber"] = null;
00588             $aDynData["kkname"] = null;
00589             $aDynData["kkmonth"] = null;
00590             $aDynData["kkyear"] = null;
00591             $aDynData["kkpruef"] = null;
00592             $this->getSession()->setVar("dynvalue", $aDynData);
00593         }
00594 
00595 
00596         unset($_REQUEST["dynvalue"]["kktype"]);
00597         unset($_REQUEST["dynvalue"]["kknumber"]);
00598         unset($_REQUEST["dynvalue"]["kkname"]);
00599         unset($_REQUEST["dynvalue"]["kkmonth"]);
00600         unset($_REQUEST["dynvalue"]["kkyear"]);
00601         unset($_REQUEST["dynvalue"]["kkpruef"]);
00602 
00603         unset($_POST["dynvalue"]["kktype"]);
00604         unset($_POST["dynvalue"]["kknumber"]);
00605         unset($_POST["dynvalue"]["kkname"]);
00606         unset($_POST["dynvalue"]["kkmonth"]);
00607         unset($_POST["dynvalue"]["kkyear"]);
00608         unset($_POST["dynvalue"]["kkpruef"]);
00609 
00610         unset($_GET["dynvalue"]["kktype"]);
00611         unset($_GET["dynvalue"]["kknumber"]);
00612         unset($_GET["dynvalue"]["kkname"]);
00613         unset($_GET["dynvalue"]["kkmonth"]);
00614         unset($_GET["dynvalue"]["kkyear"]);
00615         unset($_GET["dynvalue"]["kkpruef"]);
00616 
00617     }
00618 
00624     public function getTsProtections()
00625     {
00626         if ( $this->_aTsProducts === null ) {
00627             $oBasket = $this->getSession()->getBasket();
00628             if ( $dPrice = $oBasket->getPrice()->getBruttoPrice() ) {
00629                 $oTsProtection = oxNew('oxtsprotection');
00630                 $this->_aTsProducts = $oTsProtection->getTsProducts($dPrice);
00631             }
00632         }
00633         return $this->_aTsProducts;
00634     }
00635 
00641     public function getCheckedTsProductId()
00642     {
00643         if ( $this->_sCheckedProductId === null ) {
00644             $this->_sCheckedProductId = false;
00645             if ( $sId = oxConfig::getParameter( 'stsprotection' ) ) {
00646                 $this->_sCheckedProductId = $sId;
00647             }
00648         }
00649         return $this->_sCheckedProductId;
00650     }
00651 
00652 }