Skip to content

Commit

Permalink
Adyen endpoints optimization (#1156)
Browse files Browse the repository at this point in the history
* chore(SFI-914): adyen endpoints optimization
  • Loading branch information
shanikantsingh authored Sep 2, 2024
1 parent 636458c commit 745cb2d
Show file tree
Hide file tree
Showing 10 changed files with 195 additions and 166 deletions.
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": "24.1.0",
"version": "24.1.1",
"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 @@ -123,181 +125,184 @@ function callPaymentFromComponent(data, resolveApplePay, rejectApplePay) {
});
}

initializeCheckout()
.then(async () => {
const applePayPaymentMethod =
checkout.paymentMethodsResponse.paymentMethods.find(
(pm) => pm.type === APPLE_PAY,
);

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

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

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 applePayConfig = applePayPaymentMethod.configuration;

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)}`,
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',
};
resolve(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: 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,
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: newCalculation.grandTotalAmount.value,
amount: `${Math.round(value)}`,
},
};
resolve(applePayShippingContactUpdate);
resolve(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: 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,
},
};
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

0 comments on commit 745cb2d

Please sign in to comment.