fix(medusa-payment-klarna): Fix division by zero on free shipping (#1840)
This commit is contained in:
1 parent
4e375c2203
commit
c20d720040
4 files changed
+29
-17
No files matched your search
@@ -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,30 +76,34 @@ class KlarnaProviderService extends PaymentService {
|
|||||||
const name = []
|
const name = []
|
||||||
let total = 0
|
let total = 0
|
||||||
let tax = 0
|
let tax = 0
|
||||||
|
let taxRate = 0
|
||||||
|
|
||||||
for (const next of cart.shipping_methods) {
|
if (cart.shipping_total > 0) {
|
||||||
const totals = await this.totalsService_.getShippingMethodTotals(
|
for (const next of cart.shipping_methods) {
|
||||||
next,
|
const totals = await this.totalsService_.getShippingMethodTotals(
|
||||||
cart,
|
next,
|
||||||
{
|
cart,
|
||||||
include_tax: true,
|
{
|
||||||
}
|
include_tax: true,
|
||||||
)
|
}
|
||||||
|
)
|
||||||
|
|
||||||
name.push(next?.shipping_option.name)
|
const methodTaxRate =
|
||||||
total += totals.total
|
totals.tax_lines.reduce((acc, next) => acc + next.rate, 0) / 100
|
||||||
tax += totals.tax_total
|
|
||||||
|
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({
|
order_lines.push({
|
||||||
name: name?.join(" + ") || "Shipping fee",
|
name: name?.join(" + ") || "Shipping fee",
|
||||||
quantity: 1,
|
quantity: 1,
|
||||||
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,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user