123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Checkout\Controller\Onepage;
- use Magento\Framework\DataObject;
- use Magento\Framework\Exception\PaymentException;
- class SaveOrder extends \Magento\Checkout\Controller\Onepage
- {
- /**
- * Create order action
- *
- * @return \Magento\Framework\Controller\ResultInterface
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
- * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
- */
- public function execute()
- {
- if (!$this->_formKeyValidator->validate($this->getRequest())) {
- return $this->resultRedirectFactory->create()->setPath('*/*/');
- }
- if ($this->_expireAjax()) {
- return $this->_ajaxRedirectResponse();
- }
- $result = new DataObject();
- try {
- $agreementsValidator = $this->_objectManager->get(
- \Magento\CheckoutAgreements\Model\AgreementsValidator::class
- );
- if (!$agreementsValidator->isValid(array_keys($this->getRequest()->getPost('agreement', [])))) {
- $result->setData('success', false);
- $result->setData('error', true);
- $result->setData(
- 'error_messages',
- __(
- "The order wasn't placed. "
- . "First, agree to the terms and conditions, then try placing your order again."
- )
- );
- return $this->resultJsonFactory->create()->setData($result->getData());
- }
- $data = $this->getRequest()->getPost('payment', []);
- if ($data) {
- $data['checks'] = [
- \Magento\Payment\Model\Method\AbstractMethod::CHECK_USE_CHECKOUT,
- \Magento\Payment\Model\Method\AbstractMethod::CHECK_USE_FOR_COUNTRY,
- \Magento\Payment\Model\Method\AbstractMethod::CHECK_USE_FOR_CURRENCY,
- \Magento\Payment\Model\Method\AbstractMethod::CHECK_ORDER_TOTAL_MIN_MAX,
- \Magento\Payment\Model\Method\AbstractMethod::CHECK_ZERO_TOTAL,
- ];
- $this->getOnepage()->getQuote()->getPayment()->setQuote($this->getOnepage()->getQuote());
- $this->getOnepage()->getQuote()->getPayment()->importData($data);
- }
- $this->getOnepage()->saveOrder();
- $redirectUrl = $this->getOnepage()->getCheckout()->getRedirectUrl();
- $result->setData('success', true);
- $result->setData('error', false);
- } catch (PaymentException $e) {
- $message = $e->getMessage();
- if (!empty($message)) {
- $result->setData('error_messages', $message);
- }
- $result->setData('goto_section', 'payment');
- $result->setData(
- 'update_section',
- [
- 'name' => 'payment-method',
- 'html' => $this->_getPaymentMethodsHtml()
- ]
- );
- } catch (\Magento\Framework\Exception\LocalizedException $e) {
- $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
- $this->_objectManager->get(\Magento\Checkout\Helper\Data::class)
- ->sendPaymentFailedEmail($this->getOnepage()->getQuote(), $e->getMessage());
- $result->setData(
- 'success',
- false
- );
- $result->setData('error', true);
- $result->setData('error_messages', $e->getMessage());
- $gotoSection = $this->getOnepage()->getCheckout()->getGotoSection();
- if ($gotoSection) {
- $result->setData('goto_section', $gotoSection);
- $this->getOnepage()->getCheckout()->setGotoSection(null);
- }
- $updateSection = $this->getOnepage()->getCheckout()->getUpdateSection();
- if ($updateSection) {
- if (isset($this->_sectionUpdateFunctions[$updateSection])) {
- $updateSectionFunction = $this->_sectionUpdateFunctions[$updateSection];
- $result->setData(
- 'update_section',
- [
- 'name' => $updateSection,
- 'html' => $this->{$updateSectionFunction}(),
- ]
- );
- }
- $this->getOnepage()->getCheckout()->setUpdateSection(null);
- }
- } catch (\Exception $e) {
- $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
- $this->_objectManager->get(\Magento\Checkout\Helper\Data::class)
- ->sendPaymentFailedEmail($this->getOnepage()->getQuote(), $e->getMessage());
- $result->setData('success', false);
- $result->setData('error', true);
- $result->setData(
- 'error_messages',
- __('Something went wrong while processing your order. Please try again later.')
- );
- }
- /**
- * when there is redirect to third party, we don't want to save order yet.
- * we will save the order in return action.
- */
- if (isset($redirectUrl)) {
- $result->setData('redirect', $redirectUrl);
- }
- $this->_eventManager->dispatch(
- 'checkout_controller_onepage_saveOrder',
- [
- 'result' => $result,
- 'action' => $this
- ]
- );
- return $this->resultJsonFactory->create()->setData($result->getData());
- }
- }
|