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

Replace /sessions with /paymentMethods #1055

Merged
merged 7 commits into from
Feb 27, 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
5 changes: 4 additions & 1 deletion jest/globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ global.AdyenCheckout = () => {
return Promise.resolve({
create: () => {
return { mount: jest.fn() };
}
},
createFromAction: () => {
return { mount: jest.fn() };
},
});
};
9 changes: 0 additions & 9 deletions jest/sfccCartridgeMocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,6 @@ jest.mock('*/cartridge/controllers/middlewares/adyen/saveExpressShopperDetails',
return jest.fn();
}, {virtual: true});

jest.mock('*/cartridge/scripts/adyenSessions', () => {
return {
createSession: jest.fn( () => ({
id: 'mock_id',
sessionData: 'mock_session_data',
}))
}
}, {virtual: true});

jest.mock('*/cartridge/scripts/adyenGetPaymentMethods', () => {
return {
getMethods: jest.fn(() => ({
Expand Down
4 changes: 0 additions & 4 deletions jest/sfccPathSetup.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ jest.mock('*/cartridge/controllers/middlewares/adyen/notify', () => {
return require('../src/cartridges/int_adyen_SFRA/cartridge/controllers/middlewares/adyen/notify');
}, {virtual: true});

jest.mock('*/cartridge/controllers/middlewares/adyen/sessions', () => {
return require('../src/cartridges/int_adyen_SFRA/cartridge/controllers/middlewares/adyen/sessions');
}, {virtual: true});

jest.mock('*/cartridge/controllers/middlewares/adyen/paymentFromComponent', () => {
return require('../src/cartridges/int_adyen_SFRA/cartridge/controllers/middlewares/adyen/paymentFromComponent');
}, {virtual: true});
Expand Down
19 changes: 0 additions & 19 deletions metadata/site_import/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,6 @@
<user-id></user-id>
<password/>
</service-credential>
<service-credential service-credential-id="AdyenSessions">
<url>https://checkout-test.adyen.com/checkout/v71/sessions</url>
<user-id></user-id>
<password/>
</service-credential>
<service-credential service-credential-id="AdyenSessionsLive">
<url>https://[YOUR_LIVE_PREFIX]-checkout-live.adyenpayments.com/checkout/v71/sessions</url>
<user-id></user-id>
<password/>
</service-credential>
<service-credential service-credential-id="AdyenPaymentDetailsLive">
<url>https://[YOUR_LIVE_PREFIX]-checkout-live.adyenpayments.com/checkout/v71/payments/details</url>
<user-id></user-id>
Expand Down Expand Up @@ -156,15 +146,6 @@
<profile-id>Adyen</profile-id>
<credential-id>AdyenCheckoutPaymentMethods</credential-id>
</service>
<service service-id="AdyenSessions">
<service-type>HTTP</service-type>
<enabled>true</enabled>
<log-prefix>adyen</log-prefix>
<comm-log-enabled>true</comm-log-enabled>
<mock-mode-enabled>false</mock-mode-enabled>
<profile-id>Adyen</profile-id>
<credential-id>AdyenSessions</credential-id>
</service>
<service service-id="AdyenRecurringDisable">
<service-type>HTTP</service-type>
<enabled>true</enabled>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { onFieldValid, onBrand, createSession } = require('./commons/index');
const { onFieldValid, onBrand } = require('./commons/index');
const store = require('../../../store');

let checkout;
Expand Down Expand Up @@ -53,12 +53,6 @@ store.checkoutConfiguration.onAdditionalDetails = (state) => {
};

async function initializeCardComponent() {
// card and checkout component creation
const session = await createSession();
store.checkoutConfiguration.session = {
id: session.id,
sessionData: session.sessionData,
};
const cardNode = document.getElementById('card');
checkout = await AdyenCheckout(store.checkoutConfiguration);
card = checkout.create('card').mount(cardNode);
Expand Down Expand Up @@ -102,3 +96,8 @@ $('button[value="add-new-payment"]').on('click', (event) => {
card?.showValidation();
}
});

module.exports = {
initializeCardComponent,
submitAddCard,
};
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
@@ -0,0 +1,86 @@
/**
* @jest-environment jsdom
*/

const store = require('../../../../../store');
const {initializeCardComponent, submitAddCard} = require('../../adyenAccount')
$.fn.modal = jest.fn();

jest.mock('../../commons');
jest.mock('../../../../../store');
let checkout;
// Mocking external dependencies
jest.mock('../../../../../store', () => ({
checkoutConfiguration: {
amount: { value: 0, currency: 'EUR' },
paymentMethodsConfiguration: {
card: {
onChange: jest.fn(),
},
},
onAdditionalDetails: jest.fn(),
},
}));

describe('submitAddCard', () => {
beforeEach(() => {
store.checkoutConfiguration = {};

jest.clearAllMocks();
});

it('initialize card component', async () => {
document.body.innerHTML = `<div id="card"></div>`;
await initializeCardComponent();
expect(document.getElementById('card')).toBeDefined();
});

it('should send form data via ajax on submitAddCard call', () => {
const fakeResponse = {redirectAction : 'test'};
document.body.innerHTML = `<form id="payment-form" action="/fake-action">
<input type="text" name="fake" value="fake" />
</form>`;
$.ajax = jest.fn(({ success }) => {
success(fakeResponse);
return { fail: jest.fn() };
});
submitAddCard();
expect($.ajax).toHaveBeenCalledWith({
type: 'POST',
url: '/fake-action',
data: 'fake=fake',
async: false,
success: expect.any(Function),
});
});

it('should handle redirection action after successful form submission', () => {
const fakeRedirectAction = { type: 'redirect' };
document.body.innerHTML = `<form id="payment-form" action="/fake-action">
<input type="text" name="fake" value="fake" />
</form>`;
$.ajax = jest.fn(({ success }) => {
success({ redirectAction: fakeRedirectAction });
return { fail: jest.fn() };
});
window.location.href = '';
submitAddCard();
expect(window.location.href).toBe('http://localhost/');
});

it('should handle errors returned from the server during form submission', () => {
let formErrorsExist = false
const fakeErrorResponse = { error: 'Something went wrong' };
$.ajax = jest.fn(({ success }) => {
success(fakeErrorResponse);
return { fail: jest.fn() };
});
submitAddCard();
setTimeout(() => {
expect(formErrorsExist).toBeTruthy();
done();
}); // Timeout needed for completition of the test
});

});

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
Loading
Loading