vendor/isotope/isotope-core/system/modules/isotope/library/Isotope/CheckoutStep/PaymentMethod.php line 50

Open in your IDE?
  1. <?php
  2. /*
  3.  * Isotope eCommerce for Contao Open Source CMS
  4.  *
  5.  * Copyright (C) 2009 - 2019 terminal42 gmbh & Isotope eCommerce Workgroup
  6.  *
  7.  * @link       https://isotopeecommerce.org
  8.  * @license    https://opensource.org/licenses/lgpl-3.0.html
  9.  */
  10. namespace Isotope\CheckoutStep;
  11. use Contao\Database;
  12. use Contao\Input;
  13. use Contao\StringUtil;
  14. use Contao\System;
  15. use Contao\Widget;
  16. use Isotope\Interfaces\IsotopeCheckoutStep;
  17. use Isotope\Interfaces\IsotopeProductCollection;
  18. use Isotope\Isotope;
  19. use Isotope\Model\Payment;
  20. use Isotope\Module\Checkout;
  21. use Isotope\Template;
  22. /**
  23.  * PaymentMethod checkout step lets the user choose a payment method
  24.  */
  25. class PaymentMethod extends CheckoutStep implements IsotopeCheckoutStep
  26. {
  27.     /**
  28.      * Payment modules
  29.      * @var array
  30.      */
  31.     private $modules;
  32.     /**
  33.      * Payment options
  34.      * @var array
  35.      */
  36.     private $options;
  37.     /**
  38.      * Returns true if the current cart has payment
  39.      *
  40.      * @inheritdoc
  41.      */
  42.     public function isAvailable()
  43.     {
  44.         $available Isotope::getCart()->requiresPayment();
  45.         if (!$available) {
  46.             Isotope::getCart()->setPaymentMethod(null);
  47.         }
  48.         return $available;
  49.     }
  50.     /**
  51.      * @inheritdoc
  52.      */
  53.     public function isSkippable()
  54.     {
  55.         if (!$this->objModule->canSkipStep('payment_method')) {
  56.             return false;
  57.         }
  58.         $this->initializeModules();
  59.         return === \count($this->options);
  60.     }
  61.     /**
  62.      * @inheritdoc
  63.      */
  64.     public function generate()
  65.     {
  66.         $this->initializeModules();
  67.         if (empty($this->modules)) {
  68.             $this->blnError true;
  69.             System::log('No payment methods available for cart ID ' Isotope::getCart()->id__METHOD__TL_ERROR);
  70.             /** @var Template|\stdClass $objTemplate */
  71.             $objTemplate           = new Template('mod_message');
  72.             $objTemplate->class    'payment_method';
  73.             $objTemplate->hl       'h2';
  74.             $objTemplate->headline $GLOBALS['TL_LANG']['MSC']['payment_method'];
  75.             $objTemplate->type     'error';
  76.             $objTemplate->message  $GLOBALS['TL_LANG']['MSC']['noPaymentModules'];
  77.             return $objTemplate->parse();
  78.         }
  79.         $strClass  $GLOBALS['TL_FFL']['radio'];
  80.         /** @var Widget $objWidget */
  81.         $objWidget = new $strClass(array(
  82.             'id'            => $this->getStepClass(),
  83.             'name'          => $this->getStepClass(),
  84.             'mandatory'     => true,
  85.             'options'       => $this->options,
  86.             'value'         => Isotope::getCart()->payment_id,
  87.             'storeValues'   => true,
  88.             'tableless'     => true,
  89.         ));
  90.         // If there is only one payment method, mark it as selected by default
  91.         if (\count($this->modules) == 1) {
  92.             $objModule        reset($this->modules);
  93.             $objWidget->value $objModule->id;
  94.             Isotope::getCart()->setPaymentMethod($objModule);
  95.         }
  96.         if (Input::post('FORM_SUBMIT') == $this->objModule->getFormId()) {
  97.             $objWidget->validate();
  98.             if (!$objWidget->hasErrors()) {
  99.                 Isotope::getCart()->setPaymentMethod($this->modules[$objWidget->value]);
  100.             }
  101.         }
  102.         /** @var Template|\stdClass $objTemplate */
  103.         $objTemplate = new Template('iso_checkout_payment_method');
  104.         if (!Isotope::getCart()->hasPayment() || !isset($this->modules[Isotope::getCart()->payment_id])) {
  105.             $this->blnError true;
  106.         }
  107.         $objTemplate->headline       $GLOBALS['TL_LANG']['MSC']['payment_method'];
  108.         $objTemplate->message        $GLOBALS['TL_LANG']['MSC']['payment_method_message'];
  109.         $objTemplate->options        $objWidget->parse();
  110.         $objTemplate->paymentMethods $this->modules;
  111.         return $objTemplate->parse();
  112.     }
  113.     /**
  114.      * Return review information for last page of checkout
  115.      * @return  array
  116.      */
  117.     public function review()
  118.     {
  119.         return array(
  120.             'payment_method' => array(
  121.                 'headline' => $GLOBALS['TL_LANG']['MSC']['payment_method'],
  122.                 'info'     => Isotope::getCart()->getDraftOrder()->getPaymentMethod()->checkoutReview(),
  123.                 'note'     => Isotope::getCart()->getDraftOrder()->getPaymentMethod()->getNote(),
  124.                 'edit'     => $this->isSkippable() ? '' Checkout::generateUrlForStep(Checkout::STEP_PAYMENT),
  125.             ),
  126.         );
  127.     }
  128.     /**
  129.      * Initialize modules and options
  130.      */
  131.     private function initializeModules()
  132.     {
  133.         if (null !== $this->modules && null !== $this->options) {
  134.             return;
  135.         }
  136.         $this->modules = array();
  137.         $this->options = array();
  138.         $arrIds StringUtil::deserialize($this->objModule->iso_payment_modules);
  139.         if (!empty($arrIds) && \is_array($arrIds)) {
  140.             $arrColumns = array('id IN (' implode(','$arrIds) . ')');
  141.             if (BE_USER_LOGGED_IN !== true) {
  142.                 $arrColumns[] = "enabled='1'";
  143.             }
  144.             /** @var Payment[] $objModules */
  145.             $objModules Payment::findBy($arrColumnsnull, array('order' => Database::getInstance()->findInSet('id'$arrIds)));
  146.             if (null !== $objModules) {
  147.                 foreach ($objModules as $objModule) {
  148.                     if (!$objModule->isAvailable()) {
  149.                         continue;
  150.                     }
  151.                     $strLabel $objModule->getLabel();
  152.                     $fltPrice $objModule->getPrice();
  153.                     if ($fltPrice != 0) {
  154.                         if ($objModule->isPercentage()) {
  155.                             $strLabel .= ' (' $objModule->getPercentageLabel() . ')';
  156.                         }
  157.                         $strLabel .= ': ' Isotope::formatPriceWithCurrency($fltPrice);
  158.                     }
  159.                     if ($note $objModule->getNote()) {
  160.                         $strLabel .= '<span class="note">' $note '</span>';
  161.                     }
  162.                     $this->options[] = array(
  163.                         'value'     => $objModule->id,
  164.                         'label'     => $strLabel,
  165.                     );
  166.                     $this->modules[$objModule->id] = $objModule;
  167.                 }
  168.             }
  169.         }
  170.     }
  171. }