Files
speckle-server/packages/server/modules/gatekeeper/clients/checkout/createCheckoutSession.ts
T
Gergő Jedlicska 61ca128ce2 gergo/multiCurrency (#4379)
* feat(gatekeeper): support multiple currencies

* feat(helm): add new currency based prices to helm chart

* chore(env): add example currency based pricing values

* fix(ci): update price ids to the proper values

* feat(helm): rename price ids to fit multi currency

* feat(gatekeeper): currency input for checkout session

* Updated prices in the FE

* chore(gatekeeper): remove old checkout session flow

* Updated prices in the FE

* Fix FE

* Fix pipeline

---------

Co-authored-by: Mike Tasset <mike.tasset@gmail.com>
2025-04-11 17:37:47 +02:00

62 lines
1.8 KiB
TypeScript

/* eslint-disable camelcase */
import { getResultUrl } from '@/modules/gatekeeper/clients/getResultUrl'
import {
CreateCheckoutSession,
GetWorkspacePlanPriceId
} from '@/modules/gatekeeper/domain/billing'
import { EnvironmentResourceError } from '@/modules/shared/errors'
import { Stripe } from 'stripe'
export const createCheckoutSessionFactory =
({
stripe,
frontendOrigin,
getWorkspacePlanPrice
}: {
stripe: Stripe
frontendOrigin: string
getWorkspacePlanPrice: GetWorkspacePlanPriceId
}): CreateCheckoutSession =>
async ({
editorsCount,
workspacePlan,
billingInterval,
workspaceSlug,
workspaceId,
isCreateFlow,
currency
}) => {
const resultUrl = getResultUrl({ frontendOrigin, workspaceId, workspaceSlug })
const price = getWorkspacePlanPrice({ billingInterval, workspacePlan, currency })
const costLineItems: Stripe.Checkout.SessionCreateParams.LineItem[] = [
{ price, quantity: editorsCount }
]
const cancel_url = isCreateFlow
? `${frontendOrigin}/workspaces/create?workspaceId=${workspaceId}&payment_status=canceled&session_id={CHECKOUT_SESSION_ID}`
: `${resultUrl.toString()}&payment_status=canceled&session_id={CHECKOUT_SESSION_ID}`
const session = await stripe.checkout.sessions.create({
mode: 'subscription',
line_items: costLineItems,
success_url: `${resultUrl.toString()}&payment_status=success&session_id={CHECKOUT_SESSION_ID}`,
cancel_url
})
if (!session.url)
throw new EnvironmentResourceError('Failed to create an active checkout session')
return {
id: session.id,
url: session.url,
billingInterval,
workspacePlan,
workspaceId,
currency,
createdAt: new Date(),
updatedAt: new Date(),
paymentStatus: 'unpaid'
}
}