2
0

fix(engine): 🐛 Payment for zero-decimal currencies

This commit is contained in:
Baptiste Arnaud
2022-06-02 10:15:00 +02:00
parent 89d91f9114
commit 274f30c6c2

View File

@ -53,9 +53,9 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
: stripeKeys.live.secretKey, : stripeKeys.live.secretKey,
{ apiVersion: '2020-08-27' } { apiVersion: '2020-08-27' }
) )
const amount = Math.round( const amount =
Number(parseVariables(variables)(inputOptions.amount)) * 100 Number(parseVariables(variables)(inputOptions.amount)) *
) (isZeroDecimalCurrency(inputOptions.currency) ? 1 : 100)
if (isNaN(amount)) return badRequest(res) if (isNaN(amount)) return badRequest(res)
// Create a PaymentIntent with the order amount and currency // Create a PaymentIntent with the order amount and currency
const receiptEmail = parseVariables(variables)( const receiptEmail = parseVariables(variables)(
@ -77,7 +77,9 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
isPreview && stripeKeys.test?.publicKey isPreview && stripeKeys.test?.publicKey
? stripeKeys.test.publicKey ? stripeKeys.test.publicKey
: stripeKeys.live.publicKey, : stripeKeys.live.publicKey,
amountLabel: `${amount / 100}${ amountLabel: `${
amount / (isZeroDecimalCurrency(inputOptions.currency) ? 1 : 100)
}${
currencySymbols[inputOptions.currency] ?? ` ${inputOptions.currency}` currencySymbols[inputOptions.currency] ?? ` ${inputOptions.currency}`
}`, }`,
}) })
@ -108,4 +110,25 @@ const getStripeInfo = async (
return decrypt(credentials.data, credentials.iv) as StripeCredentialsData return decrypt(credentials.data, credentials.iv) as StripeCredentialsData
} }
// https://stripe.com/docs/currencies#zero-decimal
const isZeroDecimalCurrency = (currency: string) =>
[
'BIF',
'CLP',
'DJF',
'GNF',
'JPY',
'KMF',
'KRW',
'MGA',
'PYG',
'RWF',
'UGX',
'VND',
'VUV',
'XAF',
'XOF',
'XPF',
].includes(currency)
export default withSentry(handler) export default withSentry(handler)