From 8cbd8f95dd243472e74d4d86cf4cad3e17e7585f Mon Sep 17 00:00:00 2001 From: Gytautas Date: Fri, 22 Mar 2024 10:38:18 +0200 Subject: [PATCH] fix: email sending fix --- controllers/front/notify.php | 18 +++++++++ controllers/front/return.php | 29 ++++++++++++++ saferpayofficial.php | 38 ++++++++++--------- .../CanSendOrderConfirmationEmail.php | 2 +- src/Install/Installer.php | 2 +- src/Processor/CheckoutProcessor.php | 5 +++ upgrade/install-1.2.2.php | 2 +- 7 files changed, 76 insertions(+), 20 deletions(-) diff --git a/controllers/front/notify.php b/controllers/front/notify.php index c82a097d..2253ad45 100755 --- a/controllers/front/notify.php +++ b/controllers/front/notify.php @@ -64,6 +64,24 @@ public function postProcess() die($this->module->l('Error. Insecure cart', self::FILENAME)); } + if ($cart->orderExists()) { + if (method_exists('Order', 'getIdByCartId')) { + $orderId = Order::getIdByCartId($cartId); + } else { + // For PrestaShop 1.6 use the alternative method + $orderId = Order::getOrderByCartId($cartId); + } + + $order = new Order($orderId); + + $saferPayAuthorizedStatus = (int) Configuration::get(\Invertus\SaferPay\Config\SaferPayConfig::SAFERPAY_PAYMENT_AUTHORIZED); + $saferPayCapturedStatus = (int) Configuration::get(\Invertus\SaferPay\Config\SaferPayConfig::SAFERPAY_PAYMENT_COMPLETED); + + if ((int) $order->current_state === $saferPayAuthorizedStatus || (int) $order->current_state === $saferPayCapturedStatus) { + die($this->module->l('Order already created', self::FILENAME)); + } + } + try { $assertResponseBody = $this->assertTransaction($cartId); diff --git a/controllers/front/return.php b/controllers/front/return.php index 7e3d2575..be9fb099 100755 --- a/controllers/front/return.php +++ b/controllers/front/return.php @@ -68,6 +68,35 @@ public function postProcess() ])); } + if ($cart->orderExists()) { + if (method_exists('Order', 'getIdByCartId')) { + $orderId = Order::getIdByCartId($cartId); + } else { + // For PrestaShop 1.6 use the alternative method + $orderId = Order::getOrderByCartId($cartId); + } + + $order = new Order($orderId); + + $saferPayAuthorizedStatus = (int) Configuration::get(\Invertus\SaferPay\Config\SaferPayConfig::SAFERPAY_PAYMENT_AUTHORIZED); + $saferPayCapturedStatus = (int) Configuration::get(\Invertus\SaferPay\Config\SaferPayConfig::SAFERPAY_PAYMENT_COMPLETED); + + if ((int) $order->current_state === $saferPayAuthorizedStatus || (int) $order->current_state === $saferPayCapturedStatus) { + 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 { if ($isBusinessLicence) { $response = $this->executeTransaction((int) $cartId, (int) $selectedCard); diff --git a/saferpayofficial.php b/saferpayofficial.php index 5ec7abee..9144923d 100755 --- a/saferpayofficial.php +++ b/saferpayofficial.php @@ -576,47 +576,51 @@ public function hookActionEmailSendBefore($params) } if ($params['template'] === 'new_order') { - if (!Configuration::get(\Invertus\SaferPay\Config\SaferPayConfig::SAFERPAY_SEND_NEW_ORDER_MAIL)) { + if ((int) Configuration::get(\Invertus\SaferPay\Config\SaferPayConfig::SAFERPAY_SEND_NEW_ORDER_MAIL)) { return true; } + return false; } } - public function hookActionOrderStatusPostUpdate($params = []) + public function hookActionOrderHistoryAddAfter($params = []) { - if (!isset($params['newOrderStatus']) || !isset($params['id_order'])) { + /** @var OrderHistory $orderHistory */ + $orderHistory = $params['order_history']; + + if (!$orderHistory instanceof OrderHistory) { return; } - if ($params['newOrderStatus'] instanceof OrderState) { - $orderStatus = $params['newOrderStatus']; - } else { - $orderStatus = new OrderState((int) $params['newOrderStatus']); - } - $order = new Order($params['id_order']); + $idOrder = (int) $orderHistory->id_order; - if (!Validate::isLoadedObject($orderStatus)) { + $internalOrder = new Order($idOrder); + + if (!Validate::isLoadedObject($internalOrder)) { return; } - if (!Validate::isLoadedObject($order)) { + $order = new Order($idOrder); + + $orderStatus = new OrderState((int) $order->current_state); + + if (!Validate::isLoadedObject($orderStatus)) { return; } /** @var \Invertus\SaferPay\Service\SaferPayMailService $mailService */ $mailService = $this->getService(\Invertus\SaferPay\Service\SaferPayMailService::class); - $saferPayAuthorizedStatus = (int) Configuration::get(\Invertus\SaferPay\Config\SaferPayConfig::SAFERPAY_PAYMENT_AUTHORIZED); - if ($orderStatus->id === $saferPayAuthorizedStatus && Configuration::get(\Invertus\SaferPay\Config\SaferPayConfig::SAFERPAY_SEND_NEW_ORDER_MAIL)) { - $mailService->sendNewOrderMail($order, (int) $orderStatus->id); - } - /** @var \Invertus\SaferPay\Core\Order\Verification\CanSendOrderConfirmationEmail $canSendOrderConfirmationEmail */ $canSendOrderConfirmationEmail = $this->getService(\Invertus\SaferPay\Core\Order\Verification\CanSendOrderConfirmationEmail::class); if ($canSendOrderConfirmationEmail->verify((int) $orderStatus->id)) { - $mailService->sendOrderConfMail($order, (int) $orderStatus->id); + try { + $mailService->sendOrderConfMail($order, (int) $orderStatus->id); + } catch (\Exception $e) { + // emailalert module sometimes throws error which leads into failed payment issue + } } } diff --git a/src/Core/Order/Verification/CanSendOrderConfirmationEmail.php b/src/Core/Order/Verification/CanSendOrderConfirmationEmail.php index 231dd4ec..33bfa0b3 100644 --- a/src/Core/Order/Verification/CanSendOrderConfirmationEmail.php +++ b/src/Core/Order/Verification/CanSendOrderConfirmationEmail.php @@ -33,7 +33,7 @@ class CanSendOrderConfirmationEmail { public function verify($orderStatusId) { - if (!\Configuration::get(\Invertus\SaferPay\Config\SaferPayConfig::SAFERPAY_SEND_ORDER_CONFIRMATION)) { + if (!(int) \Configuration::get(\Invertus\SaferPay\Config\SaferPayConfig::SAFERPAY_SEND_ORDER_CONFIRMATION)) { return false; } diff --git a/src/Install/Installer.php b/src/Install/Installer.php index 8844e433..8e5c9534 100755 --- a/src/Install/Installer.php +++ b/src/Install/Installer.php @@ -94,7 +94,7 @@ private function registerHooks() $this->module->registerHook('actionEmailSendBefore'); $this->module->registerHook('displayAdminOrderTabContent'); $this->module->registerHook('actionAdminControllerSetMedia'); - $this->module->registerHook('actionOrderStatusPostUpdate'); + $this->module->registerHook('actionOrderHistoryAddAfter'); $this->module->registerHook('actionObjectOrderPaymentAddAfter'); } diff --git a/src/Processor/CheckoutProcessor.php b/src/Processor/CheckoutProcessor.php index c475bcb7..eac6cc4a 100644 --- a/src/Processor/CheckoutProcessor.php +++ b/src/Processor/CheckoutProcessor.php @@ -123,6 +123,11 @@ private function processCreateOrder(Cart $cart, $paymentMethod) { $customer = new \Customer($cart->id_customer); + // Notify and return webhooks triggers together leading into order created previously + if ($cart->orderExists()) { + return; + } + $this->module->validateOrder( $cart->id, \Configuration::get(SaferPayConfig::SAFERPAY_ORDER_STATE_CHOICE_AWAITING_PAYMENT), diff --git a/upgrade/install-1.2.2.php b/upgrade/install-1.2.2.php index 7dcd8be9..90023973 100644 --- a/upgrade/install-1.2.2.php +++ b/upgrade/install-1.2.2.php @@ -31,6 +31,6 @@ */ function upgrade_module_1_2_2($module) { - return $module->registerHook('actionOrderStatusPostUpdate') && $module->unregisterHook('actionOrderStatusUpdate'); + return $module->registerHook('actionOrderHistoryAddAfter') && $module->unregisterHook('actionOrderStatusUpdate'); }