From 78958f078ba1eade3a54453d569ed2e5a03e3ab4 Mon Sep 17 00:00:00 2001 From: Julius Zukauskas Date: Thu, 27 Jun 2024 16:47:12 +0300 Subject: [PATCH] add wait template and remove order creation from return page --- controllers/front/return.php | 188 +++++++----------------- views/templates/front/saferpay_wait.tpl | 118 +++++++++++++++ 2 files changed, 169 insertions(+), 137 deletions(-) create mode 100644 views/templates/front/saferpay_wait.tpl diff --git a/controllers/front/return.php b/controllers/front/return.php index f8e34029..f143d9e6 100755 --- a/controllers/front/return.php +++ b/controllers/front/return.php @@ -26,6 +26,7 @@ use Invertus\SaferPay\Controller\AbstractSaferPayController; use Invertus\SaferPay\Core\Payment\DTO\CheckoutData; use Invertus\SaferPay\Enum\ControllerName; +use Invertus\SaferPay\Repository\SaferPayOrderRepository; use Invertus\SaferPay\Service\SaferPayOrderStatusService; use Invertus\SaferPay\Service\TransactionFlow\SaferPayTransactionAssertion; use Invertus\SaferPay\Processor\CheckoutProcessor; @@ -50,7 +51,6 @@ public function postProcess() $fieldToken = Tools::getValue('fieldToken'); $moduleId = $this->module->id; $selectedCard = Tools::getValue('selectedCard'); - $orderId = Tools::getValue('orderId'); $cart = new Cart($cartId); @@ -61,26 +61,6 @@ public function postProcess() ])); } - $lockResult = $this->applyLock( - sprintf( - '%s-%s', - $cartId, - $secureKey - ) - ); - - if (!$lockResult->isSuccessful()) { - $this->redirectWithNotifications($this->context->link->getModuleLink( - $this->module->name, - ControllerName::FAIL, - [ - 'cartId' => $cartId, - 'secureKey' => $secureKey, - 'moduleId' => $moduleId, - ] - )); - } - if ($cart->secure_key !== $secureKey) { $this->ajaxDie(json_encode([ 'error_type' => 'unknown_error', @@ -114,103 +94,75 @@ public function postProcess() ] )); } - } - - try { - if ($isBusinessLicence) { - $response = $this->executeTransaction((int) $cartId, (int) $selectedCard); - } else { - $response = $this->executePaymentPageAssertion((int) $cartId, (int) $isBusinessLicence); - } - - $checkoutData = CheckoutData::create( - (int) $cartId, - $response->getPaymentMeans()->getBrand()->getPaymentMethod(), - (int) $isBusinessLicence - ); - - $checkoutData->setIsAuthorizedOrder(true); - $checkoutData->setOrderStatus($response->getTransaction()->getStatus()); - - /** @var CheckoutProcessor $checkoutProcessor **/ - $checkoutProcessor = $this->module->getService(CheckoutProcessor::class); - $checkoutProcessor->run($checkoutData); - - if (method_exists('Order', 'getIdByCartId')) { - $orderId = Order::getIdByCartId($cartId); - } else { - // For PrestaShop 1.6 use the alternative method - $orderId = Order::getOrderByCartId($cartId); - } - - $paymentBehaviourWithout3DS = (int) Configuration::get(SaferPayConfig::PAYMENT_BEHAVIOR_WITHOUT_3D); - - /** @var SaferPayOrderStatusService $orderStatusService */ - $orderStatusService = $this->module->getService(SaferPayOrderStatusService::class); - - $order = new Order($orderId); - - if ( - (!$response->getLiability()->getLiabilityShift() && - in_array($order->payment, SaferPayConfig::SUPPORTED_3DS_PAYMENT_METHODS) && - $paymentBehaviourWithout3DS === SaferPayConfig::PAYMENT_BEHAVIOR_WITHOUT_3D_CANCEL) || - $response->getTransaction()->getStatus() === SaferPayConfig::TRANSACTION_STATUS_CANCELED - ) { - $orderStatusService->cancel($order); - - $this->warning[] = $this->module->l('We couldn\'t authorize your payment. Please try again.', self::FILENAME); - - $this->redirectWithNotifications($this->context->link->getModuleLink( + } else { + $this->context->smarty->assign( + 'checkStatusEndpoint', + $this->context->link->getModuleLink( $this->module->name, - ControllerName::FAIL, + 'return', [ - 'cartId' => $cartId, + 'ajax' => 1, + 'action' => 'getStatus', 'secureKey' => $secureKey, - 'orderId' => $orderId, - 'moduleId' => $moduleId, + 'cartId' => $cartId, ], true - )); - } + ) + ); + parent::setTemplate('saferpay_wait.tpl'); + } + } - if ((int) Configuration::get(SaferPayConfig::PAYMENT_BEHAVIOR) === SaferPayConfig::DEFAULT_PAYMENT_BEHAVIOR_CAPTURE && - $response->getTransaction()->getStatus() !== TransactionStatus::CAPTURED - ) { - $orderStatusService->capture(new Order($orderId)); - } - Tools::redirect($this->context->link->getModuleLink( + /** + * @throws PrestaShopDatabaseException + * @throws PrestaShopException + */ + protected function processGetStatus() + { + header('Content-Type: application/json;charset=UTF-8'); + /** @var SaferPayOrderRepository $saferPayOrderRepository */ + $saferPayOrderRepository = $this->module->getService(SaferPayOrderRepository::class); + + $cartId = Tools::getValue('cartId'); + $secureKey = Tools::getValue('secureKey'); + $isBusinessLicence = (int) Tools::getValue(SaferPayConfig::IS_BUSINESS_LICENCE); + $fieldToken = Tools::getValue('fieldToken'); + $moduleId = $this->module->id; + $selectedCard = Tools::getValue('selectedCard'); + $saferPayOrderId = $saferPayOrderRepository->getIdByCartId($cartId); + $saferPayOrder = new SaferPayOrder($saferPayOrderId); + + if ($saferPayOrder->canceled || !$saferPayOrder->id_order) { + $href = $this->context->link->getModuleLink( $this->module->name, - $this->getSuccessControllerName($isBusinessLicence, $fieldToken), + ControllerName::FAIL, [ 'cartId' => $cartId, - 'orderId' => $orderId, - 'moduleId' => $moduleId, 'secureKey' => $secureKey, - 'selectedCard' => $selectedCard, + 'moduleId' => $moduleId, ], 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( + } else { + $href = $this->context->link->getModuleLink( $this->module->name, - 'failValidation', + $this->getSuccessControllerName($isBusinessLicence, $fieldToken), [ 'cartId' => $cartId, - 'orderId' => $orderId, + 'orderId' => $saferPayOrder->id_order, + 'moduleId' => $moduleId, 'secureKey' => $secureKey, - ], - true - )); + 'selectedCard' => $selectedCard, + ] + ); } + + // @todo: this place needs attention after a2a pr merged - need to check if pending status works well + $this->ajaxDie(json_encode([ + 'isFinished' => $saferPayOrder->authorized || $saferPayOrder->captured || $saferPayOrder->canceled || isset($saferPayOrder->pending), + 'href' => $href + ])); } private function getSuccessControllerName($isBusinessLicence, $fieldToken) @@ -227,42 +179,4 @@ private function getSuccessControllerName($isBusinessLicence, $fieldToken) return $successController; } - - /** - * @param int $orderId - * @param int $selectedCard - * - * @return \Invertus\SaferPay\DTO\Response\Assert\AssertBody - * @throws Exception - */ - private function executeTransaction($orderId, $selectedCard) - { - /** @var SaferPayTransactionAuthorization $saferPayTransactionAuthorization */ - $saferPayTransactionAuthorization = $this->module->getService(SaferPayTransactionAuthorization::class); - - $response = $saferPayTransactionAuthorization->authorize( - $orderId, - $selectedCard === SaferPayConfig::CREDIT_CARD_OPTION_SAVE, - $selectedCard - ); - - return $response; - } - - /** - * @param int $cartId - * @param int $isBusinessLicence - * - * @return \Invertus\SaferPay\DTO\Response\Assert\AssertBody|null - * @throws Exception - */ - private function executePaymentPageAssertion($cartId, $isBusinessLicence) - { - - /** @var SaferPayTransactionAssertion $transactionAssert */ - $transactionAssert = $this->module->getService(SaferPayTransactionAssertion::class); - $assertionResponse = $transactionAssert->assert($cartId); - - return $assertionResponse; - } } diff --git a/views/templates/front/saferpay_wait.tpl b/views/templates/front/saferpay_wait.tpl new file mode 100644 index 00000000..2981c63c --- /dev/null +++ b/views/templates/front/saferpay_wait.tpl @@ -0,0 +1,118 @@ +{** + *NOTICE OF LICENSE + * + *This source file is subject to the Open Software License (OSL 3.0) + *that is bundled with this package in the file LICENSE.txt. + *It is also available through the world-wide-web at this URL: + *http://opensource.org/licenses/osl-3.0.php + *If you did not receive a copy of the license and are unable to + *obtain it through the world-wide-web, please send an email + *to license@prestashop.com so we can send you a copy immediately. + * + *DISCLAIMER + * + * Do not edit or add to this file if you wish to upgrade PrestaShop to newer + *versions in the future. If you wish to customize PrestaShop for your + *needs please refer to http://www.prestashop.com for more information. + * + *@author INVERTUS UAB www.invertus.eu + *@copyright SIX Payment Services + *@license SIX Payment Services + *} +

{l s='Awaiting payment status' mod='saferpay'}

+
+
+
+
+
+
+
+ + +