diff --git a/.version b/.version index f1547e6..815e68d 100644 --- a/.version +++ b/.version @@ -1 +1 @@ -2.0.7 +2.0.8 diff --git a/Api/TpayConfigInterface.php b/Api/TpayConfigInterface.php index df5e95d..18f48ef 100644 --- a/Api/TpayConfigInterface.php +++ b/Api/TpayConfigInterface.php @@ -13,25 +13,25 @@ public function getTitle(): ?string; public function getCardTitle(): ?string; - public function isOriginApiEnabled(): bool; + public function isOriginApiEnabled(?int $storeId = null): bool; - public function isOpenApiEnabled(): bool; + public function isOpenApiEnabled(?int $storeId = null): bool; public function isCardEnabled(): bool; public function isOriginApiCardUse(): bool; - public function getApiPassword(): ?string; + public function getApiPassword(?int $storeId = null): ?string; - public function getOpenApiPassword(): ?string; + public function getOpenApiPassword(?int $storeId = null): ?string; - public function getApiKey(): ?string; + public function getApiKey(?int $storeId = null): ?string; public function getSecurityCode(?int $storeId = null): ?string; - public function getOpenApiClientId(): ?string; + public function getOpenApiClientId(?int $storeId = null): ?string; - public function getMerchantId(): ?int; + public function getMerchantId(?int $storeId = null): ?int; public function getBlikLevelZeroStatus(): bool; diff --git a/CHANGELOG.MD b/CHANGELOG.MD index 7c6f408..49f9efc 100644 --- a/CHANGELOG.MD +++ b/CHANGELOG.MD @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [2.0.8] +### Fixed +- Fixed admin scope config + ## [2.0.7] ### Added - Added regulations in english diff --git a/Controller/Tpay/Notification.php b/Controller/Tpay/Notification.php index c3bb882..b6cc4b8 100644 --- a/Controller/Tpay/Notification.php +++ b/Controller/Tpay/Notification.php @@ -84,32 +84,17 @@ public function executeNotification(): ?Response public function executeCardNotification(): ?Response { - try { - $notification = (new OriginJWSVerifiedPaymentNotification( - $this->tpayConfig->getSecurityCode(), - !$this->tpayConfig->useSandboxMode() - ))->getNotification(); - - $orderId = base64_decode($notification['order_id']); - - $this->tpayService->setCardOrderStatus($orderId, $notification, $this->tpayConfig); - $this->saveOriginCard($notification, $orderId); + $response = null; - return $this->response->setStatusCode(Response::STATUS_CODE_200)->setContent('TRUE'); - } catch (Exception $e) { - Util::log( - 'Notification exception', - sprintf( - '%s in file %s line: %d \n\n %s', - $e->getMessage(), - $e->getFile(), - $e->getLine(), - $e->getTraceAsString() - ) - ); + foreach ($this->storeManager->getStores() as $store) { + $response = $this->extractCardNotification($store); - return $this->response->setStatusCode(Response::STATUS_CODE_400)->setContent('FALSE'); + if (Response::STATUS_CODE_200 === $response->getStatusCode()) { + break; + } } + + return $response; } public function createCsrfValidationException(RequestInterface $request): ?InvalidRequestException @@ -213,4 +198,36 @@ private function extractNotification(StoreInterface $store): Response return $this->response->setStatusCode(Response::STATUS_CODE_400)->setContent('FALSE'); } } + + private function extractCardNotification(StoreInterface $store): ?Response + { + $storeId = (int) $store->getStoreId(); + + try { + $notification = (new OriginJWSVerifiedPaymentNotification( + $this->tpayConfig->getSecurityCode($storeId), + !$this->tpayConfig->useSandboxMode($storeId) + ))->getNotification(); + + $orderId = base64_decode($notification['order_id']); + + $this->tpayService->setCardOrderStatus($orderId, $notification, $this->tpayConfig); + $this->saveOriginCard($notification, $orderId); + + return $this->response->setStatusCode(Response::STATUS_CODE_200)->setContent('TRUE'); + } catch (Exception $e) { + Util::log( + 'Notification exception', + sprintf( + '%s in file %s line: %d \n\n %s', + $e->getMessage(), + $e->getFile(), + $e->getLine(), + $e->getTraceAsString() + ) + ); + + return $this->response->setStatusCode(Response::STATUS_CODE_400)->setContent('FALSE'); + } + } } diff --git a/Model/ApiFacade/OpenApi.php b/Model/ApiFacade/OpenApi.php index 4a58edb..8f380a6 100755 --- a/Model/ApiFacade/OpenApi.php +++ b/Model/ApiFacade/OpenApi.php @@ -17,18 +17,19 @@ class OpenApi private $cache; - public function __construct(TpayConfigInterface $tpay, CacheInterface $cache) + public function __construct(TpayConfigInterface $tpay, CacheInterface $cache, ?int $storeId = null) { $this->cache = $cache; - $this->tpayApi = new TpayApi($tpay->getOpenApiClientId(), $tpay->getOpenApiPassword(), !$tpay->useSandboxMode(), 'read', null, $tpay->buildMagentoInfo()); - $token = $this->cache->load($this->getAuthTokenCacheKey($tpay)); - + $this->tpayApi = new TpayApi($tpay->getOpenApiClientId($storeId), $tpay->getOpenApiPassword($storeId), !$tpay->useSandboxMode($storeId)); + $token = $this->cache->load($this->getAuthTokenCacheKey($tpay, $storeId)); if ($token) { $this->tpayApi->setCustomToken(unserialize($token)); } + $this->tpayApi->authorization(); + if (!$token) { - $this->cache->save(serialize($this->tpayApi->getToken()), $this->getAuthTokenCacheKey($tpay), [\Magento\Framework\App\Config::CACHE_TAG], 7100); + $this->cache->save(serialize($this->tpayApi->getToken()), $this->getAuthTokenCacheKey($tpay, $storeId), [\Magento\Framework\App\Config::CACHE_TAG], 7100); } } @@ -217,12 +218,12 @@ private function waitForBlikAccept(array $result): array return $result; } - private function getAuthTokenCacheKey(TpayConfigInterface $tpay) + private function getAuthTokenCacheKey(TpayConfigInterface $tpay, ?int $storeId = null) { return sprintf( self::AUTH_TOKEN_CACHE_KEY, md5( - join('|', [$tpay->getOpenApiClientId(), $tpay->getOpenApiPassword(), !$tpay->useSandboxMode()]) + join('|', [$tpay->getOpenApiClientId($storeId), $tpay->getOpenApiPassword($storeId), !$tpay->useSandboxMode($storeId)]) ) ); } diff --git a/Model/ApiFacade/Transaction/TransactionApiFacade.php b/Model/ApiFacade/Transaction/TransactionApiFacade.php index 7a9b766..130631b 100755 --- a/Model/ApiFacade/Transaction/TransactionApiFacade.php +++ b/Model/ApiFacade/Transaction/TransactionApiFacade.php @@ -28,10 +28,14 @@ class TransactionApiFacade /** @var bool */ private $useOpenApi = false; - public function __construct(TpayConfigInterface $tpay, CacheInterface $cache) + /** @var null|int */ + private $storeId; + + public function __construct(TpayConfigInterface $tpay, CacheInterface $cache, ?int $storeId = null) { $this->tpay = $tpay; $this->cache = $cache; + $this->storeId = $storeId; } public function isOpenApiUse(): bool @@ -74,7 +78,7 @@ public function channels(): array return []; } - $cacheKey = 'tpay_channels_'.md5(join('|', [$this->tpay->getOpenApiClientId(), $this->tpay->getOpenApiPassword(), !$this->tpay->useSandboxMode()])); + $cacheKey = 'tpay_channels_'.md5(join('|', [$this->tpay->getOpenApiClientId($this->storeId), $this->tpay->getOpenApiPassword($this->storeId), !$this->tpay->useSandboxMode($this->storeId)])); $channels = $this->cache->load($cacheKey); @@ -136,14 +140,14 @@ private function connectApi() private function createOriginApiInstance(TpayConfigInterface $tpay) { - if (!$tpay->isOriginApiEnabled()) { + if (!$tpay->isOriginApiEnabled($this->storeId)) { $this->originApi = null; return; } try { - $this->originApi = new TransactionOriginApi($tpay->getApiPassword(), $tpay->getApiKey(), $tpay->getMerchantId(), $tpay->getSecurityCode(), !$tpay->useSandboxMode()); + $this->originApi = new TransactionOriginApi($tpay->getApiPassword($this->storeId), $tpay->getApiKey($this->storeId), $tpay->getMerchantId($this->storeId), $tpay->getSecurityCode($this->storeId), !$tpay->useSandboxMode($this->storeId)); } catch (Exception $exception) { $this->originApi = null; } @@ -151,7 +155,7 @@ private function createOriginApiInstance(TpayConfigInterface $tpay) private function createOpenApiInstance(TpayConfigInterface $tpay) { - if (!$tpay->isOpenApiEnabled()) { + if (!$tpay->isOpenApiEnabled($this->storeId)) { $this->openApi = null; $this->useOpenApi = false; @@ -159,7 +163,7 @@ private function createOpenApiInstance(TpayConfigInterface $tpay) } try { - $this->openApi = new OpenApi($tpay, $this->cache); + $this->openApi = new OpenApi($tpay, $this->cache, $this->storeId); $this->useOpenApi = true; } catch (Exception $exception) { $this->openApi = null; diff --git a/Model/Config/Source/OnsiteChannels.php b/Model/Config/Source/OnsiteChannels.php index 7d788df..14e9dca 100644 --- a/Model/Config/Source/OnsiteChannels.php +++ b/Model/Config/Source/OnsiteChannels.php @@ -2,8 +2,10 @@ namespace Tpay\Magento2\Model\Config\Source; +use Magento\Framework\App\Action\Context; use Magento\Framework\App\CacheInterface; use Magento\Framework\Data\OptionSourceInterface; +use Magento\Store\Model\StoreManagerInterface; use Tpay\Magento2\Api\TpayConfigInterface; use Tpay\Magento2\Model\ApiFacade\Transaction\Dto\Channel; use Tpay\Magento2\Model\ApiFacade\Transaction\TransactionApiFacade; @@ -13,9 +15,9 @@ class OnsiteChannels implements OptionSourceInterface /** @var TransactionApiFacade */ private $transactions; - public function __construct(TpayConfigInterface $tpay, CacheInterface $cache) + public function __construct(TpayConfigInterface $tpay, Context $context, StoreManagerInterface $storeManager, CacheInterface $cache) { - $this->transactions = new TransactionApiFacade($tpay, $cache); + $this->transactions = new TransactionApiFacade($tpay, $cache, $this->getStoreId($context, $storeManager)); } public function getLabelFromValue(int $value): ?string @@ -36,4 +38,20 @@ public function toOptionArray(): array return ['value' => $channel->id, 'label' => $channel->fullName]; }, $this->transactions->channels()); } + + private function getStoreId(Context $context, StoreManagerInterface $storeManager): ?int + { + $scope = $context->getRequest()->getParam('store', null); + $websiteScope = $context->getRequest()->getParam('website', null); + $storeId = null; + + if (null !== $scope) { + $storeId = (int) $storeManager->getStore($scope)->getId(); + } elseif (null !== $websiteScope) { + $website = $storeManager->getWebsite($websiteScope); + $storeId = (int) $website->getDefaultStore()->getId(); + } + + return $storeId; + } } diff --git a/Provider/ConfigurationProvider.php b/Provider/ConfigurationProvider.php index 40c1632..74ff3e8 100755 --- a/Provider/ConfigurationProvider.php +++ b/Provider/ConfigurationProvider.php @@ -56,9 +56,9 @@ public function getTitle(): ?string return $this->getConfigData('general_settings/title'); } - public function getApiKey(): ?string + public function getApiKey(?int $storeId = null): ?string { - return $this->getConfigData('originapi_settings/api_key_tpay'); + return $this->getConfigData('originapi_settings/api_key_tpay', $storeId); } public function getCardApiKey(): ?string @@ -66,9 +66,9 @@ public function getCardApiKey(): ?string return $this->getConfigData('cardpayment_settings/cardpayment_originapi_settings/card_api_key_tpay'); } - public function getApiPassword(): ?string + public function getApiPassword(?int $storeId = null): ?string { - return $this->getConfigData('originapi_settings/api_password'); + return $this->getConfigData('originapi_settings/api_password', $storeId); } public function getCardApiPassword(): ?string @@ -99,19 +99,19 @@ public function getRegulationsURL(): string return $this->regulationsEnURL; } - public function getOpenApiPassword(): ?string + public function getOpenApiPassword(?int $storeId = null): ?string { - return $this->getConfigData('openapi_settings/open_api_password'); + return $this->getConfigData('openapi_settings/open_api_password', $storeId); } - public function getMerchantId(): ?int + public function getMerchantId(?int $storeId = null): ?int { - return (int) $this->getConfigData('general_settings/merchant_id'); + return (int) $this->getConfigData('general_settings/merchant_id', $storeId); } - public function getOpenApiClientId(): ?string + public function getOpenApiClientId(?int $storeId = null): ?string { - return $this->getConfigData('openapi_settings/open_api_client_id'); + return $this->getConfigData('openapi_settings/open_api_client_id', $storeId); } public function getSecurityCode(?int $storeId = null): ?string @@ -134,14 +134,14 @@ public function getCardTitle(): ?string return $this->getConfigData('cardpayment_settings/card_title') ?? ''; } - public function isOriginApiEnabled(): bool + public function isOriginApiEnabled(?int $storeId = null): bool { - return (bool) $this->getConfigData('originapi_settings/origin_api_active'); + return (bool) $this->getConfigData('originapi_settings/origin_api_active', $storeId); } - public function isOpenApiEnabled(): bool + public function isOpenApiEnabled(?int $storeId = null): bool { - return (bool) $this->getConfigData('openapi_settings/open_api_active'); + return (bool) $this->getConfigData('openapi_settings/open_api_active', $storeId); } public function isCardEnabled(): bool