Skip to content

Commit c8b4935

Browse files
authored
Merge pull request #125 from mollie/MOL-591/PICT-266
PICT-266: CR - Update billingAddress.email parameter
2 parents d8ace68 + dec12c2 commit c8b4935

File tree

6 files changed

+83
-130
lines changed

6 files changed

+83
-130
lines changed

processor/src/utils/app.utils.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,6 @@ export function removeEmptyProperties(obj: object) {
7070
return clonedObject;
7171
}
7272

73-
/**
74-
* Validates an email address using a regular expression.
75-
*
76-
* @param {string} email - The email address to validate.
77-
* @return {boolean} Returns true if the email is valid, false otherwise.
78-
*/
79-
export function validateEmail(email: string): boolean {
80-
const emailRegex: RegExp = /^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$/;
81-
82-
return emailRegex.test(email);
83-
}
84-
8573
export const convertCentToEUR = (amount: number, fractionDigits: number): number => {
8674
return amount / Math.pow(10, fractionDigits);
8775
};

processor/src/utils/map.utils.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,6 @@ const getSpecificPaymentParams = (method: PaymentMethod | CustomPaymentMethod, p
8787
return {
8888
dueDate: calculateDueDate(readConfiguration().mollie.bankTransferDueDate),
8989
};
90-
case PaymentMethod.przelewy24:
91-
return { billingEmail: paymentRequest.billingEmail ?? '' };
9290
case PaymentMethod.paypal:
9391
return {
9492
sessionId: paymentRequest.sessionId ?? '',
@@ -101,10 +99,6 @@ const getSpecificPaymentParams = (method: PaymentMethod | CustomPaymentMethod, p
10199
};
102100
case PaymentMethod.creditcard:
103101
return { cardToken: paymentRequest.cardToken ?? '' };
104-
case CustomPaymentMethod.blik:
105-
return {
106-
billingEmail: paymentRequest.billingEmail ?? '',
107-
};
108102
default:
109103
return {};
110104
}
@@ -154,12 +148,24 @@ export const createMollieCreatePaymentParams = (
154148

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

151+
let billingAddress = paymentRequest.billingAddress;
152+
if (
153+
(method === CustomPaymentMethod.blik || method === PaymentMethod.przelewy24) &&
154+
!billingAddress?.email &&
155+
paymentRequest.billingEmail
156+
) {
157+
billingAddress = {
158+
...paymentRequest.billingAddress,
159+
email: paymentRequest.billingEmail,
160+
};
161+
}
162+
157163
const createPaymentParams = {
158164
amount: makeMollieAmount(amountPlanned, surchargeAmountInCent),
159165
description: paymentRequest.description ?? '',
160166
redirectUrl: paymentRequest.redirectUrl ?? null,
161167
webhookUrl: defaultWebhookEndpoint,
162-
billingAddress: paymentRequest.billingAddress ?? {},
168+
billingAddress: billingAddress ?? {},
163169
shippingAddress: paymentRequest.shippingAddress ?? {},
164170
locale: paymentRequest.locale ?? null,
165171
method: method as PaymentMethod,

processor/src/validators/payment.validators.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { logger } from '../utils/logger.utils';
66
import { ConnectorActions, CustomFields } from '../utils/constant.utils';
77
import { DeterminePaymentActionType } from '../types/controller.types';
88
import { CTTransactionState, CTTransactionType } from '../types/commercetools.types';
9-
import { parseStringToJsonObject, validateEmail } from '../utils/app.utils';
9+
import { parseStringToJsonObject } from '../utils/app.utils';
1010
import { readConfiguration } from '../utils/config.utils';
1111
import { toBoolean } from 'validator';
1212
import { CustomPaymentMethod, SupportedPaymentMethods } from '../types/mollie.types';
@@ -46,24 +46,12 @@ export const validateBanktransfer = (paymentCustomFields: any, ctPayment: CTPaym
4646
ctPayment,
4747
);
4848
}
49-
50-
if (!validateEmail(paymentCustomFields.billingAddress.email)) {
51-
throwError('validateBanktransfer', 'email must be a valid email address.', ctPayment);
52-
}
5349
};
5450

5551
const validateBlik = (paymentCustomFields: any, ctPayment: CTPayment): void => {
5652
if (ctPayment.amountPlanned.currencyCode.toLowerCase() !== 'pln') {
5753
throwError('validateBlik', 'Currency Code must be PLN for payment method BLIK.', ctPayment);
5854
}
59-
60-
if (!paymentCustomFields?.billingEmail) {
61-
throwError('validateBlik', 'billingEmail is required for payment method BLIK.', ctPayment);
62-
}
63-
64-
if (!validateEmail(paymentCustomFields.billingEmail)) {
65-
throwError('validateBlik', 'billingEmail must be a valid email address.', ctPayment);
66-
}
6755
};
6856

6957
export const paymentMethodRequiredExtraParameters = (

processor/tests/utils/app.utils.spec.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
parseStringToJsonObject,
77
removeEmptyProperties,
88
roundSurchargeAmountToCent,
9-
validateEmail,
109
} from '../../src/utils/app.utils';
1110
import { logger } from '../../src/utils/logger.utils';
1211
import CustomError from '../../src/errors/custom.error';
@@ -91,16 +90,6 @@ describe('Test removeEmptyProperties', () => {
9190
});
9291
});
9392

