PlaceOrder.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\InstantPurchase\Model;
  7. use Magento\Catalog\Model\Product;
  8. use Magento\Customer\Model\Customer;
  9. use Magento\Framework\Exception\LocalizedException;
  10. use Magento\InstantPurchase\Model\QuoteManagement\PaymentConfiguration;
  11. use Magento\InstantPurchase\Model\QuoteManagement\Purchase;
  12. use Magento\InstantPurchase\Model\QuoteManagement\QuoteCreation;
  13. use Magento\InstantPurchase\Model\QuoteManagement\QuoteFilling;
  14. use Magento\InstantPurchase\Model\QuoteManagement\ShippingConfiguration;
  15. use Magento\Quote\Api\CartRepositoryInterface;
  16. use Magento\Store\Model\Store;
  17. use \Throwable;
  18. /**
  19. * Place an order using instant purchase option.
  20. *
  21. * @api
  22. * @since 100.2.0
  23. */
  24. class PlaceOrder
  25. {
  26. /**
  27. * @var CartRepositoryInterface
  28. */
  29. private $quoteRepository;
  30. /**
  31. * @var QuoteCreation
  32. */
  33. private $quoteCreation;
  34. /**
  35. * @var QuoteFilling
  36. */
  37. private $quoteFilling;
  38. /**
  39. * @var ShippingConfiguration
  40. */
  41. private $shippingConfiguration;
  42. /**
  43. * @var PaymentConfiguration
  44. */
  45. private $paymentConfiguration;
  46. /**
  47. * @var Purchase
  48. */
  49. private $purchase;
  50. /**
  51. * PlaceOrder constructor.
  52. * @param CartRepositoryInterface $quoteRepository
  53. * @param QuoteCreation $quoteCreation
  54. * @param QuoteFilling $quoteFilling
  55. * @param ShippingConfiguration $shippingConfiguration
  56. * @param PaymentConfiguration $paymentConfiguration
  57. * @param Purchase $purchase
  58. */
  59. public function __construct(
  60. CartRepositoryInterface $quoteRepository,
  61. QuoteCreation $quoteCreation,
  62. QuoteFilling $quoteFilling,
  63. ShippingConfiguration $shippingConfiguration,
  64. PaymentConfiguration $paymentConfiguration,
  65. Purchase $purchase
  66. ) {
  67. $this->quoteRepository = $quoteRepository;
  68. $this->quoteCreation = $quoteCreation;
  69. $this->quoteFilling = $quoteFilling;
  70. $this->shippingConfiguration = $shippingConfiguration;
  71. $this->paymentConfiguration = $paymentConfiguration;
  72. $this->purchase = $purchase;
  73. }
  74. /**
  75. * Place an order.
  76. *
  77. * @param Store $store
  78. * @param Customer $customer
  79. * @param InstantPurchaseOption $instantPurchaseOption
  80. * @param Product $product
  81. * @param array $productRequest
  82. * @return int order identifier
  83. * @throws LocalizedException if order can not be placed.
  84. * @throws Throwable if unpredictable error occurred.
  85. * @since 100.2.0
  86. */
  87. public function placeOrder(
  88. Store $store,
  89. Customer $customer,
  90. InstantPurchaseOption $instantPurchaseOption,
  91. Product $product,
  92. array $productRequest
  93. ) : int {
  94. $quote = $this->quoteCreation->createQuote(
  95. $store,
  96. $customer,
  97. $instantPurchaseOption->getShippingAddress(),
  98. $instantPurchaseOption->getBillingAddress()
  99. );
  100. $quote = $this->quoteFilling->fillQuote(
  101. $quote,
  102. $product,
  103. $productRequest
  104. );
  105. $quote->collectTotals();
  106. $this->quoteRepository->save($quote);
  107. $quote = $this->quoteRepository->get($quote->getId());
  108. try {
  109. $quote = $this->shippingConfiguration->configureShippingMethod(
  110. $quote,
  111. $instantPurchaseOption->getShippingMethod()
  112. );
  113. $quote = $this->paymentConfiguration->configurePayment(
  114. $quote,
  115. $instantPurchaseOption->getPaymentToken()
  116. );
  117. $orderId = $this->purchase->purchase(
  118. $quote
  119. );
  120. return $orderId;
  121. } catch (Throwable $e) {
  122. $quote->setIsActive(false);
  123. $this->quoteRepository->save($quote);
  124. throw $e;
  125. }
  126. }
  127. }