Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SaveOrder.php
Go to the documentation of this file.
1 <?php
8 
11 
13 {
21  public function execute()
22  {
23  if (!$this->_formKeyValidator->validate($this->getRequest())) {
24  return $this->resultRedirectFactory->create()->setPath('*/*/');
25  }
26 
27  if ($this->_expireAjax()) {
28  return $this->_ajaxRedirectResponse();
29  }
30 
31  $result = new DataObject();
32  try {
33  $agreementsValidator = $this->_objectManager->get(
34  \Magento\CheckoutAgreements\Model\AgreementsValidator::class
35  );
36  if (!$agreementsValidator->isValid(array_keys($this->getRequest()->getPost('agreement', [])))) {
37  $result->setData('success', false);
38  $result->setData('error', true);
39  $result->setData(
40  'error_messages',
41  __(
42  "The order wasn't placed. "
43  . "First, agree to the terms and conditions, then try placing your order again."
44  )
45  );
46  return $this->resultJsonFactory->create()->setData($result->getData());
47  }
48 
49  $data = $this->getRequest()->getPost('payment', []);
50  if ($data) {
51  $data['checks'] = [
57  ];
58  $this->getOnepage()->getQuote()->getPayment()->setQuote($this->getOnepage()->getQuote());
59  $this->getOnepage()->getQuote()->getPayment()->importData($data);
60  }
61 
62  $this->getOnepage()->saveOrder();
63 
64  $redirectUrl = $this->getOnepage()->getCheckout()->getRedirectUrl();
65  $result->setData('success', true);
66  $result->setData('error', false);
67  } catch (PaymentException $e) {
68  $message = $e->getMessage();
69  if (!empty($message)) {
70  $result->setData('error_messages', $message);
71  }
72  $result->setData('goto_section', 'payment');
73  $result->setData(
74  'update_section',
75  [
76  'name' => 'payment-method',
77  'html' => $this->_getPaymentMethodsHtml()
78  ]
79  );
80  } catch (\Magento\Framework\Exception\LocalizedException $e) {
81  $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
82  $this->_objectManager->get(\Magento\Checkout\Helper\Data::class)
83  ->sendPaymentFailedEmail($this->getOnepage()->getQuote(), $e->getMessage());
84  $result->setData(
85  'success',
86  false
87  );
88  $result->setData('error', true);
89  $result->setData('error_messages', $e->getMessage());
90  $gotoSection = $this->getOnepage()->getCheckout()->getGotoSection();
91  if ($gotoSection) {
92  $result->setData('goto_section', $gotoSection);
93  $this->getOnepage()->getCheckout()->setGotoSection(null);
94  }
95 
96  $updateSection = $this->getOnepage()->getCheckout()->getUpdateSection();
97  if ($updateSection) {
98  if (isset($this->_sectionUpdateFunctions[$updateSection])) {
99  $updateSectionFunction = $this->_sectionUpdateFunctions[$updateSection];
100  $result->setData(
101  'update_section',
102  [
103  'name' => $updateSection,
104  'html' => $this->{$updateSectionFunction}(),
105  ]
106  );
107  }
108  $this->getOnepage()->getCheckout()->setUpdateSection(null);
109  }
110  } catch (\Exception $e) {
111  $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
112  $this->_objectManager->get(\Magento\Checkout\Helper\Data::class)
113  ->sendPaymentFailedEmail($this->getOnepage()->getQuote(), $e->getMessage());
114  $result->setData('success', false);
115  $result->setData('error', true);
116  $result->setData(
117  'error_messages',
118  __('Something went wrong while processing your order. Please try again later.')
119  );
120  }
125  if (isset($redirectUrl)) {
126  $result->setData('redirect', $redirectUrl);
127  }
128 
129  $this->_eventManager->dispatch(
130  'checkout_controller_onepage_saveOrder',
131  [
132  'result' => $result,
133  'action' => $this
134  ]
135  );
136 
137  return $this->resultJsonFactory->create()->setData($result->getData());
138  }
139 }
__()
Definition: __.php:13
$message