1414use Magento \Framework \Exception \CouldNotSaveException ;
1515use Magento \Framework \Exception \LocalizedException ;
1616use Magento \Quote \Api \CartRepositoryInterface ;
17- use Magento \Quote \Api \Data \AddressInterface ;
1817use Magento \Quote \Model \Quote ;
18+ use Psr \Log \LoggerInterface ;
1919
2020/**
2121 * Payment information management service.
@@ -51,11 +51,6 @@ class PaymentInformationManagement implements \Magento\Checkout\Api\PaymentInfor
5151 */
5252 protected $ cartTotalsRepository ;
5353
54- /**
55- * @var \Psr\Log\LoggerInterface
56- */
57- private $ logger ;
58-
5954 /**
6055 * @var CartRepositoryInterface
6156 */
@@ -86,6 +81,11 @@ class PaymentInformationManagement implements \Magento\Checkout\Api\PaymentInfor
8681 */
8782 private $ addressComparator ;
8883
84+ /**
85+ * @var LoggerInterface
86+ */
87+ private $ logger ;
88+
8989 /**
9090 * @param \Magento\Quote\Api\BillingAddressManagementInterface $billingAddressManagement
9191 * @param \Magento\Quote\Api\PaymentMethodManagementInterface $paymentMethodManagement
@@ -97,6 +97,7 @@ class PaymentInformationManagement implements \Magento\Checkout\Api\PaymentInfor
9797 * @param CartRepositoryInterface|null $cartRepository
9898 * @param AddressRepositoryInterface|null $addressRepository
9999 * @param AddressComparatorInterface|null $addressComparator
100+ * @param LoggerInterface|null $logger
100101 * @codeCoverageIgnore
101102 * @SuppressWarnings(PHPMD.ExcessiveParameterList)
102103 */
@@ -110,7 +111,8 @@ public function __construct(
110111 ?PaymentSavingRateLimiterInterface $ saveRateLimiter = null ,
111112 ?CartRepositoryInterface $ cartRepository = null ,
112113 ?AddressRepositoryInterface $ addressRepository = null ,
113- ?AddressComparatorInterface $ addressComparator = null
114+ ?AddressComparatorInterface $ addressComparator = null ,
115+ ?LoggerInterface $ logger = null
114116 ) {
115117 $ this ->billingAddressManagement = $ billingAddressManagement ;
116118 $ this ->paymentMethodManagement = $ paymentMethodManagement ;
@@ -127,6 +129,7 @@ public function __construct(
127129 ?? ObjectManager::getInstance ()->get (AddressRepositoryInterface::class);
128130 $ this ->addressComparator = $ addressComparator
129131 ?? ObjectManager::getInstance ()->get (AddressComparatorInterface::class);
132+ $ this ->logger = $ logger ?? ObjectManager::getInstance ()->get (LoggerInterface::class);
130133 }
131134
132135 /**
@@ -148,15 +151,15 @@ public function savePaymentInformationAndPlaceOrder(
148151 try {
149152 $ orderId = $ this ->cartManagement ->placeOrder ($ cartId );
150153 } catch (LocalizedException $ e ) {
151- $ this ->getLogger () ->critical (
154+ $ this ->logger ->critical (
152155 'Placing an order with quote_id ' . $ cartId . ' is failed: ' . $ e ->getMessage ()
153156 );
154157 throw new CouldNotSaveException (
155158 __ ($ e ->getMessage ()),
156159 $ e
157160 );
158161 } catch (\Exception $ e ) {
159- $ this ->getLogger () ->critical ($ e );
162+ $ this ->logger ->critical ($ e );
160163 throw new CouldNotSaveException (
161164 __ ('A server error stopped your order from being placed. Please try to place your order again. ' ),
162165 $ e
@@ -196,13 +199,8 @@ public function savePaymentInformation(
196199 $ quote ->removeAddress ($ quote ->getBillingAddress ()->getId ());
197200 $ quote ->setBillingAddress ($ billingAddress );
198201 $ 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 );
206204 }
207205 }
208206 $ this ->paymentMethodManagement ->set ($ cartId , $ paymentMethod );
@@ -222,57 +220,44 @@ public function getPaymentInformation($cartId)
222220 }
223221
224222 /**
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.
241224 *
242225 * @param Quote $quote
243- * @param AddressInterface|null $billingAddress
244226 * @return void
245227 * @throws LocalizedException
246228 */
247- private function processShippingIfSameAsBilling (Quote $ quote, ? AddressInterface $ billingAddress ): void
229+ private function processShippingAddress (Quote $ quote ): void
248230 {
249231 $ 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 )) {
256240 $ 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 );
268253 }
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 ());
275254 }
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 ());
276261 }
277262 }
278263}
0 commit comments