31
31
use Magento \Framework \Exception \NoSuchEntityException ;
32
32
use Magento \Framework \HTTP \PhpEnvironment \RemoteAddress ;
33
33
use Magento \Quote \Api \CartRepositoryInterface ;
34
+ use Magento \Quote \Api \Data \AddressInterface ;
34
35
use Magento \Quote \Model \Quote ;
35
36
use Magento \Quote \Model \QuoteManagement ;
36
37
use Magento \Sales \Api \Data \InvoiceInterface ;
@@ -263,20 +264,64 @@ private function validateQuote(
263
264
}
264
265
}
265
266
267
+ /**
268
+ * Populate any missing address information
269
+ *
270
+ * @param AddressInterface $address
271
+ * @param AddressInterface $metaAddressInfo
272
+ * @return void
273
+ */
274
+ private function populateAddress (
275
+ AddressInterface $ address ,
276
+ AddressInterface $ metaAddressInfo
277
+ ): void {
278
+ if (!$ address ->getRegionCode ()) {
279
+ $ address ->setRegionCode ($ metaAddressInfo ->getRegionCode ());
280
+ }
281
+
282
+ if (!$ address ->getCountryId ()) {
283
+ $ address ->setCountryId ($ metaAddressInfo ->getCountryId ());
284
+ }
285
+
286
+ if (!$ address ->getStreetLine (1 )) {
287
+ $ address ->setStreet ($ metaAddressInfo ->getStreet ());
288
+ }
289
+
290
+ if (!$ address ->getPostcode ()) {
291
+ $ address ->setPostcode ($ metaAddressInfo ->getPostcode ());
292
+ }
293
+
294
+ if (!$ address ->getCity ()) {
295
+ $ address ->setCity ($ metaAddressInfo ->getCity ());
296
+ }
297
+
298
+ if (!$ address ->getFirstname ()) {
299
+ $ address ->setFirstname ($ metaAddressInfo ->getFirstname ());
300
+ }
301
+
302
+ if (!$ address ->getLastname ()) {
303
+ $ address ->setLastname ($ metaAddressInfo ->getLastname ());
304
+ }
305
+ }
306
+
266
307
/**
267
308
* Populate customer information in quote
268
309
*
269
310
* @param Quote $quote
270
311
* @param string $email
271
312
* @param string $firstName
272
313
* @param string $lastName
314
+ * @param AddressInterface $shippingAddress
315
+ * @param AddressInterface $billingAddress
273
316
* @return void
274
317
*/
275
318
private function populateCustomerInformation (
276
- Quote $ quote ,
277
- string $ email ,
278
- string $ firstName ,
279
- string $ lastName
319
+ Quote $ quote ,
320
+ string $ email ,
321
+ string $ firstName ,
322
+ string $ lastName ,
323
+ AddressInterface $ shippingAddress ,
324
+ AddressInterface $ billingAddress
280
325
): void {
281
326
// Populate customer information
282
327
if (!$ quote ->getCustomerEmail ()) {
@@ -289,6 +334,9 @@ private function populateCustomerInformation(
289
334
$ quote ->setCustomerLastname ($ lastName );
290
335
}
291
336
337
+ $ this ->populateAddress ($ quote ->getShippingAddress (), $ shippingAddress );
338
+ $ this ->populateAddress ($ quote ->getBillingAddress (), $ billingAddress );
339
+
292
340
$ remoteAddress = $ this ->remoteAddress ->getRemoteAddress ();
293
341
if ($ remoteAddress !== false ) {
294
342
$ quote ->setRemoteIp ($ remoteAddress );
@@ -395,15 +443,14 @@ private function buildMetaLogContext(
395
443
* @param string $lastName
396
444
* @param CreateOrderApiProductItemInterface[] $productItems
397
445
* @param CreateOrderApiShipmentDetailsInterface $shipmentDetails
446
+ * @param AddressInterface $billingAddress
398
447
* @param string $channel
399
448
* @param bool $buyerRemarketingOptIn
400
449
* @param bool $createInvoice
401
450
* @return OrderInterface
402
- * @throws GuzzleException
403
451
* @throws LocalizedException
404
452
* @throws NoSuchEntityException
405
453
* @throws Throwable
406
- * @throws UnauthorizedTokenException
407
454
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
408
455
*/
409
456
public function createOrder (
@@ -416,6 +463,7 @@ public function createOrder(
416
463
string $ lastName ,
417
464
array $ productItems ,
418
465
CreateOrderApiShipmentDetailsInterface $ shipmentDetails ,
466
+ AddressInterface $ billingAddress ,
419
467
string $ channel ,
420
468
bool $ buyerRemarketingOptIn = false ,
421
469
bool $ createInvoice = true
@@ -458,7 +506,14 @@ public function createOrder(
458
506
$ this ->validateStateAndTotals ($ storeId , $ orderId , $ quote );
459
507
460
508
// Populate basic customer details
461
- $ this ->populateCustomerInformation ($ quote , $ email , $ firstName , $ lastName );
509
+ $ this ->populateCustomerInformation (
510
+ $ quote ,
511
+ $ email ,
512
+ $ firstName ,
513
+ $ lastName ,
514
+ $ shipmentDetails ->getShippingAddress (),
515
+ $ billingAddress
516
+ );
462
517
$ this ->quoteRepository ->save ($ quote );
463
518
464
519
// Create an order
@@ -520,8 +575,9 @@ public function createOrder(
520
575
}
521
576
522
577
/**
523
- * Validate that the meta order is in the correct state and
524
- * that the totals in the magento order match the totals in meta
578
+ * Validate that the meta order is in the correct state
579
+ *
580
+ * Confirms that the totals in the magento order match the totals in meta
525
581
*
526
582
* @param int $storeId
527
583
* @param string $metaOrderId
@@ -545,7 +601,6 @@ private function validateStateAndTotals(
545
601
// Validate order state
546
602
if (strcasecmp ("FB_PROCESSING " , $ metaOrderDetails ['order_status ' ]['state ' ]) != 0 ) {
547
603
$ le = new LocalizedException (__ ('Meta order is not in the FB_PROCESSING state ' ));
548
-
549
604
$ this ->fbeHelper ->logExceptionImmediatelyToMeta (
550
605
$ le ,
551
606
$ this ->buildMetaLogContext (
@@ -557,7 +612,6 @@ private function validateStateAndTotals(
557
612
]
558
613
)
559
614
);
560
-
561
615
throw $ le ;
562
616
}
563
617
@@ -569,7 +623,6 @@ private function validateStateAndTotals(
569
623
'grand_total ' => $ metaOrderDetails ['estimated_payment_details ' ]['total_amount ' ]['amount ' ]
570
624
];
571
625
572
-
573
626
foreach (['subtotal ' , 'shipping ' , 'tax ' , 'grand_total ' ] as $ code ) {
574
627
if ($ metaTotals [$ code ] != $ magentoTotals [$ code ]) {
575
628
$ le = new LocalizedException (__ (
0 commit comments