Skip to content

Commit

Permalink
Merge pull request #108 from mollie/bugfix/MOL-577
Browse files Browse the repository at this point in the history
MOL-577: Shipping cost as LineItems
  • Loading branch information
NghiaDTr authored Nov 26, 2024
2 parents 7d51db4 + 8ed3d25 commit 791b4fe
Show file tree
Hide file tree
Showing 5 changed files with 894 additions and 694 deletions.
2 changes: 1 addition & 1 deletion processor/src/service/payment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ export const handleCreatePayment = async (ctPayment: Payment): Promise<Controlle
)
: 0;

const paymentParams = createMollieCreatePaymentParams(ctPayment, extensionUrl, surchargeAmountInCent);
const paymentParams = createMollieCreatePaymentParams(ctPayment, extensionUrl, surchargeAmountInCent, cart);

if (method && method.toString() === 'googlepay') {
paymentParams.method = PaymentMethod.creditcard;
Expand Down
2 changes: 2 additions & 0 deletions processor/src/utils/constant.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,5 @@ export const CUSTOM_OBJECT_CONTAINER_NAME = 'sctm-app-methods';
export const MOLLIE_SURCHARGE_CUSTOM_LINE_ITEM = 'mollie-surcharge-line-item';

export const MOLLIE_SURCHARGE_LINE_DESCRIPTION = 'Total surcharge amount';

export const MOLLIE_SHIPPING_LINE_DESCRIPTION = 'Shipping amount';
46 changes: 35 additions & 11 deletions processor/src/utils/map.utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { CustomFields, MOLLIE_SURCHARGE_CUSTOM_LINE_ITEM, MOLLIE_SURCHARGE_LINE_DESCRIPTION } from './constant.utils';
import {
CustomFields,
MOLLIE_SHIPPING_LINE_DESCRIPTION,
MOLLIE_SURCHARGE_CUSTOM_LINE_ITEM,
MOLLIE_SURCHARGE_LINE_DESCRIPTION,
} from './constant.utils';
import { logger } from './logger.utils';
import { calculateDueDate, makeMollieAmount } from './mollie.utils';
import { CustomPaymentMethod, ParsedMethodsRequestType } from '../types/mollie.types';
Expand Down Expand Up @@ -109,6 +114,7 @@ export const createMollieCreatePaymentParams = (
payment: Payment,
extensionUrl: string,
surchargeAmountInCent: number,
cart: Cart,
): PaymentCreateParams => {
const { amountPlanned, paymentMethodInfo } = payment;

Expand All @@ -121,16 +127,31 @@ export const createMollieCreatePaymentParams = (
);

const mollieLines = paymentRequest.lines ?? [];

// Add another line for creating Mollie payment request if surcharge exists
if (surchargeAmountInCent > 0) {
mollieLines.push(
createMollieLineForSurchargeAmount(
createMollieLineForAdditionalAmount(
MOLLIE_SURCHARGE_LINE_DESCRIPTION,
surchargeAmountInCent,
amountPlanned.fractionDigits,
amountPlanned.currencyCode,
),
);
}

// Add another line for creating Mollie payment request if shipping cost exists
if (cart?.shippingInfo?.price) {
mollieLines.push(
createMollieLineForAdditionalAmount(
MOLLIE_SHIPPING_LINE_DESCRIPTION,
cart.shippingInfo.price.centAmount,
cart.shippingInfo.price.fractionDigits,
cart.shippingInfo.price.currencyCode,
),
);
}

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

const createPaymentParams = {
Expand Down Expand Up @@ -194,21 +215,24 @@ export const createCartUpdateActions = (
return updateActions;
};

export const createMollieLineForSurchargeAmount = (
surchargeAmountInCent: number,
export const createMollieLineForAdditionalAmount = (
description: string,
amountInCent: number,
fractionDigits: number,
currency: string,
quantity: number = 1,
quantityUnit: string = 'pcs',
) => {
const totalSurchargeAmount = {
const unitPrice = {
currency,
value: convertCentToEUR(surchargeAmountInCent, fractionDigits).toFixed(2),
value: convertCentToEUR(amountInCent, fractionDigits).toFixed(2),
};

return {
description: MOLLIE_SURCHARGE_LINE_DESCRIPTION,
quantity: 1,
quantityUnit: 'pcs',
unitPrice: totalSurchargeAmount,
totalAmount: totalSurchargeAmount,
description,
quantity,
quantityUnit,
unitPrice: unitPrice,
totalAmount: unitPrice,
};
};
6 changes: 6 additions & 0 deletions processor/tests/utils/constant.utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
DEFAULT_DUE_DATE,
MOLLIE_SURCHARGE_CUSTOM_LINE_ITEM,
MOLLIE_SURCHARGE_LINE_DESCRIPTION,
MOLLIE_SHIPPING_LINE_DESCRIPTION,
} from '../../src/utils/constant.utils';
import { version } from '../../package.json';

Expand Down Expand Up @@ -90,4 +91,9 @@ describe('Test constant.utils.ts', () => {
expect(MOLLIE_SURCHARGE_LINE_DESCRIPTION).toBeDefined();
expect(MOLLIE_SURCHARGE_LINE_DESCRIPTION).toBe('Total surcharge amount');
});

test('should return correct {MOLLIE_SHIPPING_LINE_DESCRIPTION} pattern', () => {
expect(MOLLIE_SHIPPING_LINE_DESCRIPTION).toBeDefined();
expect(MOLLIE_SHIPPING_LINE_DESCRIPTION).toBe('Shipping amount');
});
});
Loading

0 comments on commit 791b4fe

Please sign in to comment.