OXID eShop CE  4.8.12
 All Classes Files Functions Variables Pages
payment.php
Go to the documentation of this file.
1 <?php
2 
7 class Payment extends oxUBase
8 {
13  protected $_oPaymentList = null;
14 
19  protected $_iPaymentCnt = null;
20 
25  protected $_aAllSets = null;
26 
31  protected $_iAllSetsCnt = null;
32 
37  protected $_oEmptyPayment = null;
38 
43  protected $_sPaymentError = null;
44 
49  protected $_sPaymentErrorText = null;
50 
55  protected $_aDynValue = null;
56 
61  protected $_sCheckedId = null;
62 
67  protected $_sCheckedPaymentId = null;
68 
73  protected $_aCreditYears = null;
74 
79  protected $_sThisTemplate = 'page/checkout/payment.tpl';
80 
85  protected $_blIsOrderStep = true;
86 
91  protected $_aTsProducts = null;
92 
97  protected $_blDynDataFiltered = false;
98 
99 
105  public function init()
106  {
107  $this->_filterDynData();
108  parent::init();
109  }
110 
122  public function render()
123  {
124  $myConfig = $this->getConfig();
125 
126  if ($myConfig->getConfigParam( 'blPsBasketReservationEnabled' )) {
127  $this->getSession()->getBasketReservations()->renewExpiration();
128  }
129 
130  parent::render();
131 
132  //if it happens that you are not in SSL
133  //then forcing to HTTPS
134 
135  //but first checking maybe there were redirection already to prevent infinite redirections due to possible buggy ssl detection on server
136  $blAlreadyRedirected = oxConfig::getParameter( 'sslredirect' ) == 'forced';
137 
138  if ( $this->getIsOrderStep() ) {
139 
140  //additional check if we really really have a user now
141  //and the basket is not empty
142  $oBasket = $this->getSession()->getBasket();
143  if ( $myConfig->getConfigParam( 'blPsBasketReservationEnabled' ) && (!$oBasket || ( $oBasket && !$oBasket->getProductsCount() )) ) {
144  oxRegistry::getUtils()->redirect( $myConfig->getShopHomeURL() .'cl=basket', true, 302 );
145  }
146 
147  $oUser = $this->getUser();
148  if (!$oUser && ($oBasket && $oBasket->getProductsCount() > 0)) {
149  oxRegistry::getUtils()->redirect( $myConfig->getShopHomeURL() .'cl=basket', false, 302 );
150  } elseif ( !$oBasket || !$oUser || ( $oBasket && !$oBasket->getProductsCount() ) ) {
151  oxRegistry::getUtils()->redirect( $myConfig->getShopHomeURL() .'cl=start', false, 302 );
152  }
153  }
154 
155  if ( $myConfig->getCurrentShopURL() != $myConfig->getSSLShopURL() && !$blAlreadyRedirected && !oxConfig::getParameter('fnc') ) {
156  $sPayError = oxConfig::getParameter( 'payerror' )?'payerror='.oxConfig::getParameter( 'payerror' ):'';
157  $sPayErrorText = oxConfig::getParameter('payerrortext')?'payerrortext='.oxConfig::getParameter( 'payerrortext' ):'';
158  $sRedirectURL = $myConfig->getShopSecureHomeURL().'sslredirect=forced&cl=payment&'.$sPayError."&".$sPayErrorText;
159  oxRegistry::getUtils()->redirect( $sRedirectURL, true, 302 );
160  }
161 
162  if ( !$this->getAllSetsCnt() ) {
163  // no fitting shipping set found, setting default empty payment
164  $this->_setDefaultEmptyPayment();
165  oxSession::setVar( 'sShipSet', null );
166  }
167 
168  $this->_unsetPaymentErrors();
169 
170  return $this->_sThisTemplate;
171  }
172 
180  protected function _setDefaultEmptyPayment()
181  {
182  // no shipping method there !!
183  if ( $this->getConfig()->getConfigParam( 'blOtherCountryOrder' ) ) {
184  $oPayment = oxNew( 'oxpayment' );
185  if ( $oPayment->load( 'oxempty' ) ) {
186  $this->_oEmptyPayment = $oPayment;
187  } else {
188  // some error with setup ??
189  $this->_sPaymentError = -2;
190  }
191  } else {
192  $this->_sPaymentError = -2;
193  }
194  }
195 
201  protected function _unsetPaymentErrors()
202  {
203  $iPayError = oxConfig::getParameter( 'payerror' );
204  $sPayErrorText = oxConfig::getParameter( 'payerrortext' );
205 
206  if (!($iPayError || $sPayErrorText)) {
207  $iPayError = oxSession::getVar( 'payerror' );
208  $sPayErrorText = oxSession::getVar( 'payerrortext' );
209  }
210 
211  if ( $iPayError ) {
212  oxSession::deleteVar( 'payerror' );
213  $this->_sPaymentError = $iPayError;
214  }
215  if ( $sPayErrorText ) {
216  oxSession::deleteVar( 'payerrortext' );
217  $this->_sPaymentErrorText = $sPayErrorText;
218  }
219  }
220 
227  public function changeshipping()
228  {
229  $oSession = $this->getSession();
230 
231  $oBasket = $oSession->getBasket();
232  $oBasket->setShipping( null );
233  $oBasket->onUpdate();
234  $oSession->setVariable( 'sShipSet', $this->getConfig()->getRequestParameter( 'sShipSet' ) );
235  }
236 
248  public function validatePayment()
249  {
250  $myConfig = $this->getConfig();
251  $oSession = $this->getSession();
252 
253  //#1308C - check user. Function is executed before render(), and oUser is not set!
254  // Set it manually for use in methods getPaymentList(), getShippingSetList()...
255  $oUser = $this->getUser();
256  if ( !$oUser ) {
257  $oSession->setVariable( 'payerror', 2 );
258  return;
259  }
260 
261  if (! ($sShipSetId = oxConfig::getParameter( 'sShipSet' ))) {
262  $sShipSetId = $oSession->getVariable('sShipSet');
263  }
264  if (! ($sPaymentId = oxConfig::getParameter( 'paymentid' ))) {
265  $sPaymentId = $oSession->getVariable('paymentid');
266  }
267  if (! ($aDynvalue = oxConfig::getParameter( 'dynvalue' ))) {
268  $aDynvalue = $oSession->getVariable('dynvalue');
269  }
270 
271  // A. additional protection
272  if ( !$myConfig->getConfigParam( 'blOtherCountryOrder' ) && $sPaymentId == 'oxempty' ) {
273  $sPaymentId = '';
274  }
275 
276  //#1308C - check if we have paymentID, and it really exists
277  if ( !$sPaymentId ) {
278  $oSession->setVariable( 'payerror', 1 );
279  return;
280  }
281 
282  if ( $this->getDynDataFiltered() && $sPaymentId == 'oxidcreditcard' ) {
283  $oSession->setVariable( 'payerror', 7 );
284  return;
285  }
286 
287  $oBasket = $oSession->getBasket();
288  $oBasket->setPayment(null);
289  $oPayment = oxNew( 'oxpayment' );
290  $oPayment->load( $sPaymentId );
291 
292  // getting basket price for payment calculation
293  $dBasketPrice = $oBasket->getPriceForPayment();
294 
295  $blOK = $oPayment->isValidPayment( $aDynvalue, $myConfig->getShopId(), $oUser, $dBasketPrice, $sShipSetId );
296 
297  if ( $blOK ) {
298  $oSession->setVariable( 'paymentid', $sPaymentId );
299  $oSession->setVariable( 'dynvalue', $aDynvalue );
300  if ( oxConfig::getParameter( 'bltsprotection' ) ) {
301  $sTsProductId = oxConfig::getParameter( 'stsprotection' );
302  $oBasket->setTsProductId($sTsProductId);
303  $oSession->setVariable( 'stsprotection', $sTsProductId );
304  } else {
305  $oSession->deleteVariable( 'stsprotection' );
306  $oBasket->setTsProductId(null);
307  }
308  $oBasket->setShipping($sShipSetId);
309  $oSession->deleteVariable( '_selected_paymentid' );
310  return 'order';
311  } else {
312  $oSession->setVariable( 'payerror', $oPayment->getPaymentErrorNumber() );
313 
314  //#1308C - delete paymentid from session, and save selected it just for view
315  $oSession->deleteVariable( 'paymentid' );
316  $oSession->setVariable( '_selected_paymentid', $sPaymentId );
317  $oSession->deleteVariable( 'stsprotection' );
318  $oBasket->setTsProductId(null);
319  return;
320  }
321  }
322 
328  public function getPaymentList()
329  {
330  if ( $this->_oPaymentList === null ) {
331  $this->_oPaymentList = false;
332 
333  $sActShipSet = oxConfig::getParameter( 'sShipSet' );
334  if ( !$sActShipSet ) {
335  $sActShipSet = oxSession::getVar( 'sShipSet' );
336  }
337 
338  $oBasket = $this->getSession()->getBasket();
339 
340  // load sets, active set, and active set payment list
341  list( $aAllSets, $sActShipSet, $aPaymentList ) = oxRegistry::get("oxDeliverySetList")->getDeliverySetData( $sActShipSet, $this->getUser(), $oBasket );
342 
343  $oBasket->setShipping( $sActShipSet );
344 
345  // calculating payment expences for preview for each payment
346  $this->_setValues( $aPaymentList, $oBasket );
347  $this->_oPaymentList = $aPaymentList;
348  $this->_aAllSets = $aAllSets;
349 
350  }
351  return $this->_oPaymentList;
352  }
353 
359  public function getAllSets()
360  {
361  if ( $this->_aAllSets === null ) {
362  $this->_aAllSets = false;
363 
364  if ($this->getPaymentList()) {
365  return $this->_aAllSets;
366  }
367  }
368  return $this->_aAllSets;
369  }
370 
376  public function getAllSetsCnt()
377  {
378  if ( $this->_iAllSetsCnt === null ) {
379  $this->_iAllSetsCnt = 0;
380 
381  if ($this->getPaymentList()) {
382  $this->_iAllSetsCnt = count($this->_aAllSets);
383  }
384  }
385  return $this->_iAllSetsCnt;
386  }
387 
396  protected function _setValues( & $aPaymentList, $oBasket = null )
397  {
398  if ( is_array($aPaymentList) ) {
399  foreach ( $aPaymentList as $oPayment ) {
400  $oPayment->calculate( $oBasket );
401  $oPayment->aDynValues = $oPayment->getDynValues();
402  if ( $oPayment->oxpayments__oxchecked->value ) {
403  $this->_sCheckedId = $oPayment->getId();
404  }
405  }
406  }
407  }
408 
414  public function getEmptyPayment()
415  {
416  return $this->_oEmptyPayment;
417  }
418 
424  public function getPaymentError()
425  {
426  return $this->_sPaymentError;
427  }
428 
434  public function getDynDataFiltered()
435  {
437  }
438 
444  public function getPaymentErrorText()
445  {
447  }
448 
454  public function isOldDebitValidationEnabled()
455  {
456  return !$this->getConfig()->getConfigParam( 'blSkipDebitOldBankInfo' );
457  }
458 
464  public function getDynValue()
465  {
466  if ( $this->_aDynValue === null ) {
467  $this->_aDynValue = false;
468 
469  // flyspray#1217 (sarunas)
470  if ( ( $aDynValue = oxSession::getVar( 'dynvalue' ) ) ) {
471  $this->_aDynValue = $aDynValue;
472  } else {
473  $this->_aDynValue = oxConfig::getParameter( "dynvalue");
474  }
475 
476  // #701A
477  // assign debit note payment params to view data
478  $aPaymentList = $this->getPaymentList();
479  if ( isset( $aPaymentList['oxiddebitnote'] ) ) {
480  $this->_assignDebitNoteParams();
481  }
482  }
483  return $this->_aDynValue;
484  }
485 
492  protected function _assignDebitNoteParams()
493  {
494  // #701A
495  $oUserPayment = oxNew( 'oxuserpayment');
496  //such info available ?
497  if ( $oUserPayment->getPaymentByPaymentType( $this->getUser(), 'oxiddebitnote' ) ) {
498  $aAddPaymentData = oxRegistry::getUtils()->assignValuesFromText( $oUserPayment->oxuserpayments__oxvalue->value );
499 
500  //checking if some of values is allready set in session - leave it
501  foreach ( $aAddPaymentData as $oData ) {
502  if ( !isset( $this->_aDynValue[$oData->name] ) ||
503  ( isset( $this->_aDynValue[$oData->name] ) && !$this->_aDynValue[$oData->name] ) ) {
504  $this->_aDynValue[$oData->name] = $oData->value;
505  }
506  }
507  }
508  }
509 
516  public function getCheckedPaymentId()
517  {
518  if ( $this->_sCheckedPaymentId === null ) {
519  if (! ($sPaymentID = oxConfig::getParameter( 'paymentid' ))) {
520  $sPaymentID = oxSession::getVar('paymentid');
521  }
522  if ( $sPaymentID ) {
523  $sCheckedId = $sPaymentID;
524  } elseif ( ( $sSelectedPaymentID = oxSession::getVar( '_selected_paymentid' ) ) ) {
525  $sCheckedId = $sSelectedPaymentID;
526  } else {
527  // #1010A.
528  if ( $oUser = $this->getUser()) {
529  $oOrder = oxNew('oxorder');
530  if ( ( $sLastPaymentId = $oOrder->getLastUserPaymentType( $oUser->getId()) ) ) {
531  $sCheckedId = $sLastPaymentId;
532  }
533  }
534  }
535 
536  // #M253 set to selected payment in db
537  if ( !$sCheckedId && $this->_sCheckedId ) {
538  $sCheckedId = $this->_sCheckedId;
539  }
540 
541  // #646
542  $oPaymentList = $this->getPaymentList();
543  if ( isset( $oPaymentList ) && $oPaymentList && !isset( $oPaymentList[$sCheckedId] ) ) {
544  end($oPaymentList);
545  $sCheckedId = key( $oPaymentList );
546  }
547  $this->_sCheckedPaymentId = $sCheckedId;
548  }
549 
551  }
552 
558  public function getPaymentCnt()
559  {
560  if ( $this->_iPaymentCnt === null ) {
561  $this->_iPaymentCnt = false;
562 
563  if ($oPaymentList = $this->getPaymentList()) {
564  $this->_iPaymentCnt = count($oPaymentList);
565  }
566  }
567  return $this->_iPaymentCnt;
568  }
569 
575  public function getCreditYears()
576  {
577  if ( $this->_aCreditYears === null ) {
578  $this->_aCreditYears = false;
579 
580  $this->_aCreditYears = range( date('Y'), date('Y') + 10 );
581  }
582  return $this->_aCreditYears;
583  }
584 
593  protected function _checkArrValuesEmpty( $aData, $aKeys )
594  {
595  if ( !is_array( $aKeys ) || count( $aKeys ) < 1 ) {
596  return false;
597  }
598 
599  foreach ( $aKeys as $sKey ) {
600  if ( isset( $aData[$sKey] ) && !empty( $aData[$sKey] ) ) {
601  return false;
602  }
603  }
604 
605  return true;
606  }
607 
608 
619  protected function _filterDynData()
620  {
621  //in case we actually ARE allowed to store the data
622  if (oxRegistry::getConfig()->getConfigParam("blStoreCreditCardInfo")) {
623  //then do nothing and reset _blDynDataFiltered
624  $this->_blDynDataFiltered = false;
625  return;
626  }
627 
628  $aDynData = $this->getSession()->getVar("dynvalue");
629 
630  $aFields = array("kktype", "kknumber", "kkname", "kkmonth", "kkyear", "kkpruef");
631 
632  if ( $aDynData ) {
633  if ( !$this->_checkArrValuesEmpty( $aDynData, $aFields ) ) {
634  $this->_blDynDataFiltered = true;
635  }
636  $aDynData["kktype"] = null;
637  $aDynData["kknumber"] = null;
638  $aDynData["kkname"] = null;
639  $aDynData["kkmonth"] = null;
640  $aDynData["kkyear"] = null;
641  $aDynData["kkpruef"] = null;
642  oxSession::setVar("dynvalue", $aDynData);
643  }
644 
645  if ( !$this->_checkArrValuesEmpty( $_REQUEST["dynvalue"], $aFields ) ||
646  !$this->_checkArrValuesEmpty( $_POST["dynvalue"], $aFields ) ||
647  !$this->_checkArrValuesEmpty( $_GET["dynvalue"], $aFields ) ) {
648  $this->_blDynDataFiltered = true;
649  }
650 
651  unset($_REQUEST["dynvalue"]["kktype"]);
652  unset($_REQUEST["dynvalue"]["kknumber"]);
653  unset($_REQUEST["dynvalue"]["kkname"]);
654  unset($_REQUEST["dynvalue"]["kkmonth"]);
655  unset($_REQUEST["dynvalue"]["kkyear"]);
656  unset($_REQUEST["dynvalue"]["kkpruef"]);
657 
658  unset($_POST["dynvalue"]["kktype"]);
659  unset($_POST["dynvalue"]["kknumber"]);
660  unset($_POST["dynvalue"]["kkname"]);
661  unset($_POST["dynvalue"]["kkmonth"]);
662  unset($_POST["dynvalue"]["kkyear"]);
663  unset($_POST["dynvalue"]["kkpruef"]);
664 
665  unset($_GET["dynvalue"]["kktype"]);
666  unset($_GET["dynvalue"]["kknumber"]);
667  unset($_GET["dynvalue"]["kkname"]);
668  unset($_GET["dynvalue"]["kkmonth"]);
669  unset($_GET["dynvalue"]["kkyear"]);
670  unset($_GET["dynvalue"]["kkpruef"]);
671 
672  }
673 
679  public function getTsProtections()
680  {
681  if ( $this->_aTsProducts === null ) {
682  $oBasket = $this->getSession()->getBasket();
683  $dVat = $oBasket->getAdditionalServicesVatPercent();
684  if ( $dPrice = $oBasket->getPrice()->getBruttoPrice() ) {
685  $oTsProtection = oxNew('oxtsprotection');
686  $oTsProtection->setVat( $dVat );
687  $this->_aTsProducts = $oTsProtection->getTsProducts($dPrice);
688  }
689  }
690  return $this->_aTsProducts;
691  }
692 
698  public function getCheckedTsProductId()
699  {
700  if ( $this->_sCheckedProductId === null ) {
701  $this->_sCheckedProductId = false;
702  if ( $sId = oxConfig::getParameter( 'stsprotection' ) ) {
703  $this->_sCheckedProductId = $sId;
704  }
705  }
706  return $this->_sCheckedProductId;
707  }
708 
714  public function getBreadCrumb()
715  {
716  $aPaths = array();
717  $aPath = array();
718 
719 
720  $aPath['title'] = oxRegistry::getLang()->translateString( 'PAY', oxRegistry::getLang()->getBaseLanguage(), false );
721  $aPath['link'] = $this->getLink();
722 
723  $aPaths[] = $aPath;
724 
725  return $aPaths;
726  }
727 
733  public function isPaymentVatSplitted()
734  {
735  return $this->getConfig()->getConfigParam('blShowVATForPayCharge');
736  }
737 
738 }