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

Adyen endpoints optimization #1153

Merged
merged 3 commits into from
Aug 28, 2024
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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "int_adyen_SFRA",
"version": "23.3.1",
"version": "23.3.2",
"description": "Adyen's official cartridge for SFRA and controllers-based SiteGenesis",
"main": "index.js",
"paths": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
const helpers = require('./adyen_checkout/helpers');
const { checkIfExpressMethodsAreReady } = require('./commons/index');
const { updateLoadedExpressMethods } = require('./commons');
const {
checkIfExpressMethodsAreReady,
createSession,
updateLoadedExpressMethods,
} = require('./commons');
const { APPLE_PAY } = require('./constants');

let checkout;
let shippingMethodsData;

async function initializeCheckout() {
const session = await fetch(window.sessionsUrl);
const sessionData = await session.json();
const sessionData = await createSession();

const shippingMethods = await fetch(window.shippingMethodsUrl);
shippingMethodsData = await shippingMethods.json();
Expand Down Expand Up @@ -120,182 +122,184 @@ function callPaymentFromComponent(data, resolveApplePay, rejectApplePay) {
rejectApplePay();
});
}
$(document).ready(() => {
initializeCheckout()
.then(async () => {
const applePayPaymentMethod =
checkout.paymentMethodsResponse.paymentMethods.find(
(pm) => pm.type === APPLE_PAY,
);

initializeCheckout()
.then(async () => {
const applePayPaymentMethod =
checkout.paymentMethodsResponse.paymentMethods.find(
(pm) => pm.type === APPLE_PAY,
);
if (!applePayPaymentMethod) {
updateLoadedExpressMethods(APPLE_PAY);
checkIfExpressMethodsAreReady();
return;
}

if (!applePayPaymentMethod) {
updateLoadedExpressMethods(APPLE_PAY);
checkIfExpressMethodsAreReady();
return;
}
const applePayConfig = applePayPaymentMethod.configuration;

const applePayConfig = applePayPaymentMethod.configuration;
const applePayButtonConfig = {
showPayButton: true,
configuration: applePayConfig,
amount: checkout.options.amount,
requiredShippingContactFields: ['postalAddress', 'email', 'phone'],
requiredBillingContactFields: ['postalAddress', 'phone'],
shippingMethods: shippingMethodsData.shippingMethods.map((sm) => ({
label: sm.displayName,
detail: sm.description,
identifier: sm.ID,
amount: `${sm.shippingCost.value}`,
})),
onAuthorized: async (resolve, reject, event) => {
try {
const customerData = event.payment.shippingContact;
const billingData = event.payment.billingContact;
const customer = formatCustomerObject(customerData, billingData);
const stateData = {
paymentMethod: {
type: APPLE_PAY,
applePayToken: event.payment.token.paymentData,
},
paymentType: 'express',
};

const applePayButtonConfig = {
showPayButton: true,
configuration: applePayConfig,
amount: checkout.options.amount,
requiredShippingContactFields: ['postalAddress', 'email', 'phone'],
requiredBillingContactFields: ['postalAddress', 'phone'],
shippingMethods: shippingMethodsData.shippingMethods.map((sm) => ({
label: sm.displayName,
detail: sm.description,
identifier: sm.ID,
amount: `${sm.shippingCost.value}`,
})),
onAuthorized: async (resolve, reject, event) => {
try {
const customerData = event.payment.shippingContact;
const billingData = event.payment.billingContact;
const customer = formatCustomerObject(customerData, billingData);
const stateData = {
paymentMethod: {
type: APPLE_PAY,
applePayToken: event.payment.token.paymentData,
},
paymentType: 'express',
};
const resolveApplePay = () => {
// ** is used instead of Math.pow
const value =
applePayButtonConfig.amount.value *
10 ** parseInt(window.digitsNumber, 10);
const finalPriceUpdate = {
newTotal: {
type: 'final',
label: applePayConfig.merchantName,
amount: `${Math.round(value)}`,
},
};
resolve(finalPriceUpdate);
};

const resolveApplePay = () => {
// ** is used instead of Math.pow
const value =
applePayButtonConfig.amount.value *
10 ** parseInt(window.digitsNumber, 10);
const finalPriceUpdate = {
await callPaymentFromComponent(
{ ...stateData, customer },
resolveApplePay,
reject,
);
} catch (error) {
reject(error);
}
},
onSubmit: () => {
// This handler is empty to prevent sending a second payment request
// We already do the payment in paymentFromComponent
},
onShippingMethodSelected: async (resolve, reject, event) => {
const { shippingMethod } = event;
const matchingShippingMethod =
shippingMethodsData.shippingMethods.find(
(sm) => sm.ID === shippingMethod.identifier,
);
const calculationResponse = await fetch(
`${window.calculateAmountUrl}?${new URLSearchParams({
shipmentUUID: matchingShippingMethod.shipmentUUID,
methodID: matchingShippingMethod.ID,
})}`,
{
method: 'POST',
},
);
if (calculationResponse.ok) {
const newCalculation = await calculationResponse.json();
applePayButtonConfig.amount = {
value: newCalculation.grandTotalAmount.value,
currency: newCalculation.grandTotalAmount.currency,
};
const applePayShippingMethodUpdate = {
newTotal: {
type: 'final',
label: applePayConfig.merchantName,
amount: `${Math.round(value)}`,
amount: newCalculation.grandTotalAmount.value,
},
};
resolve(finalPriceUpdate);
};

await callPaymentFromComponent(
{ ...stateData, customer },
resolveApplePay,
reject,
resolve(applePayShippingMethodUpdate);
} else {
reject();
}
},
onShippingContactSelected: async (resolve, reject, event) => {
const { shippingContact } = event;
const shippingMethods = await fetch(
`${window.shippingMethodsUrl}?${new URLSearchParams({
city: shippingContact.locality,
country: shippingContact.country,
countryCode: shippingContact.countryCode,
stateCode: shippingContact.administrativeArea,
})}`,
);
} catch (error) {
reject(error);
}
},
onSubmit: () => {
// This handler is empty to prevent sending a second payment request
// We already do the payment in paymentFromComponent
},
onShippingMethodSelected: async (resolve, reject, event) => {
const { shippingMethod } = event;
const matchingShippingMethod = shippingMethodsData.shippingMethods.find(
(sm) => sm.ID === shippingMethod.identifier,
);
const calculationResponse = await fetch(
`${window.calculateAmountUrl}?${new URLSearchParams({
shipmentUUID: matchingShippingMethod.shipmentUUID,
methodID: matchingShippingMethod.ID,
})}`,
{
method: 'POST',
},
);
if (calculationResponse.ok) {
const newCalculation = await calculationResponse.json();
applePayButtonConfig.amount = {
value: newCalculation.grandTotalAmount.value,
currency: newCalculation.grandTotalAmount.currency,
};
const applePayShippingMethodUpdate = {
newTotal: {
type: 'final',
label: applePayConfig.merchantName,
amount: newCalculation.grandTotalAmount.value,
},
};
resolve(applePayShippingMethodUpdate);
} else {
reject();
}
},
onShippingContactSelected: async (resolve, reject, event) => {
const { shippingContact } = event;
const shippingMethods = await fetch(
`${window.shippingMethodsUrl}?${new URLSearchParams({
city: shippingContact.locality,
country: shippingContact.country,
countryCode: shippingContact.countryCode,
stateCode: shippingContact.administrativeArea,
})}`,
);
if (shippingMethods.ok) {
shippingMethodsData = await shippingMethods.json();
if (shippingMethodsData.shippingMethods?.length) {
const selectedShippingMethod =
shippingMethodsData.shippingMethods[0];
const calculationResponse = await fetch(
`${window.calculateAmountUrl}?${new URLSearchParams({
shipmentUUID: selectedShippingMethod.shipmentUUID,
methodID: selectedShippingMethod.ID,
})}`,
{
method: 'POST',
},
);
if (calculationResponse.ok) {
const shippingMethodsStructured =
shippingMethodsData.shippingMethods.map((sm) => ({
label: sm.displayName,
detail: sm.description,
identifier: sm.ID,
amount: `${sm.shippingCost.value}`,
}));
const newCalculation = await calculationResponse.json();
const applePayShippingContactUpdate = {
newShippingMethods: shippingMethodsStructured,
newTotal: {
type: 'final',
label: applePayConfig.merchantName,
amount: newCalculation.grandTotalAmount.value,
if (shippingMethods.ok) {
shippingMethodsData = await shippingMethods.json();
if (shippingMethodsData.shippingMethods?.length) {
const selectedShippingMethod =
shippingMethodsData.shippingMethods[0];
const calculationResponse = await fetch(
`${window.calculateAmountUrl}?${new URLSearchParams({
shipmentUUID: selectedShippingMethod.shipmentUUID,
methodID: selectedShippingMethod.ID,
})}`,
{
method: 'POST',
},
};
resolve(applePayShippingContactUpdate);
);
if (calculationResponse.ok) {
const shippingMethodsStructured =
shippingMethodsData.shippingMethods.map((sm) => ({
label: sm.displayName,
detail: sm.description,
identifier: sm.ID,
amount: `${sm.shippingCost.value}`,
}));
const newCalculation = await calculationResponse.json();
const applePayShippingContactUpdate = {
newShippingMethods: shippingMethodsStructured,
newTotal: {
type: 'final',
label: applePayConfig.merchantName,
amount: newCalculation.grandTotalAmount.value,
},
};
resolve(applePayShippingContactUpdate);
} else {
reject();
}
} else {
reject();
}
} else {
reject();
}
} else {
reject();
}
},
};
},
};

const cartContainer = document.getElementsByClassName(APPLE_PAY);
const applePayButton = await createApplePayButton(applePayButtonConfig);
const isApplePayButtonAvailable = await applePayButton.isAvailable();
const cartContainer = document.getElementsByClassName(APPLE_PAY);
const applePayButton = await createApplePayButton(applePayButtonConfig);
const isApplePayButtonAvailable = await applePayButton.isAvailable();

if (isApplePayButtonAvailable) {
for (
let expressCheckoutNodesIndex = 0;
expressCheckoutNodesIndex < cartContainer.length;
expressCheckoutNodesIndex += 1
) {
applePayButton.mount(cartContainer[expressCheckoutNodesIndex]);
if (isApplePayButtonAvailable) {
for (
let expressCheckoutNodesIndex = 0;
expressCheckoutNodesIndex < cartContainer.length;
expressCheckoutNodesIndex += 1
) {
applePayButton.mount(cartContainer[expressCheckoutNodesIndex]);
}
}
}

updateLoadedExpressMethods(APPLE_PAY);
checkIfExpressMethodsAreReady();
})
.catch(() => {
updateLoadedExpressMethods(APPLE_PAY);
checkIfExpressMethodsAreReady();
});
updateLoadedExpressMethods(APPLE_PAY);
checkIfExpressMethodsAreReady();
})
.catch(() => {
updateLoadedExpressMethods(APPLE_PAY);
checkIfExpressMethodsAreReady();
});
});

module.exports = {
handleAuthorised,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ module.exports.onBrand = function onBrand(brandObject) {
module.exports.createSession = async function createSession() {
return $.ajax({
url: window.sessionsUrl,
type: 'get',
type: 'post',
data: $('#adyen-sessions-token').serialize(),
});
};

Expand Down
Loading
Loading