Skip to content

Commit

Permalink
PLGMAG2V2-815: Add delay for cancel controller to wait for order to c…
Browse files Browse the repository at this point in the history
…ancel (#296)
  • Loading branch information
vinodsowdagar authored Jan 24, 2025
1 parent 114b061 commit 93f89dc
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion Service/CancelOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Exception;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Model\OrderFactory;
use Magento\Sales\Model\Service\OrderService;
use MultiSafepay\ConnectCore\Factory\SdkFactory;
use MultiSafepay\ConnectCore\Logger\Logger;
Expand All @@ -29,19 +30,27 @@ class CancelOrder
*/
private $sdkFactory;

/**
* @var OrderFactory
*/
private $orderFactory;

/**
* @param OrderService $orderService
* @param Logger $logger
* @param SdkFactory $sdkFactory
* @param OrderFactory $orderFactory
*/
public function __construct(
OrderService $orderService,
Logger $logger,
SdkFactory $sdkFactory
SdkFactory $sdkFactory,
OrderFactory $orderFactory
) {
$this->orderService = $orderService;
$this->logger = $logger;
$this->sdkFactory = $sdkFactory;
$this->orderFactory = $orderFactory;
}

/**
Expand All @@ -59,11 +68,29 @@ public function execute(OrderInterface $order)
if ($this->isPaymentLink((int)$order->getStoreId(), $orderId)) {
$this->orderService->cancel($order->getEntityId());
$this->logger->logInfoForOrder($orderId, 'Pretransaction found, order canceled by Cancel controller');

return;
}
} catch (Exception | ClientExceptionInterface $exception) {
$this->logger->logInfoForOrder($orderId, 'Exception occurred when trying to cancel order');
$this->logger->logExceptionForOrder($orderId, $exception);
}

// Waiting for a maximum of 15 seconds for the order to be canceled by the notification before exiting
for ($count = 0; $count < 5; $count++) {
$updatedOrder = $this->orderFactory->create()->loadByIncrementId($orderId);

if (!$updatedOrder->canCancel()) {
break;
}

$this->logger->logInfoForOrder(
$orderId,
'Order not canceled yet, waiting for 3 seconds'
);
//phpcs:ignore
sleep (3);
}
}

/**
Expand Down

0 comments on commit 93f89dc

Please sign in to comment.