PaymentInformationManagement.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Checkout\Model;
  7. use Magento\Framework\Exception\CouldNotSaveException;
  8. /**
  9. * Payment information management
  10. *
  11. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  12. */
  13. class PaymentInformationManagement implements \Magento\Checkout\Api\PaymentInformationManagementInterface
  14. {
  15. /**
  16. * @var \Magento\Quote\Api\BillingAddressManagementInterface
  17. * @deprecated 100.1.0 This call was substituted to eliminate extra quote::save call
  18. */
  19. protected $billingAddressManagement;
  20. /**
  21. * @var \Magento\Quote\Api\PaymentMethodManagementInterface
  22. */
  23. protected $paymentMethodManagement;
  24. /**
  25. * @var \Magento\Quote\Api\CartManagementInterface
  26. */
  27. protected $cartManagement;
  28. /**
  29. * @var PaymentDetailsFactory
  30. */
  31. protected $paymentDetailsFactory;
  32. /**
  33. * @var \Magento\Quote\Api\CartTotalRepositoryInterface
  34. */
  35. protected $cartTotalsRepository;
  36. /**
  37. * @var \Psr\Log\LoggerInterface
  38. */
  39. private $logger;
  40. /**
  41. * @var \Magento\Quote\Api\CartRepositoryInterface
  42. */
  43. private $cartRepository;
  44. /**
  45. * @param \Magento\Quote\Api\BillingAddressManagementInterface $billingAddressManagement
  46. * @param \Magento\Quote\Api\PaymentMethodManagementInterface $paymentMethodManagement
  47. * @param \Magento\Quote\Api\CartManagementInterface $cartManagement
  48. * @param PaymentDetailsFactory $paymentDetailsFactory
  49. * @param \Magento\Quote\Api\CartTotalRepositoryInterface $cartTotalsRepository
  50. * @codeCoverageIgnore
  51. */
  52. public function __construct(
  53. \Magento\Quote\Api\BillingAddressManagementInterface $billingAddressManagement,
  54. \Magento\Quote\Api\PaymentMethodManagementInterface $paymentMethodManagement,
  55. \Magento\Quote\Api\CartManagementInterface $cartManagement,
  56. \Magento\Checkout\Model\PaymentDetailsFactory $paymentDetailsFactory,
  57. \Magento\Quote\Api\CartTotalRepositoryInterface $cartTotalsRepository
  58. ) {
  59. $this->billingAddressManagement = $billingAddressManagement;
  60. $this->paymentMethodManagement = $paymentMethodManagement;
  61. $this->cartManagement = $cartManagement;
  62. $this->paymentDetailsFactory = $paymentDetailsFactory;
  63. $this->cartTotalsRepository = $cartTotalsRepository;
  64. }
  65. /**
  66. * @inheritdoc
  67. */
  68. public function savePaymentInformationAndPlaceOrder(
  69. $cartId,
  70. \Magento\Quote\Api\Data\PaymentInterface $paymentMethod,
  71. \Magento\Quote\Api\Data\AddressInterface $billingAddress = null
  72. ) {
  73. $this->savePaymentInformation($cartId, $paymentMethod, $billingAddress);
  74. try {
  75. $orderId = $this->cartManagement->placeOrder($cartId);
  76. } catch (\Magento\Framework\Exception\LocalizedException $e) {
  77. throw new CouldNotSaveException(
  78. __($e->getMessage()),
  79. $e
  80. );
  81. } catch (\Exception $e) {
  82. $this->getLogger()->critical($e);
  83. throw new CouldNotSaveException(
  84. __('A server error stopped your order from being placed. Please try to place your order again.'),
  85. $e
  86. );
  87. }
  88. return $orderId;
  89. }
  90. /**
  91. * @inheritdoc
  92. */
  93. public function savePaymentInformation(
  94. $cartId,
  95. \Magento\Quote\Api\Data\PaymentInterface $paymentMethod,
  96. \Magento\Quote\Api\Data\AddressInterface $billingAddress = null
  97. ) {
  98. if ($billingAddress) {
  99. /** @var \Magento\Quote\Api\CartRepositoryInterface $quoteRepository */
  100. $quoteRepository = $this->getCartRepository();
  101. /** @var \Magento\Quote\Model\Quote $quote */
  102. $quote = $quoteRepository->getActive($cartId);
  103. $quote->removeAddress($quote->getBillingAddress()->getId());
  104. $quote->setBillingAddress($billingAddress);
  105. $quote->setDataChanges(true);
  106. $shippingAddress = $quote->getShippingAddress();
  107. if ($shippingAddress && $shippingAddress->getShippingMethod()) {
  108. $shippingRate = $shippingAddress->getShippingRateByCode($shippingAddress->getShippingMethod());
  109. $shippingAddress->setLimitCarrier(
  110. $shippingRate ? $shippingRate->getCarrier() : $shippingAddress->getShippingMethod()
  111. );
  112. }
  113. }
  114. $this->paymentMethodManagement->set($cartId, $paymentMethod);
  115. return true;
  116. }
  117. /**
  118. * @inheritdoc
  119. */
  120. public function getPaymentInformation($cartId)
  121. {
  122. /** @var \Magento\Checkout\Api\Data\PaymentDetailsInterface $paymentDetails */
  123. $paymentDetails = $this->paymentDetailsFactory->create();
  124. $paymentDetails->setPaymentMethods($this->paymentMethodManagement->getList($cartId));
  125. $paymentDetails->setTotals($this->cartTotalsRepository->get($cartId));
  126. return $paymentDetails;
  127. }
  128. /**
  129. * Get logger instance
  130. *
  131. * @return \Psr\Log\LoggerInterface
  132. * @deprecated 100.1.8
  133. */
  134. private function getLogger()
  135. {
  136. if (!$this->logger) {
  137. $this->logger = \Magento\Framework\App\ObjectManager::getInstance()->get(\Psr\Log\LoggerInterface::class);
  138. }
  139. return $this->logger;
  140. }
  141. /**
  142. * Get Cart repository
  143. *
  144. * @return \Magento\Quote\Api\CartRepositoryInterface
  145. * @deprecated 100.2.0
  146. */
  147. private function getCartRepository()
  148. {
  149. if (!$this->cartRepository) {
  150. $this->cartRepository = \Magento\Framework\App\ObjectManager::getInstance()
  151. ->get(\Magento\Quote\Api\CartRepositoryInterface::class);
  152. }
  153. return $this->cartRepository;
  154. }
  155. }