OrderPlace.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Braintree\Model\Paypal\Helper;
  7. use Magento\Braintree\Model\Paypal\OrderCancellationService;
  8. use Magento\Checkout\Api\AgreementsValidatorInterface;
  9. use Magento\Checkout\Helper\Data;
  10. use Magento\Checkout\Model\Type\Onepage;
  11. use Magento\Customer\Model\Group;
  12. use Magento\Customer\Model\Session;
  13. use Magento\Framework\Exception\LocalizedException;
  14. use Magento\Quote\Api\CartManagementInterface;
  15. use Magento\Quote\Model\Quote;
  16. /**
  17. * Class OrderPlace
  18. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  19. */
  20. class OrderPlace extends AbstractHelper
  21. {
  22. /**
  23. * @var CartManagementInterface
  24. */
  25. private $cartManagement;
  26. /**
  27. * @var AgreementsValidatorInterface
  28. */
  29. private $agreementsValidator;
  30. /**
  31. * @var Session
  32. */
  33. private $customerSession;
  34. /**
  35. * @var Data
  36. */
  37. private $checkoutHelper;
  38. /**
  39. * @var OrderCancellationService
  40. */
  41. private $orderCancellationService;
  42. /**
  43. * @param CartManagementInterface $cartManagement
  44. * @param AgreementsValidatorInterface $agreementsValidator
  45. * @param Session $customerSession
  46. * @param Data $checkoutHelper
  47. * @param OrderCancellationService $orderCancellationService
  48. */
  49. public function __construct(
  50. CartManagementInterface $cartManagement,
  51. AgreementsValidatorInterface $agreementsValidator,
  52. Session $customerSession,
  53. Data $checkoutHelper,
  54. OrderCancellationService $orderCancellationService
  55. ) {
  56. $this->cartManagement = $cartManagement;
  57. $this->agreementsValidator = $agreementsValidator;
  58. $this->customerSession = $customerSession;
  59. $this->checkoutHelper = $checkoutHelper;
  60. $this->orderCancellationService = $orderCancellationService;
  61. }
  62. /**
  63. * Execute operation
  64. *
  65. * @param Quote $quote
  66. * @param array $agreement
  67. * @return void
  68. * @throws \Exception
  69. */
  70. public function execute(Quote $quote, array $agreement)
  71. {
  72. if (!$this->agreementsValidator->isValid($agreement)) {
  73. throw new LocalizedException(__(
  74. "The order wasn't placed. First, agree to the terms and conditions, then try placing your order again."
  75. ));
  76. }
  77. if ($this->getCheckoutMethod($quote) === Onepage::METHOD_GUEST) {
  78. $this->prepareGuestQuote($quote);
  79. }
  80. $this->disabledQuoteAddressValidation($quote);
  81. $quote->collectTotals();
  82. try {
  83. $this->cartManagement->placeOrder($quote->getId());
  84. } catch (\Exception $e) {
  85. $this->orderCancellationService->execute($quote->getReservedOrderId());
  86. throw $e;
  87. }
  88. }
  89. /**
  90. * Get checkout method
  91. *
  92. * @param Quote $quote
  93. * @return string
  94. */
  95. private function getCheckoutMethod(Quote $quote)
  96. {
  97. if ($this->customerSession->isLoggedIn()) {
  98. return Onepage::METHOD_CUSTOMER;
  99. }
  100. if (!$quote->getCheckoutMethod()) {
  101. if ($this->checkoutHelper->isAllowedGuestCheckout($quote)) {
  102. $quote->setCheckoutMethod(Onepage::METHOD_GUEST);
  103. } else {
  104. $quote->setCheckoutMethod(Onepage::METHOD_REGISTER);
  105. }
  106. }
  107. return $quote->getCheckoutMethod();
  108. }
  109. /**
  110. * Prepare quote for guest checkout order submit
  111. *
  112. * @param Quote $quote
  113. * @return void
  114. */
  115. private function prepareGuestQuote(Quote $quote)
  116. {
  117. $quote->setCustomerId(null)
  118. ->setCustomerEmail($quote->getBillingAddress()->getEmail())
  119. ->setCustomerIsGuest(true)
  120. ->setCustomerGroupId(Group::NOT_LOGGED_IN_ID);
  121. }
  122. }