From 19467dcbcb6577ed7ba3f8174661e2275a118098 Mon Sep 17 00:00:00 2001 From: raoulritter <59527829+raoulritter@users.noreply.github.com> Date: Mon, 23 Oct 2023 10:35:11 +0200 Subject: [PATCH] [ECP-8595] - Correctly update tax amount on the GooglePay component when Shipping Address is changed. (#45) * [ECP-8595] - Correctly update tax amount on the GooglePay component when Shipping Address is changed. * [ECP-8595] - Remove the unused values and console.log --- view/frontend/web/js/googlepay/button.js | 28 +++++++++++++------ view/frontend/web/js/helpers/getRegionId.js | 9 +++--- .../web/js/helpers/processCountries.js | 10 +++++-- view/frontend/web/js/model/countries.js | 12 +++++++- 4 files changed, 43 insertions(+), 16 deletions(-) diff --git a/view/frontend/web/js/googlepay/button.js b/view/frontend/web/js/googlepay/button.js index 91a5193..18c570c 100644 --- a/view/frontend/web/js/googlepay/button.js +++ b/view/frontend/web/js/googlepay/button.js @@ -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; @@ -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': { @@ -308,12 +319,12 @@ define([ // Create payload to update quote and quote_address const shippingInformationPayload = { '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; setShippingInformation(shippingInformationPayload, this.isProductView); setTotalsInfo(totalsPayload, this.isProductView) @@ -329,7 +340,6 @@ define([ description: shippingMethod.carrier_title }; }); - const paymentDataRequestUpdate = { newShippingOptionParameters: { defaultSelectedOptionId: selectedShipping.method_code, @@ -346,7 +356,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 } diff --git a/view/frontend/web/js/helpers/getRegionId.js b/view/frontend/web/js/helpers/getRegionId.js index 5aa94de..c619ce4 100644 --- a/view/frontend/web/js/helpers/getRegionId.js +++ b/view/frontend/web/js/helpers/getRegionId.js @@ -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]; diff --git a/view/frontend/web/js/helpers/processCountries.js b/view/frontend/web/js/helpers/processCountries.js index a9bc63e..7c2bbfb 100644 --- a/view/frontend/web/js/helpers/processCountries.js +++ b/view/frontend/web/js/helpers/processCountries.js @@ -1,7 +1,7 @@ define(function () { 'use strict'; - return function (countires) { + return function (countires, byRegionCode) { const countryDirectory = {}; countires.forEach(function (country) { @@ -9,8 +9,14 @@ define(function () { 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; + } }); } }); diff --git a/view/frontend/web/js/model/countries.js b/view/frontend/web/js/model/countries.js index f0aa5ed..5a8e9dd 100644 --- a/view/frontend/web/js/model/countries.js +++ b/view/frontend/web/js/model/countries.js @@ -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(); },