From 2a4fc1248785c991c2ededc9e7c286f6cf1f287b Mon Sep 17 00:00:00 2001 From: krzGablo <147171777+krzGablo@users.noreply.github.com> Date: Tue, 18 Jun 2024 09:03:16 +0200 Subject: [PATCH] card notification config update (#54) --- CHANGELOG.MD | 6 ++ Controller/Tpay/Notification.php | 60 ++++++++++++++++++- .../CardTransaction/CardApiFacade.php | 1 + Model/ApiFacade/CardTransaction/CardOpen.php | 2 +- .../ApiFacade/CardTransaction/CardOrigin.php | 4 +- Model/ApiFacade/OpenApi.php | 2 +- Provider/ConfigurationProvider.php | 4 +- composer.json | 2 +- 8 files changed, 74 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.MD b/CHANGELOG.MD index b7bb6c3..7c6f408 100644 --- a/CHANGELOG.MD +++ b/CHANGELOG.MD @@ -4,6 +4,12 @@ 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.7] +### Added +- Added regulations in english +### Fixed +- Fixed card notification + ## [2.0.6] ### Fixed - Fixed currency validation diff --git a/Controller/Tpay/Notification.php b/Controller/Tpay/Notification.php index 470d894..c3bb882 100644 --- a/Controller/Tpay/Notification.php +++ b/Controller/Tpay/Notification.php @@ -18,6 +18,7 @@ use Tpay\Magento2\Service\TpayService; use Tpay\Magento2\Service\TpayTokensService; use Tpay\OriginApi\Utilities\Util; +use Tpay\OriginApi\Webhook\JWSVerifiedPaymentNotification as OriginJWSVerifiedPaymentNotification; use tpaySDK\Webhook\JWSVerifiedPaymentNotification; class Notification implements CsrfAwareActionInterface @@ -58,6 +59,15 @@ public function __construct( } public function execute(): ?Response + { + if (isset($_POST['card'])) { + return $this->executeCardNotification(); + } + + return $this->executeNotification(); + } + + public function executeNotification(): ?Response { $response = null; @@ -72,6 +82,36 @@ public function execute(): ?Response return $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); + + 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'); + } + } + public function createCsrfValidationException(RequestInterface $request): ?InvalidRequestException { return null; @@ -108,7 +148,7 @@ private function saveCard(array $notification, string $orderId) if (isset($notification['card_token']) && !$this->tpay->isCustomerGuest($orderId)) { $token = $this->tokensService->getWithoutAuthCustomerTokens( - $order->getCustomerId(), + (string) $order->getCustomerId(), $notification['tr_crc'] ); @@ -118,6 +158,23 @@ private function saveCard(array $notification, string $orderId) } } + private function saveOriginCard(array $notification, string $orderId) + { + $order = $this->tpayService->getOrderById($orderId); + + $payment = $this->tpayService->getPayment($orderId); + $additionalPaymentInformation = $payment->getData()['additional_information']; + + if (isset($notification['cli_auth']) && $this->tpayConfig->getCardSaveEnabled() && !$this->tpay->isCustomerGuest($orderId)) { + $this->tokensService->setCustomerToken( + (string) $order->getCustomerId(), + $notification['cli_auth'], + $notification['card'], + $additionalPaymentInformation['card_vendor'] + ); + } + } + private function extractNotification(StoreInterface $store): Response { $storeId = (int) $store->getStoreId(); @@ -127,6 +184,7 @@ private function extractNotification(StoreInterface $store): Response $this->tpayConfig->getSecurityCode($storeId), !$this->tpayConfig->useSandboxMode($storeId) ))->getNotification(); + $notification = $notification->getNotificationAssociative(); $orderId = base64_decode($notification['tr_crc']); diff --git a/Model/ApiFacade/CardTransaction/CardApiFacade.php b/Model/ApiFacade/CardTransaction/CardApiFacade.php index beb4f8d..c84b99e 100755 --- a/Model/ApiFacade/CardTransaction/CardApiFacade.php +++ b/Model/ApiFacade/CardTransaction/CardApiFacade.php @@ -74,6 +74,7 @@ private function connectApi() return; } + $this->cardOrigin = null; $this->createOpenApiInstance($this->tpay, $this->tpayConfig, $this->tokensService, $this->tpayService); } } diff --git a/Model/ApiFacade/CardTransaction/CardOpen.php b/Model/ApiFacade/CardTransaction/CardOpen.php index 172bebb..48f5816 100755 --- a/Model/ApiFacade/CardTransaction/CardOpen.php +++ b/Model/ApiFacade/CardTransaction/CardOpen.php @@ -35,7 +35,7 @@ public function __construct(TpayInterface $tpay, TpayConfigInterface $tpayConfig $this->tokensService = $tokensService; $this->tpayService = $tpayService; $this->tpayApi = new TpayApi($tpayConfig->getOpenApiClientId(), $tpayConfig->getOpenApiPassword(), !$tpayConfig->useSandboxMode()); - $this->tpayApi->authorization()->setClientName($tpayConfig->buildMagentoInfo()); + $this->tpayApi->transactions()->setClientName($tpayConfig->buildMagentoInfo()); } public function makeFullCardTransactionProcess(string $orderId, ?array $customerToken = null): string diff --git a/Model/ApiFacade/CardTransaction/CardOrigin.php b/Model/ApiFacade/CardTransaction/CardOrigin.php index aed315b..d81b497 100755 --- a/Model/ApiFacade/CardTransaction/CardOrigin.php +++ b/Model/ApiFacade/CardTransaction/CardOrigin.php @@ -69,7 +69,7 @@ public function makeFullCardTransactionProcess(string $orderId, ?array $customer private function processSavedCardPayment(string $orderId, int $cardId, ?array $customerToken = null): string { - $customerToken = $customerToken ? $customerToken : $this->tokensService->getTokenById($cardId, $this->tpay->getCustomerId($orderId)); + $customerToken = $customerToken ? $customerToken : $this->tokensService->getTokenById($cardId, $this->tpay->getCustomerId($orderId), false); if ($customerToken) { $token = $customerToken['cli_auth']; @@ -81,7 +81,7 @@ private function processSavedCardPayment(string $orderId, int $cardId, ?array $c } if (1 === (int) $paymentResult['result'] && isset($paymentResult['status']) && 'correct' === $paymentResult['status']) { - $this->tpayService->setOrderStatus($orderId, $paymentResult, $this->tpayConfig); + $this->tpayService->setCardOrderStatus($orderId, $paymentResult, $this->tpayConfig); $this->tpayService->addCommentToHistory($orderId, 'Successful payment by saved card'); return 'magento2basic/tpay/success'; diff --git a/Model/ApiFacade/OpenApi.php b/Model/ApiFacade/OpenApi.php index 4bfaea8..26978d3 100755 --- a/Model/ApiFacade/OpenApi.php +++ b/Model/ApiFacade/OpenApi.php @@ -25,7 +25,7 @@ public function __construct(TpayConfigInterface $tpay, CacheInterface $cache) if ($token) { $this->tpayApi->setCustomToken(unserialize($token)); } - $this->tpayApi->authorization()->setClientName($tpay->buildMagentoInfo()); + $this->tpayApi->transactions()->setClientName($tpay->buildMagentoInfo()); if (!$token) { $this->cache->save(serialize($this->tpayApi->getToken()), $this->getAuthTokenCacheKey($tpay)); } diff --git a/Provider/ConfigurationProvider.php b/Provider/ConfigurationProvider.php index 84f05f3..5081f6c 100755 --- a/Provider/ConfigurationProvider.php +++ b/Provider/ConfigurationProvider.php @@ -257,7 +257,9 @@ private function getTpayPluginVersion(): string { $dir = __DIR__.'/../.version'; if (file_exists($dir)) { - return file_get_contents(__DIR__.'/../.version'); + $version = file_get_contents(__DIR__.'/../.version'); + + return rtrim($version, "\n"); } return 'n/a'; diff --git a/composer.json b/composer.json index 2e574df..0fcf6ad 100755 --- a/composer.json +++ b/composer.json @@ -14,7 +14,7 @@ "ext-json": "*", "magento/framework": "^102.0 || ^103.0", "tpay-com/tpay-openapi-php": "^1.7.1", - "tpay-com/tpay-php": "^2.4.4" + "tpay-com/tpay-php": "^2.4.7" }, "autoload": { "psr-4": {