Source of file CheckoutStep4.php
Size: 6,606 Bytes - Last Modified: 2021-12-23T10:21:36+00:00
/var/www/docs.ssmods.com/process/src/src/Checkout/CheckoutStep4.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 | <?php namespace SilverCart\Checkout; use SilverCart\Admin\Model\Config; use SilverCart\Checkout\CheckoutStep; use SilverCart\Forms\Checkout\CheckoutChoosePaymentMethodForm; use SilverCart\Model\Customer\Country; use SilverCart\Model\Customer\Customer; use SilverCart\Model\Payment\PaymentMethod; use SilverStripe\ORM\ArrayList; use SilverStripe\ORM\DataList; use SilverStripe\ORM\FieldType\DBHTMLText; use SilverStripe\ORM\SS_List; use SilverStripe\Security\Member; /** * Checkout step 4. * Checkout step to choose the payment method. * * @package SilverCart * @subpackage Checkout * @author Sebastian Diel <sdiel@pixeltricks.de> * @since 16.11.2017 * @copyright 2017 pixeltricks GmbH * @license see license file in modules root directory */ class CheckoutStep4 extends CheckoutStep { /** * List of allowed actions. * * @var array */ private static $allowed_actions = [ 'CheckoutChoosePaymentMethodForm', ]; /** * List of allowed payment methods * * @var ArrayList */ protected $allowedPaymentMethods = null; /** * List of active payment methods * * @var DataList */ protected $activePaymentMethods = null; /** * Determines whether to skip this step or not. * * @var bool */ protected $skipPaymentStep = null; /** * Custom checkout step processor. * Will be called for invisible steps. * * @return void * * @author Sebastian Diel <sdiel@pixeltricks.de> * @since 19.03.2019 */ public function process() : void { if (!$this->IsVisible()) { $paymentMethod = $this->getAllowedPaymentMethods()->first(); if ($paymentMethod instanceof PaymentMethod) { $this->getCheckout()->addDataValue('PaymentMethod', $paymentMethod->ID); } } } /** * Returns the CheckoutChoosePaymentMethodForm. * * @return CheckoutChoosePaymentMethodForm * * @author Sebastian Diel <sdiel@pixeltricks.de> * @since 23.11.2017 */ public function CheckoutChoosePaymentMethodForm() : ?CheckoutChoosePaymentMethodForm { $form = null; $paymentMethodID = $this->getController()->getRequest()->postVar('PaymentMethod'); if (is_numeric($paymentMethodID)) { $paymentMethod = PaymentMethod::get()->byID($paymentMethodID); if ($paymentMethod instanceof PaymentMethod && $paymentMethod->exists() ) { $form = $paymentMethod->CheckoutChoosePaymentMethodForm(); } } return $form; } /** * Returns the allowed payment methods. * * @return ArrayList */ public function getAllowedPaymentMethods() : ArrayList { if (is_null($this->allowedPaymentMethods)) { $allowedPaymentMethods = ArrayList::create(); $shippingAddressData = (array) $this->getCheckout()->getDataValue('ShippingAddress'); if (array_key_exists('CountryID', $shippingAddressData) && Customer::currentUser() instanceof Member && is_numeric($shippingAddressData['CountryID']) ) { $shippingCountry = Country::get()->byID($shippingAddressData['CountryID']); if ($shippingCountry instanceof Country) { $allowedPaymentMethods = PaymentMethod::getAllowedPaymentMethodsFor($shippingCountry, Customer::currentUser()->getCart()); if (!($allowedPaymentMethods instanceof ArrayList)) { $allowedPaymentMethods = ArrayList::create(); } } } $this->setAllowedPaymentMethods($allowedPaymentMethods); } return $this->allowedPaymentMethods; } /** * Sets the allowed payment methods. * * @param SS_List $allowedPaymentMethods Allowed payment method * * @return CheckoutStep4 */ public function setAllowedPaymentMethods(SS_List $allowedPaymentMethods) : CheckoutStep4 { $this->allowedPaymentMethods = $allowedPaymentMethods; return $this; } /** * Returns the active payment methods. * * @return SS_List */ public function getActivePaymentMethods() : SS_List { if (is_null($this->activePaymentMethods)) { $activePaymentMethods = PaymentMethod::getActivePaymentMethods(); if (!($activePaymentMethods instanceof DataList)) { $activePaymentMethods = ArrayList::create(); } $this->setActivePaymentMethods($activePaymentMethods); } return $this->activePaymentMethods; } /** * Sets the active payment methods. * * @param SS_List $activePaymentMethods Active payment method * * @return CheckoutStep4 */ public function setActivePaymentMethods(SS_List $activePaymentMethods) : CheckoutStep4 { $this->activePaymentMethods = $activePaymentMethods; return $this; } /** * Returns whether this step is visible. * * @return bool */ public function IsVisible() : bool { $isVisible = parent::IsVisible(); if ($this->SkipPaymentStep()) { $isVisible = false; } return $isVisible; } /** * Returns whether to skip this step or not. * * @return bool * * @author Sebastian Diel <sdiel@pixeltricks.de> * @since 27.11.2017 */ public function SkipPaymentStep() : bool { if (is_null($this->skipPaymentStep)) { $this->skipPaymentStep = false; if ((Config::SkipPaymentStepIfUnique() && $this->getAllowedPaymentMethods()->count() == 1) || (Config::SkipPaymentStepIfUnique() && $this->getActivePaymentMethods()->count() == 1) ) { $this->skipPaymentStep = true; } } return $this->skipPaymentStep; } /** * Returns the rendered step summary. * * @return DBHTMLText * * @author Sebastian Diel <sdiel@pixeltricks.de> * @since 13.03.2019 */ public function StepSummary() : DBHTMLText { return $this->customise([ 'InvoiceAddress' => $this->getController()->getInvoiceAddress(), 'ShippingAddress' => $this->getController()->getShippingAddress(), ])->renderWith(self::class . '_Summary'); } } |