From 274f30c6c23575ad435ebdb70d3d863861c0a5e9 Mon Sep 17 00:00:00 2001 From: Baptiste Arnaud Date: Thu, 2 Jun 2022 10:15:00 +0200 Subject: [PATCH] =?UTF-8?q?fix(engine):=20=F0=9F=90=9B=20Payment=20for=20z?= =?UTF-8?q?ero-decimal=20currencies?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../stripe/createPaymentIntent.ts | 31 ++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/apps/viewer/pages/api/integrations/stripe/createPaymentIntent.ts b/apps/viewer/pages/api/integrations/stripe/createPaymentIntent.ts index c1df0d606..5bb2ad186 100644 --- a/apps/viewer/pages/api/integrations/stripe/createPaymentIntent.ts +++ b/apps/viewer/pages/api/integrations/stripe/createPaymentIntent.ts @@ -53,9 +53,9 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => { : stripeKeys.live.secretKey, { apiVersion: '2020-08-27' } ) - const amount = Math.round( - Number(parseVariables(variables)(inputOptions.amount)) * 100 - ) + const amount = + Number(parseVariables(variables)(inputOptions.amount)) * + (isZeroDecimalCurrency(inputOptions.currency) ? 1 : 100) if (isNaN(amount)) return badRequest(res) // Create a PaymentIntent with the order amount and currency const receiptEmail = parseVariables(variables)( @@ -77,7 +77,9 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => { isPreview && stripeKeys.test?.publicKey ? stripeKeys.test.publicKey : stripeKeys.live.publicKey, - amountLabel: `${amount / 100}${ + amountLabel: `${ + amount / (isZeroDecimalCurrency(inputOptions.currency) ? 1 : 100) + }${ currencySymbols[inputOptions.currency] ?? ` ${inputOptions.currency}` }`, }) @@ -108,4 +110,25 @@ const getStripeInfo = async ( 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)