Skip to content

Commit 41b3b6d

Browse files
asavaleMetaAmeya Savalesol-loup
authored
populate address information if it is missing (#674)
* populate address information if it is missing * update comments * fix static analysis issues * Lint fixes --------- Co-authored-by: Ameya Savale <[email protected]> Co-authored-by: Paul Kang <[email protected]>
1 parent 63641fc commit 41b3b6d

File tree

2 files changed

+68
-12
lines changed

2 files changed

+68
-12
lines changed

app/code/Meta/Sales/Api/CreateOrderApiInterface.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
namespace Meta\Sales\Api;
2222

23+
use Magento\Quote\Api\Data\AddressInterface;
2324
use Magento\Sales\Api\Data\OrderInterface;
2425
use Meta\Sales\Api\Data\CreateOrderApiProductItemInterface;
2526
use Meta\Sales\Api\Data\CreateOrderApiShipmentDetailsInterface;
@@ -43,6 +44,7 @@ interface CreateOrderApiInterface
4344
* @param string $lastName
4445
* @param CreateOrderApiProductItemInterface[] $productItems
4546
* @param CreateOrderApiShipmentDetailsInterface $shipmentDetails
47+
* @param AddressInterface $billingAddress
4648
* @param string|null $channel
4749
* @param bool $buyerRemarketingOptIn
4850
* @param bool $createInvoice
@@ -58,6 +60,7 @@ public function createOrder(
5860
string $lastName,
5961
array $productItems,
6062
CreateOrderApiShipmentDetailsInterface $shipmentDetails,
63+
AddressInterface $billingAddress,
6164
string $channel,
6265
bool $buyerRemarketingOptIn = false,
6366
bool $createInvoice = true

app/code/Meta/Sales/Model/Api/CreateOrderApi.php

Lines changed: 65 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
use Magento\Framework\Exception\NoSuchEntityException;
3232
use Magento\Framework\HTTP\PhpEnvironment\RemoteAddress;
3333
use Magento\Quote\Api\CartRepositoryInterface;
34+
use Magento\Quote\Api\Data\AddressInterface;
3435
use Magento\Quote\Model\Quote;
3536
use Magento\Quote\Model\QuoteManagement;
3637
use Magento\Sales\Api\Data\InvoiceInterface;
@@ -263,20 +264,64 @@ private function validateQuote(
263264
}
264265
}
265266

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+
266307
/**
267308
* Populate customer information in quote
268309
*
269310
* @param Quote $quote
270311
* @param string $email
271312
* @param string $firstName
272313
* @param string $lastName
314+
* @param AddressInterface $shippingAddress
315+
* @param AddressInterface $billingAddress
273316
* @return void
274317
*/
275318
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
280325
): void {
281326
// Populate customer information
282327
if (!$quote->getCustomerEmail()) {
@@ -289,6 +334,9 @@ private function populateCustomerInformation(
289334
$quote->setCustomerLastname($lastName);
290335
}
291336

337+
$this->populateAddress($quote->getShippingAddress(), $shippingAddress);
338+
$this->populateAddress($quote->getBillingAddress(), $billingAddress);
339+
292340
$remoteAddress = $this->remoteAddress->getRemoteAddress();
293341
if ($remoteAddress !== false) {
294342
$quote->setRemoteIp($remoteAddress);
@@ -395,15 +443,14 @@ private function buildMetaLogContext(
395443
* @param string $lastName
396444
* @param CreateOrderApiProductItemInterface[] $productItems
397445
* @param CreateOrderApiShipmentDetailsInterface $shipmentDetails
446+
* @param AddressInterface $billingAddress
398447
* @param string $channel
399448
* @param bool $buyerRemarketingOptIn
400449
* @param bool $createInvoice
401450
* @return OrderInterface
402-
* @throws GuzzleException
403451
* @throws LocalizedException
404452
* @throws NoSuchEntityException
405453
* @throws Throwable
406-
* @throws UnauthorizedTokenException
407454
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
408455
*/
409456
public function createOrder(
@@ -416,6 +463,7 @@ public function createOrder(
416463
string $lastName,
417464
array $productItems,
418465
CreateOrderApiShipmentDetailsInterface $shipmentDetails,
466+
AddressInterface $billingAddress,
419467
string $channel,
420468
bool $buyerRemarketingOptIn = false,
421469
bool $createInvoice = true
@@ -458,7 +506,14 @@ public function createOrder(
458506
$this->validateStateAndTotals($storeId, $orderId, $quote);
459507

460508
// 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+
);
462517
$this->quoteRepository->save($quote);
463518

464519
// Create an order
@@ -520,8 +575,9 @@ public function createOrder(
520575
}
521576

522577
/**
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
525581
*
526582
* @param int $storeId
527583
* @param string $metaOrderId
@@ -545,7 +601,6 @@ private function validateStateAndTotals(
545601
// Validate order state
546602
if (strcasecmp("FB_PROCESSING", $metaOrderDetails['order_status']['state']) != 0) {
547603
$le = new LocalizedException(__('Meta order is not in the FB_PROCESSING state'));
548-
549604
$this->fbeHelper->logExceptionImmediatelyToMeta(
550605
$le,
551606
$this->buildMetaLogContext(
@@ -557,7 +612,6 @@ private function validateStateAndTotals(
557612
]
558613
)
559614
);
560-
561615
throw $le;
562616
}
563617

@@ -569,7 +623,6 @@ private function validateStateAndTotals(
569623
'grand_total' => $metaOrderDetails['estimated_payment_details']['total_amount']['amount']
570624
];
571625

572-
573626
foreach (['subtotal', 'shipping', 'tax', 'grand_total'] as $code) {
574627
if ($metaTotals[$code] != $magentoTotals[$code]) {
575628
$le = new LocalizedException(__(

0 commit comments

Comments
 (0)