9
9
use Magento \Checkout \Api \Exception \PaymentProcessingRateLimitExceededException ;
10
10
use Magento \Checkout \Api \PaymentProcessingRateLimiterInterface ;
11
11
use Magento \Checkout \Api \PaymentSavingRateLimiterInterface ;
12
+ use Magento \Customer \Api \AddressRepositoryInterface ;
12
13
use Magento \Framework \App \ObjectManager ;
13
14
use Magento \Framework \Exception \CouldNotSaveException ;
15
+ use Magento \Framework \Exception \LocalizedException ;
14
16
use Magento \Quote \Api \CartRepositoryInterface ;
17
+ use Magento \Quote \Model \Quote ;
18
+ use Psr \Log \LoggerInterface ;
15
19
16
20
/**
17
21
* Payment information management service.
@@ -23,6 +27,7 @@ class PaymentInformationManagement implements \Magento\Checkout\Api\PaymentInfor
23
27
/**
24
28
* @var \Magento\Quote\Api\BillingAddressManagementInterface
25
29
* @deprecated 100.1.0 This call was substituted to eliminate extra quote::save call
30
+ * @see not in use anymore
26
31
*/
27
32
protected $ billingAddressManagement ;
28
33
@@ -46,11 +51,6 @@ class PaymentInformationManagement implements \Magento\Checkout\Api\PaymentInfor
46
51
*/
47
52
protected $ cartTotalsRepository ;
48
53
49
- /**
50
- * @var \Psr\Log\LoggerInterface
51
- */
52
- private $ logger ;
53
-
54
54
/**
55
55
* @var CartRepositoryInterface
56
56
*/
@@ -71,6 +71,21 @@ class PaymentInformationManagement implements \Magento\Checkout\Api\PaymentInfor
71
71
*/
72
72
private $ saveRateLimiterDisabled = false ;
73
73
74
+ /**
75
+ * @var AddressRepositoryInterface
76
+ */
77
+ private $ addressRepository ;
78
+
79
+ /**
80
+ * @var AddressComparatorInterface
81
+ */
82
+ private $ addressComparator ;
83
+
84
+ /**
85
+ * @var LoggerInterface
86
+ */
87
+ private $ logger ;
88
+
74
89
/**
75
90
* @param \Magento\Quote\Api\BillingAddressManagementInterface $billingAddressManagement
76
91
* @param \Magento\Quote\Api\PaymentMethodManagementInterface $paymentMethodManagement
@@ -80,7 +95,11 @@ class PaymentInformationManagement implements \Magento\Checkout\Api\PaymentInfor
80
95
* @param PaymentProcessingRateLimiterInterface|null $paymentRateLimiter
81
96
* @param PaymentSavingRateLimiterInterface|null $saveRateLimiter
82
97
* @param CartRepositoryInterface|null $cartRepository
98
+ * @param AddressRepositoryInterface|null $addressRepository
99
+ * @param AddressComparatorInterface|null $addressComparator
100
+ * @param LoggerInterface|null $logger
83
101
* @codeCoverageIgnore
102
+ * @SuppressWarnings(PHPMD.ExcessiveParameterList)
84
103
*/
85
104
public function __construct (
86
105
\Magento \Quote \Api \BillingAddressManagementInterface $ billingAddressManagement ,
@@ -90,7 +109,10 @@ public function __construct(
90
109
\Magento \Quote \Api \CartTotalRepositoryInterface $ cartTotalsRepository ,
91
110
?PaymentProcessingRateLimiterInterface $ paymentRateLimiter = null ,
92
111
?PaymentSavingRateLimiterInterface $ saveRateLimiter = null ,
93
- ?CartRepositoryInterface $ cartRepository = null
112
+ ?CartRepositoryInterface $ cartRepository = null ,
113
+ ?AddressRepositoryInterface $ addressRepository = null ,
114
+ ?AddressComparatorInterface $ addressComparator = null ,
115
+ ?LoggerInterface $ logger = null
94
116
) {
95
117
$ this ->billingAddressManagement = $ billingAddressManagement ;
96
118
$ this ->paymentMethodManagement = $ paymentMethodManagement ;
@@ -103,6 +125,11 @@ public function __construct(
103
125
?? ObjectManager::getInstance ()->get (PaymentSavingRateLimiterInterface::class);
104
126
$ this ->cartRepository = $ cartRepository
105
127
?? ObjectManager::getInstance ()->get (CartRepositoryInterface::class);
128
+ $ this ->addressRepository = $ addressRepository
129
+ ?? ObjectManager::getInstance ()->get (AddressRepositoryInterface::class);
130
+ $ this ->addressComparator = $ addressComparator
131
+ ?? ObjectManager::getInstance ()->get (AddressComparatorInterface::class);
132
+ $ this ->logger = $ logger ?? ObjectManager::getInstance ()->get (LoggerInterface::class);
106
133
}
107
134
108
135
/**
@@ -123,16 +150,16 @@ public function savePaymentInformationAndPlaceOrder(
123
150
}
124
151
try {
125
152
$ orderId = $ this ->cartManagement ->placeOrder ($ cartId );
126
- } catch (\ Magento \ Framework \ Exception \ LocalizedException $ e ) {
127
- $ this ->getLogger () ->critical (
153
+ } catch (LocalizedException $ e ) {
154
+ $ this ->logger ->critical (
128
155
'Placing an order with quote_id ' . $ cartId . ' is failed: ' . $ e ->getMessage ()
129
156
);
130
157
throw new CouldNotSaveException (
131
158
__ ($ e ->getMessage ()),
132
159
$ e
133
160
);
134
161
} catch (\Exception $ e ) {
135
- $ this ->getLogger () ->critical ($ e );
162
+ $ this ->logger ->critical ($ e );
136
163
throw new CouldNotSaveException (
137
164
__ ('A server error stopped your order from being placed. Please try to place your order again. ' ),
138
165
$ e
@@ -143,6 +170,8 @@ public function savePaymentInformationAndPlaceOrder(
143
170
144
171
/**
145
172
* @inheritdoc
173
+ *
174
+ * @throws LocalizedException
146
175
*/
147
176
public function savePaymentInformation (
148
177
$ cartId ,
@@ -170,12 +199,8 @@ public function savePaymentInformation(
170
199
$ quote ->removeAddress ($ quote ->getBillingAddress ()->getId ());
171
200
$ quote ->setBillingAddress ($ billingAddress );
172
201
$ quote ->setDataChanges (true );
173
- $ shippingAddress = $ quote ->getShippingAddress ();
174
- if ($ shippingAddress && $ shippingAddress ->getShippingMethod ()) {
175
- $ shippingRate = $ shippingAddress ->getShippingRateByCode ($ shippingAddress ->getShippingMethod ());
176
- if ($ shippingRate ) {
177
- $ shippingAddress ->setLimitCarrier ($ shippingRate ->getCarrier ());
178
- }
202
+ if ($ quote ->getShippingAddress ()) {
203
+ $ this ->processShippingAddress ($ quote );
179
204
}
180
205
}
181
206
$ this ->paymentMethodManagement ->set ($ cartId , $ paymentMethod );
@@ -195,16 +220,44 @@ public function getPaymentInformation($cartId)
195
220
}
196
221
197
222
/**
198
- * Get logger instance
223
+ * Processes shipping address.
199
224
*
200
- * @return \Psr\Log\LoggerInterface
201
- * @deprecated 100.1.8
225
+ * @param Quote $quote
226
+ * @return void
227
+ * @throws LocalizedException
202
228
*/
203
- private function getLogger ()
229
+ private function processShippingAddress ( Quote $ quote ): void
204
230
{
205
- if (!$ this ->logger ) {
206
- $ this ->logger = ObjectManager::getInstance ()->get (\Psr \Log \LoggerInterface::class);
231
+ $ shippingAddress = $ quote ->getShippingAddress ();
232
+ $ billingAddress = $ quote ->getBillingAddress ();
233
+ if ($ shippingAddress ->getShippingMethod ()) {
234
+ $ shippingRate = $ shippingAddress ->getShippingRateByCode ($ shippingAddress ->getShippingMethod ());
235
+ if ($ shippingRate ) {
236
+ $ shippingAddress ->setLimitCarrier ($ shippingRate ->getCarrier ());
237
+ }
238
+ }
239
+ if ($ this ->addressComparator ->isEqual ($ shippingAddress , $ billingAddress )) {
240
+ $ shippingAddress ->setSameAsBilling (1 );
241
+ }
242
+ // Save new address in the customer address book and set it id for billing and shipping quote addresses.
243
+ if ($ shippingAddress ->getSameAsBilling () && $ shippingAddress ->getSaveInAddressBook ()) {
244
+ $ shippingAddressData = $ shippingAddress ->exportCustomerAddress ();
245
+ $ customer = $ quote ->getCustomer ();
246
+ $ hasDefaultBilling = (bool )$ customer ->getDefaultBilling ();
247
+ $ hasDefaultShipping = (bool )$ customer ->getDefaultShipping ();
248
+ if (!$ hasDefaultShipping ) {
249
+ //Make provided address as default shipping address
250
+ $ shippingAddressData ->setIsDefaultShipping (true );
251
+ if (!$ hasDefaultBilling && !$ billingAddress ->getSaveInAddressBook ()) {
252
+ $ shippingAddressData ->setIsDefaultBilling (true );
253
+ }
254
+ }
255
+ $ shippingAddressData ->setCustomerId ($ quote ->getCustomerId ());
256
+ $ this ->addressRepository ->save ($ shippingAddressData );
257
+ $ quote ->addCustomerAddress ($ shippingAddressData );
258
+ $ shippingAddress ->setCustomerAddressData ($ shippingAddressData );
259
+ $ shippingAddress ->setCustomerAddressId ($ shippingAddressData ->getId ());
260
+ $ billingAddress ->setCustomerAddressId ($ shippingAddressData ->getId ());
207
261
}
208
- return $ this ->logger ;
209
262
}
210
263
}
0 commit comments