Skip to content

Commit

Permalink
Merge pull request #125 from mollie/MOL-591/PICT-266
Browse files Browse the repository at this point in the history
PICT-266: CR - Update billingAddress.email parameter
  • Loading branch information
NghiaDTr authored Jan 13, 2025
2 parents d8ace68 + dec12c2 commit c8b4935
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 130 deletions.
12 changes: 0 additions & 12 deletions processor/src/utils/app.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,6 @@ export function removeEmptyProperties(obj: object) {
return clonedObject;
}

/**
* Validates an email address using a regular expression.
*
* @param {string} email - The email address to validate.
* @return {boolean} Returns true if the email is valid, false otherwise.
*/
export function validateEmail(email: string): boolean {
const emailRegex: RegExp = /^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$/;

return emailRegex.test(email);
}

export const convertCentToEUR = (amount: number, fractionDigits: number): number => {
return amount / Math.pow(10, fractionDigits);
};
Expand Down
20 changes: 13 additions & 7 deletions processor/src/utils/map.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,6 @@ const getSpecificPaymentParams = (method: PaymentMethod | CustomPaymentMethod, p
return {
dueDate: calculateDueDate(readConfiguration().mollie.bankTransferDueDate),
};
case PaymentMethod.przelewy24:
return { billingEmail: paymentRequest.billingEmail ?? '' };
case PaymentMethod.paypal:
return {
sessionId: paymentRequest.sessionId ?? '',
Expand All @@ -101,10 +99,6 @@ const getSpecificPaymentParams = (method: PaymentMethod | CustomPaymentMethod, p
};
case PaymentMethod.creditcard:
return { cardToken: paymentRequest.cardToken ?? '' };
case CustomPaymentMethod.blik:
return {
billingEmail: paymentRequest.billingEmail ?? '',
};
default:
return {};
}
Expand Down Expand Up @@ -154,12 +148,24 @@ export const createMollieCreatePaymentParams = (

const defaultWebhookEndpoint = new URL(extensionUrl).origin + '/webhook';

let billingAddress = paymentRequest.billingAddress;
if (
(method === CustomPaymentMethod.blik || method === PaymentMethod.przelewy24) &&
!billingAddress?.email &&
paymentRequest.billingEmail
) {
billingAddress = {
...paymentRequest.billingAddress,
email: paymentRequest.billingEmail,
};
}

const createPaymentParams = {
amount: makeMollieAmount(amountPlanned, surchargeAmountInCent),
description: paymentRequest.description ?? '',
redirectUrl: paymentRequest.redirectUrl ?? null,
webhookUrl: defaultWebhookEndpoint,
billingAddress: paymentRequest.billingAddress ?? {},
billingAddress: billingAddress ?? {},
shippingAddress: paymentRequest.shippingAddress ?? {},
locale: paymentRequest.locale ?? null,
method: method as PaymentMethod,
Expand Down
14 changes: 1 addition & 13 deletions processor/src/validators/payment.validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { logger } from '../utils/logger.utils';
import { ConnectorActions, CustomFields } from '../utils/constant.utils';
import { DeterminePaymentActionType } from '../types/controller.types';
import { CTTransactionState, CTTransactionType } from '../types/commercetools.types';
import { parseStringToJsonObject, validateEmail } from '../utils/app.utils';
import { parseStringToJsonObject } from '../utils/app.utils';
import { readConfiguration } from '../utils/config.utils';
import { toBoolean } from 'validator';
import { CustomPaymentMethod, SupportedPaymentMethods } from '../types/mollie.types';
Expand Down Expand Up @@ -46,24 +46,12 @@ export const validateBanktransfer = (paymentCustomFields: any, ctPayment: CTPaym
ctPayment,
);
}

if (!validateEmail(paymentCustomFields.billingAddress.email)) {
throwError('validateBanktransfer', 'email must be a valid email address.', ctPayment);
}
};

const validateBlik = (paymentCustomFields: any, ctPayment: CTPayment): void => {
if (ctPayment.amountPlanned.currencyCode.toLowerCase() !== 'pln') {
throwError('validateBlik', 'Currency Code must be PLN for payment method BLIK.', ctPayment);
}

if (!paymentCustomFields?.billingEmail) {
throwError('validateBlik', 'billingEmail is required for payment method BLIK.', ctPayment);
}

if (!validateEmail(paymentCustomFields.billingEmail)) {
throwError('validateBlik', 'billingEmail must be a valid email address.', ctPayment);
}
};

export const paymentMethodRequiredExtraParameters = (
Expand Down
11 changes: 0 additions & 11 deletions processor/tests/utils/app.utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
parseStringToJsonObject,
removeEmptyProperties,
roundSurchargeAmountToCent,
validateEmail,
} from '../../src/utils/app.utils';
import { logger } from '../../src/utils/logger.utils';
import CustomError from '../../src/errors/custom.error';
Expand Down Expand Up @@ -91,16 +90,6 @@ describe('Test removeEmptyProperties', () => {
});
});

