diff --git a/Model/ApiFacade/OpenApi.php b/Model/ApiFacade/OpenApi.php index 78d0b0b..8b97662 100755 --- a/Model/ApiFacade/OpenApi.php +++ b/Model/ApiFacade/OpenApi.php @@ -35,9 +35,10 @@ public function makeRefund(InfoInterface $payment, float $amount): array public function channels(): array { $result = $this->transactions()->getChannels(); + $channels = []; - return array_map(function (array $channel) { - return new Channel( + foreach ($result['channels'] ?? [] as $channel) { + $channels[$channel['id']] = new Channel( $channel['id'], $channel['name'], $channel['fullName'], @@ -48,7 +49,9 @@ public function channels(): array $channel['groups'], $channel['constraints'] ); - }, $result['channels'] ?? []); + } + + return $channels; } private function handleDataStructure(array $data): array diff --git a/Model/ApiFacade/Transaction/TransactionApiFacade.php b/Model/ApiFacade/Transaction/TransactionApiFacade.php index 1281c43..377cae1 100755 --- a/Model/ApiFacade/Transaction/TransactionApiFacade.php +++ b/Model/ApiFacade/Transaction/TransactionApiFacade.php @@ -71,7 +71,7 @@ public function channels(): array } $channels = array_filter($this->openApi->channels(), function (Channel $channel) { - return true === $channel->available && true === empty($channel->constraints); + return true === $channel->available; }); $this->cache->save(serialize($channels), self::CHANNELS_CACHE_KEY, [], self::CACHE_LIFETIME); diff --git a/Model/ConstraintValidator.php b/Model/ConstraintValidator.php new file mode 100644 index 0000000..d0a4949 --- /dev/null +++ b/Model/ConstraintValidator.php @@ -0,0 +1,52 @@ +checkoutSession = $session; + } + + public function validate(array $constraints): bool + { + foreach ($constraints as $constraint) { + switch ($constraint['type']) { + case 'min': + if (!$this->validateMinimalTotal((float) $constraint['value'])) { + return false; + } + + break; + case 'max': + if (!$this->validateMaximalTotal((float) $constraint['value'])) { + return false; + } + + break; + default: + break; + } + } + + return true; + } + + private function validateMinimalTotal(float $minimal): bool + { + return $this->checkoutSession->getQuote()->getGrandTotal() > $minimal; + } + + private function validateMaximalTotal(float $maximal): bool + { + return $this->checkoutSession->getQuote()->getGrandTotal() < $maximal; + } +} diff --git a/Model/MethodListPlugin.php b/Model/MethodListPlugin.php index e4f8310..d191164 100644 --- a/Model/MethodListPlugin.php +++ b/Model/MethodListPlugin.php @@ -10,11 +10,12 @@ use Magento\Store\Model\ScopeInterface; use Magento\Store\Model\StoreManagerInterface; use tpaycom\magento2basic\Api\TpayInterface; +use tpaycom\magento2basic\Model\ApiFacade\Transaction\TransactionApiFacade; use tpaycom\magento2basic\Model\Config\Source\OnsiteChannels; class MethodListPlugin { - private const CONFIG_PATH = 'payment/tpaycom_magento2basic/onsite_channels'; + private const CONFIG_PATH = 'payment/tpaycom_magento2basic/openapi_settings/onsite_channels'; /** @var Data */ private $data; @@ -34,31 +35,55 @@ class MethodListPlugin /** @var Session */ private $checkoutSession; - public function __construct(Data $data, ScopeConfigInterface $scopeConfig, OnsiteChannels $onsiteChannels, StoreManagerInterface $storeManager, Tpay $tpay, Session $checkoutSession) - { + /** @var TransactionApiFacade */ + private $transactions; + + /** @var ConstraintValidator */ + private $constraintValidator; + + public function __construct( + Data $data, + ScopeConfigInterface $scopeConfig, + OnsiteChannels $onsiteChannels, + StoreManagerInterface $storeManager, + Tpay $tpay, + Session $checkoutSession, + TransactionApiFacade $transactions, + ConstraintValidator $constraintValidator + ) { $this->data = $data; $this->scopeConfig = $scopeConfig; $this->onsiteChannels = $onsiteChannels; $this->storeManager = $storeManager; $this->tpay = $tpay; $this->checkoutSession = $checkoutSession; + $this->transactions = $transactions; + $this->constraintValidator = $constraintValidator; } public function afterGetAvailableMethods(MethodList $compiled, $result) { $onsiteChannels = $this->scopeConfig->getValue(self::CONFIG_PATH, ScopeInterface::SCOPE_STORE); - $channels = $onsiteChannels ? explode(',', $onsiteChannels) : []; + $channelList = $onsiteChannels ? explode(',', $onsiteChannels) : []; + $channels = $this->transactions->channels(); - if (!$this->tpay->isCartValid((float) $this->checkoutSession->getQuote()->getGrandTotal())) { + if (!$this->tpay->isCartValid($this->checkoutSession->getQuote()->getGrandTotal())) { return $result; } $result[] = $this->getMethodInstance('tpay.com - Płatność kartą', 'tpaycom_magento2basic_cards'); $result = $this->filterResult($result); - foreach ($channels as $onsiteChannel) { + foreach ($channelList as $onsiteChannel) { + $channel = $channels[$onsiteChannel]; + + if (!empty($channel->constraints) && !$this->constraintValidator->validate($channel->constraints)) { + continue; + } + + $title = $this->onsiteChannels->getLabelFromValue($onsiteChannel); $result[] = $this->getMethodInstance( - $this->onsiteChannels->getLabelFromValue($onsiteChannel), + $title, "generic-{$onsiteChannel}" ); }