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

Fix/v6 mandatory country code with redirects #2558

Merged
merged 5 commits into from
Feb 19, 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
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const createElements = (

if (!PaymentMethodElement) {
console.warn(
`Dropin: You support the payment method '${
`\nDropin: You support the payment method '${
paymentMethod.type
}' but this component has not been configured. Make sure to import the Class '${getComponentNameOfPaymentType(
paymentMethod.type
Expand Down
64 changes: 37 additions & 27 deletions packages/lib/src/core/core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,25 @@ import { Dropin, Ideal } from '../components';
import { es_ES } from '../language/locales';
import { CheckoutSessionSetupResponse } from './CheckoutSession/types';

jest.spyOn(Session.prototype, 'setupSession').mockImplementation(() => {
const sessionSetupResponseMock: CheckoutSessionSetupResponse = {
id: 'session-id',
sessionData: 'session-data',
amount: {
value: 1000,
currency: 'USD'
},
expiresAt: '',
paymentMethods: {
paymentMethods: [{ name: 'Ideal', type: 'ideal' }],
storedPaymentMethods: []
},
returnUrl: '',
configuration: {},
shopperLocale: 'en-US'
};
const sessionSetupResponseMock: CheckoutSessionSetupResponse = {
id: 'session-id',
sessionData: 'session-data',
amount: {
value: 1000,
currency: 'USD'
},
expiresAt: '',
paymentMethods: {
paymentMethods: [{ name: 'Ideal', type: 'ideal' }],
storedPaymentMethods: []
},
returnUrl: '',
configuration: {},
shopperLocale: 'en-US',
countryCode: 'US'
};

jest.spyOn(Session.prototype, 'setupSession').mockImplementation(() => {
return Promise.resolve(sessionSetupResponseMock);
});

Expand Down Expand Up @@ -58,7 +59,7 @@ describe('Core', () => {
expect(Object.keys(checkout.modules).length).toBe(5);
});

test('should create the modules when initializing on Sesssions flow', async () => {
test('should create the modules when initializing on Sessions flow', async () => {
const checkout = new AdyenCheckout({
countryCode: 'US',
environment: 'test',
Expand Down Expand Up @@ -305,17 +306,26 @@ describe('Core', () => {
}).toThrow('You must specify a countryCode when initializing checkout');
});

const onError = jest.fn(() => {});

test('SessionsFlow, without a countryCode, should throw an error', async () => {
expect(() => {
const checkout = new AdyenCheckout({
countryCode: 'US',
environment: 'test',
clientKey: 'test_123456',
session: { id: 'session-id', sessionData: 'session-data' }
});
delete sessionSetupResponseMock.countryCode;

const checkout = new AdyenCheckout({
countryCode: 'US',
environment: 'test',
clientKey: 'test_123456',
session: { id: 'session-id', sessionData: 'session-data' },
onError
});

let err;

await checkout.initialize().catch(e => {
err = e;
});

checkout.initialize();
}).toThrow('You must specify a countryCode when creating a session');
expect(onError).toBeCalledWith(err);
});
});
});
9 changes: 5 additions & 4 deletions packages/lib/src/core/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,15 @@ class Core implements ICore {

public initialize(): Promise<this> {
if (this.session) {
if (!hasOwnProperty(this.options.session, 'countryCode')) {
throw new AdyenCheckoutError(IMPLEMENTATION_ERROR, 'You must specify a countryCode when creating a session');
}
return this.session
.setupSession(this.options)
.then(sessionResponse => {
const { amount, shopperLocale, countryCode, paymentMethods, ...rest } = sessionResponse;

if (!hasOwnProperty(sessionResponse, 'countryCode')) {
throw new AdyenCheckoutError(IMPLEMENTATION_ERROR, 'You must specify a countryCode when creating a session');
}

this.setOptions({
...rest,
amount: this.options.order ? this.options.order.remainingAmount : amount,
Expand All @@ -104,7 +105,7 @@ class Core implements ICore {
})
.catch(error => {
if (this.options.onError) this.options.onError(error);
return this;
return Promise.reject(error);
});
}

Expand Down
2 changes: 1 addition & 1 deletion packages/playground/src/pages/Dropin/manual.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export async function initManual() {
}

const dropin = new Dropin(checkout, {
paymentMethodComponents: [Card, GooglePay, PayPal, Ach, Affirm, WeChat, Giftcard, AmazonPay],
paymentMethodComponents: [Card, GooglePay, PayPal, Ach, Affirm, WeChat, Giftcard, AmazonPay, Ideal],
instantPaymentTypes: ['googlepay'],
paymentMethodsConfiguration: {
card: {
Expand Down
2 changes: 1 addition & 1 deletion packages/playground/src/pages/Dropin/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export async function initSession() {
console.log('onPaymentFailed', result, element);
},
onError: (error, component) => {
console.info(JSON.stringify(error), component);
console.error('error', JSON.stringify(error.name), JSON.stringify(error.message), component);
},
onChange: (state, component) => {
console.log('onChange', state);
Expand Down
Loading