Cancel.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Paypal\Controller\Express\AbstractExpress;
  8. use Magento\Framework\Controller\ResultFactory;
  9. class Cancel extends \Magento\Paypal\Controller\Express\AbstractExpress
  10. {
  11. /**
  12. * Cancel Express Checkout
  13. *
  14. * @return \Magento\Framework\Controller\Result\Redirect
  15. */
  16. public function execute()
  17. {
  18. try {
  19. $this->_initToken(false);
  20. // TODO verify if this logic of order cancellation is deprecated
  21. // if there is an order - cancel it
  22. $orderId = $this->_getCheckoutSession()->getLastOrderId();
  23. /** @var \Magento\Sales\Model\Order $order */
  24. $order = $orderId ? $this->_orderFactory->create()->load($orderId) : false;
  25. if ($order && $order->getId() && $order->getQuoteId() == $this->_getCheckoutSession()->getQuoteId()) {
  26. $order->cancel()->save();
  27. $this->_getCheckoutSession()
  28. ->unsLastQuoteId()
  29. ->unsLastSuccessQuoteId()
  30. ->unsLastOrderId()
  31. ->unsLastRealOrderId();
  32. $this->messageManager->addSuccessMessage(
  33. __('Express Checkout and Order have been canceled.')
  34. );
  35. } else {
  36. $this->messageManager->addSuccessMessage(
  37. __('Express Checkout has been canceled.')
  38. );
  39. }
  40. } catch (\Magento\Framework\Exception\LocalizedException $e) {
  41. $this->messageManager->addExceptionMessage($e, $e->getMessage());
  42. } catch (\Exception $e) {
  43. $this->messageManager->addExceptionMessage($e, __('Unable to cancel Express Checkout'));
  44. }
  45. /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
  46. $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
  47. return $resultRedirect->setPath('checkout/cart');
  48. }
  49. }