GuestPaymentInformationManagement.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Checkout\Model;
  8. use Magento\Framework\App\ObjectManager;
  9. use Magento\Framework\App\ResourceConnection;
  10. use Magento\Quote\Api\CartRepositoryInterface;
  11. use Magento\Framework\Exception\CouldNotSaveException;
  12. use Magento\Quote\Model\Quote;
  13. /**
  14. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  15. */
  16. class GuestPaymentInformationManagement implements \Magento\Checkout\Api\GuestPaymentInformationManagementInterface
  17. {
  18. /**
  19. * @var \Magento\Quote\Api\GuestBillingAddressManagementInterface
  20. */
  21. protected $billingAddressManagement;
  22. /**
  23. * @var \Magento\Quote\Api\GuestPaymentMethodManagementInterface
  24. */
  25. protected $paymentMethodManagement;
  26. /**
  27. * @var \Magento\Quote\Api\GuestCartManagementInterface
  28. */
  29. protected $cartManagement;
  30. /**
  31. * @var \Magento\Checkout\Api\PaymentInformationManagementInterface
  32. */
  33. protected $paymentInformationManagement;
  34. /**
  35. * @var \Magento\Quote\Model\QuoteIdMaskFactory
  36. */
  37. protected $quoteIdMaskFactory;
  38. /**
  39. * @var CartRepositoryInterface
  40. */
  41. protected $cartRepository;
  42. /**
  43. * @var \Psr\Log\LoggerInterface
  44. */
  45. private $logger;
  46. /**
  47. * @var ResourceConnection
  48. */
  49. private $connectionPool;
  50. /**
  51. * @param \Magento\Quote\Api\GuestBillingAddressManagementInterface $billingAddressManagement
  52. * @param \Magento\Quote\Api\GuestPaymentMethodManagementInterface $paymentMethodManagement
  53. * @param \Magento\Quote\Api\GuestCartManagementInterface $cartManagement
  54. * @param \Magento\Checkout\Api\PaymentInformationManagementInterface $paymentInformationManagement
  55. * @param \Magento\Quote\Model\QuoteIdMaskFactory $quoteIdMaskFactory
  56. * @param CartRepositoryInterface $cartRepository
  57. * @param ResourceConnection|null
  58. * @codeCoverageIgnore
  59. */
  60. public function __construct(
  61. \Magento\Quote\Api\GuestBillingAddressManagementInterface $billingAddressManagement,
  62. \Magento\Quote\Api\GuestPaymentMethodManagementInterface $paymentMethodManagement,
  63. \Magento\Quote\Api\GuestCartManagementInterface $cartManagement,
  64. \Magento\Checkout\Api\PaymentInformationManagementInterface $paymentInformationManagement,
  65. \Magento\Quote\Model\QuoteIdMaskFactory $quoteIdMaskFactory,
  66. CartRepositoryInterface $cartRepository,
  67. ResourceConnection $connectionPool = null
  68. ) {
  69. $this->billingAddressManagement = $billingAddressManagement;
  70. $this->paymentMethodManagement = $paymentMethodManagement;
  71. $this->cartManagement = $cartManagement;
  72. $this->paymentInformationManagement = $paymentInformationManagement;
  73. $this->quoteIdMaskFactory = $quoteIdMaskFactory;
  74. $this->cartRepository = $cartRepository;
  75. $this->connectionPool = $connectionPool ?: ObjectManager::getInstance()->get(ResourceConnection::class);
  76. }
  77. /**
  78. * {@inheritDoc}
  79. */
  80. public function savePaymentInformationAndPlaceOrder(
  81. $cartId,
  82. $email,
  83. \Magento\Quote\Api\Data\PaymentInterface $paymentMethod,
  84. \Magento\Quote\Api\Data\AddressInterface $billingAddress = null
  85. ) {
  86. $salesConnection = $this->connectionPool->getConnection('sales');
  87. $checkoutConnection = $this->connectionPool->getConnection('checkout');
  88. $salesConnection->beginTransaction();
  89. $checkoutConnection->beginTransaction();
  90. try {
  91. $this->savePaymentInformation($cartId, $email, $paymentMethod, $billingAddress);
  92. try {
  93. $orderId = $this->cartManagement->placeOrder($cartId);
  94. } catch (\Magento\Framework\Exception\LocalizedException $e) {
  95. throw new CouldNotSaveException(
  96. __($e->getMessage()),
  97. $e
  98. );
  99. } catch (\Exception $e) {
  100. $this->getLogger()->critical($e);
  101. throw new CouldNotSaveException(
  102. __('An error occurred on the server. Please try to place the order again.'),
  103. $e
  104. );
  105. }
  106. $salesConnection->commit();
  107. $checkoutConnection->commit();
  108. } catch (\Exception $e) {
  109. $salesConnection->rollBack();
  110. $checkoutConnection->rollBack();
  111. throw $e;
  112. }
  113. return $orderId;
  114. }
  115. /**
  116. * {@inheritDoc}
  117. */
  118. public function savePaymentInformation(
  119. $cartId,
  120. $email,
  121. \Magento\Quote\Api\Data\PaymentInterface $paymentMethod,
  122. \Magento\Quote\Api\Data\AddressInterface $billingAddress = null
  123. ) {
  124. $quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id');
  125. /** @var Quote $quote */
  126. $quote = $this->cartRepository->getActive($quoteIdMask->getQuoteId());
  127. if ($billingAddress) {
  128. $billingAddress->setEmail($email);
  129. $quote->removeAddress($quote->getBillingAddress()->getId());
  130. $quote->setBillingAddress($billingAddress);
  131. $quote->setDataChanges(true);
  132. } else {
  133. $quote->getBillingAddress()->setEmail($email);
  134. }
  135. $this->limitShippingCarrier($quote);
  136. $this->paymentMethodManagement->set($cartId, $paymentMethod);
  137. return true;
  138. }
  139. /**
  140. * {@inheritDoc}
  141. */
  142. public function getPaymentInformation($cartId)
  143. {
  144. $quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id');
  145. return $this->paymentInformationManagement->getPaymentInformation($quoteIdMask->getQuoteId());
  146. }
  147. /**
  148. * Get logger instance
  149. *
  150. * @return \Psr\Log\LoggerInterface
  151. * @deprecated 100.1.8
  152. */
  153. private function getLogger()
  154. {
  155. if (!$this->logger) {
  156. $this->logger = \Magento\Framework\App\ObjectManager::getInstance()->get(\Psr\Log\LoggerInterface::class);
  157. }
  158. return $this->logger;
  159. }
  160. /**
  161. * Limits shipping rates request by carrier from shipping address.
  162. *
  163. * @param Quote $quote
  164. *
  165. * @return void
  166. * @see \Magento\Shipping\Model\Shipping::collectRates
  167. */
  168. private function limitShippingCarrier(Quote $quote) : void
  169. {
  170. $shippingAddress = $quote->getShippingAddress();
  171. if ($shippingAddress && $shippingAddress->getShippingMethod()) {
  172. $shippingDataArray = explode('_', $shippingAddress->getShippingMethod());
  173. $shippingCarrier = array_shift($shippingDataArray);
  174. $shippingAddress->setLimitCarrier($shippingCarrier);
  175. }
  176. }
  177. }