2022-02-12 10:23:58 +01:00
|
|
|
import { User } from 'db'
|
|
|
|
import { loadStripe } from '@stripe/stripe-js'
|
|
|
|
import { sendRequest } from 'utils'
|
|
|
|
|
2022-05-13 15:22:44 -07:00
|
|
|
type Props = {
|
|
|
|
user: User
|
|
|
|
currency: 'usd' | 'eur'
|
|
|
|
plan: 'pro' | 'team'
|
|
|
|
workspaceId: string
|
|
|
|
}
|
|
|
|
|
|
|
|
export const pay = async ({ user, currency, plan, workspaceId }: Props) => {
|
2022-02-12 10:23:58 +01:00
|
|
|
if (!process.env.NEXT_PUBLIC_STRIPE_PUBLIC_KEY)
|
|
|
|
throw new Error('NEXT_PUBLIC_STRIPE_PUBLIC_KEY is missing in env')
|
|
|
|
const stripe = await loadStripe(process.env.NEXT_PUBLIC_STRIPE_PUBLIC_KEY)
|
|
|
|
const { data, error } = await sendRequest<{ sessionId: string }>({
|
|
|
|
method: 'POST',
|
|
|
|
url: '/api/stripe/checkout',
|
2022-05-13 15:22:44 -07:00
|
|
|
body: { email: user.email, currency, plan, workspaceId },
|
2022-02-12 10:23:58 +01:00
|
|
|
})
|
|
|
|
if (error || !data) return
|
|
|
|
return stripe?.redirectToCheckout({
|
|
|
|
sessionId: data?.sessionId,
|
|
|
|
})
|
|
|
|
}
|