Save.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Controller\Adminhtml\Order\Create;
  7. use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
  8. use Magento\Framework\Exception\PaymentException;
  9. class Save extends \Magento\Sales\Controller\Adminhtml\Order\Create implements HttpPostActionInterface
  10. {
  11. /**
  12. * Saving quote and create order
  13. *
  14. * @return \Magento\Framework\Controller\ResultInterface
  15. *
  16. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  17. */
  18. public function execute()
  19. {
  20. $path = 'sales/*/';
  21. $pathParams = [];
  22. try {
  23. // check if the creation of a new customer is allowed
  24. if (!$this->_authorization->isAllowed('Magento_Customer::manage')
  25. && !$this->_getSession()->getCustomerId()
  26. && !$this->_getSession()->getQuote()->getCustomerIsGuest()
  27. ) {
  28. return $this->resultForwardFactory->create()->forward('denied');
  29. }
  30. $this->_getOrderCreateModel()->getQuote()->setCustomerId($this->_getSession()->getCustomerId());
  31. $this->_processActionData('save');
  32. $paymentData = $this->getRequest()->getPost('payment');
  33. if ($paymentData) {
  34. $paymentData['checks'] = [
  35. \Magento\Payment\Model\Method\AbstractMethod::CHECK_USE_INTERNAL,
  36. \Magento\Payment\Model\Method\AbstractMethod::CHECK_USE_FOR_COUNTRY,
  37. \Magento\Payment\Model\Method\AbstractMethod::CHECK_USE_FOR_CURRENCY,
  38. \Magento\Payment\Model\Method\AbstractMethod::CHECK_ORDER_TOTAL_MIN_MAX,
  39. \Magento\Payment\Model\Method\AbstractMethod::CHECK_ZERO_TOTAL,
  40. ];
  41. $this->_getOrderCreateModel()->setPaymentData($paymentData);
  42. $this->_getOrderCreateModel()->getQuote()->getPayment()->addData($paymentData);
  43. }
  44. $order = $this->_getOrderCreateModel()
  45. ->setIsValidate(true)
  46. ->importPostData($this->getRequest()->getPost('order'))
  47. ->createOrder();
  48. $this->_getSession()->clearStorage();
  49. $this->messageManager->addSuccessMessage(__('You created the order.'));
  50. if ($this->_authorization->isAllowed('Magento_Sales::actions_view')) {
  51. $pathParams = ['order_id' => $order->getId()];
  52. $path = 'sales/order/view';
  53. } else {
  54. $path = 'sales/order/index';
  55. }
  56. } catch (PaymentException $e) {
  57. $this->_getOrderCreateModel()->saveQuote();
  58. $message = $e->getMessage();
  59. if (!empty($message)) {
  60. $this->messageManager->addErrorMessage($message);
  61. }
  62. } catch (\Magento\Framework\Exception\LocalizedException $e) {
  63. // customer can be created before place order flow is completed and should be stored in current session
  64. $this->_getSession()->setCustomerId((int)$this->_getSession()->getQuote()->getCustomerId());
  65. $message = $e->getMessage();
  66. if (!empty($message)) {
  67. $this->messageManager->addErrorMessage($message);
  68. }
  69. } catch (\Exception $e) {
  70. $this->messageManager->addExceptionMessage($e, __('Order saving error: %1', $e->getMessage()));
  71. }
  72. return $this->resultRedirectFactory->create()->setPath($path, $pathParams);
  73. }
  74. }