1313use Magento \InstantPurchase \Model \QuoteManagement \QuoteCreation ;
1414use Magento \InstantPurchase \Model \QuoteManagement \QuoteFilling ;
1515use Magento \InstantPurchase \Model \QuoteManagement \ShippingConfiguration ;
16+ use Magento \Quote \Api \CartRepositoryInterface ;
1617use Magento \Store \Model \Store ;
18+ use \Throwable ;
1719
1820/**
1921 * Place an order using instant purchase option.
2224 */
2325class PlaceOrder
2426{
27+ /**
28+ * @var CartRepositoryInterface
29+ */
30+ private $ quoteRepository ;
31+
2532 /**
2633 * @var QuoteCreation
2734 */
@@ -49,19 +56,22 @@ class PlaceOrder
4956
5057 /**
5158 * PlaceOrder constructor.
59+ * @param CartRepositoryInterface $quoteRepository
5260 * @param QuoteCreation $quoteCreation
5361 * @param QuoteFilling $quoteFilling
5462 * @param ShippingConfiguration $shippingConfiguration
5563 * @param PaymentConfiguration $paymentConfiguration
5664 * @param Purchase $purchase
5765 */
5866 public function __construct (
67+ CartRepositoryInterface $ quoteRepository ,
5968 QuoteCreation $ quoteCreation ,
6069 QuoteFilling $ quoteFilling ,
6170 ShippingConfiguration $ shippingConfiguration ,
6271 PaymentConfiguration $ paymentConfiguration ,
6372 Purchase $ purchase
6473 ) {
74+ $ this ->quoteRepository = $ quoteRepository ;
6575 $ this ->quoteCreation = $ quoteCreation ;
6676 $ this ->quoteFilling = $ quoteFilling ;
6777 $ this ->shippingConfiguration = $ shippingConfiguration ;
@@ -79,6 +89,7 @@ public function __construct(
7989 * @param array $productRequest
8090 * @return int order identifier
8191 * @throws LocalizedException if order can not be placed.
92+ * @throws Throwable if unpredictable error occurred.
8293 */
8394 public function placeOrder (
8495 Store $ store ,
@@ -98,17 +109,28 @@ public function placeOrder(
98109 $ product ,
99110 $ productRequest
100111 );
101- $ quote = $ this ->shippingConfiguration ->configureShippingMethod (
102- $ quote ,
103- $ instantPurchaseOption ->getShippingMethod ()
104- );
105- $ quote = $ this ->paymentConfiguration ->configurePayment (
106- $ quote ,
107- $ instantPurchaseOption ->getPaymentToken ()
108- );
109- $ orderId = $ this ->purchase ->purchase (
110- $ quote
111- );
112- return $ orderId ;
112+
113+ $ quote ->collectTotals ();
114+ $ this ->quoteRepository ->save ($ quote );
115+ $ quote = $ this ->quoteRepository ->get ($ quote ->getId ());
116+
117+ try {
118+ $ quote = $ this ->shippingConfiguration ->configureShippingMethod (
119+ $ quote ,
120+ $ instantPurchaseOption ->getShippingMethod ()
121+ );
122+ $ quote = $ this ->paymentConfiguration ->configurePayment (
123+ $ quote ,
124+ $ instantPurchaseOption ->getPaymentToken ()
125+ );
126+ $ orderId = $ this ->purchase ->purchase (
127+ $ quote
128+ );
129+ return $ orderId ;
130+ } catch (Throwable $ e ) {
131+ $ quote ->setIsActive (false );
132+ $ this ->quoteRepository ->save ($ quote );
133+ throw $ e ;
134+ }
113135 }
114136}
0 commit comments