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
89 changes: 73 additions & 16 deletions view/frontend/web/js/applepay/button.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ define([
},
amount: {
value: this.isProductView
// TODO refactor this part
? formatAmount(totalsModel().getTotal() * 100)
: formatAmount(getCartSubtotal() * 100),
currency: currency
Expand All @@ -237,6 +238,7 @@ define([

onShippingContactSelect: function (resolve, reject, event) {
let self = this;
debugger;
candemiralp marked this conversation as resolved.
Show resolved Hide resolved

// Get the address.
let address = event.shippingContact,
Expand Down Expand Up @@ -282,28 +284,47 @@ 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);

debugger;
candemiralp marked this conversation as resolved.
Show resolved Hide resolved
setTotalsInfo(totalsPayload, this.isProductView)
.done((response) => {
debugger;
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 +338,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 +358,47 @@ define([

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

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
}
Fixed Show fixed Hide fixed

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);

debugger;
setTotalsInfo(totalsPayload, this.isProductView)
.done((response) => {
debugger;
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 +412,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