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

This commit is contained in:
Carlos R. L. Rodrigues
2024-08-19 14:19:49 -03:00
committed by GitHub
parent aa6e504771
commit dd82a56ec5
3 changed files with 22 additions and 3 deletions

View File

@@ -18,6 +18,7 @@ import {
PaymentSessionStatus,
isDefined,
isPaymentProviderError,
isPresent,
} from "@medusajs/utils"
import {
ErrorCodes,
@@ -281,7 +282,7 @@ abstract class StripeBase extends AbstractPaymentProvider<StripeOptions> {
return result
} else {
if (amount && data.amount === amountNumeric) {
if (isPresent(amount) && data.amount === amountNumeric) {
return { data }
}
@@ -302,7 +303,7 @@ abstract class StripeBase extends AbstractPaymentProvider<StripeOptions> {
try {
// Prevent from updating the amount from here as it should go through
// the updatePayment method to perform the correct logic
if (data.amount) {
if (isPresent(data.amount)) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
"Cannot update amount, use updatePayment instead"

View File

@@ -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)
})
})

View File

@@ -56,7 +56,7 @@ export function getSmallestUnit(
numeric = Math.ceil(numeric / 10) * 10
}
return numeric
return parseInt(numeric.toString().split(".").shift()!, 10)
}
/**