diff --git a/Controller/Tpay/Notification.php b/Controller/Tpay/Notification.php index 673995b..fa5eb1b 100644 --- a/Controller/Tpay/Notification.php +++ b/Controller/Tpay/Notification.php @@ -13,11 +13,12 @@ use Magento\Sales\Model\Order; use Tpay\Magento2\Api\TpayConfigInterface; use Tpay\Magento2\Api\TpayInterface; +use Tpay\Magento2\Notification\Strategy\Factory\NotificationProcessorFactoryInterface; use Tpay\Magento2\Service\TpayService; use Tpay\Magento2\Service\TpayTokensService; +use Tpay\OpenApi\Webhook\JWSVerifiedPaymentNotification; use Tpay\OriginApi\Utilities\Util; use Tpay\OriginApi\Webhook\JWSVerifiedPaymentNotification as OriginJWSVerifiedPaymentNotification; -use tpaySDK\Webhook\JWSVerifiedPaymentNotification; class Notification implements CsrfAwareActionInterface { @@ -30,6 +31,9 @@ class Notification implements CsrfAwareActionInterface /** @var TpayService */ protected $tpayService; + /** @var NotificationProcessorFactoryInterface */ + protected $notificationProcessorFactory; + /** @var TpayTokensService */ private $tokensService; @@ -41,27 +45,41 @@ public function __construct( TpayConfigInterface $tpayConfig, TpayService $tpayService, TpayTokensService $tokensService, - ResponseInterface $response + ResponseInterface $response, + NotificationProcessorFactoryInterface $notificationProcessorFactory ) { $this->tpay = $tpayModel; $this->tpayConfig = $tpayConfig; $this->tpayService = $tpayService; $this->tokensService = $tokensService; $this->response = $response; + $this->notificationProcessorFactory = $notificationProcessorFactory; Util::$loggingEnabled = false; } public function execute(): ?Response { - if (isset($_POST['card'])) { - $orderId = base64_decode($_POST['order_id']); + try { + if (isset($_POST['card'])) { + $orderId = base64_decode($_POST['order_id']); - return $this->extractCardNotification($this->getOrderStore($orderId)); - } + return $this->extractCardNotification($this->getOrderStore($orderId)); + } - $orderId = base64_decode($_POST['tr_crc']); + if (isset($_POST['event'])) { + return $this->response->setStatusCode(Response::STATUS_CODE_400)->setContent('kurwa mac'); + } + + $orderId = base64_decode($_POST['tr_crc']); + + $strategy = $this->notificationProcessorFactory->create($_POST); + + $strategy->process($this->getOrderStore($orderId)); + } catch (\Throwable $e) { + return $this->response->setStatusCode(Response::STATUS_CODE_400)->setContent($e->getMessage()); + } - return $this->extractNotification($this->getOrderStore($orderId)); + return $this->response->setStatusCode(Response::STATUS_CODE_200)->setContent('TRUE'); } public function createCsrfValidationException(RequestInterface $request): ?InvalidRequestException diff --git a/Model/Alias.php b/Model/Alias.php new file mode 100644 index 0000000..9c84a64 --- /dev/null +++ b/Model/Alias.php @@ -0,0 +1,13 @@ +_init(ResourceModel\Alias::class); + } +} diff --git a/Model/ResourceModel/Alias.php b/Model/ResourceModel/Alias.php new file mode 100644 index 0000000..ada2fbd --- /dev/null +++ b/Model/ResourceModel/Alias.php @@ -0,0 +1,13 @@ +_init('tpay_blik_aliases', 'id'); + } +} diff --git a/Notification/NotificationProcessor.php b/Notification/NotificationProcessor.php new file mode 100644 index 0000000..4894b77 --- /dev/null +++ b/Notification/NotificationProcessor.php @@ -0,0 +1,23 @@ +factory = $factory; + } + + public function process() + { + $strategy = $this->factory->create(); + + return $strategy->process(); + } +} diff --git a/Notification/Strategy/BlikAliasNotificationProcessor.php b/Notification/Strategy/BlikAliasNotificationProcessor.php new file mode 100644 index 0000000..dba4974 --- /dev/null +++ b/Notification/Strategy/BlikAliasNotificationProcessor.php @@ -0,0 +1,10 @@ +tpayConfig = $tpayConfig; + $this->tpayService = $tpayService; + } + + public function process(?int $storeId) + { + $notification = (new JWSVerifiedPaymentNotification( + $this->tpayConfig->getSecurityCode($storeId), + !$this->tpayConfig->useSandboxMode($storeId) + ))->getNotification(); + + $notification = $notification->getNotificationAssociative(); + $orderId = base64_decode($notification['tr_crc']); + + if ('PAID' === $notification['tr_status']) { + $response = $this->getPaidTransactionResponse($orderId); + + return $this->response->setStatusCode(Response::STATUS_CODE_200)->setContent($response); + } + + $this->saveCard($notification, $orderId); + $this->tpayService->setOrderStatus($orderId, $notification, $this->tpayConfig); + } +} diff --git a/Notification/Strategy/Factory/NotificationProcessorFactory.php b/Notification/Strategy/Factory/NotificationProcessorFactory.php new file mode 100644 index 0000000..29e44b0 --- /dev/null +++ b/Notification/Strategy/Factory/NotificationProcessorFactory.php @@ -0,0 +1,29 @@ +strategies = $strategies; + } + + public function create(array $data): NotificationProcessorInterface + { + if (isset($_POST['card'])) { + return $this->strategies['card']; + } + + if (isset($_POST['event'])) { + return $this->strategies['blikAlias']; + } + + return $this->strategies['default']; + } +} diff --git a/Notification/Strategy/Factory/NotificationProcessorFactoryInterface.php b/Notification/Strategy/Factory/NotificationProcessorFactoryInterface.php new file mode 100644 index 0000000..a246cf2 --- /dev/null +++ b/Notification/Strategy/Factory/NotificationProcessorFactoryInterface.php @@ -0,0 +1,10 @@ + + @@ -29,6 +30,15 @@ TpayPaymentMethodValueHandlerPool + + + + Tpay\Magento2\Notification\Strategy\DefaultNotificationProcessor + Tpay\Magento2\Notification\Strategy\CardNotificationProcessor + Tpay\Magento2\Notification\Strategy\BlikAliasNotificationProcessor + + + Tpay_Magento2