Skip to content

Commit

Permalink
[ECP-8595] - Correctly update tax amount on the GooglePay component w…
Browse files Browse the repository at this point in the history
…hen Shipping Address is changed.
  • Loading branch information
raoulritter committed Oct 10, 2023
1 parent 52037f8 commit 08de848
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 16 deletions.
31 changes: 22 additions & 9 deletions view/frontend/web/js/googlepay/button.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ define([
activateCart(this.isProductView)
.then(() => getShippingMethods(payload, this.isProductView))
.then(function (response) {
// Stop if no shipping methods.
// Stop if no shipping methods.
if (response.length === 0) {
reject($t('There are no shipping methods available for you right now. Please try again or use an alternative payment method.'));
return;
Expand All @@ -290,13 +290,24 @@ define([
const selectedShipping = data.shippingOptionData.id === 'shipping_option_unselected'
? response[0]
: response.find(({ method_code: id }) => id === data.shippingOptionData.id);
const regionId = getRegionId(data.shippingAddress.countryCode, data.shippingAddress.locality);
// Create payload to get totals
const regionId = getRegionId(data.shippingAddress.countryCode,
data.shippingAddress.administrativeArea || data.shippingAddress.locality,
true
);

// Create payload to get totals and use Dummy values where we do not get them
const address = {
'countryId': data.shippingAddress.countryCode,
'region': data.shippingAddress.locality,
'regionId': regionId,
'postcode': data.shippingAddress.postalCode
'regionCode': null,
'postcode': data.shippingAddress.postalCode,
'firstname': '',
'lastname': '',
'city': '',
'telephone' : '',
'street': ['', ''],

};
const totalsPayload = {
'addressInformation': {
Expand All @@ -308,16 +319,19 @@ define([
// Create payload to update quote and quote_address
const shippingInformationPayload = {
'addressInformation': {
...totalsPayload.addressInformation,
// ...totalsPayload.addressInformation,
'shipping_address': address,
'billing_address': address
'billing_address': address,
'shipping_method_code': selectedShipping.method_code,
'shipping_carrier_code': selectedShipping.carrier_code
}
};
delete shippingInformationPayload.addressInformation.address;
// delete shippingInformationPayload.addressInformation.address;

setShippingInformation(shippingInformationPayload, this.isProductView);
setTotalsInfo(totalsPayload, this.isProductView)
.done(function (totals) {
console.log(totals.tax_amount)
const shippingMethods = response.map((shippingMethod) => {
const label = shippingMethod.price_incl_tax
? formatCurrency(shippingMethod.price_incl_tax, totals.quote_currency_code) + ' - ' + shippingMethod.method_title
Expand All @@ -329,7 +343,6 @@ define([
description: shippingMethod.carrier_title
};
});

const paymentDataRequestUpdate = {
newShippingOptionParameters: {
defaultSelectedOptionId: selectedShipping.method_code,
Expand All @@ -346,7 +359,7 @@ define([
],
currencyCode: totals.quote_currency_code,
totalPriceStatus: 'FINAL',
totalPrice: totals.grand_total.toString(),
totalPrice: (totals.grand_total+ totals.tax_amount).toString(),
totalPriceLabel: 'Total',
countryCode: configModel().getConfig().countryCode
}
Expand Down
9 changes: 5 additions & 4 deletions view/frontend/web/js/helpers/getRegionId.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
define(['Adyen_ExpressCheckout/js/model/countries'], function (countriesModel) {
'use strict';

return function (countryCode, regionName) {
const countries = countriesModel().getCountires();

return function (countryCode, regionName, byRegionCode = false) {
const countries = countriesModel().getCountires(byRegionCode);
if (typeof regionName !== 'string') {
return null;
}
regionName = regionName.toLowerCase().replace(/[^A-Z0-9]/ig, '');
if(!byRegionCode){
regionName = regionName.toLowerCase().replace(/[^A-Z0-9]/ig, '');
}
if (typeof countries[countryCode] !== 'undefined'
&& typeof countries[countryCode][regionName] !== 'undefined') {
return countries[countryCode][regionName];
Expand Down
10 changes: 8 additions & 2 deletions view/frontend/web/js/helpers/processCountries.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
define(function () {
'use strict';

return function (countires) {
return function (countires, byRegionCode) {
const countryDirectory = {};

countires.forEach(function (country) {
countryDirectory[country.two_letter_abbreviation] = {};
if (typeof country.available_regions !== 'undefined') {
country.available_regions.forEach(function (region) {
const regionName = region.name.toLowerCase().replace(/[^A-Z0-9]/ig, '');
const regionCode = region.code;

countryDirectory[country.two_letter_abbreviation][regionName] = region.id;
if(!!byRegionCode){
countryDirectory[country.two_letter_abbreviation][regionCode] = region.id;
}
else{
countryDirectory[country.two_letter_abbreviation][regionName] = region.id;
}
});
}
});
Expand Down
12 changes: 11 additions & 1 deletion view/frontend/web/js/model/countries.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,17 @@ define([
}
},

getCountires: function () {
getCountires: function (byRegionCode) {
if(!!byRegionCode) {
this.fetchingCountries = true;
getCountries()
.done(function (countries) {
const processedCountries = processCountries(countries, byRegionCode);

this.setCountries(processedCountries);
this.fetchingCountries = false;
}.bind(this));
}
return this.countries();
},

Expand Down

0 comments on commit 08de848

Please sign in to comment.