📄 Add Commercial License for ee folder (#1532)
This commit is contained in:
51
ee/packages/billing/helpers/createCheckoutSessionUrl.ts
Normal file
51
ee/packages/billing/helpers/createCheckoutSessionUrl.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { env } from '@typebot.io/env'
|
||||
import Stripe from 'stripe'
|
||||
|
||||
type Props = {
|
||||
customerId: string
|
||||
workspaceId: string
|
||||
currency: 'usd' | 'eur'
|
||||
plan: 'STARTER' | 'PRO'
|
||||
returnUrl: string
|
||||
userId: string
|
||||
}
|
||||
|
||||
export const createCheckoutSessionUrl =
|
||||
(stripe: Stripe) =>
|
||||
async ({ customerId, workspaceId, currency, plan, returnUrl }: Props) => {
|
||||
const session = await stripe.checkout.sessions.create({
|
||||
success_url: `${returnUrl}?stripe=${plan}&success=true`,
|
||||
cancel_url: `${returnUrl}?stripe=cancel`,
|
||||
allow_promotion_codes: true,
|
||||
customer: customerId,
|
||||
customer_update: {
|
||||
address: 'auto',
|
||||
name: 'never',
|
||||
},
|
||||
mode: 'subscription',
|
||||
metadata: {
|
||||
workspaceId,
|
||||
plan,
|
||||
},
|
||||
currency,
|
||||
billing_address_collection: 'required',
|
||||
automatic_tax: { enabled: true },
|
||||
line_items: [
|
||||
{
|
||||
price:
|
||||
plan === 'STARTER'
|
||||
? env.STRIPE_STARTER_PRICE_ID
|
||||
: env.STRIPE_PRO_PRICE_ID,
|
||||
quantity: 1,
|
||||
},
|
||||
{
|
||||
price:
|
||||
plan === 'STARTER'
|
||||
? env.STRIPE_STARTER_CHATS_PRICE_ID
|
||||
: env.STRIPE_PRO_CHATS_PRICE_ID,
|
||||
},
|
||||
],
|
||||
})
|
||||
|
||||
return session.url
|
||||
}
|
||||
21
ee/packages/billing/helpers/formatPrice.ts
Normal file
21
ee/packages/billing/helpers/formatPrice.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { guessIfUserIsEuropean } from './guessIfUserIsEuropean'
|
||||
|
||||
type FormatPriceParams = {
|
||||
currency?: 'eur' | 'usd'
|
||||
maxFractionDigits?: number
|
||||
}
|
||||
|
||||
export const formatPrice = (
|
||||
price: number,
|
||||
{ currency, maxFractionDigits = 0 }: FormatPriceParams = {
|
||||
maxFractionDigits: 0,
|
||||
}
|
||||
) => {
|
||||
const isEuropean = guessIfUserIsEuropean()
|
||||
const formatter = new Intl.NumberFormat(isEuropean ? 'fr-FR' : 'en-US', {
|
||||
style: 'currency',
|
||||
currency: currency?.toUpperCase() ?? (isEuropean ? 'EUR' : 'USD'),
|
||||
maximumFractionDigits: maxFractionDigits,
|
||||
})
|
||||
return formatter.format(price)
|
||||
}
|
||||
19
ee/packages/billing/helpers/getChatsLimit.ts
Normal file
19
ee/packages/billing/helpers/getChatsLimit.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { Plan } from '@typebot.io/prisma'
|
||||
import { chatsLimits } from '../constants'
|
||||
import { Workspace } from '@typebot.io/schemas'
|
||||
|
||||
export const getChatsLimit = ({
|
||||
plan,
|
||||
customChatsLimit,
|
||||
}: Pick<Workspace, 'plan'> & {
|
||||
customChatsLimit?: Workspace['customChatsLimit']
|
||||
}) => {
|
||||
if (
|
||||
plan === Plan.UNLIMITED ||
|
||||
plan === Plan.LIFETIME ||
|
||||
plan === Plan.OFFERED
|
||||
)
|
||||
return 'inf'
|
||||
if (plan === Plan.CUSTOM) return customChatsLimit ?? 'inf'
|
||||
return chatsLimits[plan]
|
||||
}
|
||||
12
ee/packages/billing/helpers/getSeatsLimit.ts
Normal file
12
ee/packages/billing/helpers/getSeatsLimit.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { Workspace } from '@typebot.io/schemas'
|
||||
import { seatsLimits } from '../constants'
|
||||
import { Plan } from '@typebot.io/prisma'
|
||||
|
||||
export const getSeatsLimit = ({
|
||||
plan,
|
||||
customSeatsLimit,
|
||||
}: Pick<Workspace, 'plan' | 'customSeatsLimit'>) => {
|
||||
if (plan === Plan.UNLIMITED) return 'inf'
|
||||
if (plan === Plan.CUSTOM) return customSeatsLimit ? customSeatsLimit : 'inf'
|
||||
return seatsLimits[plan]
|
||||
}
|
||||
56
ee/packages/billing/helpers/guessIfUserIsEuropean.ts
Normal file
56
ee/packages/billing/helpers/guessIfUserIsEuropean.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
const europeanUnionCountryCodes = [
|
||||
'AT',
|
||||
'BE',
|
||||
'BG',
|
||||
'CY',
|
||||
'CZ',
|
||||
'DE',
|
||||
'DK',
|
||||
'EE',
|
||||
'ES',
|
||||
'FI',
|
||||
'FR',
|
||||
'GR',
|
||||
'HR',
|
||||
'HU',
|
||||
'IE',
|
||||
'IT',
|
||||
'LT',
|
||||
'LU',
|
||||
'LV',
|
||||
'MT',
|
||||
'NL',
|
||||
'PL',
|
||||
'PT',
|
||||
'RO',
|
||||
'SE',
|
||||
'SI',
|
||||
'SK',
|
||||
]
|
||||
|
||||
const europeanUnionExclusiveLanguageCodes = [
|
||||
'fr',
|
||||
'de',
|
||||
'it',
|
||||
'el',
|
||||
'pl',
|
||||
'fi',
|
||||
'nl',
|
||||
'hr',
|
||||
'cs',
|
||||
'hu',
|
||||
'ro',
|
||||
'sl',
|
||||
'sv',
|
||||
'bg',
|
||||
]
|
||||
|
||||
export const guessIfUserIsEuropean = () => {
|
||||
if (typeof window === 'undefined') return false
|
||||
return window.navigator.languages.some((language) => {
|
||||
const [languageCode, countryCode] = language.split('-')
|
||||
return countryCode
|
||||
? europeanUnionCountryCodes.includes(countryCode)
|
||||
: europeanUnionExclusiveLanguageCodes.includes(languageCode)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user