Skip to content

Commit

Permalink
fix: replaced /sessions with /paymentMethods
Browse files Browse the repository at this point in the history
  • Loading branch information
zenit2001 committed Feb 13, 2024
1 parent b1611c5 commit 8a1eab8
Show file tree
Hide file tree
Showing 13 changed files with 83 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

exports[`Render Generic Component should hide giftcard container 1`] = `
{
"amount": "mocked_amount",
"amount": {
"currency": "mocked_currency",
"value": "mocked_amount",
},
"countryCode": "mocked_countrycode",
"paymentMethodsConfiguration": {
"amazonpay": {
Expand All @@ -18,18 +21,18 @@ exports[`Render Generic Component should hide giftcard container 1`] = `
"configuration": undefined,
},
},
"session": {
"adyenDescriptions": {},
"id": "mock_id",
"paymentMethodsResponse": {
"imagePath": "example.com",
"sessionData": "mock_session_data",
},
}
`;

exports[`Render Generic Component should render 1`] = `
{
"amount": "mocked_amount",
"amount": {
"currency": "mocked_currency",
"value": "mocked_amount",
},
"countryCode": "mocked_countrycode",
"paymentMethodsConfiguration": {
"amazonpay": {
Expand All @@ -45,11 +48,8 @@ exports[`Render Generic Component should render 1`] = `
"configuration": undefined,
},
},
"session": {
"adyenDescriptions": {},
"id": "mock_id",
"paymentMethodsResponse": {
"imagePath": "example.com",
"sessionData": "mock_session_data",
},
}
`;
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jest.mock('../../commons');
jest.mock('../../../../../store');

const { renderGenericComponent, setInstallments, renderPosTerminals, isCartModified, renderGiftCardLogo, setGiftCardContainerVisibility, applyGiftCards } = require('../renderGenericComponent');
const { createSession } = require('../../commons');
const { getPaymentMethods } = require('../../commons');
const { fetchGiftCards } = require('../../commons');
const store = require('../../../../../store');
const giftCardHtml = `
Expand Down Expand Up @@ -59,19 +59,17 @@ beforeEach(() => {
storedPaymentMethods: [{ supportedShopperInteractions: ['Ecommerce'] }],
paymentMethods: [{ type: 'amazonpay' }],
},
options: {
amount: 'mocked_amount',
countryCode: 'mocked_countrycode',
},
}));
window.installments = '[[0,2,["amex","hipercard"]]]';
store.checkout = {
options: {}
};
createSession.mockReturnValue({
store.checkoutConfiguration = {
amount: {value : 'mocked_amount', currency : 'mocked_currency'},
countryCode: 'mocked_countrycode',
};
getPaymentMethods.mockReturnValue({
adyenConnectedTerminals: { uniqueTerminalIds: ['mocked_id'] },
id: 'mock_id',
sessionData: 'mock_session_data',
imagePath: 'example.com',
adyenDescriptions: {},
});
Expand All @@ -83,7 +81,7 @@ describe('Render Generic Component', () => {
store.componentsObj = { foo: 'bar', bar: 'baz' };
store.checkoutConfiguration.paymentMethodsConfiguration = { amazonpay: {} };
await renderGenericComponent();
expect(createSession).toBeCalled();
expect(getPaymentMethods).toBeCalled();
expect(store.checkoutConfiguration).toMatchSnapshot();
expect(
document.querySelector('input[type=radio][name=brandCode]').value,
Expand All @@ -96,7 +94,7 @@ describe('Render Generic Component', () => {
store.componentsObj = { foo: 'bar', bar: 'baz' };
store.checkoutConfiguration.paymentMethodsConfiguration = { amazonpay: {} };
await renderGenericComponent();
expect(createSession).toBeCalled();
expect(getPaymentMethods).toBeCalled();
expect(store.checkoutConfiguration).toMatchSnapshot();
expect(
document.querySelector('.gift-card-selection').style.display,
Expand Down Expand Up @@ -171,8 +169,8 @@ describe('Render Generic Component', () => {
});

it('handles errors in initializeCheckout', async () => {
createSession.mockRejectedValue(new Error('Session creation failed'));
await expect(renderGenericComponent()).rejects.toThrow('Session creation failed');
getPaymentMethods.mockRejectedValue(new Error('Payments method call failed'));
await expect(renderGenericComponent()).rejects.toThrow('Payments method call failed');
});

it('correctly sets Pos Terminals', () => {
Expand Down Expand Up @@ -276,7 +274,7 @@ describe('Render Generic Component', () => {
const renderGiftCardComponent = require('*/cartridge/client/default/js/adyen_checkout/renderGiftcardComponent');
store.checkoutConfiguration = {
amount: { currency: 'USD', value: 50 },
session: {
paymentMethodsResponse: {
imagePath: 'test_image_path',
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ beforeEach(() => {
}));
window.Configuration = { amount: 0 };
store.checkoutConfiguration = {
session: {
paymentMethodsResponse: {
imagePath: 'test_image_path',
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function getCardConfig() {
store.isValid = state.isValid;
const method = state.data.paymentMethod.storedPaymentMethodId
? `storedCard${state.data.paymentMethod.storedPaymentMethodId}`
: store.selectedMethod;
: constants.SCHEME;
store.updateSelectedPayment(method, 'isValid', store.isValid);
store.updateSelectedPayment(method, 'stateData', state.data);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const store = require('../../../../store');
const { renderPaymentMethod } = require('./renderPaymentMethod');
const helpers = require('./helpers');
const { installmentLocales } = require('./localesUsingInstallments');
const { createSession, fetchGiftCards } = require('../commons');
const { getPaymentMethods, fetchGiftCards } = require('../commons');
const constants = require('../constants');
const {
createElementsToShowRemainingGiftCardAmount,
Expand Down Expand Up @@ -217,27 +217,25 @@ function setGiftCardContainerVisibility() {
}

async function initializeCheckout() {
const session = await createSession();
const paymentMethodsResponse = await getPaymentMethods();
const giftCardsData = await fetchGiftCards();

store.checkoutConfiguration.session = {
id: session.id,
sessionData: session.sessionData,
imagePath: session.imagePath,
adyenDescriptions: session.adyenDescriptions,
setCheckoutConfiguration(paymentMethodsResponse);

store.checkoutConfiguration.paymentMethodsResponse = {
...paymentMethodsResponse.AdyenPaymentMethods,
imagePath: paymentMethodsResponse.imagePath,
};
store.checkout = await AdyenCheckout(store.checkoutConfiguration);
setGiftCardContainerVisibility();

const { totalDiscountedAmount, giftCards } = giftCardsData;
if (giftCards?.length) {
store.addedGiftCards = giftCards;
const lastGiftCard = store.addedGiftCards[store.addedGiftCards.length - 1];
store.partialPaymentsOrderObj = { ...lastGiftCard, totalDiscountedAmount };
}

setCheckoutConfiguration(store.checkout.options);
setInstallments(store.checkout.options.amount);
setInstallments(paymentMethodsResponse.amount);
setAmazonPayConfig(store.checkout.paymentMethodsResponse);
document.querySelector('#paymentMethodsList').innerHTML = '';

Expand All @@ -251,20 +249,23 @@ async function initializeCheckout() {
(pm) => pm.type !== constants.GIFTCARD,
);

renderStoredPaymentMethods(
storedPaymentMethodsWithoutGiftCards,
session.imagePath,
);
// Rendering stored payment methods if one-click is enabled in BM
if (window.adyenRecurringPaymentsEnabled) {
renderStoredPaymentMethods(
storedPaymentMethodsWithoutGiftCards,
paymentMethodsResponse.imagePath,
);
}

await renderPaymentMethods(
paymentMethodsWithoutGiftCards,
session.imagePath,
session.adyenDescriptions,
paymentMethodsResponse.imagePath,
paymentMethodsResponse.adyenDescriptions,
);

renderPosTerminals(session.adyenConnectedTerminals);
renderPosTerminals(paymentMethodsResponse.adyenConnectedTerminals);

renderGiftCardLogo(session.imagePath);
renderGiftCardLogo(paymentMethodsResponse.imagePath);

const firstPaymentMethod = document.querySelector(
'input[type=radio][name=brandCode]',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ function renderGiftCardSelectForm() {
return;
}

const { imagePath } = store.checkoutConfiguration.session;
const { imagePath } = store.checkoutConfiguration.paymentMethodsResponse;

giftCardBrands().forEach((giftCard) => {
const newListItem = document.createElement('li');
Expand Down Expand Up @@ -277,7 +277,7 @@ function clearGiftCardsContainer() {

function renderAddedGiftCard(card) {
const giftCardData = card.giftCard;
const { imagePath } = store.checkoutConfiguration.session;
const { imagePath } = store.checkoutConfiguration.paymentMethodsResponse;

const { giftCardsList, giftCardAddButton } = getGiftCardElements();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async function mountAmazonPayComponent() {
locale: window.locale,
});

const amazonPayConfig = paymentMethodsResponse.find(
const amazonPayConfig = paymentMethodsResponse?.paymentMethods.find(
(pm) => pm.type === AMAZON_PAY,
)?.configuration;
if (!amazonPayConfig) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ async function mountAmazonPayComponent() {
try {
const data = await getPaymentMethods();
const paymentMethodsResponse = data.AdyenPaymentMethods;
const amazonPayConfig = paymentMethodsResponse.find(
const amazonPayConfig = paymentMethodsResponse?.paymentMethods.find(
(pm) => pm.type === 'amazonpay',
)?.configuration;
if (!amazonPayConfig) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ function callPaymentFromComponent(data, resolveApplePay, rejectApplePay) {
initializeCheckout()
.then(async () => {
const applePayPaymentMethod =
paymentMethodsResponse?.AdyenPaymentMethods.find(
paymentMethodsResponse?.AdyenPaymentMethods?.paymentMethods.find(
(pm) => pm.type === APPLE_PAY,
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module.exports = {
NOTENOUGHBALANCE: 'NotEnoughBalance',
SUCCESS: 'Success',
GIFTCARD: 'giftcard',
SCHEME: 'scheme',
GIROPAY: 'giropay',
APPLE_PAY: 'applepay',
ACTIONTYPE: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,17 @@ describe('getCheckoutPaymentMethods', () => {
it('returns AdyenPaymentMethods', () => {
getCheckoutPaymentMethods(req, res, next);
expect(res.json).toHaveBeenCalledWith({
AdyenPaymentMethods: [{
type: 'visa'
}, ],
AdyenPaymentMethods: {
paymentMethods: [
{
"type": "visa",
},
],
},
amount: {
currency: "EUR",
value: 1000,
},
adyenConnectedTerminals: {
"foo": "bar",
},
Expand All @@ -35,6 +43,7 @@ describe('getCheckoutPaymentMethods', () => {
"paypal": "PayPal example description",
},
imagePath: "mocked_loading_contextimages/logos/medium/",
countryCode: "NL",
});
expect(next).toHaveBeenCalled();
});
Expand All @@ -47,6 +56,10 @@ describe('getCheckoutPaymentMethods', () => {
getCheckoutPaymentMethods(req, res, next);
expect(res.json).toHaveBeenCalledWith({
AdyenPaymentMethods: [],
amount: {
currency: "EUR",
value: 1000,
},
adyenConnectedTerminals: {
"foo": "bar",
},
Expand All @@ -55,6 +68,7 @@ describe('getCheckoutPaymentMethods', () => {
"paypal": "PayPal example description",
},
imagePath: "mocked_loading_contextimages/logos/medium/",
countryCode: "NL",
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,34 @@ function getConnectedTerminals() {

function getCheckoutPaymentMethods(req, res, next) {
const currentBasket = BasketMgr.getCurrentBasket();
let countryCode = getCountryCode(currentBasket, req.locale);
const adyenURL = `${AdyenHelper.getLoadingContext()}images/logos/medium/`;
const connectedTerminals = getConnectedTerminals();
if (
const countryCode =
currentBasket.getShipments().length > 0 &&
currentBasket.getShipments()[0].shippingAddress
) {
countryCode = currentBasket
.getShipments()[0]
.shippingAddress.getCountryCode();
}
? currentBasket.getShipments()[0].shippingAddress.getCountryCode().value
: getCountryCode(currentBasket, req.locale).value;
const adyenURL = `${AdyenHelper.getLoadingContext()}images/logos/medium/`;
const connectedTerminals = JSON.parse(getConnectedTerminals());
const currency = currentBasket.getTotalGrossPrice().currencyCode;
const paymentAmount = currentBasket.getTotalGrossPrice().isAvailable()
? AdyenHelper.getCurrencyValueForApi(currentBasket.getTotalGrossPrice())
: new dw.value.Money(1000, currency);
let paymentMethods;
try {
paymentMethods = getPaymentMethods.getMethods(
BasketMgr.getCurrentBasket(),
countryCode.value?.toString() || countryCode.value,
).paymentMethods;
currentBasket,
AdyenHelper.getCustomer(req.currentCustomer),
countryCode,
);
} catch (err) {
paymentMethods = [];
}

res.json({
AdyenPaymentMethods: paymentMethods,
imagePath: adyenURL,
adyenDescriptions: paymentMethodDescriptions,
adyenConnectedTerminals: JSON.parse(connectedTerminals),
adyenConnectedTerminals: connectedTerminals,
amount: { value: paymentAmount.value, currency },
countryCode,
});
return next();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
window.ShowConfirmationPaymentFromComponent = "${ShowConfirmationPaymentFromComponent}";
window.AdyenSFRA6Enabled = '${pdict.adyen.SFRA6Enabled}';
window.klarnaWidgetEnabled = ${adyenKlarnaWidgetEnabled};
window.sessionsUrl = "${URLUtils.https('Adyen-Sessions')}";
window.adyenRecurringPaymentsEnabled = ${adyenRecurringPaymentsEnabled};
window.getPaymentMethodsURL = "${URLUtils.https('Adyen-GetPaymentMethods')}";
window.checkBalanceUrl = "${URLUtils.https('Adyen-CheckBalance')}";
window.partialPaymentsOrderUrl = "${URLUtils.https('Adyen-PartialPaymentsOrder')}";
window.partialPaymentUrl = "${URLUtils.https('Adyen-partialPayment')}";
Expand Down

0 comments on commit 8a1eab8

Please sign in to comment.