From cc0c22ea5bc0e99f2fb7203a6f6410b76b87d632 Mon Sep 17 00:00:00 2001 From: nicholas Date: Fri, 16 Feb 2024 14:51:53 +0100 Subject: [PATCH 1/4] Handling redirects when countryCode is mandatory --- packages/lib/src/core/core.ts | 27 ++++++++++++------- .../playground/src/pages/Dropin/manual.js | 4 +-- .../playground/src/pages/Dropin/session.js | 6 ++--- 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/packages/lib/src/core/core.ts b/packages/lib/src/core/core.ts index 0c3b6f8ad6..2356fd212d 100644 --- a/packages/lib/src/core/core.ts +++ b/packages/lib/src/core/core.ts @@ -82,14 +82,15 @@ class Core implements ICore { public initialize(): Promise { 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, @@ -103,8 +104,9 @@ class Core implements ICore { return this; }) .catch(error => { + console.log('### core:::: error'); if (this.options.onError) this.options.onError(error); - return this; + return Promise.reject(error); }); } @@ -208,12 +210,17 @@ class Core implements ICore { public update = (options: Partial = {}): Promise => { this.setOptions(options); - return this.initialize().then(() => { - // Update each component under this instance - // here we should update only the new options that have been received from core - this.components.forEach(c => c.update(options)); - return this; - }); + return this.initialize() + .then(() => { + // Update each component under this instance + // here we should update only the new options that have been received from core + this.components.forEach(c => c.update(options)); + return this; + }) + .catch(e => { + console.log('### core:::: CATCH e', e); + return this; + }); }; /** diff --git a/packages/playground/src/pages/Dropin/manual.js b/packages/playground/src/pages/Dropin/manual.js index df2173379b..bfc7cf920a 100644 --- a/packages/playground/src/pages/Dropin/manual.js +++ b/packages/playground/src/pages/Dropin/manual.js @@ -10,7 +10,7 @@ export async function initManual() { window.checkout = await AdyenCheckout({ amount, - countryCode, + // countryCode, clientKey: process.env.__CLIENT_KEY__, paymentMethodsResponse, @@ -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: { diff --git a/packages/playground/src/pages/Dropin/session.js b/packages/playground/src/pages/Dropin/session.js index abf8cfa374..0a3bd348dd 100644 --- a/packages/playground/src/pages/Dropin/session.js +++ b/packages/playground/src/pages/Dropin/session.js @@ -12,8 +12,8 @@ export async function initSession() { shopperLocale, shopperReference, telephoneNumber: '+611223344', - shopperEmail: 'shopper.ctp1@adyen.com', - countryCode + shopperEmail: 'shopper.ctp1@adyen.com' + // countryCode }); const checkout = await AdyenCheckout({ @@ -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); From 1c8fd6d96fa2e0c7cf160df09d068dd85f0eab76 Mon Sep 17 00:00:00 2001 From: nicholas Date: Fri, 16 Feb 2024 15:01:54 +0100 Subject: [PATCH 2/4] Removed log --- packages/lib/src/core/core.ts | 18 ++++++------------ packages/playground/src/pages/Dropin/manual.js | 2 +- .../playground/src/pages/Dropin/session.js | 4 ++-- 3 files changed, 9 insertions(+), 15 deletions(-) diff --git a/packages/lib/src/core/core.ts b/packages/lib/src/core/core.ts index 2356fd212d..68794f0e9f 100644 --- a/packages/lib/src/core/core.ts +++ b/packages/lib/src/core/core.ts @@ -104,7 +104,6 @@ class Core implements ICore { return this; }) .catch(error => { - console.log('### core:::: error'); if (this.options.onError) this.options.onError(error); return Promise.reject(error); }); @@ -210,17 +209,12 @@ class Core implements ICore { public update = (options: Partial = {}): Promise => { this.setOptions(options); - return this.initialize() - .then(() => { - // Update each component under this instance - // here we should update only the new options that have been received from core - this.components.forEach(c => c.update(options)); - return this; - }) - .catch(e => { - console.log('### core:::: CATCH e', e); - return this; - }); + return this.initialize().then(() => { + // Update each component under this instance + // here we should update only the new options that have been received from core + this.components.forEach(c => c.update(options)); + return this; + }); }; /** diff --git a/packages/playground/src/pages/Dropin/manual.js b/packages/playground/src/pages/Dropin/manual.js index bfc7cf920a..191835549e 100644 --- a/packages/playground/src/pages/Dropin/manual.js +++ b/packages/playground/src/pages/Dropin/manual.js @@ -10,7 +10,7 @@ export async function initManual() { window.checkout = await AdyenCheckout({ amount, - // countryCode, + countryCode, clientKey: process.env.__CLIENT_KEY__, paymentMethodsResponse, diff --git a/packages/playground/src/pages/Dropin/session.js b/packages/playground/src/pages/Dropin/session.js index 0a3bd348dd..97f714b08e 100644 --- a/packages/playground/src/pages/Dropin/session.js +++ b/packages/playground/src/pages/Dropin/session.js @@ -12,8 +12,8 @@ export async function initSession() { shopperLocale, shopperReference, telephoneNumber: '+611223344', - shopperEmail: 'shopper.ctp1@adyen.com' - // countryCode + shopperEmail: 'shopper.ctp1@adyen.com', + countryCode }); const checkout = await AdyenCheckout({ From dd83d1a908f1b721aebcc2844b0106f1244e689f Mon Sep 17 00:00:00 2001 From: nicholas Date: Fri, 16 Feb 2024 15:58:12 +0100 Subject: [PATCH 3/4] Add new line escape sequence to warning --- packages/lib/src/components/Dropin/elements/createElements.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/lib/src/components/Dropin/elements/createElements.ts b/packages/lib/src/components/Dropin/elements/createElements.ts index 91995ca28d..1f828fa0ca 100644 --- a/packages/lib/src/components/Dropin/elements/createElements.ts +++ b/packages/lib/src/components/Dropin/elements/createElements.ts @@ -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 From 46b38cfcf8b7a32e8c6ee1c58113e01540b2fde2 Mon Sep 17 00:00:00 2001 From: nicholas Date: Fri, 16 Feb 2024 17:33:27 +0100 Subject: [PATCH 4/4] Fixing unit test --- packages/lib/src/core/core.test.ts | 64 +++++++++++++++++------------- 1 file changed, 37 insertions(+), 27 deletions(-) diff --git a/packages/lib/src/core/core.test.ts b/packages/lib/src/core/core.test.ts index 80e0dd08ad..808c8b1ccf 100644 --- a/packages/lib/src/core/core.test.ts +++ b/packages/lib/src/core/core.test.ts @@ -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); }); @@ -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', @@ -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); }); }); });