Skip to content

Commit

Permalink
Merge branch 'main' of github.com:mollie/commercetools-connector
Browse files Browse the repository at this point in the history
  • Loading branch information
Tung-Huynh-Shopmacher committed Jul 29, 2024
2 parents 1b2cbce + df817d6 commit 97c535f
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 64 deletions.
8 changes: 6 additions & 2 deletions processor/src/service/payment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
createMollieCreatePaymentParams,
mapCommercetoolsPaymentCustomFieldsToMollieListParams,
} from '../utils/map.utils';
import { CentPrecisionMoney, Payment, UpdateAction } from '@commercetools/platform-sdk';
import { CentPrecisionMoney, Extension, Payment, UpdateAction } from '@commercetools/platform-sdk';
import CustomError from '../errors/custom.error';
import { createMolliePayment, getPaymentById, listPaymentMethods } from '../mollie/payment.mollie';
import { cancelPaymentRefund, createPaymentRefund } from '../mollie/refund.mollie';
Expand Down Expand Up @@ -40,6 +40,8 @@ import {
CreateParameters,
} from '@mollie/api-client/dist/types/src/binders/payments/refunds/parameters';
import { parseStringToJsonObject } from '../utils/app.utils';
import { getPaymentExtension } from '../commercetools/extensions.commercetools';
import { HttpDestination } from '@commercetools/platform-sdk/dist/declarations/src/generated/models/extension';

/**
* Handles listing payment methods by payment.
Expand Down Expand Up @@ -151,7 +153,9 @@ const getPaymentStatusUpdateAction = (
* @param ctPayment
*/
export const handleCreatePayment = async (ctPayment: Payment): Promise<ControllerResponseType> => {
const paymentParams = createMollieCreatePaymentParams(ctPayment);
const extensionUrl = (((await getPaymentExtension()) as Extension)?.destination as HttpDestination).url;

const paymentParams = createMollieCreatePaymentParams(ctPayment, extensionUrl);

const molliePayment = await createMolliePayment(paymentParams);

Expand Down
4 changes: 2 additions & 2 deletions processor/src/utils/map.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ const getSpecificPaymentParams = (method: PaymentMethod, paymentRequest: any) =>
}
};

