From 21ee441ab0c9275b001bef077d0795bc8812d326 Mon Sep 17 00:00:00 2001 From: Arkadiusz Pabisz Date: Tue, 22 Aug 2023 12:04:29 +0200 Subject: [PATCH] add assertion when payment is aborted --- controllers/front/notify.php | 2 +- controllers/front/return.php | 64 +++++++++++++++---- controllers/front/success.php | 57 ++++------------- .../SaferPayTransactionAssertion.php | 6 +- 4 files changed, 67 insertions(+), 62 deletions(-) diff --git a/controllers/front/notify.php b/controllers/front/notify.php index 5ef08d566..ffb79d754 100755 --- a/controllers/front/notify.php +++ b/controllers/front/notify.php @@ -118,7 +118,7 @@ private function assertTransaction($cartId) { /** @var SaferPayTransactionAssertion $transactionAssert */ $transactionAssert = $this->module->getService(SaferPayTransactionAssertion::class); - $assertionResponse = $transactionAssert->assert(Order::getOrderByCartId($cartId)); + $assertionResponse = $transactionAssert->assert(Order::getOrderByCartId($cartId), true); return $assertionResponse; } diff --git a/controllers/front/return.php b/controllers/front/return.php index 5f4ea0032..8c8b0dce0 100755 --- a/controllers/front/return.php +++ b/controllers/front/return.php @@ -60,18 +60,56 @@ public function postProcess() ])); } - Tools::redirect($this->context->link->getModuleLink( - $this->module->name, - $this->getSuccessControllerName($isBusinessLicence, $fieldToken), - [ - 'cartId' => $cartId, - 'orderId' => $orderId, - 'moduleId' => $moduleId, - 'secureKey' => $secureKey, - 'selectedCard' => $selectedCard - ], - true - )); + try { + $this->assertTransaction($orderId); + + Tools::redirect($this->context->link->getModuleLink( + $this->module->name, + $this->getSuccessControllerName($isBusinessLicence, $fieldToken), + [ + 'cartId' => $cartId, + 'orderId' => $orderId, + 'moduleId' => $moduleId, + 'secureKey' => $secureKey, + 'selectedCard' => $selectedCard, + ], + true + )); + + } catch (Exception $e) { + PrestaShopLogger::addLog( + sprintf( + 'Failed to assert transaction. Message: %s. File name: %s', + $e->getMessage(), + self::FILENAME + ) + ); + + Tools::redirect($this->context->link->getModuleLink( + $this->module->name, + 'failValidation', + [ + 'cartId' => $cartId, + 'orderId' => $orderId, + 'secureKey' => $secureKey, + ], + true + )); + } + } + + /** + * @param $cartId + * @return AssertBody + * @throws Exception + */ + private function assertTransaction($orderId) + { + /** @var SaferPayTransactionAssertion $transactionAssert */ + $transactionAssert = $this->module->getService(SaferPayTransactionAssertion::class); + $assertionResponse = $transactionAssert->assert($orderId, false); + + return $assertionResponse; } private function getSuccessControllerName($isBusinessLicence, $fieldToken) @@ -88,4 +126,4 @@ private function getSuccessControllerName($isBusinessLicence, $fieldToken) return $successController; } -} \ No newline at end of file +} diff --git a/controllers/front/success.php b/controllers/front/success.php index ceba7a6e2..39e56e934 100755 --- a/controllers/front/success.php +++ b/controllers/front/success.php @@ -21,9 +21,7 @@ *@license SIX Payment Services */ -use Invertus\SaferPay\Api\Enum\TransactionStatus; use Invertus\SaferPay\Controller\AbstractSaferPayController; -use Invertus\SaferPay\Service\TransactionFlow\SaferPayTransactionAssertion; class SaferPayOfficialSuccessModuleFrontController extends AbstractSaferPayController { @@ -51,49 +49,16 @@ public function postProcess() Tools::redirect($redirectLink); } - try { - // TODO The shopping cart should be locked to prevent problems with the notify.php process running at the same time. - // $this->assertTransaction($orderId); - - Tools::redirect($this->context->link->getPageLink( - 'order-confirmation', - true, - null, - [ - 'id_cart' => $cartId, - 'id_module' => $moduleId, - 'id_order' => $orderId, - 'key' => $secureKey, - ] - )); - } catch (Exception $e) { - PrestaShopLogger::addLog( - sprintf( - 'Failed to assert transaction. Message: %s. File name: %s', - $e->getMessage(), - self::FILENAME - ) - ); - - Tools::redirect($this->context->link->getModuleLink( - $this->module->name, - 'failValidation', - [ - 'cartId' => $cartId, - 'orderId' => $orderId, - 'secureKey' => $secureKey - ], - true - )); - } - } - - private function assertTransaction($orderId) - { - /** @var SaferPayTransactionAssertion $transactionAssert */ - $transactionAssert = $this->module->getService(SaferPayTransactionAssertion::class); - $assertionResponse = $transactionAssert->assert($orderId); - - return $assertionResponse; + Tools::redirect($this->context->link->getPageLink( + 'order-confirmation', + true, + null, + [ + 'id_cart' => $cartId, + 'id_module' => $moduleId, + 'id_order' => $orderId, + 'key' => $secureKey, + ] + )); } } diff --git a/src/Service/TransactionFlow/SaferPayTransactionAssertion.php b/src/Service/TransactionFlow/SaferPayTransactionAssertion.php index e1eca4a39..9fe8804cf 100755 --- a/src/Service/TransactionFlow/SaferPayTransactionAssertion.php +++ b/src/Service/TransactionFlow/SaferPayTransactionAssertion.php @@ -71,7 +71,7 @@ public function __construct( * @return AssertBody * @throws \Exception */ - public function assert($orderId) + public function assert($orderId, $changeOrderStatus) { $saferPayOrder = $this->getSaferPayOrder($orderId); $order = new Order($orderId); @@ -87,7 +87,9 @@ public function assert($orderId) $saferPayOrder->transaction_id = $assertBody->getTransaction()->getId(); $saferPayOrder->update(); - $this->orderStatusService->assert($order, $assertBody->getTransaction()->getStatus()); + if ($changeOrderStatus) { + $this->orderStatusService->assert($order, $assertBody->getTransaction()->getStatus()); + } return $assertBody; }