Skip to content

Commit

Permalink
Merge branch 'develop' into feature/MOL-438/PICT-252
Browse files Browse the repository at this point in the history
  • Loading branch information
tdang1-shopmacher committed Oct 2, 2024
2 parents 2b6bf51 + a179c6f commit ad27086
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 4 deletions.
8 changes: 5 additions & 3 deletions processor/src/validators/payment.validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const validateCardToken = (cardToken: string | undefined, ctPayment: CTPayment):
}
};

const validateBanktransfer = (paymentCustomFields: any, ctPayment: CTPayment): void => {
export const validateBanktransfer = (paymentCustomFields: any, ctPayment: CTPayment): void => {
if (!paymentCustomFields?.billingAddress || !paymentCustomFields?.billingAddress?.email) {
throwError(
'validateBanktransfer',
Expand Down Expand Up @@ -66,8 +66,10 @@ const validateBlik = (paymentCustomFields: any, ctPayment: CTPayment): void => {
}
};

const paymentMethodRequiredExtraParameters = (method: string): method is MolliePaymentMethods | CustomPaymentMethod => {
return [MolliePaymentMethods.creditcard, CustomPaymentMethod.blik].includes(
export const paymentMethodRequiredExtraParameters = (
method: string,
): method is MolliePaymentMethods | CustomPaymentMethod => {
return [MolliePaymentMethods.creditcard, CustomPaymentMethod.blik, MolliePaymentMethods.banktransfer].includes(
method as MolliePaymentMethods | CustomPaymentMethod,
);
};
Expand Down
87 changes: 86 additions & 1 deletion processor/tests/validators/payment.validators.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import {
checkValidRefundTransactionForCreate,
checkValidRefundTransactionForCancel,
checkValidSuccessAuthorizationTransaction,
validateBanktransfer,
paymentMethodRequiredExtraParameters,
} from './../../src/validators/payment.validators';
import { describe, it, expect, jest, afterEach } from '@jest/globals';
import { describe, it, expect, jest, afterEach, test } from '@jest/globals';
import CustomError from '../../src/errors/custom.error';
import SkipError from '../../src/errors/skip.error';
import { logger } from '../../src/utils/logger.utils';
Expand Down Expand Up @@ -281,6 +283,62 @@ describe('checkPaymentMethodInput', () => {

expect(checkPaymentMethodSpecificParameters).toBeCalledTimes(1);
});

it('should validate the billing email for banktransfer method', () => {
const paymentValidators = require('../../src/validators/payment.validators');

jest.spyOn(paymentValidators, 'checkPaymentMethodSpecificParameters');
jest.spyOn(paymentValidators, 'validateBanktransfer');

const CTPayment: Payment = {
id: '5c8b0375-305a-4f19-ae8e-07806b101999',
version: 1,
createdAt: '2024-07-04T14:07:35.625Z',
lastModifiedAt: '2024-07-04T14:07:35.625Z',
amountPlanned: {
type: 'centPrecision',
currencyCode: 'EUR',
centAmount: 1000,
fractionDigits: 2,
},
paymentStatus: {},
transactions: [],
interfaceInteractions: [],
paymentMethodInfo: {
method: 'banktransfer',
},
custom: {
type: {
typeId: 'type',
id: 'sctm-payment-custom-fields',
},
fields: {
sctm_create_payment_request:
'{"description":"Test","locale":"en_GB","redirectUrl":"https://www.google.com/","cardToken":"token_12345"}',
},
},
};

try {
checkPaymentMethodInput(ConnectorActions.CreatePayment, CTPayment);
} catch (error) {
expect(checkPaymentMethodSpecificParameters).toBeCalledTimes(1);
expect(checkPaymentMethodSpecificParameters).toBeCalledWith(
CTPayment,
CTPayment.paymentMethodInfo.method as string,
);

expect(validateBanktransfer).toBeCalledTimes(1);

expect(logger.error).toBeCalledTimes(1);
expect(logger.error).toBeCalledWith(
'SCTM - validateBanktransfer - email is required for payment method banktransfer. Please make sure you have sent it in billingAddress.email of the custom field.',
{
commerceToolsPayment: CTPayment,
},
);
}
});
});

describe('checkPaymentMethodSpecificParameters', () => {
Expand Down Expand Up @@ -1354,3 +1412,30 @@ describe('validateCommerceToolsPaymentPayload', () => {
expect(checkValidSuccessAuthorizationTransaction).toReturnWith(true);
});
});

describe('paymentMethodRequiredExtraParameters', () => {
test.each([
{
method: 'creditcard',
result: true,
},
{
method: 'banktransfer',
result: true,
},
{
method: 'blik',
result: true,
},
{
method: 'applepay',
result: false,
},
{
method: 'ideal',
result: false,
},
])('should return $result for method $method', ({ method, result }) => {
expect(paymentMethodRequiredExtraParameters(method)).toBe(result);
});
});

0 comments on commit ad27086

Please sign in to comment.