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

[PW-7527] Fix: Updating the cart removes express buttons #15

Merged
merged 2 commits into from
Dec 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Observer/AddGooglePayShortcuts.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function execute(Observer $observer)
$currentPageIdentifier = $this->getCurrentPageIdentifier($observer);
if (!in_array(
$currentPageIdentifier,
$this->configuration->getShowApplePayOn()
$this->configuration->getShowGooglePayOn()
)) {
return;
}
Expand Down
43 changes: 28 additions & 15 deletions view/frontend/web/js/applepay/button.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,17 @@ define([
defaults: {
shippingMethods: {},
isProductView: false,
maskedId: null
maskedId: null,
applePayComponent: null
},

initialize: async function (config, element) {
this._super();

configModel().setConfig(config);
countriesModel();

this.isProductView = config.isProductView;
let applePaymentMethod = await getPaymentMethod('applepay', this.isProductView);

// If express methods is not set then set it.
if (this.isProductView) {
Expand Down Expand Up @@ -102,21 +105,28 @@ define([
});
}.bind(this));

applePaymentMethod = await getPaymentMethod('applepay', this.isProductView);
let applePaymentMethod = await getPaymentMethod('applepay', this.isProductView);

if (!isConfigSet(applePaymentMethod, ['merchantId', 'merchantName'])) {
return;
}

this.initialiseApplePayComponent(applePaymentMethod, element);
} else if (!isConfigSet(applePaymentMethod, ['merchantId', 'merchantName'])) {
return;
} else {
let applePaymentMethod = await getPaymentMethod('applepay', this.isProductView);
if (!applePaymentMethod) {
const cart = customerData.get('cart');
cart.subscribe(function () {
this.reloadApplePayButton(element);
}.bind(this));
} else {
if (!isConfigSet(applePaymentMethod, ['merchantId', 'merchantName'])) {
console.log('Required configuration for Apple Pay is missing.');
return;
}
this.initialiseApplePayComponent(applePaymentMethod, element);
}
}

configModel().setConfig(config);
countriesModel();

this.initialiseApplePayComponent(applePaymentMethod, element);
},

