redirect = $redirect; $this->checkoutSession = $checkoutSession; $this->btcService = $btcService; $this->storeManager = $storeManager; } public function execute(Observer $observer) { $order = $this->checkoutSession->getLastRealOrder(); $orderIdArr[] = $order->getIncrementId(); if (count($order->getData()) > 0) { $paymentMethod = $order->getPayment()->getMethod(); if ($paymentMethod === 'btcpay') { $currentStoreId = (int)$this->storeManager->getStore()->getId(); //Check Order Status $invoices = $this->btcService->getInvoicesByOrderIds($currentStoreId, $orderIdArr); $invoices = $invoices->all(); if (count($invoices) !== 0) { $invoice = $invoices[0]; $status = $invoice->getStatus(); if ($status === 'New' || $status === 'Expired') { //Only cancel when no other open invoices for the same orderId $btcpayInvoices = $this->btcService->getInvoicesByOrderIds($currentStoreId, [$order->getIncrementId()]); $isEverythingNew = true; foreach ($btcpayInvoices->all() as $invoice) { if (!$invoice->isNew()) { $isEverythingNew = false; break; } } if ($isEverythingNew) { //Cancel Order $order->cancel(); $order->addCommentToStatusHistory(__('The customer left the payment page. Not paid.')); $order->save(); //Restore Quote $this->checkoutSession->restoreQuote(); } } } } } } }