13
13
use Magento \InstantPurchase \Model \QuoteManagement \QuoteCreation ;
14
14
use Magento \InstantPurchase \Model \QuoteManagement \QuoteFilling ;
15
15
use Magento \InstantPurchase \Model \QuoteManagement \ShippingConfiguration ;
16
+ use Magento \Quote \Api \CartRepositoryInterface ;
16
17
use Magento \Store \Model \Store ;
18
+ use \Throwable ;
17
19
18
20
/**
19
21
* Place an order using instant purchase option.
22
24
*/
23
25
class PlaceOrder
24
26
{
27
+ /**
28
+ * @var CartRepositoryInterface
29
+ */
30
+ private $ quoteRepository ;
31
+
25
32
/**
26
33
* @var QuoteCreation
27
34
*/
@@ -49,19 +56,22 @@ class PlaceOrder
49
56
50
57
/**
51
58
* PlaceOrder constructor.
59
+ * @param CartRepositoryInterface $quoteRepository
52
60
* @param QuoteCreation $quoteCreation
53
61
* @param QuoteFilling $quoteFilling
54
62
* @param ShippingConfiguration $shippingConfiguration
55
63
* @param PaymentConfiguration $paymentConfiguration
56
64
* @param Purchase $purchase
57
65
*/
58
66
public function __construct (
67
+ CartRepositoryInterface $ quoteRepository ,
59
68
QuoteCreation $ quoteCreation ,
60
69
QuoteFilling $ quoteFilling ,
61
70
ShippingConfiguration $ shippingConfiguration ,
62
71
PaymentConfiguration $ paymentConfiguration ,
63
72
Purchase $ purchase
64
73
) {
74
+ $ this ->quoteRepository = $ quoteRepository ;
65
75
$ this ->quoteCreation = $ quoteCreation ;
66
76
$ this ->quoteFilling = $ quoteFilling ;
67
77
$ this ->shippingConfiguration = $ shippingConfiguration ;
@@ -79,6 +89,7 @@ public function __construct(
79
89
* @param array $productRequest
80
90
* @return int order identifier
81
91
* @throws LocalizedException if order can not be placed.
92
+ * @throws Throwable if unpredictable error occurred.
82
93
*/
83
94
public function placeOrder (
84
95
Store $ store ,
@@ -98,17 +109,28 @@ public function placeOrder(
98
109
$ product ,
99
110
$ productRequest
100
111
);
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
+ }
113
135
}
114
136
}
0 commit comments