OverviewPost.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Multishipping\Controller\Checkout;
  7. use Magento\Multishipping\Model\Checkout\Type\Multishipping\State;
  8. use Magento\Customer\Api\AccountManagementInterface;
  9. use Magento\Customer\Api\CustomerRepositoryInterface;
  10. use Magento\Framework\Exception\PaymentException;
  11. use Magento\Framework\Session\SessionManagerInterface;
  12. /**
  13. * Class OverviewPost
  14. *
  15. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  16. */
  17. class OverviewPost extends \Magento\Multishipping\Controller\Checkout
  18. {
  19. /**
  20. * @var \Magento\Framework\Data\Form\FormKey\Validator
  21. */
  22. protected $formKeyValidator;
  23. /**
  24. * @var \Psr\Log\LoggerInterface
  25. */
  26. protected $logger;
  27. /**
  28. * @var \Magento\Checkout\Api\AgreementsValidatorInterface
  29. */
  30. protected $agreementsValidator;
  31. /**
  32. * @var SessionManagerInterface
  33. */
  34. private $session;
  35. /**
  36. * @param \Magento\Framework\App\Action\Context $context
  37. * @param \Magento\Customer\Model\Session $customerSession
  38. * @param CustomerRepositoryInterface $customerRepository
  39. * @param AccountManagementInterface $accountManagement
  40. * @param \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator
  41. * @param \Psr\Log\LoggerInterface $logger
  42. * @param \Magento\Checkout\Api\AgreementsValidatorInterface $agreementValidator
  43. * @param SessionManagerInterface $session
  44. */
  45. public function __construct(
  46. \Magento\Framework\App\Action\Context $context,
  47. \Magento\Customer\Model\Session $customerSession,
  48. CustomerRepositoryInterface $customerRepository,
  49. AccountManagementInterface $accountManagement,
  50. \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator,
  51. \Psr\Log\LoggerInterface $logger,
  52. \Magento\Checkout\Api\AgreementsValidatorInterface $agreementValidator,
  53. SessionManagerInterface $session
  54. ) {
  55. $this->formKeyValidator = $formKeyValidator;
  56. $this->logger = $logger;
  57. $this->agreementsValidator = $agreementValidator;
  58. $this->session = $session;
  59. parent::__construct(
  60. $context,
  61. $customerSession,
  62. $customerRepository,
  63. $accountManagement
  64. );
  65. }
  66. /**
  67. * Overview action
  68. *
  69. * @return void
  70. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  71. */
  72. public function execute()
  73. {
  74. if (!$this->formKeyValidator->validate($this->getRequest())) {
  75. $this->_forward('backToAddresses');
  76. return;
  77. }
  78. if (!$this->_validateMinimumAmount()) {
  79. return;
  80. }
  81. try {
  82. if (!$this->agreementsValidator->isValid(array_keys($this->getRequest()->getPost('agreement', [])))) {
  83. $this->messageManager->addError(
  84. __('Please agree to all Terms and Conditions before placing the order.')
  85. );
  86. $this->_redirect('*/*/billing');
  87. return;
  88. }
  89. $payment = $this->getRequest()->getPost('payment');
  90. $paymentInstance = $this->_getCheckout()->getQuote()->getPayment();
  91. if (isset($payment['cc_number'])) {
  92. $paymentInstance->setCcNumber($payment['cc_number']);
  93. }
  94. if (isset($payment['cc_cid'])) {
  95. $paymentInstance->setCcCid($payment['cc_cid']);
  96. }
  97. $this->_getCheckout()->createOrders();
  98. $this->_getState()->setCompleteStep(State::STEP_OVERVIEW);
  99. if ($this->session->getAddressErrors()) {
  100. $this->_getState()->setActiveStep(State::STEP_RESULTS);
  101. $this->_redirect('*/*/results');
  102. } else {
  103. $this->_getState()->setActiveStep(State::STEP_SUCCESS);
  104. $this->_getCheckout()->getCheckoutSession()->clearQuote();
  105. $this->_getCheckout()->getCheckoutSession()->setDisplaySuccess(true);
  106. $this->_redirect('*/*/success');
  107. }
  108. } catch (PaymentException $e) {
  109. $message = $e->getMessage();
  110. if (!empty($message)) {
  111. $this->messageManager->addError($message);
  112. }
  113. $this->_redirect('*/*/billing');
  114. } catch (\Magento\Checkout\Exception $e) {
  115. $this->_objectManager->get(
  116. \Magento\Checkout\Helper\Data::class
  117. )->sendPaymentFailedEmail(
  118. $this->_getCheckout()->getQuote(),
  119. $e->getMessage(),
  120. 'multi-shipping'
  121. );
  122. $this->_getCheckout()->getCheckoutSession()->clearQuote();
  123. $this->messageManager->addError($e->getMessage());
  124. $this->_redirect('*/cart');
  125. } catch (\Magento\Framework\Exception\LocalizedException $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->messageManager->addError($e->getMessage());
  134. $this->_redirect('*/*/billing');
  135. } catch (\Exception $e) {
  136. $this->logger->critical($e);
  137. try {
  138. $this->_objectManager->get(
  139. \Magento\Checkout\Helper\Data::class
  140. )->sendPaymentFailedEmail(
  141. $this->_getCheckout()->getQuote(),
  142. $e->getMessage(),
  143. 'multi-shipping'
  144. );
  145. } catch (\Exception $e) {
  146. $this->logger->error($e->getMessage());
  147. }
  148. $this->messageManager->addError(__('Order place error'));
  149. $this->_redirect('*/*/billing');
  150. }
  151. }
  152. }