payment.php

Go to the documentation of this file.
00001 <?php
00002 
00006 require_once oxConfig::getInstance()->getConfigParam( 'sCoreDir' ) . "ccval/ccval.php";
00007 
00012 class Payment extends oxUBase
00013 {
00018     protected $_oPaymentList = null;
00019 
00024     protected $_iPaymentCnt = null;
00025 
00030     protected $_aAllSets = null;
00031 
00036     protected $_iAllSetsCnt = null;
00037 
00042     protected $_oEmptyPayment = null;
00043 
00048     protected $_sPaymentError = null;
00049 
00054     protected $_sPaymentErrorText = null;
00055 
00060     protected $_aDynValue = null;
00061 
00066     protected $_sCheckedId = null;
00067 
00072     protected $_sCheckedPaymentId = null;
00073 
00078     protected $_aCreditYears = null;
00079 
00084     protected $_sThisTemplate = 'payment.tpl';
00085 
00090     protected $_blIsOrderStep = true;
00091 
00097     public function init()
00098     {
00099         parent::init();
00100 
00101         if ( ( $soxAddressId = oxConfig::getParameter( 'oxaddressid' ) ) ) {
00102             oxSession::setVar( 'deladdrid', $soxAddressId );
00103         }
00104     }
00105 
00122     public function render()
00123     {
00124         parent::render();
00125 
00126         $myConfig  = $this->getConfig();
00127 
00128         //if it happens that you are not in SSL
00129         //then forcing to HTTPS
00130 
00131         //but first checking maybe there were redirection already to prevent infinite redirections due to possible buggy ssl detection on server
00132         $blAlreadyRedirected = oxConfig::getParameter( 'sslredirect' ) == 'forced';
00133 
00134         if ( $myConfig->getCurrentShopURL() != $myConfig->getSSLShopURL() && !$blAlreadyRedirected && !oxConfig::getParameter('fnc') ) {
00135             $sPayError = oxConfig::getParameter( 'payerror' )?'payerror='.oxConfig::getParameter( 'payerror' ):'';
00136             $sPayErrorText = oxConfig::getParameter('payerrortext')?'payerrortext='.oxConfig::getParameter( 'payerrortext' ):'';
00137             $sRedirectURL = $myConfig->getShopSecureHomeURL().'sslredirect=forced&cl=payment&'.$sPayError."&".$sPayErrorText;
00138             oxUtils::getInstance()->redirect( $sRedirectURL );
00139         }
00140 
00141         //additional check if we really really have a user now
00142         //and the basket is not empty
00143         $oUser = $this->getUser();
00144         $oBasket = $this->getSession()->getBasket();
00145         if ( !$oBasket || !$oUser || ( $oBasket && !$oBasket->getProductsCount() ) ) {
00146             oxUtils::getInstance()->redirect( $myConfig->getShopHomeURL() );
00147         }
00148 
00149         // passing payments to view
00150         $this->_aViewData[ 'payments' ] = $this->getPaymentList();
00151 
00152         // #955A. must recalculate count
00153         $this->_aViewData['allShipsetsCnt'] = $this->getAllSetsCnt();
00154         $this->_aViewData['allShipsets']    = $this->getAllSets();
00155 
00156         if ( !$this->getAllSetsCnt() ) {
00157             // no fitting shipping set found, setting default empty payment
00158             $this->_setDefaultEmptyPayment();
00159             oxSession::setVar( 'sShipSet', null );
00160         }
00161 
00162         $this->_unsetPaymentErrors();
00163 
00164         $this->_aViewData['oxemptypayment'] = $this->getEmptyPayment();
00165         $this->_aViewData['payerror']       = $this->getPaymentError();
00166         $this->_aViewData['payerrortext']   = $this->getPaymentErrorText();
00167 
00168         $this->_aViewData['dynvalue']  = $this->getDynValue();
00169 
00170         // get checked payment ID
00171         $this->_aViewData['defpaymentid'] = $this->getCheckedPaymentId();
00172         $this->_aViewData['paymencnt']    = $this->getPaymentCnt();
00173 
00174         //add a array with current years for the credit card drop down box
00175         $this->_aViewData['creditYears'] = $this->getCreditYears();
00176 
00177         return $this->_sThisTemplate;
00178     }
00179 
00187     protected function _setDefaultEmptyPayment()
00188     {
00189         // no shipping method there !!
00190         if ( $this->getConfig()->getConfigParam( 'blOtherCountryOrder' ) ) {
00191             $oPayment = oxNew( 'oxpayment' );
00192             if ( $oPayment->load( 'oxempty' ) ) {
00193                 $this->_oEmptyPayment = $oPayment;
00194             } else { // some error with setup ??
00195                 $this->_sPaymentError = -2;
00196             }
00197         } else {
00198             $this->_sPaymentError = -2;
00199         }
00200     }
00201 
00207     protected function _unsetPaymentErrors()
00208     {
00209         $iPayError     = oxConfig::getParameter( 'payerror' );
00210         $iPayErrorText = oxConfig::getParameter( 'payerrortext' );
00211 
00212         if ( $iPayError ) {
00213             oxSession::deleteVar( 'payerror' );
00214             $this->_sPaymentError = $iPayError;
00215             //QWERTY
00216         }
00217         if ( $iPayErrorText ) {
00218             oxSession::deleteVar( 'payerrortext' );
00219             $this->_sPaymentErrorText = $iPayErrorText;
00220             //QWERTY
00221         }
00222     }
00223 
00230     public function changeshipping()
00231     {
00232         $mySession = $this->getSession();
00233 
00234         oxSession::setVar( 'sShipSet', oxConfig::getParameter( 'sShipSet' ) );
00235         $oBasket = $mySession->getBasket();
00236         $oBasket->setShipping( null );
00237         $oBasket->onUpdate();
00238     }
00239 
00253     public function validatePayment()
00254     {
00255         $myConfig  = $this->getConfig();
00256         $mySession = $this->getSession();
00257 
00258         //#1308C - check user. Function is executed before render(), and oUser is not set!
00259         // Set it manually for use in methods getPaymentList(), getShippingSetList()...
00260         $oUser = $this->getUser();
00261         if ( !$oUser ) {
00262             oxSession::setVar( 'payerror', 2 );
00263             return;
00264         }
00265 
00266         if (! ($sShipSetId = oxConfig::getParameter( 'sShipSet' ))) {
00267             $sShipSetId = oxSession::getVar('sShipSet');
00268         }
00269         $sPaymentId = oxConfig::getParameter( 'paymentid' );
00270         $aDynvalue  = oxConfig::getParameter( 'dynvalue' );
00271 
00272         // A. additional protection
00273         if ( !$myConfig->getConfigParam( 'blOtherCountryOrder' ) && $sPaymentId == 'oxempty' ) {
00274             $sPaymentId = '';
00275         }
00276 
00277         //#1308C - check if we have paymentID, and it really exists
00278         if ( !$sPaymentId ) {
00279             oxSession::setVar( 'payerror', 1 );
00280             return;
00281         }
00282 
00283         $oBasket = $mySession->getBasket();
00284         $oBasket->setPayment(null);
00285         $oPayment = oxNew( 'oxpayment' );
00286         $oPayment->load( $sPaymentId );
00287 
00288         // getting basket price for payment calculation
00289         $dBasketPrice = $oBasket->getPriceForPayment();
00290 
00291         $blOK = $oPayment->isValidPayment( $aDynvalue, $myConfig->getShopId(), $oUser, $dBasketPrice, $sShipSetId );
00292 
00293         if ( $blOK ) {
00294             oxSession::setVar( 'paymentid', $sPaymentId );
00295             oxSession::setVar( 'dynvalue', $aDynvalue );
00296             oxSession::deleteVar( '_selected_paymentid' );
00297             return 'order';
00298         } else {
00299             oxSession::setVar( 'payerror', 1 );
00300 
00301             //#1308C - delete paymentid from session, and save selected it just for view
00302             oxSession::deleteVar( 'paymentid' );
00303             oxSession::setVar( '_selected_paymentid', $sPaymentId );
00304             return;
00305         }
00306     }
00307 
00313     public function getPaymentList()
00314     {
00315         if ( $this->_oPaymentList === null ) {
00316             $this->_oPaymentList = false;
00317 
00318             $sActShipSet = oxConfig::getParameter( 'sShipSet' );
00319             $oBasket = $this->getSession()->getBasket();
00320 
00321             // load sets, active set, and active set payment list
00322             list( $aAllSets, $sActShipSet, $aPaymentList ) = oxDeliverySetList::getInstance()->getDeliverySetData( $sActShipSet, $this->getUser(), $oBasket );
00323 
00324             oxSession::setVar( 'sShipSet', $sActShipSet );
00325             // calculating payment expences for preview for each payment
00326             $this->_setDeprecatedValues( $aPaymentList, $oBasket );
00327             $this->_oPaymentList = $aPaymentList;
00328             $this->_aAllSets     = $aAllSets;
00329 
00330         }
00331         return $this->_oPaymentList;
00332     }
00333 
00339     public function getAllSets()
00340     {
00341         if ( $this->_aAllSets === null ) {
00342             $this->_aAllSets = false;
00343 
00344             if ($this->getPaymentList()) {
00345                 return $this->_aAllSets;
00346             }
00347         }
00348         return $this->_aAllSets;
00349     }
00350 
00356     public function getAllSetsCnt()
00357     {
00358         if ( $this->_iAllSetsCnt === null ) {
00359             $this->_iAllSetsCnt = 0;
00360 
00361             if ($this->getPaymentList()) {
00362                 $this->_iAllSetsCnt = count($this->_aAllSets);
00363             }
00364         }
00365         return $this->_iAllSetsCnt;
00366     }
00367 
00376     protected function _setDeprecatedValues( & $aPaymentList, $oBasket = null )
00377     {
00378         if ( is_array($aPaymentList) ) {
00379             $oLang = oxLang::getInstance();
00380             foreach ( $aPaymentList as $oPayment ) {
00381                 $oPrice = $oPayment->getPaymentPrice( $oBasket );
00382                 $oPayment->dAddPaymentSum = $oPrice->getBruttoPrice();
00383                 $oPayment->fAddPaymentSum = $oLang->formatCurrency( $oPayment->dAddPaymentSum, $oBasket->getBasketCurrency() );
00384                 $oPayment->aDynValues     = $oPayment->getDynValues();
00385                 if ( $oPayment->oxpayments__oxchecked->value ) {
00386                     $this->_sCheckedId = $oPayment->getId();
00387                 }
00388             }
00389         }
00390     }
00391 
00397     public function getEmptyPayment()
00398     {
00399         return $this->_oEmptyPayment;
00400     }
00401 
00407     public function getPaymentError()
00408     {
00409         return $this->_sPaymentError;
00410     }
00411 
00417     public function getPaymentErrorText()
00418     {
00419         return $this->_sPaymentErrorText;
00420     }
00421 
00427     public function getDynValue()
00428     {
00429         if ( $this->_aDynValue === null ) {
00430             $this->_aDynValue = false;
00431 
00432             // #1217 R
00433             if ( ( $aDynValue = oxSession::getVar( 'dynvalue' ) ) ) {
00434                 $this->_aDynValue  = $aDynValue;
00435             } else {
00436                 $this->_aDynValue  = oxConfig::getParameter( "dynvalue");
00437             }
00438 
00439             // #701A
00440             // assign debit note payment params to view data
00441             $aPaymentList = $this->getPaymentList();
00442             if ( isset( $aPaymentList['oxiddebitnote'] ) ) {
00443                 $this->_assignDebitNoteParams();
00444             }
00445         }
00446         return $this->_aDynValue;
00447     }
00448 
00455     protected function _assignDebitNoteParams()
00456     {
00457         // #701A
00458         $oUserPayment = oxNew( 'oxuserpayment');
00459         //such info available ?
00460         if ( $oUserPayment->getPaymentByPaymentType( $this->getUser(), 'oxiddebitnote' ) ) {
00461             $aAddPaymentData = oxUtils::getInstance()->assignValuesFromText( $oUserPayment->oxuserpayments__oxvalue->value );
00462 
00463             //checking if some of values is allready set in session - leave it
00464             foreach ( $aAddPaymentData as $oData ) {
00465                 if ( !isset( $this->_aDynValue[$oData->name] ) ||
00466                    (  isset( $this->_aDynValue[$oData->name] ) && !$this->_aDynValue[$oData->name] ) ) {
00467                     $this->_aDynValue[$oData->name] = $oData->value;
00468                 }
00469             }
00470         }
00471     }
00472 
00479     public function getCheckedPaymentId()
00480     {
00481         if ( $this->_sCheckedPaymentId === null ) {
00482             if ( ( $sPaymentID = oxConfig::getParameter( 'paymentid' ) ) ) {
00483                 $sCheckedId = $sPaymentID;
00484             } elseif ( ( $sSelectedPaymentID = oxSession::getVar( '_selected_paymentid' ) ) ) {
00485                 $sCheckedId = $sSelectedPaymentID;
00486             } else {
00487                 // #1010A.
00488                 if ( $oUser = $this->getUser()) {
00489                     $oOrder = oxNew('oxorder');
00490                     if ( ( $sLastPaymentId = $oOrder->getLastUserPaymentType( $oUser->getId()) ) ) {
00491                         $sCheckedId = $sLastPaymentId;
00492                     }
00493                 }
00494             }
00495 
00496             // #M253 set to selected payment in db
00497             if ( !$sCheckedId && $this->_sCheckedId ) {
00498                 $sCheckedId = $this->_sCheckedId;
00499             }
00500 
00501             // #646
00502             $oPaymentList = $this->getPaymentList();
00503             if ( isset( $oPaymentList ) && $oPaymentList && !isset( $oPaymentList[$sCheckedId] ) ) {
00504                 end($oPaymentList);
00505                 $sCheckedId = key( $oPaymentList );
00506             }
00507             $this->_sCheckedPaymentId = $sCheckedId;
00508         }
00509 
00510         return $this->_sCheckedPaymentId;
00511     }
00512 
00518     public function getPaymentCnt()
00519     {
00520         if ( $this->_iPaymentCnt === null ) {
00521             $this->_iPaymentCnt = false;
00522 
00523             if ($oPaymentList = $this->getPaymentList()) {
00524                 $this->_iPaymentCnt = count($oPaymentList);
00525             }
00526         }
00527         return $this->_iPaymentCnt;
00528     }
00529 
00535     public function getCreditYears()
00536     {
00537         if ( $this->_aCreditYears === null ) {
00538             $this->_aCreditYears = false;
00539 
00540             $this->_aCreditYears = range( date('Y'), date('Y') + 10 );
00541         }
00542         return $this->_aCreditYears;
00543     }
00544 
00545 }

Generated on Tue Sep 29 16:45:16 2009 for OXID eShop CE by  doxygen 1.5.5