fix(payment-stripe): fix smallest unit calculation (#8663)

This commit is contained in:
Carlos R. L. Rodrigues
2024-08-19 19:19:49 +02:00
committed by GitHub
parent aa6e504771
commit dd82a56ec5
3 changed files with 22 additions and 3 deletions
@@ -0,0 +1,18 @@
import { getSmallestUnit } from "../get-smallest-unit"
describe("getSmallestUnit", () => {
it("should convert an amount to the format required by Stripe based on currency", () => {
// 0 decimals
expect(getSmallestUnit(50098, "JPY")).toBe(50098)
// 3 decimals
expect(getSmallestUnit(5.124, "KWD")).toBe(5130)
// 2 decimals
expect(getSmallestUnit(100.54, "USD")).toBe(10054)
expect(getSmallestUnit(5.126, "KWD")).toBe(5130)
expect(getSmallestUnit(0.54, "USD")).toBe(54)
expect(getSmallestUnit(0.054, "USD")).toBe(5)
expect(getSmallestUnit(0.005104, "USD")).toBe(0)
})
})
@@ -56,7 +56,7 @@ export function getSmallestUnit(
numeric = Math.ceil(numeric / 10) * 10
}
return numeric
return parseInt(numeric.toString().split(".").shift()!, 10)
}
/**