Skip to content

Commit

Permalink
Merge pull request #42 from airwallex/leon-awx
Browse files Browse the repository at this point in the history
Leon awx
  • Loading branch information
leon-zhang-awx authored May 8, 2024
2 parents d76b4a6 + c056f40 commit e26f13c
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
17 changes: 12 additions & 5 deletions Model/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,13 @@ public function addToCart(): string
}
}

private function error($message) {
return json_encode([
'type' => 'error',
'message' => $message
]);
}

/**
* Post Address to get method and quote data
*
Expand All @@ -481,7 +488,7 @@ public function postAddress(): string
{
$countryId = $this->request->getParam('country_id');
if (!$countryId) {
throw new Exception(__('Country is required.'));
return $this->error(__('Country is required.'));
}
$city = $this->request->getParam('city');

Expand Down Expand Up @@ -516,7 +523,7 @@ public function postAddress(): string
$res = [];
if (!$quote->isVirtual()) {
if (!count($methods)) {
throw new Exception(__('There are no available shipping method found.'));
return $this->error(__('There are no available shipping method found.'));
}

$selectedMethod = $methods[0];
Expand Down Expand Up @@ -584,13 +591,13 @@ public function validateAddresses()
$quote = $this->checkoutHelper->getQuote();
$errors = $this->shippingAddressValidationRule->validate($quote);
if ($errors and $errors[0]->getErrors()) {
throw new Exception(__(implode(' ', $errors[0]->getErrors())));
return $this->error(__(implode(' ', $errors[0]->getErrors())));
}
$errors = $this->billingAddressValidationRule->validate($quote);
if ($errors and $errors[0]->getErrors()) {
throw new Exception(__(implode(' ', $errors[0]->getErrors())));
return $this->error(__(implode(' ', $errors[0]->getErrors())));
}
return 'ok';
return '{"type": "success"}';
}

/**
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "airwallex/payments-plugin-magento",
"type": "magento2-module",
"description": "",
"version": "1.4.5",
"version": "1.5.0",
"require": {
"ext-json": "*",
"php": "^7.4|^8.0",
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Airwallex_Payments" setup_version="1.4.5">
<module name="Airwallex_Payments" setup_version="1.5.0">
<sequence>
<module name="Magento_Sales"/>
<module name="Magento_Payment"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ define(
let resp = await $.ajax(postOptions);

let obj = JSON.parse(resp);
if (obj.type && obj.type === 'error') {
throw new Error(obj.message);
}

this.updateExpressData(obj.quote_data);
this.updateMethods(obj.methods, obj.selected_method);
addressHandler.regionId = obj.region_id || 0;
Expand Down Expand Up @@ -189,7 +193,7 @@ define(

async validateAddresses() {
let url = urlBuilder.build('rest/V1/airwallex/payments/validate-addresses');
await storage.get(url, undefined, 'application/json', {});
return await storage.get(url, undefined, 'application/json', {});
},

placeOrder(pay) {
Expand All @@ -210,7 +214,11 @@ define(

(new Promise(async (resolve, reject) => {
try {
await this.validateAddresses();
let resp = await this.validateAddresses();
let obj = JSON.parse(resp);
if (obj.type && obj.type === 'error') {
throw new Error(obj.message);
}

if (this.paymentConfig.is_recaptcha_enabled) {
payload.xReCaptchaValue = await utils.recaptchaToken();
Expand Down

0 comments on commit e26f13c

Please sign in to comment.