export const createMollieCreatePaymentParams = (payment: Payment): PaymentCreateParams => {
export const createMollieCreatePaymentParams = (payment: Payment, extensionUrl: string): PaymentCreateParams => {
const { amountPlanned, paymentMethodInfo } = payment;

const [method, issuer] = paymentMethodInfo?.method?.split(',') ?? [null, null];
Expand All @@ -104,7 +104,7 @@ export const createMollieCreatePaymentParams = (payment: Payment): PaymentCreate
payment.id,
);

const defaultWebhookEndpoint = new URL(process.env.CONNECT_SERVICE_URL ?? '').origin + '/webhook';
const defaultWebhookEndpoint = new URL(extensionUrl).origin + '/webhook';

return {
amount: makeMollieAmount(amountPlanned),
Expand Down
11 changes: 11 additions & 0 deletions processor/tests/service/payment.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,17 @@ import CustomError from '../../src/errors/custom.error';
import { logger } from '../../src/utils/logger.utils';
import { getPaymentByMolliePaymentId, updatePayment } from '../../src/commercetools/payment.commercetools';
import { CreateParameters } from '@mollie/api-client/dist/types/src/binders/payments/refunds/parameters';
import { getPaymentExtension } from '../../src/commercetools/extensions.commercetools';

const uuid = '5c8b0375-305a-4f19-ae8e-07806b101999';
jest.mock('uuid', () => ({
v4: () => uuid,
}));

jest.mock('../../src/commercetools/extensions.commercetools', () => ({
getPaymentExtension: jest.fn(),
}));

jest.mock('../../src/commercetools/payment.commercetools', () => ({
getPaymentByMolliePaymentId: jest.fn(),
updatePayment: jest.fn(),
Expand Down Expand Up @@ -407,6 +413,11 @@ describe('Test createPayment', () => {
} as molliePayment;

(createMolliePayment as jest.Mock).mockReturnValueOnce(molliePayment);
(getPaymentExtension as jest.Mock).mockReturnValueOnce({
destination: {
url: 'https://example.com',
},
});

const actual = await handleCreatePayment(CTPayment);

Expand Down
73 changes: 40 additions & 33 deletions processor/tests/utils/map.utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import { Payment } from '@commercetools/platform-sdk';
import { MethodsListParams, PaymentCreateParams, PaymentMethod } from '@mollie/api-client';
import { makeMollieAmount } from '../../src/utils/mollie.utils';

const defaultWebhookEndpoint = new URL(process.env.CONNECT_SERVICE_URL ?? '').origin + '/webhook';

describe('Test map.utils.ts', () => {
let mockCtPayment: Payment;
let mockMolObject: MethodsListParams;
Expand Down Expand Up @@ -56,7 +54,7 @@ describe('Test map.utils.ts', () => {
});

describe('createMollieCreatePaymentParams', () => {
it('should able to create a mollie payment params from CommerceTools payment object for with method as creditcard', () => {
it('should able to create a mollie payment params from CommerceTools payment object for with method as creditcard', async () => {
const CTPayment: Payment = {
id: '5c8b0375-305a-4f19-ae8e-07806b101999',
version: 1,
Expand All @@ -75,9 +73,9 @@ describe('createMollieCreatePaymentParams', () => {
method: 'creditcard',
},
};
const extensionUrl = 'https://example.com/webhook';

const mollieCreatePaymentParams = createMollieCreatePaymentParams(CTPayment);
const defaultWebhookEndpoint = new URL(process.env.CONNECT_SERVICE_URL ?? '').origin + '/webhook';
const mollieCreatePaymentParams = createMollieCreatePaymentParams(CTPayment, extensionUrl);
const mollieAmount = makeMollieAmount(CTPayment.amountPlanned);

expect(mollieCreatePaymentParams).toEqual({
Expand All @@ -88,7 +86,7 @@ describe('createMollieCreatePaymentParams', () => {
},
locale: null,
redirectUrl: null,
webhookUrl: defaultWebhookEndpoint,
webhookUrl: extensionUrl,
description: '',
applicationFee: {},
billingAddress: {},
Expand All @@ -101,7 +99,7 @@ describe('createMollieCreatePaymentParams', () => {
});
});

it('should able to create a mollie payment params from CommerceTools payment object with method as creditcard which has custom field', () => {
it('should able to create a mollie payment params from CommerceTools payment object with method as creditcard which has custom field', async () => {
const customFieldObject = {
description: 'Test payment',
locale: 'en_GB',
Expand Down Expand Up @@ -137,8 +135,9 @@ describe('createMollieCreatePaymentParams', () => {
},
},
};
const extensionUrl = 'https://example.com/webhook';

const mollieCreatePaymentParams = createMollieCreatePaymentParams(CTPayment);
const mollieCreatePaymentParams = createMollieCreatePaymentParams(CTPayment, extensionUrl);

expect(mollieCreatePaymentParams).toEqual({
method: 'creditcard',
Expand All @@ -148,7 +147,7 @@ describe('createMollieCreatePaymentParams', () => {
},
locale: customFieldObject.locale,
redirectUrl: customFieldObject.redirectUrl,
webhookUrl: defaultWebhookEndpoint, // Always use our default webhook endpoint
webhookUrl: extensionUrl, // Always use our default webhook endpoint
description: customFieldObject.description,
applicationFee: {},
billingAddress: {},
Expand All @@ -161,7 +160,7 @@ describe('createMollieCreatePaymentParams', () => {
});
});

it('should able to create a mollie payment params from CommerceTools payment object with method as ideal', () => {
it('should able to create a mollie payment params from CommerceTools payment object with method as ideal', async () => {
const customFieldObject = {
description: 'Test payment',
locale: 'en_GB',
Expand Down Expand Up @@ -202,8 +201,9 @@ describe('createMollieCreatePaymentParams', () => {
},
},
};
const extensionUrl = 'https://example.com/webhook';

const mollieCreatePaymentParams: PaymentCreateParams = createMollieCreatePaymentParams(CTPayment);
const mollieCreatePaymentParams: PaymentCreateParams = createMollieCreatePaymentParams(CTPayment, extensionUrl);
expect(mollieCreatePaymentParams).toEqual({
method: PaymentMethod.ideal,
amount: {
Expand All @@ -212,7 +212,7 @@ describe('createMollieCreatePaymentParams', () => {
},
locale: customFieldObject.locale,
redirectUrl: customFieldObject.redirectUrl,
webhookUrl: defaultWebhookEndpoint,
webhookUrl: extensionUrl,
description: customFieldObject.description,
issuer: 'ideal_TEST',
include: customFieldObject.include,
Expand All @@ -224,7 +224,7 @@ describe('createMollieCreatePaymentParams', () => {
});
});

it('should able to create a mollie payment params from CommerceTools payment object with method as bancontact', () => {
it('should able to create a mollie payment params from CommerceTools payment object with method as bancontact', async () => {
const customFieldObject = {
description: 'Test payment',
locale: 'en_GB',
Expand Down Expand Up @@ -266,8 +266,9 @@ describe('createMollieCreatePaymentParams', () => {
},
},
};
const extensionUrl = 'https://example.com/webhook';

const mollieCreatePaymentParams: PaymentCreateParams = createMollieCreatePaymentParams(CTPayment);
const mollieCreatePaymentParams: PaymentCreateParams = createMollieCreatePaymentParams(CTPayment, extensionUrl);
expect(mollieCreatePaymentParams).toEqual({
method: PaymentMethod.bancontact,
amount: {
Expand All @@ -276,7 +277,7 @@ describe('createMollieCreatePaymentParams', () => {
},
locale: customFieldObject.locale,
redirectUrl: customFieldObject.redirectUrl,
webhookUrl: defaultWebhookEndpoint,
webhookUrl: extensionUrl,
description: customFieldObject.description,
include: customFieldObject.include,
issuer: '',
Expand All @@ -288,7 +289,7 @@ describe('createMollieCreatePaymentParams', () => {
});
});

it('should able to create a mollie payment params from CommerceTools payment object with method as banktransfer', () => {
it('should able to create a mollie payment params from CommerceTools payment object with method as banktransfer', async () => {
const customFieldObject = {
description: 'Test payment',
locale: 'en_GB',
Expand Down Expand Up @@ -325,8 +326,9 @@ describe('createMollieCreatePaymentParams', () => {
},
},
};
const extensionUrl = 'https://example.com/webhook';

const mollieCreatePaymentParams: PaymentCreateParams = createMollieCreatePaymentParams(CTPayment);
const mollieCreatePaymentParams: PaymentCreateParams = createMollieCreatePaymentParams(CTPayment, extensionUrl);
expect(mollieCreatePaymentParams).toEqual({
method: PaymentMethod.banktransfer,
amount: {
Expand All @@ -335,7 +337,7 @@ describe('createMollieCreatePaymentParams', () => {
},
locale: customFieldObject.locale,
redirectUrl: customFieldObject.redirectUrl,
webhookUrl: defaultWebhookEndpoint,
webhookUrl: extensionUrl,
description: customFieldObject.description,
dueDate: customFieldObject.dueDate,
billingEmail: customFieldObject.billingEmail,
Expand All @@ -349,7 +351,7 @@ describe('createMollieCreatePaymentParams', () => {
});
});

it('should able to create a mollie payment params from CommerceTools payment object with method as przelewy24', () => {
it('should able to create a mollie payment params from CommerceTools payment object with method as przelewy24', async () => {
const customFieldObject = {
description: 'Test payment',
locale: 'en_GB',
Expand Down Expand Up @@ -385,8 +387,9 @@ describe('createMollieCreatePaymentParams', () => {
},
},
};
const extensionUrl = 'https://example.com/webhook';

const mollieCreatePaymentParams: PaymentCreateParams = createMollieCreatePaymentParams(CTPayment);
const mollieCreatePaymentParams: PaymentCreateParams = createMollieCreatePaymentParams(CTPayment, extensionUrl);
expect(mollieCreatePaymentParams).toEqual({
method: PaymentMethod.przelewy24,
amount: {
Expand All @@ -395,7 +398,7 @@ describe('createMollieCreatePaymentParams', () => {
},
locale: customFieldObject.locale,
redirectUrl: customFieldObject.redirectUrl,
webhookUrl: defaultWebhookEndpoint,
webhookUrl: extensionUrl,
description: customFieldObject.description,
billingEmail: customFieldObject.billingEmail,
include: '',
Expand All @@ -408,7 +411,7 @@ describe('createMollieCreatePaymentParams', () => {
});
});

it('should able to create a mollie payment params from CommerceTools payment object with method as kbc', () => {
it('should able to create a mollie payment params from CommerceTools payment object with method as kbc', async () => {
const customFieldObject = {
description: 'Test payment',
locale: 'en_GB',
Expand Down Expand Up @@ -443,8 +446,9 @@ describe('createMollieCreatePaymentParams', () => {
},
},
};
const extensionUrl = 'https://example.com/webhook';

const mollieCreatePaymentParams: PaymentCreateParams = createMollieCreatePaymentParams(CTPayment);
const mollieCreatePaymentParams: PaymentCreateParams = createMollieCreatePaymentParams(CTPayment, extensionUrl);
expect(mollieCreatePaymentParams).toEqual({
method: PaymentMethod.kbc,
amount: {
Expand All @@ -453,7 +457,7 @@ describe('createMollieCreatePaymentParams', () => {
},
locale: customFieldObject.locale,
redirectUrl: customFieldObject.redirectUrl,
webhookUrl: defaultWebhookEndpoint,
webhookUrl: extensionUrl,
description: customFieldObject.description,
include: '',
issuer: '',
Expand Down Expand Up @@ -515,7 +519,7 @@ describe('createMollieCreatePaymentParams', () => {
// });
// });

it('should able to create a mollie payment params from CommerceTools payment object with method as applepay', () => {
it('should able to create a mollie payment params from CommerceTools payment object with method as applepay', async () => {
const customFieldObject = {
description: 'Test payment',
locale: 'en_GB',
Expand Down Expand Up @@ -551,8 +555,9 @@ describe('createMollieCreatePaymentParams', () => {
},
},
};
const extensionUrl = 'https://example.com/webhook';

const mollieCreatePaymentParams: PaymentCreateParams = createMollieCreatePaymentParams(CTPayment);
const mollieCreatePaymentParams: PaymentCreateParams = createMollieCreatePaymentParams(CTPayment, extensionUrl);
expect(mollieCreatePaymentParams).toEqual({
method: PaymentMethod.applepay,
amount: {
Expand All @@ -561,7 +566,7 @@ describe('createMollieCreatePaymentParams', () => {
},
locale: customFieldObject.locale,
redirectUrl: customFieldObject.redirectUrl,
webhookUrl: defaultWebhookEndpoint,
webhookUrl: extensionUrl,
description: customFieldObject.description,
applePayPaymentToken: customFieldObject.applePayPaymentToken,
include: '',
Expand All @@ -574,7 +579,7 @@ describe('createMollieCreatePaymentParams', () => {
});
});

it('should able to create a mollie payment params from CommerceTools payment object with method as paypal', () => {
it('should able to create a mollie payment params from CommerceTools payment object with method as paypal', async () => {
const customFieldObject = {
description: 'Test payment',
locale: 'en_GB',
Expand Down Expand Up @@ -611,8 +616,9 @@ describe('createMollieCreatePaymentParams', () => {
},
},
};
const extensionUrl = 'https://example.com/webhook';

const mollieCreatePaymentParams: PaymentCreateParams = createMollieCreatePaymentParams(CTPayment);
const mollieCreatePaymentParams: PaymentCreateParams = createMollieCreatePaymentParams(CTPayment, extensionUrl);
expect(mollieCreatePaymentParams).toEqual({
method: PaymentMethod.paypal,
amount: {
Expand All @@ -621,7 +627,7 @@ describe('createMollieCreatePaymentParams', () => {
},
locale: customFieldObject.locale,
redirectUrl: customFieldObject.redirectUrl,
webhookUrl: defaultWebhookEndpoint,
webhookUrl: extensionUrl,
description: customFieldObject.description,
sessionId: customFieldObject.sessionId,
digitalGoods: customFieldObject.digitalGoods,
Expand All @@ -635,7 +641,7 @@ describe('createMollieCreatePaymentParams', () => {
});
});

it('should able to create a mollie payment params from CommerceTools payment object with method as giftcard', () => {
it('should able to create a mollie payment params from CommerceTools payment object with method as giftcard', async () => {
const customFieldObject = {
description: 'Test payment',
locale: 'en_GB',
Expand Down Expand Up @@ -672,8 +678,9 @@ describe('createMollieCreatePaymentParams', () => {
},
},
};
const extensionUrl = 'https://example.com/webhook';

const mollieCreatePaymentParams: PaymentCreateParams = createMollieCreatePaymentParams(CTPayment);
const mollieCreatePaymentParams: PaymentCreateParams = createMollieCreatePaymentParams(CTPayment, extensionUrl);
expect(mollieCreatePaymentParams).toEqual({
method: PaymentMethod.giftcard,
amount: {
Expand All @@ -682,7 +689,7 @@ describe('createMollieCreatePaymentParams', () => {
},
locale: customFieldObject.locale,
redirectUrl: customFieldObject.redirectUrl,
webhookUrl: defaultWebhookEndpoint,
webhookUrl: extensionUrl,
description: customFieldObject.description,
voucherNumber: customFieldObject.voucherNumber,
voucherPin: customFieldObject.voucherPin,
Expand Down
Loading

0 comments on commit 97c535f

Please sign in to comment.