From ab3cc5bbcfa27db357b5fd5a5f47b0cdda04f40d Mon Sep 17 00:00:00 2001 From: Win Date: Thu, 1 Aug 2024 14:36:59 +0700 Subject: [PATCH 1/4] MOL-134/PICT-214: adjust condition for credit card component --- processor/src/service/payment.service.ts | 25 ++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/processor/src/service/payment.service.ts b/processor/src/service/payment.service.ts index c17fb68..f9b185f 100644 --- a/processor/src/service/payment.service.ts +++ b/processor/src/service/payment.service.ts @@ -55,24 +55,25 @@ export const handleListPaymentMethodsByPayment = async (ctPayment: Payment): Pro try { const mollieOptions = await mapCommercetoolsPaymentCustomFieldsToMollieListParams(ctPayment); const methods: List = await listPaymentMethods(mollieOptions); + const enableCardComponent = toBoolean(readConfiguration().mollie.cardComponent, true); + const ctUpdateActions: UpdateAction[] = []; + + if (enableCardComponent) { + methods.splice( + methods.findIndex((method: Method) => method.id === PaymentMethod.creditcard), + 1, + ); + } const availableMethods = JSON.stringify({ count: methods.length, methods: methods.length ? methods : [], }); - const ctUpdateActions: UpdateAction[] = [setCustomFields(CustomFields.payment.response, availableMethods)]; - - const hasCardPayment = methods.findIndex((method: Method) => method.id === PaymentMethod.creditcard); - - if (hasCardPayment >= 0) { - ctUpdateActions.push( - setCustomFields( - CustomFields.payment.profileId, - toBoolean(readConfiguration().mollie.cardComponent, true) ? readConfiguration().mollie.profileId : '', - ), - ); - } + ctUpdateActions.push( + setCustomFields(CustomFields.payment.profileId, enableCardComponent ? readConfiguration().mollie.profileId : ''), + ); + ctUpdateActions.push(setCustomFields(CustomFields.payment.response, availableMethods)); return { statusCode: 200, From fc49eeef90f5da5975706daa9e9dee441ca20651 Mon Sep 17 00:00:00 2001 From: Win Date: Thu, 1 Aug 2024 14:37:29 +0700 Subject: [PATCH 2/4] MOL-134/PICT-214: adjust condition for credit card component --- processor/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/processor/package.json b/processor/package.json index 8da67b8..b9145bd 100644 --- a/processor/package.json +++ b/processor/package.json @@ -1,7 +1,7 @@ { "name": "shopmacher-mollie-processor", "description": "Integration between commercetools and mollie payment service provider", - "version": "0.0.20", + "version": "0.0.21", "main": "index.js", "private": true, "scripts": { From 5a8dab53a594d8528a45efe9e61b8c102eb5de23 Mon Sep 17 00:00:00 2001 From: Win Date: Thu, 1 Aug 2024 16:41:32 +0700 Subject: [PATCH 3/4] MOL-134/PICT-214: add test cases --- .../tests/service/payment.service.spec.ts | 190 +++++++++++++++++- 1 file changed, 189 insertions(+), 1 deletion(-) diff --git a/processor/tests/service/payment.service.spec.ts b/processor/tests/service/payment.service.spec.ts index d52d073..f0da235 100644 --- a/processor/tests/service/payment.service.spec.ts +++ b/processor/tests/service/payment.service.spec.ts @@ -1,4 +1,4 @@ -import { describe, test, expect, jest, beforeEach, afterEach, it } from '@jest/globals'; +import { describe, test, jest, beforeEach, afterEach, expect, it } from '@jest/globals'; import { CustomFields, Payment } from '@commercetools/platform-sdk'; import { handlePaymentWebhook, @@ -76,6 +76,8 @@ describe('Test listPaymentMethodsByPayment', () => { }); let mockResource: Payment; + let mockResponse: Record; + test('call listPaymentMethodsByPayment with valid object reference', async () => { (listPaymentMethods as jest.Mock).mockReturnValueOnce([ { @@ -170,6 +172,192 @@ describe('Test listPaymentMethodsByPayment', () => { expect(response?.actions?.length).toBeGreaterThan(0); expect(response?.actions?.[0]?.action).toBe('setCustomField'); }); + + test('call listPaymentMethodsByPayment with failure result', async () => { + mockResource = { + id: 'RANDOMID_12345', + paymentMethodInfo: { + paymentInterface: 'mollie', + method: 'card', + }, + amountPlanned: { + type: 'centPrecision', + currencyCode: 'VND', + centAmount: 1000, + fractionDigits: 2, + }, + custom: { + fields: { + sctm_payment_methods_request: { + locale: 'de_DE', + }, + }, + } as unknown as CustomFields, + } as unknown as Payment; + + mockResponse = { + statusCode: 200, + actions: [ + { + action: 'setCustomField', + name: 'sctm_payment_methods_response', + value: '{"methods":[]}', + }, + ], + }; + + const response = await handleListPaymentMethodsByPayment(mockResource); + + expect(response).toBeDefined(); + expect(response.statusCode).toBe(200); + expect(response?.actions?.length).toEqual(0); + expect(response?.actions?.[0]?.action).toBe(undefined); + expect(JSON.stringify(response)).not.toContain('count'); + }); + + test('call listPaymentMethodsByPayment with cardComponent deactivated', async () => { + (listPaymentMethods as jest.Mock).mockReturnValueOnce([ + { + resource: 'method', + id: 'creditcard', + description: 'creditcard', + minimumAmount: { value: '0.01', currency: 'EUR' }, + maximumAmount: null, + image: { + size1x: 'https://www.mollie.com/external/icons/payment-methods/creditcard.png', + size2x: 'https://www.mollie.com/external/icons/payment-methods/creditcard%402x.png', + svg: 'https://www.mollie.com/external/icons/payment-methods/creditcard.svg', + }, + status: 'activated', + _links: { + self: { + href: 'https://api.mollie.com/v2/methods/creditcard', + type: 'application/hal+json', + }, + }, + }, + { + resource: 'method', + id: 'giftcard', + description: 'Geschenkkarten', + minimumAmount: { value: '0.01', currency: 'EUR' }, + maximumAmount: null, + image: { + size1x: 'https://www.mollie.com/external/icons/payment-methods/giftcard.png', + size2x: 'https://www.mollie.com/external/icons/payment-methods/giftcard%402x.png', + svg: 'https://www.mollie.com/external/icons/payment-methods/giftcard.svg', + }, + status: 'activated', + _links: { + self: { + href: 'https://api.mollie.com/v2/methods/giftcard', + type: 'application/hal+json', + }, + }, + }, + ]); + + mockResource = { + id: 'RANDOMID_12345', + paymentMethodInfo: { + paymentInterface: 'mollie', + method: 'card', + }, + amountPlanned: { + type: 'centPrecision', + currencyCode: 'EUR', + centAmount: 1000, + fractionDigits: 2, + }, + custom: { + fields: { + sctm_payment_methods_request: { + locale: 'de_DE', + }, + }, + } as unknown as CustomFields, + } as unknown as Payment; + + const response = await handleListPaymentMethodsByPayment(mockResource); + expect(response).toBeDefined(); + expect(response.statusCode).toBe(200); + expect(response?.actions?.length).toBeGreaterThan(0); + expect(response?.actions?.[0]?.action).toBe('setCustomField'); + expect(JSON.stringify(response)).toContain('creditcard'); + }); + + test('call listPaymentMethodsByPayment with cardComponent activated', async () => { + (listPaymentMethods as jest.Mock).mockReturnValueOnce([ + { + resource: 'method', + id: 'creditcard', + description: 'creditcard', + minimumAmount: { value: '0.01', currency: 'EUR' }, + maximumAmount: null, + image: { + size1x: 'https://www.mollie.com/external/icons/payment-methods/creditcard.png', + size2x: 'https://www.mollie.com/external/icons/payment-methods/creditcard%402x.png', + svg: 'https://www.mollie.com/external/icons/payment-methods/creditcard.svg', + }, + status: 'activated', + _links: { + self: { + href: 'https://api.mollie.com/v2/methods/creditcard', + type: 'application/hal+json', + }, + }, + }, + { + resource: 'method', + id: 'giftcard', + description: 'Geschenkkarten', + minimumAmount: { value: '0.01', currency: 'EUR' }, + maximumAmount: null, + image: { + size1x: 'https://www.mollie.com/external/icons/payment-methods/giftcard.png', + size2x: 'https://www.mollie.com/external/icons/payment-methods/giftcard%402x.png', + svg: 'https://www.mollie.com/external/icons/payment-methods/giftcard.svg', + }, + status: 'activated', + _links: { + self: { + href: 'https://api.mollie.com/v2/methods/giftcard', + type: 'application/hal+json', + }, + }, + }, + ]); + + mockResource = { + id: 'RANDOMID_12345', + paymentMethodInfo: { + paymentInterface: 'mollie', + method: 'card', + }, + amountPlanned: { + type: 'centPrecision', + currencyCode: 'EUR', + centAmount: 1000, + fractionDigits: 2, + }, + custom: { + fields: { + sctm_payment_methods_request: { + locale: 'de_DE', + }, + }, + } as unknown as CustomFields, + } as unknown as Payment; + + process.env.MOLLIE_CARD_COMPONENT = '1'; + + const response = await handleListPaymentMethodsByPayment(mockResource); + expect(response).toBeDefined(); + expect(response.statusCode).toBe(200); + expect(response?.actions?.length).toBeGreaterThan(0); + expect(response?.actions?.[0]?.action).toBe('setCustomField'); + expect(JSON.stringify(response)).not.toContain('creditcard'); + }); }); describe('Test getCreatePaymentUpdateAction', () => { From c85f21fc66f58205ece50ad44fce3091d54ddcd3 Mon Sep 17 00:00:00 2001 From: Win Date: Thu, 1 Aug 2024 17:05:46 +0700 Subject: [PATCH 4/4] MOL-134/PICT-214: add test cases --- processor/tests/service/payment.service.spec.ts | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/processor/tests/service/payment.service.spec.ts b/processor/tests/service/payment.service.spec.ts index f0da235..7b6290b 100644 --- a/processor/tests/service/payment.service.spec.ts +++ b/processor/tests/service/payment.service.spec.ts @@ -76,7 +76,6 @@ describe('Test listPaymentMethodsByPayment', () => { }); let mockResource: Payment; - let mockResponse: Record; test('call listPaymentMethodsByPayment with valid object reference', async () => { (listPaymentMethods as jest.Mock).mockReturnValueOnce([ @@ -195,17 +194,6 @@ describe('Test listPaymentMethodsByPayment', () => { } as unknown as CustomFields, } as unknown as Payment; - mockResponse = { - statusCode: 200, - actions: [ - { - action: 'setCustomField', - name: 'sctm_payment_methods_response', - value: '{"methods":[]}', - }, - ], - }; - const response = await handleListPaymentMethodsByPayment(mockResource); expect(response).toBeDefined();