From ca48572a8057c4d48fbcfa541e70289079dbe52a Mon Sep 17 00:00:00 2001 From: krzGablo <147171777+krzGablo@users.noreply.github.com> Date: Mon, 18 Mar 2024 19:53:37 +0100 Subject: [PATCH] Api connection fix (#47) --- Api/TpayConfigInterface.php | 2 + .../CardTransaction/CardApiFacade.php | 37 +++++++++++++++++- Model/ApiFacade/Refund/RefundApiFacade.php | 12 +++++- .../ApiFacade/TpayConfig/CardConfigFacade.php | 36 +++++++++++++++++- Model/ApiFacade/TpayConfig/ConfigFacade.php | 38 +++++++++++++++++-- .../Transaction/TransactionApiFacade.php | 32 ++++++++++++---- Model/MethodListPlugin.php | 11 +++++- Model/TpayConfigProvider.php | 4 ++ Model/TpayPayment.php | 4 ++ Provider/ConfigurationProvider.php | 9 ++++- 10 files changed, 165 insertions(+), 20 deletions(-) diff --git a/Api/TpayConfigInterface.php b/Api/TpayConfigInterface.php index 85e3121..aa329cf 100644 --- a/Api/TpayConfigInterface.php +++ b/Api/TpayConfigInterface.php @@ -7,6 +7,8 @@ /** @api */ interface TpayConfigInterface { + public function isTpayActive(): bool; + public function getTitle(): ?string; public function getCardTitle(): ?string; diff --git a/Model/ApiFacade/CardTransaction/CardApiFacade.php b/Model/ApiFacade/CardTransaction/CardApiFacade.php index daec085..f2cab91 100755 --- a/Model/ApiFacade/CardTransaction/CardApiFacade.php +++ b/Model/ApiFacade/CardTransaction/CardApiFacade.php @@ -17,6 +17,18 @@ class CardApiFacade /** @var CardOpen */ private $cardOpen; + /** @var TpayInterface */ + private $tpay; + + /** @var TpayConfigInterface */ + private $tpayConfig; + + /** @var TpayTokensService */ + private $tokensService; + + /** @var TpayService */ + private $tpayService; + /** @var StoreManagerInterface */ private $storeManager; @@ -25,9 +37,11 @@ class CardApiFacade public function __construct(TpayInterface $tpay, TpayConfigInterface $tpayConfig, TpayTokensService $tokensService, TpayService $tpayService, StoreManagerInterface $storeManager) { + $this->tpay = $tpay; + $this->tpayConfig = $tpayConfig; + $this->tokensService = $tokensService; + $this->tpayService = $tpayService; $this->storeManager = $storeManager; - $this->createCardOriginApiInstance($tpay, $tpayConfig, $tokensService, $tpayService); - $this->createOpenApiInstance($tpay, $tpayConfig, $tokensService, $tpayService); } public function makeCardTransaction(string $orderId, ?array $customerToken = null): string @@ -37,14 +51,33 @@ public function makeCardTransaction(string $orderId, ?array $customerToken = nul public function payTransaction(string $orderId, array $additionalPaymentInformation, ?string $transactionId = null, ?array $customerToken = null): string { + $this->connectApi(); + return $this->useOpenCard ? $this->cardOpen->payTransaction($orderId, $additionalPaymentInformation, $transactionId, $customerToken) : 'error'; } + public function isOpenApiUse(): bool + { + $this->connectApi(); + + return (bool) $this->useOpenCard; + } + private function getCurrent() { + $this->connectApi(); + return $this->useOpenCard ? $this->cardOpen : $this->cardOrigin; } + private function connectApi() + { + if (null == $this->cardOpen && null === $this->cardOrigin) { + $this->createCardOriginApiInstance($this->tpay, $this->tpayConfig, $this->tokensService, $this->tpayService); + $this->createOpenApiInstance($this->tpay, $this->tpayConfig, $this->tokensService, $this->tpayService); + } + } + private function createCardOriginApiInstance(TpayInterface $tpay, TpayConfigInterface $tpayConfig, TpayTokensService $tokensService, TpayService $tpayService) { if (!$tpayConfig->isCardEnabled()) { diff --git a/Model/ApiFacade/Refund/RefundApiFacade.php b/Model/ApiFacade/Refund/RefundApiFacade.php index aff61a1..d206fbf 100755 --- a/Model/ApiFacade/Refund/RefundApiFacade.php +++ b/Model/ApiFacade/Refund/RefundApiFacade.php @@ -25,8 +25,6 @@ class RefundApiFacade public function __construct(TpayConfigInterface $tpay) { $this->tpay = $tpay; - $this->createRefundOriginApiInstance($tpay); - $this->createOpenApiInstance($tpay); } public function makeRefund(InfoInterface $payment, float $amount) @@ -43,9 +41,19 @@ public function makeRefund(InfoInterface $payment, float $amount) private function getCurrentApi() { + $this->connectApi(); + return $this->useOpenApi ? $this->openApi : $this->originApi; } + private function connectApi() + { + if (null == $this->openApi && null === $this->originApi) { + $this->createRefundOriginApiInstance($this->tpay); + $this->createOpenApiInstance($this->tpay); + } + } + private function createRefundOriginApiInstance(TpayConfigInterface $tpay) { try { diff --git a/Model/ApiFacade/TpayConfig/CardConfigFacade.php b/Model/ApiFacade/TpayConfig/CardConfigFacade.php index f9fcf0a..8b9e613 100755 --- a/Model/ApiFacade/TpayConfig/CardConfigFacade.php +++ b/Model/ApiFacade/TpayConfig/CardConfigFacade.php @@ -19,17 +19,41 @@ class CardConfigFacade /** @var ConfigOpen */ private $openApi; + /** @var TpayInterface */ + private $tpay; + + /** @var TpayConfigInterface */ + private $tpayConfig; + + /** @var Repository */ + private $assetRepository; + + /** @var TpayTokensService */ + private $tokensService; + + /** @var StoreManagerInterface */ + private $storeManager; + + /** @var TpayService */ + private $tpayService; + /** @var bool */ private $useOpenApi; public function __construct(TpayInterface $tpay, TpayConfigInterface $tpayConfig, Repository $assetRepository, TpayTokensService $tokensService, StoreManagerInterface $storeManager, TpayService $tpayService) { - $this->createOriginApiInstance($tpay, $tpayConfig, $assetRepository, $tokensService, $tpayService); - $this->createOpenApiInstance($tpay, $tpayConfig, $assetRepository, $tokensService, $storeManager); + $this->tpay = $tpay; + $this->tpayConfig = $tpayConfig; + $this->assetRepository = $assetRepository; + $this->tokensService = $tokensService; + $this->storeManager = $storeManager; + $this->tpayService = $tpayService; } public function getConfig(): array { + $this->connectApi(); + return $this->getCurrentApi() ? $this->getCurrentApi()->getCardConfig() : []; } @@ -38,6 +62,14 @@ private function getCurrentApi() return $this->useOpenApi ? $this->openApi : $this->originApi; } + private function connectApi() + { + if (null == $this->openApi && null === $this->originApi) { + $this->createOriginApiInstance($this->tpay, $this->tpayConfig, $this->assetRepository, $this->tokensService, $this->tpayService); + $this->createOpenApiInstance($this->tpay, $this->tpayConfig, $this->assetRepository, $this->tokensService, $this->storeManager); + } + } + private function createOriginApiInstance(TpayInterface $tpay, TpayConfigInterface $tpayConfig, Repository $assetRepository, TpayTokensService $tokensService, TpayService $tpayService) { if (!$tpayConfig->isCardEnabled()) { diff --git a/Model/ApiFacade/TpayConfig/ConfigFacade.php b/Model/ApiFacade/TpayConfig/ConfigFacade.php index 25427c2..0c91220 100755 --- a/Model/ApiFacade/TpayConfig/ConfigFacade.php +++ b/Model/ApiFacade/TpayConfig/ConfigFacade.php @@ -22,18 +22,41 @@ class ConfigFacade /** @var CardConfigFacade */ private $cardConfig; + /** @var TpayInterface */ + private $tpay; + + /** @var TpayConfigInterface */ + private $tpayConfig; + + /** @var Repository */ + private $assetRepository; + + /** @var TpayTokensService */ + private $tokensService; + + /** @var StoreManagerInterface */ + private $storeManager; + + /** @var TpayService */ + private $tpayService; + /** @var bool */ private $useOpenApi; public function __construct(TpayInterface $tpay, TpayConfigInterface $tpayConfig, Repository $assetRepository, TpayTokensService $tokensService, StoreManagerInterface $storeManager, TpayService $tpayService) { - $this->createOriginApiInstance($tpay, $tpayConfig, $assetRepository, $tokensService); - $this->createOpenApiInstance($tpay, $tpayConfig, $assetRepository, $tokensService, $storeManager); - $this->cardConfig = new CardConfigFacade($tpay, $tpayConfig, $assetRepository, $tokensService, $storeManager, $tpayService); + $this->tpay = $tpay; + $this->tpayConfig = $tpayConfig; + $this->assetRepository = $assetRepository; + $this->tokensService = $tokensService; + $this->storeManager = $storeManager; + $this->tpayService = $tpayService; } public function getConfig(): array { + $this->connectApi(); + return array_merge($this->getCurrentApi() ? $this->getCurrentApi()->getConfig() : [], $this->cardConfig->getConfig()); } @@ -42,6 +65,15 @@ private function getCurrentApi() return $this->useOpenApi ? $this->openApi : $this->originConfig; } + private function connectApi() + { + if (null == $this->openApi && null === $this->originConfig) { + $this->createOriginApiInstance($this->tpay, $this->tpayConfig, $this->assetRepository, $this->tokensService); + $this->createOpenApiInstance($this->tpay, $this->tpayConfig, $this->assetRepository, $this->tokensService, $this->storeManager); + $this->cardConfig = new CardConfigFacade($this->tpay, $this->tpayConfig, $this->assetRepository, $this->tokensService, $this->storeManager, $this->tpayService); + } + } + private function createOriginApiInstance(TpayInterface $tpay, TpayConfigInterface $tpayConfig, Repository $assetRepository, TpayTokensService $tokensService) { if (!$tpayConfig->isOriginApiEnabled()) { diff --git a/Model/ApiFacade/Transaction/TransactionApiFacade.php b/Model/ApiFacade/Transaction/TransactionApiFacade.php index 82ca067..bb8480c 100755 --- a/Model/ApiFacade/Transaction/TransactionApiFacade.php +++ b/Model/ApiFacade/Transaction/TransactionApiFacade.php @@ -20,21 +20,25 @@ class TransactionApiFacade /** @var OpenApi */ private $openApi; - /** @var bool */ - private $useOpenApi; + /** @var TpayConfigInterface */ + private $tpay; /** @var CacheInterface */ private $cache; + /** @var bool */ + private $useOpenApi = false; + public function __construct(TpayConfigInterface $tpay, CacheInterface $cache) { - $this->createOriginApiInstance($tpay); - $this->createOpenApiInstance($tpay); + $this->tpay = $tpay; $this->cache = $cache; } public function isOpenApiUse(): bool { + $this->connectApi(); + return $this->useOpenApi; } @@ -65,16 +69,18 @@ public function blik($blikTransactionId, $blikCode): array /** @return list */ public function channels(): array { + $this->connectApi(); + + if (!$this->useOpenApi) { + return []; + } + $channels = $this->cache->load(self::CHANNELS_CACHE_KEY); if ($channels) { return unserialize($channels); } - if (false === $this->useOpenApi) { - return []; - } - $channels = array_filter($this->openApi->channels(), function (Channel $channel) { return true === $channel->available; }); @@ -114,9 +120,19 @@ public function originApiFieldCorrect(array $data): array private function getCurrentApi() { + $this->connectApi(); + return $this->useOpenApi ? $this->openApi : $this->originApi; } + private function connectApi() + { + if (null == $this->openApi && null === $this->originApi) { + $this->createOriginApiInstance($this->tpay); + $this->createOpenApiInstance($this->tpay); + } + } + private function createOriginApiInstance(TpayConfigInterface $tpay) { if (!$tpay->isOriginApiEnabled()) { diff --git a/Model/MethodListPlugin.php b/Model/MethodListPlugin.php index 2070264..6b6d391 100644 --- a/Model/MethodListPlugin.php +++ b/Model/MethodListPlugin.php @@ -18,6 +18,9 @@ class MethodListPlugin { private const CONFIG_PATH = 'payment/tpaycom_magento2basic/openapi_settings/onsite_channels'; + /** @var TpayInterface */ + protected $paymentMethod; + /** @var Data */ private $data; @@ -54,7 +57,8 @@ public function __construct( TpayConfigInterface $tpayConfig, Session $checkoutSession, TransactionApiFacade $transactions, - ConstraintValidator $constraintValidator + ConstraintValidator $constraintValidator, + TpayInterface $paymentMethod ) { $this->data = $data; $this->scopeConfig = $scopeConfig; @@ -65,10 +69,15 @@ public function __construct( $this->checkoutSession = $checkoutSession; $this->transactions = $transactions; $this->constraintValidator = $constraintValidator; + $this->paymentMethod = $paymentMethod; } public function afterGetAvailableMethods(MethodList $compiled, $result) { + if (!$this->paymentMethod->isAvailable()) { + return $result; + } + $onsiteChannels = $this->scopeConfig->getValue(self::CONFIG_PATH, ScopeInterface::SCOPE_STORE); $channelList = $onsiteChannels ? explode(',', $onsiteChannels) : []; $channels = $this->transactions->channels(); diff --git a/Model/TpayConfigProvider.php b/Model/TpayConfigProvider.php index ea50b14..9fdc9e1 100644 --- a/Model/TpayConfigProvider.php +++ b/Model/TpayConfigProvider.php @@ -45,6 +45,10 @@ public function __construct( public function getConfig(): array { + if (!$this->paymentMethod->isAvailable()) { + return []; + } + $config = $this->configFacade->getConfig(); $channels = $this->transactionApi->channels(); diff --git a/Model/TpayPayment.php b/Model/TpayPayment.php index 43a2208..55da154 100755 --- a/Model/TpayPayment.php +++ b/Model/TpayPayment.php @@ -211,6 +211,10 @@ public function getTpayFormData(?string $orderId = null): array public function isAvailable(?CartInterface $quote = null) { + if (!$this->configurationProvider->isTpayActive()) { + return false; + } + $minAmount = $this->configurationProvider->getMinOrderTotal(); $maxAmount = $this->configurationProvider->getMaxOrderTotal(); diff --git a/Provider/ConfigurationProvider.php b/Provider/ConfigurationProvider.php index 76423b6..8841e2d 100755 --- a/Provider/ConfigurationProvider.php +++ b/Provider/ConfigurationProvider.php @@ -27,6 +27,11 @@ public function __construct(ScopeConfigInterface $scopeConfig, ProductMetadataIn $this->productMetadataInterface = $productMetadataInterface; } + public function isTpayActive(): bool + { + return (bool) $this->getConfigData('active'); + } + public function getBlikLevelZeroStatus(): bool { return (bool) $this->getConfigData('general_settings/blik_level_zero'); @@ -147,12 +152,12 @@ public function getRSAKey(): ?string return $this->getConfigData('cardpayment_settings/rsa_key'); } - public function getHashType(): string + public function getHashType(): ?string { return $this->getConfigData('cardpayment_settings/cardpayment_originapi_settings/hash_type'); } - public function getVerificationCode(): string + public function getVerificationCode(): ?string { return $this->getConfigData('cardpayment_settings/cardpayment_originapi_settings/verification_code'); }