Skip to content

Commit d792601

Browse files
refactor: reduce complexity for surcharge logic
1 parent 89fd3de commit d792601

2 files changed

Lines changed: 132 additions & 19 deletions

File tree

processor/src/utils/map.utils.ts

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -145,32 +145,20 @@ const getSpecificPaymentParams = (
145145
}
146146
};
147147

148-
export const createMollieCreatePaymentParams = (
149-
payment: Payment,
150-
extensionUrl: string,
148+
const addAdditionalMollieLines = (
149+
mollieLines: any[],
151150
surchargeAmountInCent: number,
152151
cart: Cart,
153-
banktransferDueDate?: string,
154-
): PaymentCreateParams => {
155-
const { amountPlanned, paymentMethodInfo } = payment;
156-
const [method, issuer] = paymentMethodInfo?.method?.split(',') ?? [null, null];
157-
158-
const paymentRequest = parseStringToJsonObject(
159-
payment.custom?.fields?.[CustomFields.createPayment.request],
160-
CustomFields.createPayment.request,
161-
'SCTM - PAYMENT PROCESSING',
162-
payment.id,
163-
);
164-
165-
const mollieLines = paymentRequest.lines ?? [];
166-
152+
fractionDigits: number,
153+
currencyCode: string,
154+
): void => {
167155
if (surchargeAmountInCent > 0) {
168156
mollieLines.push(
169157
createMollieLineForAdditionalAmount(
170158
MOLLIE_SURCHARGE_LINE_DESCRIPTION,
171159
surchargeAmountInCent,
172-
amountPlanned.fractionDigits,
173-
amountPlanned.currencyCode,
160+
fractionDigits,
161+
currencyCode,
174162
),
175163
);
176164
}
@@ -186,6 +174,33 @@ export const createMollieCreatePaymentParams = (
186174
),
187175
);
188176
}
177+
};
178+
179+
export const createMollieCreatePaymentParams = (
180+
payment: Payment,
181+
extensionUrl: string,
182+
surchargeAmountInCent: number,
183+
cart: Cart,
184+
banktransferDueDate?: string,
185+
): PaymentCreateParams => {
186+
const { amountPlanned, paymentMethodInfo } = payment;
187+
const [method, issuer] = paymentMethodInfo?.method?.split(',') ?? [null, null];
188+
189+
const paymentRequest = parseStringToJsonObject(
190+
payment.custom?.fields?.[CustomFields.createPayment.request],
191+
CustomFields.createPayment.request,
192+
'SCTM - PAYMENT PROCESSING',
193+
payment.id,
194+
);
195+
196+
const mollieLines = paymentRequest.lines ?? [];
197+
addAdditionalMollieLines(
198+
mollieLines,
199+
surchargeAmountInCent,
200+
cart,
201+
amountPlanned.fractionDigits,
202+
amountPlanned.currencyCode,
203+
);
189204

190205
const createPaymentParams = {
191206
amount: makeMollieAmount(amountPlanned, surchargeAmountInCent),

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

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,6 +1254,104 @@ describe('createMollieCreatePaymentParams', () => {
12541254
lines: [],
12551255
});
12561256
});
1257+
1258+
it('should able to create a mollie payment params including both surcharge and shipping lines', async () => {
1259+
const cart = {
1260+
id: 'cart-test-id',
1261+
shippingInfo: {
1262+
price: {
1263+
type: 'centPrecision',
1264+
currencyCode: 'EUR',
1265+
centAmount: 5000,
1266+
fractionDigits: 2,
1267+
},
1268+
},
1269+
} as Cart;
1270+
1271+
const customFieldObject = {
1272+
description: 'Test payment',
1273+
locale: 'en_GB',
1274+
redirectUrl: 'https://example.com/success',
1275+
webhookUrl: 'https://example.com/webhook',
1276+
};
1277+
1278+
const CTPayment: Payment = {
1279+
id: '5c8b0375-305a-4f19-ae8e-07806b101999',
1280+
version: 1,
1281+
createdAt: '2024-07-04T14:07:35.625Z',
1282+
lastModifiedAt: '2024-07-04T14:07:35.625Z',
1283+
amountPlanned: {
1284+
type: 'centPrecision',
1285+
currencyCode: 'EUR',
1286+
centAmount: 1000,
1287+
fractionDigits: 2,
1288+
},
1289+
paymentStatus: {},
1290+
transactions: [],
1291+
interfaceInteractions: [],
1292+
paymentMethodInfo: {
1293+
method: PaymentMethod.klarna,
1294+
},
1295+
custom: {
1296+
type: {
1297+
typeId: 'type',
1298+
id: 'sctm_payment',
1299+
},
1300+
fields: {
1301+
sctm_create_payment_request: JSON.stringify(customFieldObject),
1302+
},
1303+
},
1304+
};
1305+
const extensionUrl = 'https://example.com/webhook';
1306+
const surchargeAmountInCent = 1500;
1307+
1308+
const mollieCreatePaymentParams: PaymentCreateParams = createMollieCreatePaymentParams(
1309+
CTPayment,
1310+
extensionUrl,
1311+
surchargeAmountInCent,
1312+
cart,
1313+
);
1314+
1315+
expect(mollieCreatePaymentParams).toEqual({
1316+
method: PaymentMethod.klarna,
1317+
amount: {
1318+
currency: 'EUR',
1319+
value: '25.00',
1320+
},
1321+
locale: customFieldObject.locale,
1322+
redirectUrl: customFieldObject.redirectUrl,
1323+
webhookUrl: extensionUrl,
1324+
description: customFieldObject.description,
1325+
lines: [
1326+
{
1327+
description: MOLLIE_SURCHARGE_LINE_DESCRIPTION,
1328+
quantity: 1,
1329+
quantityUnit: 'pcs',
1330+
unitPrice: {
1331+
currency: 'EUR',
1332+
value: '15.00',
1333+
},
1334+
totalAmount: {
1335+
currency: 'EUR',
1336+
value: '15.00',
1337+
},
1338+
},
1339+
{
1340+
description: MOLLIE_SHIPPING_LINE_DESCRIPTION,
1341+
quantity: 1,
1342+
quantityUnit: 'pcs',
1343+
unitPrice: {
1344+
currency: 'EUR',
1345+
value: '50.00',
1346+
},
1347+
totalAmount: {
1348+
currency: 'EUR',
1349+
value: '50.00',
1350+
},
1351+
},
1352+
],
1353+
});
1354+
});
12571355
});
12581356

12591357
describe('Test createCartUpdateActions', () => {

0 commit comments

Comments
 (0)