Skip to content

Commit

Permalink
fix: email sending fix
Browse files Browse the repository at this point in the history
  • Loading branch information
GytisZum committed Mar 22, 2024
1 parent 0539f59 commit 8cbd8f9
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 20 deletions.
18 changes: 18 additions & 0 deletions controllers/front/notify.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
29 changes: 29 additions & 0 deletions controllers/front/return.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
38 changes: 21 additions & 17 deletions saferpayofficial.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Install/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand Down
5 changes: 5 additions & 0 deletions src/Processor/CheckoutProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion upgrade/install-1.2.2.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

0 comments on commit 8cbd8f9

Please sign in to comment.