99use Magento \Checkout \Api \Exception \PaymentProcessingRateLimitExceededException ;
1010use Magento \Checkout \Api \PaymentProcessingRateLimiterInterface ;
1111use Magento \Checkout \Api \PaymentSavingRateLimiterInterface ;
12+ use Magento \Customer \Api \AddressRepositoryInterface ;
1213use Magento \Framework \App \ObjectManager ;
1314use Magento \Framework \Exception \CouldNotSaveException ;
15+ use Magento \Framework \Exception \LocalizedException ;
1416use Magento \Quote \Api \CartRepositoryInterface ;
17+ use Magento \Quote \Model \Quote ;
18+ use Psr \Log \LoggerInterface ;
1519
1620/**
1721 * Payment information management service.
@@ -23,6 +27,7 @@ class PaymentInformationManagement implements \Magento\Checkout\Api\PaymentInfor
2327 /**
2428 * @var \Magento\Quote\Api\BillingAddressManagementInterface
2529 * @deprecated 100.1.0 This call was substituted to eliminate extra quote::save call
30+ * @see not in use anymore
2631 */
2732 protected $ billingAddressManagement ;
2833
@@ -46,11 +51,6 @@ class PaymentInformationManagement implements \Magento\Checkout\Api\PaymentInfor
4651 */
4752 protected $ cartTotalsRepository ;
4853
49- /**
50- * @var \Psr\Log\LoggerInterface
51- */
52- private $ logger ;
53-
5454 /**
5555 * @var CartRepositoryInterface
5656 */
@@ -71,6 +71,21 @@ class PaymentInformationManagement implements \Magento\Checkout\Api\PaymentInfor
7171 */
7272 private $ saveRateLimiterDisabled = false ;
7373
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+
7489 /**
7590 * @param \Magento\Quote\Api\BillingAddressManagementInterface $billingAddressManagement
7691 * @param \Magento\Quote\Api\PaymentMethodManagementInterface $paymentMethodManagement
@@ -80,7 +95,11 @@ class PaymentInformationManagement implements \Magento\Checkout\Api\PaymentInfor
8095 * @param PaymentProcessingRateLimiterInterface|null $paymentRateLimiter
8196 * @param PaymentSavingRateLimiterInterface|null $saveRateLimiter
8297 * @param CartRepositoryInterface|null $cartRepository
98+ * @param AddressRepositoryInterface|null $addressRepository
99+ * @param AddressComparatorInterface|null $addressComparator
100+ * @param LoggerInterface|null $logger
83101 * @codeCoverageIgnore
102+ * @SuppressWarnings(PHPMD.ExcessiveParameterList)
84103 */
85104 public function __construct (
86105 \Magento \Quote \Api \BillingAddressManagementInterface $ billingAddressManagement ,
@@ -90,7 +109,10 @@ public function __construct(
90109 \Magento \Quote \Api \CartTotalRepositoryInterface $ cartTotalsRepository ,
91110 ?PaymentProcessingRateLimiterInterface $ paymentRateLimiter = null ,
92111 ?PaymentSavingRateLimiterInterface $ saveRateLimiter = null ,
93- ?CartRepositoryInterface $ cartRepository = null
112+ ?CartRepositoryInterface $ cartRepository = null ,
113+ ?AddressRepositoryInterface $ addressRepository = null ,
114+ ?AddressComparatorInterface $ addressComparator = null ,
115+ ?LoggerInterface $ logger = null
94116 ) {
95117 $ this ->billingAddressManagement = $ billingAddressManagement ;
96118 $ this ->paymentMethodManagement = $ paymentMethodManagement ;
@@ -103,6 +125,11 @@ public function __construct(
103125 ?? ObjectManager::getInstance ()->get (PaymentSavingRateLimiterInterface::class);
104126 $ this ->cartRepository = $ cartRepository
105127 ?? 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);
106133 }
107134
108135 /**
@@ -123,16 +150,16 @@ public function savePaymentInformationAndPlaceOrder(
123150 }
124151 try {
125152 $ orderId = $ this ->cartManagement ->placeOrder ($ cartId );
126- } catch (\ Magento \ Framework \ Exception \ LocalizedException $ e ) {
127- $ this ->getLogger () ->critical (
153+ } catch (LocalizedException $ e ) {
154+ $ this ->logger ->critical (
128155 'Placing an order with quote_id ' . $ cartId . ' is failed: ' . $ e ->getMessage ()
129156 );
130157 throw new CouldNotSaveException (
131158 __ ($ e ->getMessage ()),
132159 $ e
133160 );
134161 } catch (\Exception $ e ) {
135- $ this ->getLogger () ->critical ($ e );
162+ $ this ->logger ->critical ($ e );
136163 throw new CouldNotSaveException (
137164 __ ('A server error stopped your order from being placed. Please try to place your order again. ' ),
138165 $ e
@@ -143,6 +170,8 @@ public function savePaymentInformationAndPlaceOrder(
143170
144171 /**
145172 * @inheritdoc
173+ *
174+ * @throws LocalizedException
146175 */
147176 public function savePaymentInformation (
148177 $ cartId ,
@@ -170,12 +199,8 @@ public function savePaymentInformation(
170199 $ quote ->removeAddress ($ quote ->getBillingAddress ()->getId ());
171200 $ quote ->setBillingAddress ($ billingAddress );
172201 $ 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 );
179204 }
180205 }
181206 $ this ->paymentMethodManagement ->set ($ cartId , $ paymentMethod );
@@ -195,16 +220,44 @@ public function getPaymentInformation($cartId)
195220 }
196221
197222 /**
198- * Get logger instance
223+ * Processes shipping address.
199224 *
200- * @return \Psr\Log\LoggerInterface
201- * @deprecated 100.1.8
225+ * @param Quote $quote
226+ * @return void
227+ * @throws LocalizedException
202228 */
203- private function getLogger ()
229+ private function processShippingAddress ( Quote $ quote ): void
204230 {
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 ());
207261 }
208- return $ this ->logger ;
209262 }
210263}
0 commit comments