|
@@ -31,16 +31,19 @@ export function toMollieOrderLines(order: Order): CreateParameters['lines'] {
|
|
|
const lines: CreateParameters['lines'] = order.lines.map(line => ({
|
|
const lines: CreateParameters['lines'] = order.lines.map(line => ({
|
|
|
name: line.productVariant.name,
|
|
name: line.productVariant.name,
|
|
|
quantity: line.quantity,
|
|
quantity: line.quantity,
|
|
|
- unitPrice: toAmount(line.proratedUnitPriceWithTax, order.currencyCode), // totalAmount has to match unitPrice * quantity
|
|
|
|
|
|
|
+ unitPrice: toAmount(line.proratedLinePriceWithTax / line.quantity, order.currencyCode), // totalAmount has to match unitPrice * quantity
|
|
|
totalAmount: toAmount(line.proratedLinePriceWithTax, order.currencyCode),
|
|
totalAmount: toAmount(line.proratedLinePriceWithTax, order.currencyCode),
|
|
|
vatRate: String(line.taxRate),
|
|
vatRate: String(line.taxRate),
|
|
|
- vatAmount: toAmount(line.lineTax, order.currencyCode),
|
|
|
|
|
|
|
+ vatAmount: toAmount(
|
|
|
|
|
+ calculateLineTaxAmount(line.taxRate, line.proratedLinePriceWithTax),
|
|
|
|
|
+ order.currencyCode
|
|
|
|
|
+ ),
|
|
|
}));
|
|
}));
|
|
|
// Add shippingLines
|
|
// Add shippingLines
|
|
|
lines.push(...order.shippingLines.map(line => ({
|
|
lines.push(...order.shippingLines.map(line => ({
|
|
|
name: line.shippingMethod?.name || 'Shipping',
|
|
name: line.shippingMethod?.name || 'Shipping',
|
|
|
quantity: 1,
|
|
quantity: 1,
|
|
|
- unitPrice: toAmount(line.priceWithTax, order.currencyCode),
|
|
|
|
|
|
|
+ unitPrice: toAmount(line.discountedPriceWithTax, order.currencyCode),
|
|
|
totalAmount: toAmount(line.discountedPriceWithTax, order.currencyCode),
|
|
totalAmount: toAmount(line.discountedPriceWithTax, order.currencyCode),
|
|
|
vatRate: String(line.taxRate),
|
|
vatRate: String(line.taxRate),
|
|
|
vatAmount: toAmount(line.discountedPriceWithTax - line.discountedPrice, order.currencyCode),
|
|
vatAmount: toAmount(line.discountedPriceWithTax - line.discountedPrice, order.currencyCode),
|
|
@@ -49,7 +52,7 @@ export function toMollieOrderLines(order: Order): CreateParameters['lines'] {
|
|
|
lines.push(...order.surcharges.map(surcharge => ({
|
|
lines.push(...order.surcharges.map(surcharge => ({
|
|
|
name: surcharge.description,
|
|
name: surcharge.description,
|
|
|
quantity: 1,
|
|
quantity: 1,
|
|
|
- unitPrice: toAmount(surcharge.price, order.currencyCode),
|
|
|
|
|
|
|
+ unitPrice: toAmount(surcharge.priceWithTax, order.currencyCode),
|
|
|
totalAmount: toAmount(surcharge.priceWithTax, order.currencyCode),
|
|
totalAmount: toAmount(surcharge.priceWithTax, order.currencyCode),
|
|
|
vatRate: String(surcharge.taxRate),
|
|
vatRate: String(surcharge.taxRate),
|
|
|
vatAmount: toAmount(surcharge.priceWithTax - surcharge.price, order.currencyCode),
|
|
vatAmount: toAmount(surcharge.priceWithTax - surcharge.price, order.currencyCode),
|
|
@@ -67,6 +70,17 @@ export function toAmount(value: number, orderCurrency: string): Amount {
|
|
|
};
|
|
};
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+/**
|
|
|
|
|
+ * Recalculate tax amount per order line instead of per unit for Mollie.
|
|
|
|
|
+ * Vendure calculates tax per unit, but Mollie expects the tax to be calculated per order line (the total of the quantities).
|
|
|
|
|
+ * See https://github.com/vendure-ecommerce/vendure/issues/1939#issuecomment-1362962133 for more information on the rounding issue.
|
|
|
|
|
+ */
|
|
|
|
|
+export function calculateLineTaxAmount(taxRate: number, orderLinePriceWithTax: number): number {
|
|
|
|
|
+ const taxMultiplier = taxRate / 100;
|
|
|
|
|
+ return orderLinePriceWithTax * (taxMultiplier / (1+taxMultiplier)); // I.E. €99,99 * (0,2 ÷ 1,2) with a 20% taxrate
|
|
|
|
|
+
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* Lookup one of Mollies allowed locales based on an orders countrycode or channel default.
|
|
* Lookup one of Mollies allowed locales based on an orders countrycode or channel default.
|
|
|
* If both lookups fail, resolve to en_US to prevent payment failure
|
|
* If both lookups fail, resolve to en_US to prevent payment failure
|