describe('Test validateEmail', () => {
it('should return false when the targeted string is an invalid email', () => {
expect(validateEmail('123123')).toBe(false);
});

it('should return true when the targeted string is a valid email', () => {
expect(validateEmail('[email protected]')).toBe(true);
});
});

describe('Test convertCentToEUR', () => {
it('should return correct result', () => {
expect(convertCentToEUR(100, 2)).toBe(1);
Expand Down
68 changes: 68 additions & 0 deletions processor/tests/utils/map.utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,74 @@ describe('createMollieCreatePaymentParams', () => {
// });
// });

it('should be able to create mollie payment params including billing email address with payment przelewy24', async () => {
const cart = {
id: 'cart-test-id',
shippingInfo: {},
} as Cart;

const customFieldObject = {
description: 'Test payment',
locale: 'en_GB',
redirectUrl: 'https://example.com/success',
webhookUrl: 'https://example.com/webhook',
billingEmail: '[email protected]',
};

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: 2000,
fractionDigits: 2,
},
paymentStatus: {},
transactions: [],
interfaceInteractions: [],
paymentMethodInfo: {
method: PaymentMethod.przelewy24,
},
custom: {
type: {
typeId: 'type',
id: 'sctm_payment',
},
fields: {
sctm_create_payment_request: JSON.stringify(customFieldObject),
},
},
};

const extensionUrl = 'https://example.com/webhook';

const mollieCreatePaymentParams: PaymentCreateParams = createMollieCreatePaymentParams(
CTPayment,
extensionUrl,
0,
cart,
);

expect(mollieCreatePaymentParams).toEqual({
method: PaymentMethod.przelewy24,
amount: {
currency: 'EUR',
value: '20.00',
},
locale: customFieldObject.locale,
redirectUrl: customFieldObject.redirectUrl,
webhookUrl: extensionUrl,
description: customFieldObject.description,
billingAddress: {
email: customFieldObject.billingEmail,
},
lines: [],
});
});

it('should able to create a mollie payment params from CommerceTools payment object including a line item for shipping amount', async () => {
const cart = {
id: 'cart-test-id',
Expand Down
88 changes: 1 addition & 87 deletions processor/tests/validators/payment.validators.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -745,93 +745,7 @@ describe('checkPaymentMethodSpecificParameters', () => {
}
});

it('should throw an error if the payment method is blik and the billing email is not provided', () => {
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: 'PLN',
centAmount: 1000,
fractionDigits: 2,
},
paymentStatus: {},
transactions: [],
interfaceInteractions: [],
paymentMethodInfo: {
method: 'blik',
},
custom: {
type: {
typeId: 'type',
id: 'sctm-payment-custom-fields',
},
fields: {
sctm_create_payment_request:
'{"description":"Test","locale":"en_GB","redirectUrl":"https://www.google.com/"}',
},
},
};

try {
checkPaymentMethodSpecificParameters(CTPayment, CTPayment.paymentMethodInfo.method as string);
} catch (error: unknown) {
expect(error).toBeInstanceOf(CustomError);
expect((error as CustomError).message).toBe(
'SCTM - validateBlik - billingEmail is required for payment method BLIK.',
);
expect(logger.error).toBeCalledTimes(1);
expect(logger.error).toBeCalledWith(`SCTM - validateBlik - billingEmail is required for payment method BLIK.`, {
commerceToolsPayment: CTPayment,
});
}
});

it('should throw an error if the payment method is blik and the billing email is provided incorrectly', () => {
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: 'PLN',
centAmount: 1000,
fractionDigits: 2,
},
paymentStatus: {},
transactions: [],
interfaceInteractions: [],
paymentMethodInfo: {
method: 'blik',
},
custom: {
type: {
typeId: 'type',
id: 'sctm-payment-custom-fields',
},
fields: {
sctm_create_payment_request:
'{"description":"Test","locale":"en_GB","redirectUrl":"https://www.google.com/","billingEmail":"123123"}',
},
},
};

try {
checkPaymentMethodSpecificParameters(CTPayment, CTPayment.paymentMethodInfo.method as string);
} catch (error: unknown) {
expect(error).toBeInstanceOf(CustomError);
expect((error as CustomError).message).toBe('SCTM - validateBlik - billingEmail must be a valid email address.');
expect(logger.error).toBeCalledTimes(1);
expect(logger.error).toBeCalledWith(`SCTM - validateBlik - billingEmail must be a valid email address.`, {
commerceToolsPayment: CTPayment,
});
}
});

it('should should not throw any error or terminate the process if the payment method is blik and the currency code is PLN and the billing email is provided correctly', () => {
it('should not throw any error or terminate the process if the payment method is blik and the currency code is PLN and the billing email is provided correctly', () => {
const CTPayment: Payment = {
id: '5c8b0375-305a-4f19-ae8e-07806b101999',
version: 1,
Expand Down

0 comments on commit c8b4935

Please sign in to comment.