Skip to content

Commit

Permalink
Merge pull request #23 from mollie/feature/MOL-134/PICT-214
Browse files Browse the repository at this point in the history
Feature/mol 134/pict 214
  • Loading branch information
Tung-Huynh-Shopmacher authored Aug 2, 2024
2 parents 5fbfb24 + c85f21f commit 6b464bf
Show file tree
Hide file tree
Showing 3 changed files with 191 additions and 14 deletions.
2 changes: 1 addition & 1 deletion processor/package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
25 changes: 13 additions & 12 deletions processor/src/service/payment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,24 +55,25 @@ export const handleListPaymentMethodsByPayment = async (ctPayment: Payment): Pro
try {
const mollieOptions = await mapCommercetoolsPaymentCustomFieldsToMollieListParams(ctPayment);
const methods: List<Method> = 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,
Expand Down
178 changes: 177 additions & 1 deletion processor/tests/service/payment.service.spec.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -76,6 +76,7 @@ describe('Test listPaymentMethodsByPayment', () => {
});

let mockResource: Payment;

test('call listPaymentMethodsByPayment with valid object reference', async () => {
(listPaymentMethods as jest.Mock).mockReturnValueOnce([
{
Expand Down Expand Up @@ -170,6 +171,181 @@ 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;

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', () => {
Expand Down

0 comments on commit 6b464bf

Please sign in to comment.