fix(payment-stripe): fix smallest unit calculation (#8663)
This commit is contained in:
1 parent
aa6e504771
commit
dd82a56ec5
3 files changed
+22
-3
No files matched your search
@@ -18,6 +18,7 @@ import {
|
|||||||
PaymentSessionStatus,
|
PaymentSessionStatus,
|
||||||
isDefined,
|
isDefined,
|
||||||
isPaymentProviderError,
|
isPaymentProviderError,
|
||||||
|
isPresent,
|
||||||
} from "@medusajs/utils"
|
} from "@medusajs/utils"
|
||||||
import {
|
import {
|
||||||
ErrorCodes,
|
ErrorCodes,
|
||||||
@@ -281,7 +282,7 @@ abstract class StripeBase extends AbstractPaymentProvider<StripeOptions> {
|
|||||||
|
|
||||||
return result
|
return result
|
||||||
} else {
|
} else {
|
||||||
if (amount && data.amount === amountNumeric) {
|
if (isPresent(amount) && data.amount === amountNumeric) {
|
||||||
return { data }
|
return { data }
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -302,7 +303,7 @@ abstract class StripeBase extends AbstractPaymentProvider<StripeOptions> {
|
|||||||
try {
|
try {
|
||||||
// Prevent from updating the amount from here as it should go through
|
// Prevent from updating the amount from here as it should go through
|
||||||
// the updatePayment method to perform the correct logic
|
// the updatePayment method to perform the correct logic
|
||||||
if (data.amount) {
|
if (isPresent(data.amount)) {
|
||||||
throw new MedusaError(
|
throw new MedusaError(
|
||||||
MedusaError.Types.INVALID_DATA,
|
MedusaError.Types.INVALID_DATA,
|
||||||
"Cannot update amount, use updatePayment instead"
|
"Cannot update amount, use updatePayment instead"
|
||||||
|
|||||||
@@ -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
|
numeric = Math.ceil(numeric / 10) * 10
|
||||||
}
|
}
|
||||||
|
|
||||||
return numeric
|
return parseInt(numeric.toString().split(".").shift()!, 10)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in new issue
Block a user