94-
describe('Test validateEmail', () => {
95-
it('should return false when the targeted string is an invalid email', () => {
96-
expect(validateEmail('123123')).toBe(false);
97-
});
98-
99-
it('should return true when the targeted string is a valid email', () => {
100-
expect(validateEmail('n.tran@shopmacher.de')).toBe(true);
101-
});
102-
});
103-
10493
describe('Test convertCentToEUR', () => {
10594
it('should return correct result', () => {
10695
expect(convertCentToEUR(100, 2)).toBe(1);

processor/tests/utils/map.utils.spec.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,74 @@ describe('createMollieCreatePaymentParams', () => {
842842
// });
843843
// });
844844

845+
it('should be able to create mollie payment params including billing email address with payment przelewy24', async () => {
846+
const cart = {
847+
id: 'cart-test-id',
848+
shippingInfo: {},
849+
} as Cart;
850+
851+
const customFieldObject = {
852+
description: 'Test payment',
853+
locale: 'en_GB',
854+
redirectUrl: 'https://example.com/success',
855+
webhookUrl: 'https://example.com/webhook',
856+
billingEmail: 'dummy@gmail.com',
857+
};
858+
859+
const CTPayment: Payment = {
860+
id: '5c8b0375-305a-4f19-ae8e-07806b101999',
861+
version: 1,
862+
createdAt: '2024-07-04T14:07:35.625Z',
863+
lastModifiedAt: '2024-07-04T14:07:35.625Z',
864+
amountPlanned: {
865+
type: 'centPrecision',
866+
currencyCode: 'EUR',
867+
centAmount: 2000,
868+
fractionDigits: 2,
869+
},
870+
paymentStatus: {},
871+
transactions: [],
872+
interfaceInteractions: [],
873+
paymentMethodInfo: {
874+
method: PaymentMethod.przelewy24,
875+
},
876+
custom: {
877+
type: {
878+
typeId: 'type',
879+
id: 'sctm_payment',
880+
},
881+
fields: {
882+
sctm_create_payment_request: JSON.stringify(customFieldObject),
883+
},
884+
},
885+
};
886+
887+
const extensionUrl = 'https://example.com/webhook';
888+
889+
const mollieCreatePaymentParams: PaymentCreateParams = createMollieCreatePaymentParams(
890+
CTPayment,
891+
extensionUrl,
892+
0,
893+
cart,
894+
);
895+
896+
expect(mollieCreatePaymentParams).toEqual({
897+
method: PaymentMethod.przelewy24,
898+
amount: {
899+
currency: 'EUR',
900+
value: '20.00',
901+
},
902+
locale: customFieldObject.locale,
903+
redirectUrl: customFieldObject.redirectUrl,
904+
webhookUrl: extensionUrl,
905+
description: customFieldObject.description,
906+
billingAddress: {
907+
email: customFieldObject.billingEmail,
908+
},
909+
lines: [],
910+
});
911+
});
912+
845913
it('should able to create a mollie payment params from CommerceTools payment object including a line item for shipping amount', async () => {
846914
const cart = {
847915
id: 'cart-test-id',

processor/tests/validators/payment.validators.spec.ts

Lines changed: 1 addition & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -745,93 +745,7 @@ describe('checkPaymentMethodSpecificParameters', () => {
745745
}
746746
});
747747

748-
it('should throw an error if the payment method is blik and the billing email is not provided', () => {
749-
const CTPayment: Payment = {
750-
id: '5c8b0375-305a-4f19-ae8e-07806b101999',
751-
version: 1,
752-
createdAt: '2024-07-04T14:07:35.625Z',
753-
lastModifiedAt: '2024-07-04T14:07:35.625Z',
754-
amountPlanned: {
755-
type: 'centPrecision',
756-
currencyCode: 'PLN',
757-
centAmount: 1000,
758-
fractionDigits: 2,
759-
},
760-
paymentStatus: {},
761-
transactions: [],
762-
interfaceInteractions: [],
763-
paymentMethodInfo: {
764-
method: 'blik',
765-
},
766-
custom: {
767-
type: {
768-
typeId: 'type',
769-
id: 'sctm-payment-custom-fields',
770-
},
771-
fields: {
772-
sctm_create_payment_request:
773-
'{"description":"Test","locale":"en_GB","redirectUrl":"https://www.google.com/"}',
774-
},
775-
},
776-
};
777-
778-
try {
779-
checkPaymentMethodSpecificParameters(CTPayment, CTPayment.paymentMethodInfo.method as string);
780-
} catch (error: unknown) {
781-
expect(error).toBeInstanceOf(CustomError);
782-
expect((error as CustomError).message).toBe(
783-
'SCTM - validateBlik - billingEmail is required for payment method BLIK.',
784-
);
785-
expect(logger.error).toBeCalledTimes(1);
786-
expect(logger.error).toBeCalledWith(`SCTM - validateBlik - billingEmail is required for payment method BLIK.`, {
787-
commerceToolsPayment: CTPayment,
788-
});
789-
}
790-
});
791-
792-
it('should throw an error if the payment method is blik and the billing email is provided incorrectly', () => {
793-
const CTPayment: Payment = {
794-
id: '5c8b0375-305a-4f19-ae8e-07806b101999',
795-
version: 1,
796-
createdAt: '2024-07-04T14:07:35.625Z',
797-
lastModifiedAt: '2024-07-04T14:07:35.625Z',
798-
amountPlanned: {
799-
type: 'centPrecision',
800-
currencyCode: 'PLN',
801-
centAmount: 1000,
802-
fractionDigits: 2,
803-
},
804-
paymentStatus: {},
805-
transactions: [],
806-
interfaceInteractions: [],
807-
paymentMethodInfo: {
808-
method: 'blik',
809-
},
810-
custom: {
811-
type: {
812-
typeId: 'type',
813-
id: 'sctm-payment-custom-fields',
814-
},
815-
fields: {
816-
sctm_create_payment_request:
817-
'{"description":"Test","locale":"en_GB","redirectUrl":"https://www.google.com/","billingEmail":"123123"}',
818-
},
819-
},
820-
};
821-
822-
try {
823-
checkPaymentMethodSpecificParameters(CTPayment, CTPayment.paymentMethodInfo.method as string);
824-
} catch (error: unknown) {
825-
expect(error).toBeInstanceOf(CustomError);
826-
expect((error as CustomError).message).toBe('SCTM - validateBlik - billingEmail must be a valid email address.');
827-
expect(logger.error).toBeCalledTimes(1);
828-
expect(logger.error).toBeCalledWith(`SCTM - validateBlik - billingEmail must be a valid email address.`, {
829-
commerceToolsPayment: CTPayment,
830-
});
831-
}
832-
});
833-
834-
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', () => {
748+
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', () => {
835749
const CTPayment: Payment = {
836750
id: '5c8b0375-305a-4f19-ae8e-07806b101999',
837751
version: 1,

0 commit comments

Comments
 (0)