fix(medusa-payment-klarna): Fix division by zero on free shipping (#1840)

This commit is contained in:
Sebastian Rindom authored and GitHub committed 2022-07-13 09:26:45 +02:00
1 parent 4e375c2203
commit c20d720040
4 files changed
+19 -7

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
---
"medusa-payment-klarna": patch
---
Bug fixed where the free shipping tax rate was incorrect due to division by zero.
@@ -75,7 +75,7 @@ Object {
"ends_at": "2022-12-31T00:00:00.000Z", "ends_at": "2022-12-31T00:00:00.000Z",
"id": "pl_no_customer_groups", "id": "pl_no_customer_groups",
"name": "Loyalty Reward - Winter Sale", "name": "Loyalty Reward - Winter Sale",
"prices": Array [ "prices": ArrayContaining [
Object { Object {
"amount": 100, "amount": 100,
"created_at": Any<String>, "created_at": Any<String>,
@@ -471,7 +471,7 @@ describe("/admin/price-lists", () => {
status: "draft", status: "draft",
starts_at: "2022-09-01T00:00:00.000Z", starts_at: "2022-09-01T00:00:00.000Z",
ends_at: "2022-12-31T00:00:00.000Z", ends_at: "2022-12-31T00:00:00.000Z",
prices: [ prices: expect.arrayContaining([
{ {
id: expect.any(String), id: expect.any(String),
amount: 100, amount: 100,
@@ -483,6 +483,7 @@ describe("/admin/price-lists", () => {
region_id: null, region_id: null,
created_at: expect.any(String), created_at: expect.any(String),
updated_at: expect.any(String), updated_at: expect.any(String),
deleted_at: null,
}, },
{ {
id: expect.any(String), id: expect.any(String),
@@ -495,6 +496,7 @@ describe("/admin/price-lists", () => {
region_id: null, region_id: null,
created_at: expect.any(String), created_at: expect.any(String),
updated_at: expect.any(String), updated_at: expect.any(String),
deleted_at: null,
}, },
{ {
id: expect.any(String), id: expect.any(String),
@@ -507,6 +509,7 @@ describe("/admin/price-lists", () => {
region_id: null, region_id: null,
created_at: expect.any(String), created_at: expect.any(String),
updated_at: expect.any(String), updated_at: expect.any(String),
deleted_at: null,
}, },
{ {
id: expect.any(String), id: expect.any(String),
@@ -534,7 +537,7 @@ describe("/admin/price-lists", () => {
updated_at: expect.any(String), updated_at: expect.any(String),
deleted_at: null, deleted_at: null,
}, },
], ]),
customer_groups: [ customer_groups: [
{ {
id: "customer-group-1", id: "customer-group-1",
@@ -76,7 +76,9 @@ class KlarnaProviderService extends PaymentService {
const name = [] const name = []
let total = 0 let total = 0
let tax = 0 let tax = 0
let taxRate = 0
if (cart.shipping_total > 0) {
for (const next of cart.shipping_methods) { for (const next of cart.shipping_methods) {
const totals = await this.totalsService_.getShippingMethodTotals( const totals = await this.totalsService_.getShippingMethodTotals(
next, next,
@@ -86,12 +88,14 @@ class KlarnaProviderService extends PaymentService {
} }
) )
const methodTaxRate =
totals.tax_lines.reduce((acc, next) => acc + next.rate, 0) / 100
name.push(next?.shipping_option.name) name.push(next?.shipping_option.name)
total += totals.total taxRate += (totals.total / cart.shipping_total) * methodTaxRate
tax += totals.tax_total tax += totals.tax_total
} }
}
const taxRate = tax / (total - tax)
order_lines.push({ order_lines.push({
name: name?.join(" + ") || "Shipping fee", name: name?.join(" + ") || "Shipping fee",
@@ -99,7 +103,7 @@ class KlarnaProviderService extends PaymentService {
type: "shipping_fee", type: "shipping_fee",
unit_price: total, unit_price: total,
tax_rate: taxRate * 10000, tax_rate: taxRate * 10000,
total_amount: total, total_amount: cart.shipping_total,
total_tax_amount: tax, total_tax_amount: tax,
}) })
} }