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

This commit is contained in:
Sebastian Rindom
2022-07-13 09:26:45 +02:00
committed by GitHub
parent 4e375c2203
commit c20d720040
4 changed files with 29 additions and 17 deletions
+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",
"id": "pl_no_customer_groups",
"name": "Loyalty Reward - Winter Sale",
"prices": Array [
"prices": ArrayContaining [
Object {
"amount": 100,
"created_at": Any<String>,
@@ -471,7 +471,7 @@ describe("/admin/price-lists", () => {
status: "draft",
starts_at: "2022-09-01T00:00:00.000Z",
ends_at: "2022-12-31T00:00:00.000Z",
prices: [
prices: expect.arrayContaining([
{
id: expect.any(String),
amount: 100,
@@ -483,6 +483,7 @@ describe("/admin/price-lists", () => {
region_id: null,
created_at: expect.any(String),
updated_at: expect.any(String),
deleted_at: null,
},
{
id: expect.any(String),
@@ -495,6 +496,7 @@ describe("/admin/price-lists", () => {
region_id: null,
created_at: expect.any(String),
updated_at: expect.any(String),
deleted_at: null,
},
{
id: expect.any(String),
@@ -507,6 +509,7 @@ describe("/admin/price-lists", () => {
region_id: null,
created_at: expect.any(String),
updated_at: expect.any(String),
deleted_at: null,
},
{
id: expect.any(String),
@@ -534,7 +537,7 @@ describe("/admin/price-lists", () => {
updated_at: expect.any(String),
deleted_at: null,
},
],
]),
customer_groups: [
{
id: "customer-group-1",
@@ -76,30 +76,34 @@ class KlarnaProviderService extends PaymentService {
const name = []
let total = 0
let tax = 0
let taxRate = 0
for (const next of cart.shipping_methods) {
const totals = await this.totalsService_.getShippingMethodTotals(
next,
cart,
{
include_tax: true,
}
)
if (cart.shipping_total > 0) {
for (const next of cart.shipping_methods) {
const totals = await this.totalsService_.getShippingMethodTotals(
next,
cart,
{
include_tax: true,
}
)
name.push(next?.shipping_option.name)
total += totals.total
tax += totals.tax_total
const methodTaxRate =
totals.tax_lines.reduce((acc, next) => acc + next.rate, 0) / 100
name.push(next?.shipping_option.name)
taxRate += (totals.total / cart.shipping_total) * methodTaxRate
tax += totals.tax_total
}
}
const taxRate = tax / (total - tax)
order_lines.push({
name: name?.join(" + ") || "Shipping fee",
quantity: 1,
type: "shipping_fee",
unit_price: total,
tax_rate: taxRate * 10000,
total_amount: total,
total_amount: cart.shipping_total,
total_tax_amount: tax,
})
}