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

Adding Google Pay on PDP #1198

Merged
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
Original file line number Diff line number Diff line change
@@ -1,61 +1,3 @@
const {
checkIfExpressMethodsAreReady,
updateLoadedExpressMethods,
getPaymentMethods,
} = require('./commons');
const { GOOGLE_PAY } = require('./constants');
const googlePayExpressModule = require('./googlePayExpressCommon');

let checkout;
let paymentMethodsResponse;

async function initializeCheckout() {
const paymentMethods = await getPaymentMethods();
paymentMethodsResponse = await paymentMethods.json();
const applicationInfo = paymentMethodsResponse?.applicationInfo;
checkout = await AdyenCheckout({
environment: window.environment,
clientKey: window.clientKey,
locale: window.locale,
analytics: {
analyticsData: { applicationInfo },
},
});
}

async function init() {
initializeCheckout()
.then(async () => {
const googlePayPaymentMethod =
paymentMethodsResponse?.AdyenPaymentMethods?.paymentMethods.find(
(pm) => pm.type === GOOGLE_PAY,
);
if (!googlePayPaymentMethod) {
updateLoadedExpressMethods(GOOGLE_PAY);
checkIfExpressMethodsAreReady();
return;
}

const googlePayConfig = googlePayPaymentMethod.configuration;
const googlePayButtonConfig = {
showPayButton: true,
buttonType: 'buy',
configuration: googlePayConfig,
returnUrl: window.returnUrl,
isExpress: true,
};

const googlePayButton = checkout.create(
GOOGLE_PAY,
googlePayButtonConfig,
);
googlePayButton.mount('#googlepay-container');
updateLoadedExpressMethods(GOOGLE_PAY);
checkIfExpressMethodsAreReady();
})
.catch(() => {
updateLoadedExpressMethods(GOOGLE_PAY);
checkIfExpressMethodsAreReady();
});
}

init();
googlePayExpressModule.init();
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const {
checkIfExpressMethodsAreReady,
updateLoadedExpressMethods,
getPaymentMethods,
} = require('./commons');
const { GOOGLE_PAY } = require('./constants');

let checkout;
let paymentMethodsResponse;

async function initializeCheckout() {
const paymentMethods = await getPaymentMethods();
paymentMethodsResponse = await paymentMethods.json();
const applicationInfo = paymentMethodsResponse?.applicationInfo;
checkout = await AdyenCheckout({
environment: window.environment,
clientKey: window.clientKey,
locale: window.locale,
analytics: {
analyticsData: { applicationInfo },
},
});
}

async function init() {
initializeCheckout()
.then(async () => {
const googlePayPaymentMethod =
paymentMethodsResponse?.AdyenPaymentMethods?.paymentMethods.find(
(pm) => pm.type === GOOGLE_PAY,
);
if (!googlePayPaymentMethod) {
updateLoadedExpressMethods(GOOGLE_PAY);
checkIfExpressMethodsAreReady();
return;
}

const googlePayConfig = googlePayPaymentMethod.configuration;
const googlePayButtonConfig = {
showPayButton: true,
buttonType: 'buy',
configuration: googlePayConfig,
returnUrl: window.returnUrl,
isExpress: true,
};

const googlePayButton = checkout.create(
GOOGLE_PAY,
googlePayButtonConfig,
);
googlePayButton.mount('.googlepay');
updateLoadedExpressMethods(GOOGLE_PAY);
checkIfExpressMethodsAreReady();
})
.catch(() => {
updateLoadedExpressMethods(GOOGLE_PAY);
checkIfExpressMethodsAreReady();
});
}

module.exports = {
init,
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const applePayExpressModule = require('../applePayExpressCommon');
const { APPLE_PAY } = require('../constants');
const googlePayExpressModule = require('../googlePayExpressCommon');
const { APPLE_PAY, GOOGLE_PAY } = require('../constants');

function getProductForm(product) {
const $productInputEl = document.createElement('input');
Expand All @@ -24,6 +25,7 @@ function getValueForCurrency(amount, currency) {
function getExpressPaymentButtons(product) {
const expressMethodsConfig = {
[APPLE_PAY]: window.isApplePayExpressOnPdpEnabled === 'true',
[GOOGLE_PAY]: window.isGooglePayExpressOnPdpEnabled === 'true',
};
const enabledExpressPaymentButtons = [];
Object.keys(expressMethodsConfig).forEach((key) => {
Expand All @@ -43,6 +45,10 @@ function renderApplePayButton() {
applePayExpressModule.init();
}

function renderGooglePayButton() {
googlePayExpressModule.init();
}

function renderExpressPaymentButtons() {
$('body').on('product:renderExpressPaymentButtons', (e, response) => {
const { product = {} } = response;
Expand All @@ -61,6 +67,7 @@ function renderExpressPaymentButtons() {
$productForm,
);
renderApplePayButton();
renderGooglePayButton();
} else {
$expressPaymentButtonsContainer.replaceChildren();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
<div data-method="applepay" class="expressComponent applepay" style="padding:0"></div>
<div data-method="amazonpay" class="expressComponent" id="amazonpay-container"></div>
<div data-method="paypal" class="expressComponent" id="paypal-container" style="padding:0"></div>
<div data-method="googlepay" class="expressComponent" id="googlepay-container" style="padding:0"></div>
<div data-method="googlepay" class="expressComponent googlepay" style="padding:0"></div>
</div>
<script src="${URLUtils.httpsStatic('/js/expressPaymentMethodsVisibility.js')}" type="text/javascript"></script>
</isif>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
window.createTemporaryBasketUrl = "${URLUtils.https('Adyen-CreateTemporaryBasket')}";
window.isExpressPdp = true;
window.isApplePayExpressOnPdpEnabled = "${AdyenConfigs.isApplePayExpressOnPdpEnabled()}";
window.isGooglePayExpressOnPdpEnabled = "${AdyenConfigs.isGooglePayExpressOnPdpEnabled()}";
window.fractionDigits = "${AdyenHelper.getFractionDigits()}"
</script>

Expand Down
Loading