OXID eShop CE  4.9.6
 All Classes Files Functions Variables Pages
payment.php
Go to the documentation of this file.
1 <?php
2 
7 class Payment extends oxUBase
8 {
9 
15  protected $_oPaymentList = null;
16 
22  protected $_iPaymentCnt = null;
23 
29  protected $_aAllSets = null;
30 
36  protected $_iAllSetsCnt = null;
37 
43  protected $_oEmptyPayment = null;
44 
50  protected $_sPaymentError = null;
51 
57  protected $_sPaymentErrorText = null;
58 
64  protected $_aDynValue = null;
65 
71  protected $_sCheckedId = null;
72 
78  protected $_sCheckedPaymentId = null;
79 
85  protected $_aCreditYears = null;
86 
92  protected $_sThisTemplate = 'page/checkout/payment.tpl';
93 
99  protected $_blIsOrderStep = true;
100 
106  protected $_aTsProducts = null;
107 
113  protected $_blDynDataFiltered = false;
114 
115 
119  public function init()
120  {
121  $this->_filterDynData();
122  parent::init();
123  }
124 
136  public function render()
137  {
138  $myConfig = $this->getConfig();
139 
140  if ($myConfig->getConfigParam('blPsBasketReservationEnabled')) {
141  $this->getSession()->getBasketReservations()->renewExpiration();
142  }
143 
144  parent::render();
145 
146  //if it happens that you are not in SSL
147  //then forcing to HTTPS
148 
149  //but first checking maybe there were redirection already to prevent infinite redirections
150  //due to possible buggy ssl detection on server
151  $blAlreadyRedirected = oxRegistry::getConfig()->getRequestParameter('sslredirect') == 'forced';
152 
153  if ($this->getIsOrderStep()) {
154 
155  //additional check if we really really have a user now
156  //and the basket is not empty
157  $oBasket = $this->getSession()->getBasket();
158  $blPsBasketReservationEnabled = $myConfig->getConfigParam('blPsBasketReservationEnabled');
159  if ($blPsBasketReservationEnabled && (!$oBasket || ($oBasket && !$oBasket->getProductsCount()))) {
160  oxRegistry::getUtils()->redirect($myConfig->getShopHomeURL() .'cl=basket', true, 302);
161  }
162 
163  $oUser = $this->getUser();
164  if (!$oUser && ($oBasket && $oBasket->getProductsCount() > 0)) {
165  oxRegistry::getUtils()->redirect($myConfig->getShopHomeURL() . 'cl=basket', false, 302);
166  } elseif (!$oBasket || !$oUser || ($oBasket && !$oBasket->getProductsCount())) {
167  oxRegistry::getUtils()->redirect($myConfig->getShopHomeURL() . 'cl=start', false, 302);
168  }
169  }
170 
171  $sFncParameter = oxRegistry::getConfig()->getRequestParameter('fnc');
172  if ($myConfig->getCurrentShopURL() != $myConfig->getSSLShopURL() && !$blAlreadyRedirected && !$sFncParameter) {
173  $sPayErrorParameter = oxRegistry::getConfig()->getRequestParameter('payerror');
174  $sPayErrorTextParameter = oxRegistry::getConfig()->getRequestParameter('payerrortext');
175  $shopSecureHomeURL = $myConfig->getShopSecureHomeURL();
176 
177  $sPayError = $sPayErrorParameter ?'payerror='.$sPayErrorParameter:'';
178  $sPayErrorText = $sPayErrorTextParameter ?'payerrortext='.$sPayErrorTextParameter:'';
179  $sRedirectURL = $shopSecureHomeURL .'sslredirect=forced&cl=payment&'.$sPayError."&".$sPayErrorText;
180  oxRegistry::getUtils()->redirect($sRedirectURL, true, 302);
181  }
182 
183  if (!$this->getAllSetsCnt()) {
184  // no fitting shipping set found, setting default empty payment
185  $this->_setDefaultEmptyPayment();
186  oxRegistry::getSession()->setVariable('sShipSet', null);
187  }
188 
189  $this->_unsetPaymentErrors();
190 
191  return $this->_sThisTemplate;
192  }
193 
199  protected function _setDefaultEmptyPayment()
200  {
201  // no shipping method there !!
202  if ($this->getConfig()->getConfigParam('blOtherCountryOrder')) {
203  $oPayment = oxNew('oxpayment');
204  if ($oPayment->load('oxempty')) {
205  $this->_oEmptyPayment = $oPayment;
206  } else {
207  // some error with setup ??
208  $this->_sPaymentError = -2;
209  }
210  } else {
211  $this->_sPaymentError = -2;
212  }
213  }
214 
218  protected function _unsetPaymentErrors()
219  {
220  $iPayError = oxRegistry::getConfig()->getRequestParameter('payerror');
221  $sPayErrorText = oxRegistry::getConfig()->getRequestParameter('payerrortext');
222 
223  if (!($iPayError || $sPayErrorText)) {
224  $iPayError = oxRegistry::getSession()->getVariable('payerror');
225  $sPayErrorText = oxRegistry::getSession()->getVariable('payerrortext');
226  }
227 
228  if ($iPayError) {
229  oxRegistry::getSession()->deleteVariable('payerror');
230  $this->_sPaymentError = $iPayError;
231  }
232  if ($sPayErrorText) {
233  oxRegistry::getSession()->deleteVariable('payerrortext');
234  $this->_sPaymentErrorText = $sPayErrorText;
235  }
236  }
237 
242  public function changeshipping()
243  {
244  $oSession = $this->getSession();
245 
246  $oBasket = $oSession->getBasket();
247  $oBasket->setShipping(null);
248  $oBasket->onUpdate();
249  $oSession->setVariable('sShipSet', $this->getConfig()->getRequestParameter('sShipSet'));
250  }
251 
263  public function validatePayment()
264  {
265  $myConfig = $this->getConfig();
266  $oSession = $this->getSession();
267 
268  //#1308C - check user. Function is executed before render(), and oUser is not set!
269  // Set it manually for use in methods getPaymentList(), getShippingSetList()...
270  $oUser = $this->getUser();
271  if (!$oUser) {
272  $oSession->setVariable('payerror', 2);
273 
274  return;
275  }
276 
277  if (!($sShipSetId = oxRegistry::getConfig()->getRequestParameter('sShipSet'))) {
278  $sShipSetId = $oSession->getVariable('sShipSet');
279  }
280  if (!($sPaymentId = oxRegistry::getConfig()->getRequestParameter('paymentid'))) {
281  $sPaymentId = $oSession->getVariable('paymentid');
282  }
283  if (!($aDynvalue = oxRegistry::getConfig()->getRequestParameter('dynvalue'))) {
284  $aDynvalue = $oSession->getVariable('dynvalue');
285  }
286 
287  // A. additional protection
288  if (!$myConfig->getConfigParam('blOtherCountryOrder') && $sPaymentId == 'oxempty') {
289  $sPaymentId = '';
290  }
291 
292  //#1308C - check if we have paymentID, and it really exists
293  if (!$sPaymentId) {
294  $oSession->setVariable('payerror', 1);
295 
296  return;
297  }
298 
299  if ($this->getDynDataFiltered() && $sPaymentId == 'oxidcreditcard') {
300  $oSession->setVariable('payerror', 7);
301 
302  return;
303  }
304 
305  $oBasket = $oSession->getBasket();
306  $oBasket->setPayment(null);
307  $oPayment = oxNew('oxpayment');
308  $oPayment->load($sPaymentId);
309 
310  // getting basket price for payment calculation
311  $dBasketPrice = $oBasket->getPriceForPayment();
312 
313  $blOK = $oPayment->isValidPayment($aDynvalue, $myConfig->getShopId(), $oUser, $dBasketPrice, $sShipSetId);
314 
315  if ($blOK) {
316  $oSession->setVariable('paymentid', $sPaymentId);
317  $oSession->setVariable('dynvalue', $aDynvalue);
318  if (oxRegistry::getConfig()->getRequestParameter('bltsprotection')) {
319  $sTsProductId = oxRegistry::getConfig()->getRequestParameter('stsprotection');
320  $oBasket->setTsProductId($sTsProductId);
321  $oSession->setVariable('stsprotection', $sTsProductId);
322  } else {
323  $oSession->deleteVariable('stsprotection');
324  $oBasket->setTsProductId(null);
325  }
326  $oBasket->setShipping($sShipSetId);
327  $oSession->deleteVariable('_selected_paymentid');
328 
329  return 'order';
330  } else {
331  $oSession->setVariable('payerror', $oPayment->getPaymentErrorNumber());
332 
333  //#1308C - delete paymentid from session, and save selected it just for view
334  $oSession->deleteVariable('paymentid');
335  $oSession->setVariable('_selected_paymentid', $sPaymentId);
336  $oSession->deleteVariable('stsprotection');
337  $oBasket->setTsProductId(null);
338 
339  return;
340  }
341  }
342 
348  public function getPaymentList()
349  {
350  if ($this->_oPaymentList === null) {
351  $this->_oPaymentList = false;
352 
353  $sActShipSet = oxRegistry::getConfig()->getRequestParameter('sShipSet');
354  if (!$sActShipSet) {
355  $sActShipSet = oxRegistry::getSession()->getVariable('sShipSet');
356  }
357 
358  $oBasket = $this->getSession()->getBasket();
359 
360  // load sets, active set, and active set payment list
361  list($aAllSets, $sActShipSet, $aPaymentList) =
362  oxRegistry::get("oxDeliverySetList")->getDeliverySetData($sActShipSet, $this->getUser(), $oBasket);
363 
364  $oBasket->setShipping($sActShipSet);
365 
366  // calculating payment expences for preview for each payment
367  $this->_setValues($aPaymentList, $oBasket);
368  $this->_oPaymentList = $aPaymentList;
369  $this->_aAllSets = $aAllSets;
370 
371  }
372 
373  return $this->_oPaymentList;
374  }
375 
381  public function getAllSets()
382  {
383  if ($this->_aAllSets === null) {
384  $this->_aAllSets = false;
385 
386  if ($this->getPaymentList()) {
387  return $this->_aAllSets;
388  }
389  }
390 
391  return $this->_aAllSets;
392  }
393 
399  public function getAllSetsCnt()
400  {
401  if ($this->_iAllSetsCnt === null) {
402  $this->_iAllSetsCnt = 0;
403 
404  if ($this->getPaymentList()) {
405  $this->_iAllSetsCnt = count($this->_aAllSets);
406  }
407  }
408 
409  return $this->_iAllSetsCnt;
410  }
411 
418  protected function _setValues(& $aPaymentList, $oBasket = null)
419  {
420  if (is_array($aPaymentList)) {
421  foreach ($aPaymentList as $oPayment) {
422  $oPayment->calculate($oBasket);
423  $oPayment->aDynValues = $oPayment->getDynValues();
424  if ($oPayment->oxpayments__oxchecked->value) {
425  $this->_sCheckedId = $oPayment->getId();
426  }
427  }
428  }
429  }
430 
436  public function getEmptyPayment()
437  {
438  return $this->_oEmptyPayment;
439  }
440 
446  public function getPaymentError()
447  {
448  return $this->_sPaymentError;
449  }
450 
456  public function getDynDataFiltered()
457  {
459  }
460 
466  public function getPaymentErrorText()
467  {
469  }
470 
476  public function isOldDebitValidationEnabled()
477  {
478  return !$this->getConfig()->getConfigParam('blSkipDebitOldBankInfo');
479  }
480 
486  public function getDynValue()
487  {
488  if ($this->_aDynValue === null) {
489  $this->_aDynValue = false;
490 
491  // flyspray#1217 (sarunas)
492  if (($aDynValue = oxRegistry::getSession()->getVariable('dynvalue'))) {
493  $this->_aDynValue = $aDynValue;
494  } else {
495  $this->_aDynValue = oxRegistry::getConfig()->getRequestParameter("dynvalue");
496  }
497 
498  // #701A
499  // assign debit note payment params to view data
500  $aPaymentList = $this->getPaymentList();
501  if (isset($aPaymentList['oxiddebitnote'])) {
502  $this->_assignDebitNoteParams();
503  }
504  }
505 
506  return $this->_aDynValue;
507  }
508 
513  protected function _assignDebitNoteParams()
514  {
515  // #701A
516  $oUserPayment = oxNew('oxuserpayment');
517  //such info available ?
518  if ($oUserPayment->getPaymentByPaymentType($this->getUser(), 'oxiddebitnote')) {
519  $sUserPaymentField = 'oxuserpayments__oxvalue';
520  $aAddPaymentData = oxRegistry::getUtils()->assignValuesFromText($oUserPayment->$sUserPaymentField->value);
521 
522  //checking if some of values is allready set in session - leave it
523  foreach ($aAddPaymentData as $oData) {
524  if (!isset($this->_aDynValue[$oData->name]) ||
525  (isset($this->_aDynValue[$oData->name]) && !$this->_aDynValue[$oData->name])
526  ) {
527  $this->_aDynValue[$oData->name] = $oData->value;
528  }
529  }
530  }
531  }
532 
539  public function getCheckedPaymentId()
540  {
541  if ($this->_sCheckedPaymentId === null) {
542  if (!($sPaymentID = oxRegistry::getConfig()->getRequestParameter('paymentid'))) {
543  $sPaymentID = oxRegistry::getSession()->getVariable('paymentid');
544  }
545  if ($sPaymentID) {
546  $sCheckedId = $sPaymentID;
547  } elseif (($sSelectedPaymentID = oxRegistry::getSession()->getVariable('_selected_paymentid'))) {
548  $sCheckedId = $sSelectedPaymentID;
549  } else {
550  // #1010A.
551  if ($oUser = $this->getUser()) {
552  $oOrder = oxNew('oxorder');
553  if (($sLastPaymentId = $oOrder->getLastUserPaymentType($oUser->getId()))) {
554  $sCheckedId = $sLastPaymentId;
555  }
556  }
557  }
558 
559  // #M253 set to selected payment in db
560  if (!$sCheckedId && $this->_sCheckedId) {
561  $sCheckedId = $this->_sCheckedId;
562  }
563 
564  // #646
565  $oPaymentList = $this->getPaymentList();
566  if (isset($oPaymentList) && $oPaymentList && !isset($oPaymentList[$sCheckedId])) {
567  end($oPaymentList);
568  $sCheckedId = key($oPaymentList);
569  }
570  $this->_sCheckedPaymentId = $sCheckedId;
571  }
572 
574  }
575 
581  public function getPaymentCnt()
582  {
583  if ($this->_iPaymentCnt === null) {
584  $this->_iPaymentCnt = false;
585 
586  if ($oPaymentList = $this->getPaymentList()) {
587  $this->_iPaymentCnt = count($oPaymentList);
588  }
589  }
590 
591  return $this->_iPaymentCnt;
592  }
593 
599  public function getCreditYears()
600  {
601  if ($this->_aCreditYears === null) {
602  $this->_aCreditYears = false;
603 
604  $this->_aCreditYears = range(date('Y'), date('Y') + 10);
605  }
606 
607  return $this->_aCreditYears;
608  }
609 
618  protected function _checkArrValuesEmpty($aData, $aKeys)
619  {
620  if (!is_array($aKeys) || count($aKeys) < 1) {
621  return false;
622  }
623 
624  foreach ($aKeys as $sKey) {
625  if (isset($aData[$sKey]) && !empty($aData[$sKey])) {
626  return false;
627  }
628  }
629 
630  return true;
631  }
632 
633 
644  protected function _filterDynData()
645  {
646  //in case we actually ARE allowed to store the data
647  if (oxRegistry::getConfig()->getConfigParam("blStoreCreditCardInfo")) {
648  //then do nothing and reset _blDynDataFiltered
649  $this->_blDynDataFiltered = false;
650 
651  return;
652  }
653 
654  $aDynData = $this->getSession()->getVariable("dynvalue");
655 
656  $aFields = array("kktype", "kknumber", "kkname", "kkmonth", "kkyear", "kkpruef");
657 
658  if ($aDynData) {
659  if (!$this->_checkArrValuesEmpty($aDynData, $aFields)) {
660  $this->_blDynDataFiltered = true;
661  }
662  $aDynData["kktype"] = null;
663  $aDynData["kknumber"] = null;
664  $aDynData["kkname"] = null;
665  $aDynData["kkmonth"] = null;
666  $aDynData["kkyear"] = null;
667  $aDynData["kkpruef"] = null;
668  oxRegistry::getSession()->setVariable("dynvalue", $aDynData);
669  }
670 
671  if (!$this->_checkArrValuesEmpty($_REQUEST["dynvalue"], $aFields) ||
672  !$this->_checkArrValuesEmpty($_POST["dynvalue"], $aFields) ||
673  !$this->_checkArrValuesEmpty($_GET["dynvalue"], $aFields)
674  ) {
675  $this->_blDynDataFiltered = true;
676  }
677 
678  unset($_REQUEST["dynvalue"]["kktype"]);
679  unset($_REQUEST["dynvalue"]["kknumber"]);
680  unset($_REQUEST["dynvalue"]["kkname"]);
681  unset($_REQUEST["dynvalue"]["kkmonth"]);
682  unset($_REQUEST["dynvalue"]["kkyear"]);
683  unset($_REQUEST["dynvalue"]["kkpruef"]);
684 
685  unset($_POST["dynvalue"]["kktype"]);
686  unset($_POST["dynvalue"]["kknumber"]);
687  unset($_POST["dynvalue"]["kkname"]);
688  unset($_POST["dynvalue"]["kkmonth"]);
689  unset($_POST["dynvalue"]["kkyear"]);
690  unset($_POST["dynvalue"]["kkpruef"]);
691 
692  unset($_GET["dynvalue"]["kktype"]);
693  unset($_GET["dynvalue"]["kknumber"]);
694  unset($_GET["dynvalue"]["kkname"]);
695  unset($_GET["dynvalue"]["kkmonth"]);
696  unset($_GET["dynvalue"]["kkyear"]);
697  unset($_GET["dynvalue"]["kkpruef"]);
698 
699  }
700 
706  public function getTsProtections()
707  {
708  if ($this->_aTsProducts === null) {
709  $oBasket = $this->getSession()->getBasket();
710  $dVat = $oBasket->getAdditionalServicesVatPercent();
711  if ($dPrice = $oBasket->getPrice()->getBruttoPrice()) {
712  $oTsProtection = oxNew('oxtsprotection');
713  $oTsProtection->setVat($dVat);
714  $this->_aTsProducts = $oTsProtection->getTsProducts($dPrice);
715  }
716  }
717 
718  return $this->_aTsProducts;
719  }
720 
726  public function getCheckedTsProductId()
727  {
728  if ($this->_sCheckedProductId === null) {
729  $this->_sCheckedProductId = false;
730  if ($sId = oxRegistry::getConfig()->getRequestParameter('stsprotection')) {
731  $this->_sCheckedProductId = $sId;
732  }
733  }
734 
735  return $this->_sCheckedProductId;
736  }
737 
743  public function getBreadCrumb()
744  {
745  $aPaths = array();
746  $aPath = array();
747 
748 
749  $iBaseLanguage = oxRegistry::getLang()->getBaseLanguage();
750  $aPath['title'] = oxRegistry::getLang()->translateString('PAY', $iBaseLanguage, false);
751  $aPath['link'] = $this->getLink();
752 
753  $aPaths[] = $aPath;
754 
755  return $aPaths;
756  }
757 
763  public function isPaymentVatSplitted()
764  {
765  return $this->getConfig()->getConfigParam('blShowVATForPayCharge');
766  }
767 }