resultRedirectFactory->create(); $orderId = (int)$this->getRequest()->getParam('order_id'); try { $order = $this->orderRepository->get($orderId); if ((int)$order->getCustomerId() !== (int)$this->customerSession->getCustomerId()) { throw new LocalizedException(__('The requested order is not available.')); } if ((string)$order->getStatus() !== 'pending' || !$order->canCancel()) { throw new LocalizedException(__('This order can no longer be canceled.')); } if (!$this->orderManagement->cancel($orderId)) { throw new LocalizedException(__('The order could not be canceled.')); } $this->messageManager->addSuccessMessage(__('The order has been canceled.')); } catch (LocalizedException $exception) { $this->messageManager->addErrorMessage($exception->getMessage()); } catch (\Throwable $exception) { $this->logger->error('Customer order cancellation failed', [ 'order_id' => $orderId, 'customer_id' => (int)$this->customerSession->getCustomerId(), 'exception' => $exception, ]); $this->messageManager->addErrorMessage( __('The order could not be canceled. Please contact customer service.') ); } return $result->setPath('sales/order/history', ['status' => 'pending']); } }