Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ECP-8486] Update quote totals and set tax included value in the express Apple Pay checkout #38

Merged
merged 10 commits into from
Aug 29, 2023
81 changes: 64 additions & 17 deletions view/frontend/web/js/applepay/button.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,28 +282,44 @@ define([
self.shippingMethod = result[i].method_code;
}
}

let address = {
'countryId': self.shippingAddress.country_id,
'region': self.shippingAddress.region,
'regionId': getRegionId(self.shippingAddress.country_id, self.shippingAddress.region),
'postcode': self.shippingAddress.postcode
};


// Create payload to get totals
let totalsPayload = {
'addressInformation': {
'address': {
'countryId': self.shippingAddress.country_id,
'region': self.shippingAddress.region,
'regionId': getRegionId(self.shippingAddress.country_id, self.shippingAddress.region),
'postcode': self.shippingAddress.postcode
},
'address': address,
'shipping_method_code': self.shippingMethods[shippingMethods[0].identifier].method_code,
'shipping_carrier_code': self.shippingMethods[shippingMethods[0].identifier].carrier_code
}
};

// Create payload to update quote
let shippingInformationPayload = {
'addressInformation': {
...totalsPayload.addressInformation,
'shipping_address': address,
'billing_address': address
}
};

delete shippingInformationPayload.addressInformation.address;
setShippingInformation(shippingInformationPayload, this.isProductView);

setTotalsInfo(totalsPayload, this.isProductView)
.done((response) => {
let applePayShippingContactUpdate = {};

applePayShippingContactUpdate.newShippingMethods = shippingMethods;
applePayShippingContactUpdate.newTotal = {
label: $t('Grand Total'),
amount: response.grand_total.toString()
amount: (response.grand_total + response.tax_amount).toString()
};
applePayShippingContactUpdate.newLineItems = [
{
Expand All @@ -317,6 +333,15 @@ define([
amount: shippingMethods[0].amount.toString()
}
];

if (response.tax_amount > 0) {
applePayShippingContactUpdate.newLineItems.push({
type: 'final',
label: $t('Tax'),
amount: response.tax_amount.toString()
})
}

resolve(applePayShippingContactUpdate);
// Pass shipping methods back
}).fail((e) => {
Expand All @@ -328,29 +353,42 @@ define([

onShippingMethodSelect: function (resolve, reject, event) {
let self = this;

let shippingMethod = event.shippingMethod;
let payload = {

let address = {
'countryId': self.shippingAddress.country_id,
'region': self.shippingAddress.region,
'regionId': getRegionId(self.shippingAddress.country_id, self.shippingAddress.region),
'postcode': self.shippingAddress.postcode
};

let totalsPayload = {
'addressInformation': {
'address': {
'countryId': self.shippingAddress.country_id,
'region': self.shippingAddress.region,
'regionId': getRegionId(self.shippingAddress.country_id, self.shippingAddress.region),
'postcode': self.shippingAddress.postcode
},
'address': address,
'shipping_method_code': self.shippingMethods[shippingMethod.identifier].method_code,
'shipping_carrier_code': self.shippingMethods[shippingMethod.identifier].carrier_code
}
};

setTotalsInfo(payload, this.isProductView)
let shippingInformationPayload = {
'addressInformation': {
...totalsPayload.addressInformation,
'shipping_address': address,
'billing_address': address
}
};

delete shippingInformationPayload.addressInformation.address;
setShippingInformation(shippingInformationPayload, this.isProductView);

setTotalsInfo(totalsPayload, this.isProductView)
.done((response) => {
let applePayShippingMethodUpdate = {};

applePayShippingMethodUpdate.newTotal = {
type: 'final',
label: $t('Grand Total'),
amount: response.grand_total.toString()
amount: (response.grand_total + response.tax_amount).toString()
};
applePayShippingMethodUpdate.newLineItems = [
{
Expand All @@ -364,6 +402,15 @@ define([
amount: shippingMethod.amount.toString()
}
];

if (response.tax_amount > 0) {
applePayShippingMethodUpdate.newLineItems.push({
type: 'final',
label: $t('Tax'),
amount: response.tax_amount.toString()
})
}

self.shippingMethod = shippingMethod.identifier;
resolve(applePayShippingMethodUpdate);
}).fail((e) => {
Expand Down