Skip to content

Commit

Permalink
PAA-232: Validate shipping address when call postAddress api
Browse files Browse the repository at this point in the history
  • Loading branch information
leon-zhang-awx committed May 7, 2024
1 parent 18f5421 commit aa58e3d
Showing 1 changed file with 11 additions and 16 deletions.
27 changes: 11 additions & 16 deletions Model/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
use Magento\Checkout\Api\Data\ShippingInformationInterfaceFactory;
use Airwallex\Payments\Model\Ui\ConfigProvider;
use Airwallex\Payments\Model\Client\Request\PaymentIntents\Get;
use Magento\Customer\Model\Address\Validator\Country;
use Magento\Quote\Model\ValidationRules\ShippingAddressValidationRule;

class Service implements ServiceInterface
{
Expand Down Expand Up @@ -83,7 +83,7 @@ class Service implements ServiceInterface
private ShippingInformationInterfaceFactory $shippingInformationFactory;
private ConfigProvider $configProvider;
private ApplePayValidateMerchant $validateMerchant;
private Country $country;
private ShippingAddressValidationRule $rule;

/**
* Index constructor.
Expand Down Expand Up @@ -113,7 +113,7 @@ class Service implements ServiceInterface
* @param ConfigProvider $configProvider
* @param Get $intentGet
* @param ApplePayValidateMerchant $validateMerchant
* @param Country $country
* @param ShippingAddressValidationRule $rule
*/
public function __construct(
PaymentIntents $paymentIntents,
Expand Down Expand Up @@ -141,7 +141,7 @@ public function __construct(
ConfigProvider $configProvider,
Get $intentGet,
ApplePayValidateMerchant $validateMerchant,
Country $country
ShippingAddressValidationRule $rule
) {
$this->paymentIntents = $paymentIntents;
$this->configuration = $configuration;
Expand All @@ -168,7 +168,7 @@ public function __construct(
$this->configProvider = $configProvider;
$this->intentGet = $intentGet;
$this->validateMerchant = $validateMerchant;
$this->country = $country;
$this->rule = $rule;
}
/**
* Return URL
Expand Down Expand Up @@ -470,16 +470,8 @@ public function postAddress(): string
}

$region = $this->request->getParam('region');

$city = $this->request->getParam('city');
if (!$city) {
throw new Exception(__('City is required.'));
}

$postcode = $this->request->getParam('postcode');
if (!$postcode) {
throw new Exception(__('Postal code is required.'));
}

$regionId = $this->regionFactory->create()->loadByName($region, $countryId)->getRegionId();
if (!$regionId) {
Expand All @@ -501,10 +493,13 @@ public function postAddress(): string
$address->setRegion($region->getRegion());
$address->setPostcode($postcode);

$errors = $this->country->validate($address);
$errors = $this->rule->validate($quote);
if (count($errors)) {
$msg = implode(' ', $errors);
throw new Exception(__(str_replace('"regionId" is required', 'Shippment region is incorrect', $msg)));
foreach ($errors as $error) {
foreach($error->getErrors() as $e) {
throw new Exception(__($e));
}
}
}

$methods = $this->shipmentEstimation->estimateByExtendedAddress($cartId, $address);
Expand Down

0 comments on commit aa58e3d

Please sign in to comment.