Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
OverviewPost.php
Go to the documentation of this file.
1 <?php
7 
13 
20 {
24  protected $formKeyValidator;
25 
29  protected $logger;
30 
35 
39  private $session;
40 
51  public function __construct(
52  \Magento\Framework\App\Action\Context $context,
53  \Magento\Customer\Model\Session $customerSession,
56  \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator,
57  \Psr\Log\LoggerInterface $logger,
58  \Magento\Checkout\Api\AgreementsValidatorInterface $agreementValidator,
60  ) {
61  $this->formKeyValidator = $formKeyValidator;
62  $this->logger = $logger;
63  $this->agreementsValidator = $agreementValidator;
64  $this->session = $session;
65 
66  parent::__construct(
67  $context,
68  $customerSession,
71  );
72  }
73 
80  public function execute()
81  {
82  if (!$this->formKeyValidator->validate($this->getRequest())) {
83  $this->_forward('backToAddresses');
84  return;
85  }
86  if (!$this->_validateMinimumAmount()) {
87  return;
88  }
89 
90  try {
91  if (!$this->agreementsValidator->isValid(array_keys($this->getRequest()->getPost('agreement', [])))) {
92  $this->messageManager->addError(
93  __('Please agree to all Terms and Conditions before placing the order.')
94  );
95  $this->_redirect('*/*/billing');
96  return;
97  }
98 
99  $payment = $this->getRequest()->getPost('payment');
100  $paymentInstance = $this->_getCheckout()->getQuote()->getPayment();
101  if (isset($payment['cc_number'])) {
102  $paymentInstance->setCcNumber($payment['cc_number']);
103  }
104  if (isset($payment['cc_cid'])) {
105  $paymentInstance->setCcCid($payment['cc_cid']);
106  }
107  $this->_getCheckout()->createOrders();
108  $this->_getState()->setCompleteStep(State::STEP_OVERVIEW);
109 
110  if ($this->session->getAddressErrors()) {
111  $this->_getState()->setActiveStep(State::STEP_RESULTS);
112  $this->_redirect('*/*/results');
113  } else {
114  $this->_getState()->setActiveStep(State::STEP_SUCCESS);
115  $this->_getCheckout()->getCheckoutSession()->clearQuote();
116  $this->_getCheckout()->getCheckoutSession()->setDisplaySuccess(true);
117  $this->_redirect('*/*/success');
118  }
119  } catch (PaymentException $e) {
120  $message = $e->getMessage();
121  if (!empty($message)) {
122  $this->messageManager->addError($message);
123  }
124  $this->_redirect('*/*/billing');
125  } catch (\Magento\Checkout\Exception $e) {
126  $this->_objectManager->get(
127  \Magento\Checkout\Helper\Data::class
128  )->sendPaymentFailedEmail(
129  $this->_getCheckout()->getQuote(),
130  $e->getMessage(),
131  'multi-shipping'
132  );
133  $this->_getCheckout()->getCheckoutSession()->clearQuote();
134  $this->messageManager->addError($e->getMessage());
135  $this->_redirect('*/cart');
136  } catch (\Magento\Framework\Exception\LocalizedException $e) {
137  $this->_objectManager->get(
138  \Magento\Checkout\Helper\Data::class
139  )->sendPaymentFailedEmail(
140  $this->_getCheckout()->getQuote(),
141  $e->getMessage(),
142  'multi-shipping'
143  );
144  $this->messageManager->addError($e->getMessage());
145  $this->_redirect('*/*/billing');
146  } catch (\Exception $e) {
147  $this->logger->critical($e);
148  try {
149  $this->_objectManager->get(
150  \Magento\Checkout\Helper\Data::class
151  )->sendPaymentFailedEmail(
152  $this->_getCheckout()->getQuote(),
153  $e->getMessage(),
154  'multi-shipping'
155  );
156  } catch (\Exception $e) {
157  $this->logger->error($e->getMessage());
158  }
159  $this->messageManager->addError(__('Order place error'));
160  $this->_redirect('*/*/billing');
161  }
162  }
163 }
_redirect($path, $arguments=[])
Definition: Action.php:167
__()
Definition: __.php:13
$message
$payment
Definition: order.php:17
_forward($action, $controller=null, $module=null, array $params=null)
Definition: Action.php:137
__construct(\Magento\Framework\App\Action\Context $context, \Magento\Customer\Model\Session $customerSession, CustomerRepositoryInterface $customerRepository, AccountManagementInterface $accountManagement, \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator, \Psr\Log\LoggerInterface $logger, \Magento\Checkout\Api\AgreementsValidatorInterface $agreementValidator, SessionManagerInterface $session)
$agreementsValidator
execute()
$formKeyValidator
$logger