14
14
use Magento \Framework \Exception \CouldNotSaveException ;
15
15
use Magento \Framework \Exception \LocalizedException ;
16
16
use Magento \Quote \Api \CartRepositoryInterface ;
17
- use Magento \Quote \Api \Data \AddressInterface ;
18
17
use Magento \Quote \Model \Quote ;
18
+ use Psr \Log \LoggerInterface ;
19
19
20
20
/**
21
21
* Payment information management service.
@@ -51,11 +51,6 @@ class PaymentInformationManagement implements \Magento\Checkout\Api\PaymentInfor
51
51
*/
52
52
protected $ cartTotalsRepository ;
53
53
54
- /**
55
- * @var \Psr\Log\LoggerInterface
56
- */
57
- private $ logger ;
58
-
59
54
/**
60
55
* @var CartRepositoryInterface
61
56
*/
@@ -86,6 +81,11 @@ class PaymentInformationManagement implements \Magento\Checkout\Api\PaymentInfor
86
81
*/
87
82
private $ addressComparator ;
88
83
84
+ /**
85
+ * @var LoggerInterface
86
+ */
87
+ private $ logger ;
88
+
89
89
/**
90
90
* @param \Magento\Quote\Api\BillingAddressManagementInterface $billingAddressManagement
91
91
* @param \Magento\Quote\Api\PaymentMethodManagementInterface $paymentMethodManagement
@@ -97,6 +97,7 @@ class PaymentInformationManagement implements \Magento\Checkout\Api\PaymentInfor
97
97
* @param CartRepositoryInterface|null $cartRepository
98
98
* @param AddressRepositoryInterface|null $addressRepository
99
99
* @param AddressComparatorInterface|null $addressComparator
100
+ * @param LoggerInterface|null $logger
100
101
* @codeCoverageIgnore
101
102
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
102
103
*/
@@ -110,7 +111,8 @@ public function __construct(
110
111
?PaymentSavingRateLimiterInterface $ saveRateLimiter = null ,
111
112
?CartRepositoryInterface $ cartRepository = null ,
112
113
?AddressRepositoryInterface $ addressRepository = null ,
113
- ?AddressComparatorInterface $ addressComparator = null
114
+ ?AddressComparatorInterface $ addressComparator = null ,
115
+ ?LoggerInterface $ logger = null
114
116
) {
115
117
$ this ->billingAddressManagement = $ billingAddressManagement ;
116
118
$ this ->paymentMethodManagement = $ paymentMethodManagement ;
@@ -127,6 +129,7 @@ public function __construct(
127
129
?? ObjectManager::getInstance ()->get (AddressRepositoryInterface::class);
128
130
$ this ->addressComparator = $ addressComparator
129
131
?? ObjectManager::getInstance ()->get (AddressComparatorInterface::class);
132
+ $ this ->logger = $ logger ?? ObjectManager::getInstance ()->get (LoggerInterface::class);
130
133
}
131
134
132
135
/**
@@ -148,15 +151,15 @@ public function savePaymentInformationAndPlaceOrder(
148
151
try {
149
152
$ orderId = $ this ->cartManagement ->placeOrder ($ cartId );
150
153
} catch (LocalizedException $ e ) {
151
- $ this ->getLogger () ->critical (
154
+ $ this ->logger ->critical (
152
155
'Placing an order with quote_id ' . $ cartId . ' is failed: ' . $ e ->getMessage ()
153
156
);
154
157
throw new CouldNotSaveException (
155
158
__ ($ e ->getMessage ()),
156
159
$ e
157
160
);
158
161
} catch (\Exception $ e ) {
159
- $ this ->getLogger () ->critical ($ e );
162
+ $ this ->logger ->critical ($ e );
160
163
throw new CouldNotSaveException (
161
164
__ ('A server error stopped your order from being placed. Please try to place your order again. ' ),
162
165
$ e
@@ -196,13 +199,8 @@ public function savePaymentInformation(
196
199
$ quote ->removeAddress ($ quote ->getBillingAddress ()->getId ());
197
200
$ quote ->setBillingAddress ($ billingAddress );
198
201
$ quote ->setDataChanges (true );
199
- $ this ->processShippingIfSameAsBilling ($ quote , $ billingAddress );
200
- $ shippingAddress = $ quote ->getShippingAddress ();
201
- if ($ shippingAddress && $ shippingAddress ->getShippingMethod ()) {
202
- $ shippingRate = $ shippingAddress ->getShippingRateByCode ($ shippingAddress ->getShippingMethod ());
203
- if ($ shippingRate ) {
204
- $ shippingAddress ->setLimitCarrier ($ shippingRate ->getCarrier ());
205
- }
202
+ if ($ quote ->getShippingAddress ()) {
203
+ $ this ->processShippingAddress ($ quote );
206
204
}
207
205
}
208
206
$ this ->paymentMethodManagement ->set ($ cartId , $ paymentMethod );
@@ -222,57 +220,44 @@ public function getPaymentInformation($cartId)
222
220
}
223
221
224
222
/**
225
- * Get logger instance
226
- *
227
- * @return \Psr\Log\LoggerInterface
228
- * @deprecated 100.1.8
229
- * @see not in use anymore
230
- */
231
- private function getLogger ()
232
- {
233
- if (!$ this ->logger ) {
234
- $ this ->logger = ObjectManager::getInstance ()->get (\Psr \Log \LoggerInterface::class);
235
- }
236
- return $ this ->logger ;
237
- }
238
-
239
- /**
240
- * Save shipping address information
223
+ * Processes shipping address.
241
224
*
242
225
* @param Quote $quote
243
- * @param AddressInterface|null $billingAddress
244
226
* @return void
245
227
* @throws LocalizedException
246
228
*/
247
- private function processShippingIfSameAsBilling (Quote $ quote, ? AddressInterface $ billingAddress ): void
229
+ private function processShippingAddress (Quote $ quote ): void
248
230
{
249
231
$ shippingAddress = $ quote ->getShippingAddress ();
250
- if ($ shippingAddress
251
- && (
252
- (bool )$ shippingAddress ->getSameAsBilling ()
253
- || $ this ->addressComparator ->isEqual ($ shippingAddress , $ billingAddress )
254
- )
255
- ) {
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 )) {
256
240
$ shippingAddress ->setSameAsBilling (1 );
257
- if ($ shippingAddress ->getSaveInAddressBook ()) {
258
- $ shippingAddressData = $ shippingAddress ->exportCustomerAddress ();
259
- $ customer = $ quote ->getCustomer ();
260
- $ hasDefaultBilling = (bool )$ customer ->getDefaultBilling ();
261
- $ hasDefaultShipping = (bool )$ customer ->getDefaultShipping ();
262
- if (!$ hasDefaultShipping ) {
263
- //Make provided address as default shipping address
264
- $ shippingAddressData ->setIsDefaultShipping (true );
265
- if (!$ hasDefaultBilling && !$ quote ->getBillingAddress ()->getSaveInAddressBook ()) {
266
- $ shippingAddressData ->setIsDefaultBilling (true );
267
- }
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 );
268
253
}
269
- $ shippingAddressData ->setCustomerId ($ quote ->getCustomerId ());
270
- $ this ->addressRepository ->save ($ shippingAddressData );
271
- $ quote ->addCustomerAddress ($ shippingAddressData );
272
- $ shippingAddress ->setCustomerAddressData ($ shippingAddressData );
273
- $ shippingAddress ->setCustomerAddressId ($ shippingAddressData ->getId ());
274
- $ quote ->getBillingAddress ()->setCustomerAddressId ($ shippingAddressData ->getId ());
275
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 ());
276
261
}
277
262
}
278
263
}
0 commit comments