initialiseApplePayComponent: async function (applePaymentMethod, element) {
Expand Down Expand Up @@ -147,17 +157,20 @@ define([
},

unmountApplePay: function () {
if (this.applepay) {
this.applepay.unmount();
if (this.applePayComponent) {
this.applePayComponent.unmount();
}
},

reloadApplePayButton: async function (element) {
const applePaymentMethod = await getPaymentMethod('applepay', this.isProductView);
const pdpResponse = await getExpressMethods().getRequest(element);

setExpressMethods(pdpResponse);
totalsModel().setTotal(pdpResponse.totals.grand_total);
if (this.isProductView) {
const pdpResponse = await getExpressMethods().getRequest(element);

setExpressMethods(pdpResponse);
totalsModel().setTotal(pdpResponse.totals.grand_total);
}

this.unmountApplePay();

Expand Down
106 changes: 58 additions & 48 deletions view/frontend/web/js/googlepay/button.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ define([
googlePayAllowed: null,
isProductView: false,
maskedId: null,
googlePayComponent: null,
googlePayTxVariant: null
},

Expand All @@ -75,52 +76,66 @@ define([
'googlePayAllowed'
]);

configModel().setConfig(config);
countriesModel();

this.isProductView = config.isProductView;
let googlePaymentMethod = await this.getGooglePayTxVariant();

// If express methods is not set then set it.
if (this.isProductView) {
const response = await getExpressMethods().getRequest(element);
const cart = customerData.get('cart');

cart.subscribe(function () {
this.reloadGooglePayButton(element);
}.bind(this));
this.initializeOnPDP(config, element);
} else {
let googlePaymentMethod = await getPaymentMethod('googlepay', this.isProductView);

if (!googlePaymentMethod) {
const cart = customerData.get('cart');
cart.subscribe(function () {
this.reloadGooglePayButton(element);
}.bind(this));
} else {
if (!isConfigSet(googlePaymentMethod, ['gatewayMerchantId', 'merchantId'])) {
console.log('Required configuration for Google Pay is missing.');
return;
}
this.initialiseGooglePayComponent(googlePaymentMethod, element);
}
}
},

setExpressMethods(response);
totalsModel().setTotal(response.totals.grand_total);
initializeOnPDP: async function (config, element) {
const response = await getExpressMethods().getRequest(element);
const cart = customerData.get('cart');

const $priceBox = getPdpPriceBox();
const pdpForm = getPdpForm(element);
cart.subscribe(function () {
this.reloadGooglePayButton(element);
}.bind(this));

$priceBox.on('priceUpdated', async function () {
const isValid = new Promise((resolve, reject) => {
return validatePdpForm(resolve, reject, pdpForm, true);
});
setExpressMethods(response);
totalsModel().setTotal(response.totals.grand_total);

isValid
.then(function () {
this.reloadGooglePayButton(element);
}.bind(this))
.catch(function (error) {
console.log(error);
});
}.bind(this));
const $priceBox = getPdpPriceBox();
const pdpForm = getPdpForm(element);

let googlePaymentMethod = await this.getGooglePayTxVariant();
$priceBox.on('priceUpdated', async function () {
const isValid = new Promise((resolve, reject) => {
return validatePdpForm(resolve, reject, pdpForm, true);
});

isValid
.then(function () {
this.reloadGooglePayButton(element);
}.bind(this))
.catch(function (error) {
console.log(error);
});
}.bind(this));

if (!isConfigSet(googlePaymentMethod, ['gatewayMerchantId', 'merchantId'])) {
return;
}
let googlePaymentMethod = await getPaymentMethod('googlepay', this.isProductView);

this.initialiseGooglePayComponent(googlePaymentMethod, element);
} else if (!isConfigSet(googlePaymentMethod, ['gatewayMerchantId', 'merchantId'])) {
if (!isConfigSet(googlePaymentMethod, ['gatewayMerchantId', 'merchantId'])) {
return;
}

configModel().setConfig(config);
countriesModel();

this.initialiseGooglePayComponent(googlePaymentMethod, element);
},

Expand All @@ -136,12 +151,12 @@ define([
});
const googlePayConfig = this.getGooglePayConfig(googlePaymentMethod, element);

this.googlepay = checkoutComponent.create(googlePaymentMethod, googlePayConfig);
this.googlePayComponent = checkoutComponent.create(googlePaymentMethod, googlePayConfig);

this.googlepay.isAvailable()
this.googlePayComponent.isAvailable()
.then(function () {
this.googlePayAllowed(true);
this.googlepay.mount(element);
this.googlePayComponent.mount(element);
}.bind(this))
.catch(function (error) {
console.log(error);
Expand All @@ -150,18 +165,20 @@ define([
},

unmountGooglePay: function () {
if (this.googlepay) {
this.googlepay.unmount();
if (this.googlePayComponent) {
this.googlePayComponent.unmount();
}
},

reloadGooglePayButton: async function (element) {
let googlePaymentMethod = await this.getGooglePayTxVariant();
let googlePaymentMethod = await getPaymentMethod('googlepay', this.isProductView);

const pdpResponse = await getExpressMethods().getRequest(element);
if (this.isProductView) {
const pdpResponse = await getExpressMethods().getRequest(element);

setExpressMethods(pdpResponse);
totalsModel().setTotal(pdpResponse.totals.grand_total);
setExpressMethods(pdpResponse);
totalsModel().setTotal(pdpResponse.totals.grand_total);
}

this.unmountGooglePay();

Expand Down Expand Up @@ -219,13 +236,6 @@ define([
};
},

getGooglePayTxVariant: async function () {
let googlePaymentMethod = await getPaymentMethod('paywithgoogle', this.isProductView) ?? await getPaymentMethod('googlepay', this.isProductView);
this.googlePayTxVariant = googlePaymentMethod;

return googlePaymentMethod;
},

onPaymentDataChanged: function (data) {
return new Promise((resolve, reject) => {
const payload = {
Expand Down
10 changes: 9 additions & 1 deletion view/frontend/web/js/helpers/getPaymentMethod.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,17 @@ define([
}

function findPaymentMethod(paymentMethods, type) {
return paymentMethods.find(function (paymentMethod) {
let found = paymentMethods.find(function (paymentMethod) {
return paymentMethod.type === type;
});

if (!found && 'googlepay' === type) {
found = paymentMethods.find(function (paymentMethod) {
return paymentMethod.type === 'paywithgoogle';
});
}

return found;
}

return customerData.getInitCustomerData()
Expand Down