Cancel.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Sales\Controller\Adminhtml\Order;
  8. use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
  9. class Cancel extends \Magento\Sales\Controller\Adminhtml\Order implements HttpPostActionInterface
  10. {
  11. /**
  12. * Authorization level of a basic admin session
  13. *
  14. * @see _isAllowed()
  15. */
  16. const ADMIN_RESOURCE = 'Magento_Sales::cancel';
  17. /**
  18. * Cancel order
  19. *
  20. * @return \Magento\Backend\Model\View\Result\Redirect
  21. */
  22. public function execute()
  23. {
  24. $resultRedirect = $this->resultRedirectFactory->create();
  25. if (!$this->isValidPostRequest()) {
  26. $this->messageManager->addErrorMessage(__('You have not canceled the item.'));
  27. return $resultRedirect->setPath('sales/*/');
  28. }
  29. $order = $this->_initOrder();
  30. if ($order) {
  31. try {
  32. $this->orderManagement->cancel($order->getEntityId());
  33. $this->messageManager->addSuccessMessage(__('You canceled the order.'));
  34. } catch (\Magento\Framework\Exception\LocalizedException $e) {
  35. $this->messageManager->addErrorMessage($e->getMessage());
  36. } catch (\Exception $e) {
  37. $this->messageManager->addErrorMessage(__('You have not canceled the item.'));
  38. $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
  39. }
  40. return $resultRedirect->setPath('sales/order/view', ['order_id' => $order->getId()]);
  41. }
  42. return $resultRedirect->setPath('sales/*/');
  43. }
  44. }