diff --git a/Model/MethodListPlugin.php b/Model/MethodListPlugin.php index ac7a93f..a7a74e1 100644 --- a/Model/MethodListPlugin.php +++ b/Model/MethodListPlugin.php @@ -2,11 +2,11 @@ namespace tpaycom\magento2basic\Model; +use Magento\Checkout\Model\Session; use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Payment\Helper\Data; use Magento\Payment\Model\MethodInterface; use Magento\Payment\Model\MethodList; -use Magento\Quote\Api\Data\CartInterface; use Magento\Store\Model\ScopeInterface; use Magento\Store\Model\StoreManagerInterface; use tpaycom\magento2basic\Api\TpayInterface; @@ -31,17 +31,17 @@ class MethodListPlugin /** @var Tpay */ private $tpay; - /** @var CartInterface */ - private $cartInterface; + /** @var Session */ + private $checkoutSession; - public function __construct(Data $data, ScopeConfigInterface $scopeConfig, OnsiteChannels $onsiteChannels, StoreManagerInterface $storeManager, Tpay $tpay, CartInterface $cartInterface) + public function __construct(Data $data, ScopeConfigInterface $scopeConfig, OnsiteChannels $onsiteChannels, StoreManagerInterface $storeManager, Tpay $tpay, Session $checkoutSession) { $this->data = $data; $this->scopeConfig = $scopeConfig; $this->onsiteChannels = $onsiteChannels; $this->storeManager = $storeManager; $this->tpay = $tpay; - $this->cartInterface = $cartInterface; + $this->checkoutSession = $checkoutSession; } public function afterGetAvailableMethods(MethodList $compiled, $result) @@ -49,7 +49,7 @@ public function afterGetAvailableMethods(MethodList $compiled, $result) $onsiteChannels = $this->scopeConfig->getValue(self::CONFIG_PATH, ScopeInterface::SCOPE_STORE); $channels = $onsiteChannels ? explode(',', $onsiteChannels) : []; - if (!$this->tpay->isAvailable($this->cartInterface)) { + if (!$this->tpay->isCartValid((float)$this->checkoutSession->getQuote()->getGrandTotal())) { return $result; } diff --git a/Model/Tpay.php b/Model/Tpay.php index c12805c..ca248ee 100755 --- a/Model/Tpay.php +++ b/Model/Tpay.php @@ -440,6 +440,22 @@ public function getClientId(): ?string return $this->getConfigData('client_id'); } + public function isCartValid(?float $grandTotal = null): bool + { + $minAmount = $this->getConfigData('min_order_total'); + $maxAmount = $this->getConfigData('max_order_total'); + + if ($grandTotal && ($grandTotal < $minAmount || ($maxAmount && $grandTotal > $maxAmount))) { + return false; + } + + if (!$this->getMerchantId()) { + return false; + } + + return !is_null($grandTotal); + } + /** Check that the BLIK should be available for order/quote amount */ protected function checkBlikAmount(): bool {