Skip to content

Commit

Permalink
Update stripeController.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
aelassas committed May 7, 2024
1 parent 3d34b05 commit fc4d67a
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
58 changes: 58 additions & 0 deletions api/src/common/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fs from 'node:fs/promises'
import path from 'node:path'
import mongoose from 'mongoose'
import validator from 'validator'
import Stripe from 'stripe'
import { v1 as uuid } from 'uuid'

/**
Expand Down Expand Up @@ -153,3 +154,60 @@ export const isValidEmail = (email?: string) => !!email && validator.isEmail(ema
* @returns {string}
*/
export const generateToken = () => `${uuid()}-${Date.now()}`

/**
* The IETF language tag of the locale Checkout is displayed in.
*
* @param {string} locale
* @returns {Stripe.Checkout.SessionCreateParams.Locale}
*/
export const getStripeLocale = (locale: string): Stripe.Checkout.SessionCreateParams.Locale => {
const locales = [
'bg',
'cs',
'da',
'de',
'el',
'en',
'en-GB',
'es',
'es-419',
'et',
'fi',
'fil',
'fr',
'fr-CA',
'hr',
'hu',
'id',
'it',
'ja',
'ko',
'lt',
'lv',
'ms',
'mt',
'nb',
'nl',
'pl',
'pt',
'pt-BR',
'ro',
'ru',
'sk',
'sl',
'sv',
'th',
'tr',
'vi',
'zh',
'zh-HK',
'zh-TW',
]

if (locales.includes(locale)) {
return locale as Stripe.Checkout.SessionCreateParams.Locale
}

return 'auto'
}
2 changes: 1 addition & 1 deletion api/src/controllers/stripeController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export const createCheckoutSession = async (req: Request, res: Response) => {
mode: 'payment',
return_url: `${helper.trimEnd(env.FRONTEND_HOST, '/')}/checkout-session/{CHECKOUT_SESSION_ID}`,
customer: customer.id,
locale: locale.toLowerCase() as Stripe.Checkout.SessionCreateParams.Locale,
locale: helper.getStripeLocale(locale),
payment_intent_data: {
description,
},
Expand Down

0 comments on commit fc4d67a

Please sign in to comment.