SaveOrder.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Checkout\Controller\Onepage;
  7. use Magento\Framework\DataObject;
  8. use Magento\Framework\Exception\PaymentException;
  9. class SaveOrder extends \Magento\Checkout\Controller\Onepage
  10. {
  11. /**
  12. * Create order action
  13. *
  14. * @return \Magento\Framework\Controller\ResultInterface
  15. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  16. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  17. */
  18. public function execute()
  19. {
  20. if (!$this->_formKeyValidator->validate($this->getRequest())) {
  21. return $this->resultRedirectFactory->create()->setPath('*/*/');
  22. }
  23. if ($this->_expireAjax()) {
  24. return $this->_ajaxRedirectResponse();
  25. }
  26. $result = new DataObject();
  27. try {
  28. $agreementsValidator = $this->_objectManager->get(
  29. \Magento\CheckoutAgreements\Model\AgreementsValidator::class
  30. );
  31. if (!$agreementsValidator->isValid(array_keys($this->getRequest()->getPost('agreement', [])))) {
  32. $result->setData('success', false);
  33. $result->setData('error', true);
  34. $result->setData(
  35. 'error_messages',
  36. __(
  37. "The order wasn't placed. "
  38. . "First, agree to the terms and conditions, then try placing your order again."
  39. )
  40. );
  41. return $this->resultJsonFactory->create()->setData($result->getData());
  42. }
  43. $data = $this->getRequest()->getPost('payment', []);
  44. if ($data) {
  45. $data['checks'] = [
  46. \Magento\Payment\Model\Method\AbstractMethod::CHECK_USE_CHECKOUT,
  47. \Magento\Payment\Model\Method\AbstractMethod::CHECK_USE_FOR_COUNTRY,
  48. \Magento\Payment\Model\Method\AbstractMethod::CHECK_USE_FOR_CURRENCY,
  49. \Magento\Payment\Model\Method\AbstractMethod::CHECK_ORDER_TOTAL_MIN_MAX,
  50. \Magento\Payment\Model\Method\AbstractMethod::CHECK_ZERO_TOTAL,
  51. ];
  52. $this->getOnepage()->getQuote()->getPayment()->setQuote($this->getOnepage()->getQuote());
  53. $this->getOnepage()->getQuote()->getPayment()->importData($data);
  54. }
  55. $this->getOnepage()->saveOrder();
  56. $redirectUrl = $this->getOnepage()->getCheckout()->getRedirectUrl();
  57. $result->setData('success', true);
  58. $result->setData('error', false);
  59. } catch (PaymentException $e) {
  60. $message = $e->getMessage();
  61. if (!empty($message)) {
  62. $result->setData('error_messages', $message);
  63. }
  64. $result->setData('goto_section', 'payment');
  65. $result->setData(
  66. 'update_section',
  67. [
  68. 'name' => 'payment-method',
  69. 'html' => $this->_getPaymentMethodsHtml()
  70. ]
  71. );
  72. } catch (\Magento\Framework\Exception\LocalizedException $e) {
  73. $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
  74. $this->_objectManager->get(\Magento\Checkout\Helper\Data::class)
  75. ->sendPaymentFailedEmail($this->getOnepage()->getQuote(), $e->getMessage());
  76. $result->setData(
  77. 'success',
  78. false
  79. );
  80. $result->setData('error', true);
  81. $result->setData('error_messages', $e->getMessage());
  82. $gotoSection = $this->getOnepage()->getCheckout()->getGotoSection();
  83. if ($gotoSection) {
  84. $result->setData('goto_section', $gotoSection);
  85. $this->getOnepage()->getCheckout()->setGotoSection(null);
  86. }
  87. $updateSection = $this->getOnepage()->getCheckout()->getUpdateSection();
  88. if ($updateSection) {
  89. if (isset($this->_sectionUpdateFunctions[$updateSection])) {
  90. $updateSectionFunction = $this->_sectionUpdateFunctions[$updateSection];
  91. $result->setData(
  92. 'update_section',
  93. [
  94. 'name' => $updateSection,
  95. 'html' => $this->{$updateSectionFunction}(),
  96. ]
  97. );
  98. }
  99. $this->getOnepage()->getCheckout()->setUpdateSection(null);
  100. }
  101. } catch (\Exception $e) {
  102. $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
  103. $this->_objectManager->get(\Magento\Checkout\Helper\Data::class)
  104. ->sendPaymentFailedEmail($this->getOnepage()->getQuote(), $e->getMessage());
  105. $result->setData('success', false);
  106. $result->setData('error', true);
  107. $result->setData(
  108. 'error_messages',
  109. __('Something went wrong while processing your order. Please try again later.')
  110. );
  111. }
  112. /**
  113. * when there is redirect to third party, we don't want to save order yet.
  114. * we will save the order in return action.
  115. */
  116. if (isset($redirectUrl)) {
  117. $result->setData('redirect', $redirectUrl);
  118. }
  119. $this->_eventManager->dispatch(
  120. 'checkout_controller_onepage_saveOrder',
  121. [
  122. 'result' => $result,
  123. 'action' => $this
  124. ]
  125. );
  126. return $this->resultJsonFactory->create()->setData($result->getData());
  127. }
  128. }