From acb1763f9d2d2ec72eb5d3582811ab86576622fd Mon Sep 17 00:00:00 2001 From: kGablo Date: Wed, 10 Jan 2024 12:38:18 +0100 Subject: [PATCH] Card update --- Controller/tpay/CardPayment.php | 9 +++++++-- .../CardTransaction/CardApiFacade.php | 14 +++++++++++++- Model/ApiFacade/TpayConfig/ConfigFacade.php | 14 +++++++++++--- Model/GenericPaymentPlugin.php | 3 +++ Model/MethodListPlugin.php | 19 ++++++++++++++++++- Model/Tpay.php | 8 +------- Model/TpayConfigProvider.php | 4 +++- Service/TpayService.php | 3 +-- 8 files changed, 57 insertions(+), 17 deletions(-) diff --git a/Controller/tpay/CardPayment.php b/Controller/tpay/CardPayment.php index 2c5a3ea..e614cce 100644 --- a/Controller/tpay/CardPayment.php +++ b/Controller/tpay/CardPayment.php @@ -8,6 +8,7 @@ use Magento\Framework\App\ResourceConnection; use Magento\Framework\Model\Context as ModelContext; use Magento\Framework\Registry; +use Magento\Store\Model\StoreManagerInterface; use Tpay\OriginApi\Utilities\Util; use tpaycom\magento2basic\Api\TpayInterface; use tpaycom\magento2basic\Model\ApiFacade\CardTransaction\CardApiFacade; @@ -28,12 +29,16 @@ class CardPayment extends Action /** @var TpayTokensService */ private $tokensService; - public function __construct(Context $context, TpayInterface $tpayModel, TpayService $tpayService, Session $checkoutSession, ModelContext $modelContext, Registry $registry, ResourceConnection $resourceConnection) + /** @var StoreManagerInterface */ + private $storeManager; + + public function __construct(Context $context, TpayInterface $tpayModel, TpayService $tpayService, Session $checkoutSession, ModelContext $modelContext, Registry $registry, ResourceConnection $resourceConnection, StoreManagerInterface $storeManager) { $this->tpay = $tpayModel; $this->tpayService = $tpayService; $this->checkoutSession = $checkoutSession; $this->tokensService = new TpayTokensService($modelContext, $registry, $resourceConnection); + $this->storeManager = $storeManager; Util::$loggingEnabled = false; parent::__construct($context); } @@ -44,7 +49,7 @@ public function execute() $orderId = $this->checkoutSession->getLastRealOrderId(); if ($orderId) { - $cardTransaction = new CardApiFacade($this->tpay, $this->tokensService, $this->tpayService); + $cardTransaction = new CardApiFacade($this->tpay, $this->tokensService, $this->tpayService, $this->storeManager); $redirectUrl = $cardTransaction->makeCardTransaction($orderId); return $this->_redirect($redirectUrl); diff --git a/Model/ApiFacade/CardTransaction/CardApiFacade.php b/Model/ApiFacade/CardTransaction/CardApiFacade.php index a081af6..8dad2f8 100755 --- a/Model/ApiFacade/CardTransaction/CardApiFacade.php +++ b/Model/ApiFacade/CardTransaction/CardApiFacade.php @@ -3,6 +3,7 @@ namespace tpaycom\magento2basic\Model\ApiFacade\CardTransaction; use Exception; +use Magento\Store\Model\StoreManagerInterface; use tpaycom\magento2basic\Api\TpayInterface; use tpaycom\magento2basic\Service\TpayService; use tpaycom\magento2basic\Service\TpayTokensService; @@ -15,11 +16,15 @@ class CardApiFacade /** @var CardOpen */ private $cardOpen; + /** @var StoreManagerInterface */ + private $storeManager; + /** @var bool */ private $useOpenCard; - public function __construct(TpayInterface $tpay, TpayTokensService $tokensService, TpayService $tpayService) + public function __construct(TpayInterface $tpay, TpayTokensService $tokensService, TpayService $tpayService, StoreManagerInterface $storeManager) { + $this->storeManager = $storeManager; $this->cardOrigin = new CardOrigin($tpay, $tokensService, $tpayService); $this->createOpenApiInstance($tpay, $tokensService, $tpayService); } @@ -36,6 +41,13 @@ private function getCurrent() private function createOpenApiInstance(TpayInterface $tpay, TpayTokensService $tokensService, TpayService $tpayService) { + if ($this->storeManager->getStore()->getCurrentCurrencyCode() !== 'PLN') { + $this->cardOpen = null; + $this->useOpenCard = false; + + return; + } + try { $this->cardOpen = new CardOpen($tpay, $tokensService, $tpayService); $this->useOpenCard = true; diff --git a/Model/ApiFacade/TpayConfig/ConfigFacade.php b/Model/ApiFacade/TpayConfig/ConfigFacade.php index 562e744..28f9535 100755 --- a/Model/ApiFacade/TpayConfig/ConfigFacade.php +++ b/Model/ApiFacade/TpayConfig/ConfigFacade.php @@ -4,6 +4,7 @@ use Exception; use Magento\Framework\View\Asset\Repository; +use Magento\Store\Model\StoreManagerInterface; use tpaycom\magento2basic\Api\TpayInterface; use tpaycom\magento2basic\Service\TpayTokensService; @@ -18,10 +19,10 @@ class ConfigFacade /** @var bool */ private $useOpenApi; - public function __construct(TpayInterface $tpay, Repository $assetRepository, TpayTokensService $tokensService) + public function __construct(TpayInterface $tpay, Repository $assetRepository, TpayTokensService $tokensService, StoreManagerInterface $storeManager) { $this->originApi = new ConfigOrigin($tpay, $assetRepository, $tokensService); - $this->createOpenApiInstance($tpay, $assetRepository, $tokensService); + $this->createOpenApiInstance($tpay, $assetRepository, $tokensService, $storeManager); } public function getConfig(): array @@ -34,8 +35,15 @@ private function getCurrentApi() return $this->useOpenApi ? $this->openApi : $this->originApi; } - private function createOpenApiInstance(TpayInterface $tpay, Repository $assetRepository, TpayTokensService $tokensService) + private function createOpenApiInstance(TpayInterface $tpay, Repository $assetRepository, TpayTokensService $tokensService, StoreManagerInterface $storeManager) { + if ($storeManager->getStore()->getCurrentCurrencyCode() !== 'PLN') { + $this->openApi = null; + $this->useOpenApi = false; + + return; + } + try { $this->openApi = new ConfigOpen($tpay, $assetRepository, $tokensService); $this->useOpenApi = true; diff --git a/Model/GenericPaymentPlugin.php b/Model/GenericPaymentPlugin.php index 075fba6..3094276 100644 --- a/Model/GenericPaymentPlugin.php +++ b/Model/GenericPaymentPlugin.php @@ -13,6 +13,9 @@ public function beforeImportData(Payment $compiled, array $data): array $data['channel'] = explode('-', $data['method'])[1]; $data['method'] = TpayInterface::CODE; } + if ($data['method'] == 'tpaycom_magento2basic_cards') { + $data['method'] = TpayInterface::CODE; + } return [$data]; } diff --git a/Model/MethodListPlugin.php b/Model/MethodListPlugin.php index 4be087e..2e46a81 100644 --- a/Model/MethodListPlugin.php +++ b/Model/MethodListPlugin.php @@ -7,6 +7,7 @@ use Magento\Payment\Model\MethodInterface; use Magento\Payment\Model\MethodList; use Magento\Store\Model\ScopeInterface; +use Magento\Store\Model\StoreManagerInterface; use tpaycom\magento2basic\Api\TpayInterface; use tpaycom\magento2basic\Model\Config\Source\OnsiteChannels; @@ -23,11 +24,15 @@ class MethodListPlugin /** @var OnsiteChannels */ private $onsiteChannels; - public function __construct(Data $data, ScopeConfigInterface $scopeConfig, OnsiteChannels $onsiteChannels) + /** @var StoreManagerInterface */ + private $storeManager; + + public function __construct(Data $data, ScopeConfigInterface $scopeConfig, OnsiteChannels $onsiteChannels, StoreManagerInterface $storeManager) { $this->data = $data; $this->scopeConfig = $scopeConfig; $this->onsiteChannels = $onsiteChannels; + $this->storeManager = $storeManager; } public function afterGetAvailableMethods(MethodList $compiled, $result) @@ -36,6 +41,7 @@ public function afterGetAvailableMethods(MethodList $compiled, $result) $channels = $onsiteChannels ? explode(',', $onsiteChannels) : []; $result[] = $this->getMethodInstance('tpay.com - Płatność kartą', 'tpaycom_magento2basic_cards'); + $result = $this->filterResult($result); foreach ($channels as $onsiteChannel) { $result[] = $this->getMethodInstance( @@ -55,4 +61,15 @@ public function getMethodInstance(string $title, string $code): MethodInterface return $method; } + + private function filterResult(array $result): array + { + if ($this->storeManager->getStore()->getCurrentCurrencyCode() === 'PLN') { + return $result; + } + + return array_filter($result, function ($method) { + return $method->getCode() !== 'tpaycom_magento2basic'; + }); + } } diff --git a/Model/Tpay.php b/Model/Tpay.php index d3f0176..c5fbde7 100755 --- a/Model/Tpay.php +++ b/Model/Tpay.php @@ -277,7 +277,7 @@ public function isAvailable(?CartInterface $quote = null) return false; } - if (!$this->getMerchantId() || ($quote && !$this->isAvailableForCurrency($quote->getCurrency()->getQuoteCurrencyCode()))) { + if (!$this->getMerchantId()) { return false; } @@ -460,12 +460,6 @@ protected function getOrder(?string $orderId = null): \Magento\Sales\Api\Data\Or return $this->orderRepository->getByIncrementId($orderId); } - /** Availability for currency */ - protected function isAvailableForCurrency(string $currencyCode): bool - { - return !(!in_array($currencyCode, $this->availableCurrencyCodes)); - } - private function getMagentoVersion() { $objectManager = ObjectManager::getInstance(); diff --git a/Model/TpayConfigProvider.php b/Model/TpayConfigProvider.php index afb0094..a053780 100644 --- a/Model/TpayConfigProvider.php +++ b/Model/TpayConfigProvider.php @@ -7,6 +7,7 @@ use Magento\Checkout\Model\ConfigProviderInterface; use Magento\Framework\View\Asset\Repository; use Magento\Payment\Helper\Data as PaymentHelper; +use Magento\Store\Model\StoreManagerInterface; use tpaycom\magento2basic\Api\TpayInterface; use tpaycom\magento2basic\Model\ApiFacade\TpayConfig\ConfigFacade; use tpaycom\magento2basic\Model\ApiFacade\Transaction\TransactionApiFacade; @@ -29,12 +30,13 @@ class TpayConfigProvider implements ConfigProviderInterface public function __construct( PaymentHelper $paymentHelper, Repository $assetRepository, + StoreManagerInterface $storeManager, TpayTokensService $tokensService, TransactionApiFacade $transactionApiFacade ) { $this->paymentHelper = $paymentHelper; $this->transactionApi = $transactionApiFacade; - $this->configFacade = new ConfigFacade($this->getPaymentMethodInstance(), $assetRepository, $tokensService); + $this->configFacade = new ConfigFacade($this->getPaymentMethodInstance(), $assetRepository, $tokensService, $storeManager); } public function getConfig(): array diff --git a/Service/TpayService.php b/Service/TpayService.php index 0a517e6..3810ecd 100644 --- a/Service/TpayService.php +++ b/Service/TpayService.php @@ -242,10 +242,9 @@ private function registerCaptureNotificationTpay(OrderPaymentInterface $payment, $order = $payment->getOrder(); $amount = (float) $amount; $invoice = $this->getInvoiceForTransactionId($order, $payment->getTransactionId()); - $orderCurrency = $order->getOrderCurrency()->getCode(); // register new capture - if (!$invoice && 'PLN' === $orderCurrency && $payment->isCaptureFinal($amount)) { + if (!$invoice && $payment->isCaptureFinal($amount)) { $invoice = $order->prepareInvoice()->register(); $invoice->setOrder($order); $order->addRelatedObject($invoice);