Skip to content

Commit

Permalink
Fix: recreate intent when quote price is changed
Browse files Browse the repository at this point in the history
  • Loading branch information
leon-zhang-awx committed May 4, 2024
1 parent 6b96446 commit 4db8870
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 29 deletions.
4 changes: 4 additions & 0 deletions Model/Client/Request/PaymentIntents/Get.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@

class Get extends AbstractClient implements BearerAuthenticationInterface
{
public const INTENT_STATUS_SUCCESS = 'SUCCEEDED';

public const INTENT_STATUS_REQUIRES_CAPTURE = 'REQUIRES_CAPTURE';

/**
* @var string
*/
Expand Down
2 changes: 1 addition & 1 deletion Model/PaymentIntents.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public function removeIntents(): void
*/
private function getCacheKey(Quote $quote): string
{
return 'airwallex-intent-' . $quote->getId();
return 'airwallex-intent-' . $quote->getId() . '-' . sprintf("%.4f", $quote->getGrandTotal());
}

/**
Expand Down
7 changes: 4 additions & 3 deletions Model/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ public function postAddress(): string
$cartId = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id')->getQuoteId();
}

$address = $quote->getBillingAddress();
$address = $quote->getShippingAddress();
$address->setCountryId($this->request->getParam('country_id'));
$address->setCity($this->request->getParam('city'));
$address->setRegion($region->getRegion());
Expand Down Expand Up @@ -519,7 +519,7 @@ public function postAddress(): string
* Apple pay validate merchant
*
* @return string
* @throws Exception
* @throws Exception|GuzzleException
*/
public function validateMerchant()
{
Expand Down Expand Up @@ -566,7 +566,8 @@ protected function checkIntent($id)
$resp = $this->intentGet->setPaymentIntentId($id)->send();

$respArr = json_decode($resp, true);
if (!in_array($respArr['status'], ["SUCCEEDED", "REQUIRES_CAPTURE"], true)) {
$okStatus = [$this->intentGet::INTENT_STATUS_SUCCESS, $this->intentGet::INTENT_STATUS_REQUIRES_CAPTURE];
if (!in_array($respArr['status'], $okStatus, true)) {
throw new Exception(__('Something went wrong while processing your request. Please try again later.'));
}
}
Expand Down
3 changes: 1 addition & 2 deletions view/base/web/css/admin.css
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
*/

.airwallex-admin-config .heading {
padding-left: 20rem;
background: url('../img/airwallex_logo.svg') no-repeat 0 50% / 18rem auto;
height: 40px;
padding-left: 20rem;
}

.airwallex-admin-config .button-container {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,26 +102,26 @@ define(
return;
}

let recreateGooglepay = async () => {
let recreatePays = async () => {
if (!$(this.showMinicartSelector).hasClass('active')) {
return;
}

Airwallex.destroyElement('googlePayButton');
this.destroyElement()
await this.fetchExpressData();
if (this.from === 'minicart' && utils.isCartEmpty(this.expressData)) {
return;
}
googlepay.create(this);
this.createPays()
};

let cartData = customerData.get('cart');
cartData.subscribe(recreateGooglepay, this);
cartData.subscribe(recreatePays, this);

if (this.from !== 'minicart' || utils.isFromMinicartAndShouldNotShow(this.from)) {
return;
}
$(this.showMinicartSelector).on("click", recreateGooglepay);
$(this.showMinicartSelector).on("click", recreatePays);
},

async initialize() {
Expand Down Expand Up @@ -164,22 +164,31 @@ define(
if (this.from === 'minicart' && utils.isCartEmpty(this.expressData)) {
return;
}
googlepay.create(this);
applepay.create(this);
this.createPays()
},

initHashPaymentEvent() {
window.addEventListener('hashchange', async () => {
if (window.location.hash === '#payment') {
Airwallex.destroyElement('googlePayButton');
this.destroyElement()
// we need update quote, because we choose shipping method last step
await this.fetchExpressData();
googlepay.create(this);
this.createPays()
}
});
},

placeOrder() {
destroyElement() {
Airwallex.destroyElement('googlePayButton');
Airwallex.destroyElement('applePayButton');
},

createPays() {
googlepay.create(this);
applepay.create(this);
},

placeOrder(pay) {
$('body').trigger('processStart');
const payload = {
cartId: utils.getCartId(),
Expand Down Expand Up @@ -218,7 +227,8 @@ define(
addressHandler.setIntentConfirmBillingAddressFromOfficial(this.expressData.billing_address);
params.payment_method.billing = addressHandler.intentConfirmBillingAddressFromOfficial;
}
await applepay.confirmIntent(params);

await eval(pay).confirmIntent(params)

payload.intent_id = intentResponse.intent_id;
const endResult = await storage.post(
Expand All @@ -227,8 +237,8 @@ define(

resolve(endResult);
} catch (e) {
Airwallex.destroyElement('googlePayButton');
googlepay.create(this);
this.destroyElement()
this.createPays()
reject(e);
}
})).then(response => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,13 @@ define([
methods: [],
selectedMethod: {},
intermediateShippingAddress: {},
applePayRequiredBillingContactFields: [
requiredShippingContactFields: [
'email',
'name',
'phone',
'postalAddress',
],
applePayRequiredShippingContactFields: [
'email',
'name',
'phone',
requiredBillingContactFields: [
'postalAddress',
],

Expand Down Expand Up @@ -100,13 +97,17 @@ define([
}, utils.isLoggedIn(), utils.getCartId());
}
addressHandler.setIntentConfirmBillingAddressFromApple(billing, shipping.emailAddress);
that.placeOrder();
that.placeOrder('applepay');
});
},

getRequestOptions() {
let paymentDataRequest = this.getOptions();

if (!utils.isRequireShippingOption()) {
paymentDataRequest.requiredShippingContactFields = this.requiredShippingContactFields.filter(e => e !== 'postalAddress');
}

const transactionInfo = {
amount: {
value: utils.formatCurrency(this.expressData.grand_total),
Expand All @@ -126,8 +127,8 @@ define([
origin: window.location.origin,
totalPriceLabel: this.paymentConfig.express_seller_name || '',
countryCode: this.paymentConfig.country_code,
requiredBillingContactFields: this.applePayRequiredBillingContactFields,
requiredShippingContactFields: this.applePayRequiredShippingContactFields,
requiredBillingContactFields: this.requiredBillingContactFields,
requiredShippingContactFields: this.requiredShippingContactFields,
autoCapture: this.paymentConfig.is_express_capture_enabled,
};
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ define([
}, utils.isLoggedIn(), utils.getCartId());
}
addressHandler.setIntentConfirmBillingAddressFromGoogle(event.detail.paymentData);
that.placeOrder();
that.placeOrder('googlepay');
});
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,6 @@ define([
},

error(response) {
console.log(response);
let modalSelector = $('#awx-modal');
modal({title: 'Error'}, modalSelector);

Expand Down

0 comments on commit 4db8870

Please sign in to comment.