feat(workspace): 🚸 Improve plan upgrade flow
This commit is contained in:
@ -1,25 +1,60 @@
|
||||
import { User } from 'db'
|
||||
import { Plan, User } from 'db'
|
||||
import { loadStripe } from '@stripe/stripe-js/pure'
|
||||
import { isEmpty, sendRequest } from 'utils'
|
||||
import { isDefined, isEmpty, sendRequest } from 'utils'
|
||||
|
||||
type Props = {
|
||||
user: User
|
||||
customerId?: string
|
||||
currency: 'usd' | 'eur'
|
||||
plan: 'pro' | 'team'
|
||||
workspaceId: string
|
||||
}
|
||||
|
||||
export const pay = async ({ user, currency, plan, workspaceId }: Props) => {
|
||||
export const pay = async ({
|
||||
customerId,
|
||||
...props
|
||||
}: Props): Promise<{ newPlan: Plan } | undefined | void> =>
|
||||
isDefined(customerId)
|
||||
? updatePlan({ ...props, customerId })
|
||||
: redirectToCheckout(props)
|
||||
|
||||
const updatePlan = async ({
|
||||
customerId,
|
||||
plan,
|
||||
workspaceId,
|
||||
currency,
|
||||
}: Omit<Props, 'user'>): Promise<{ newPlan: Plan } | undefined> => {
|
||||
const { data, error } = await sendRequest<{ message: string }>({
|
||||
method: 'POST',
|
||||
url: '/api/stripe/update-subscription',
|
||||
body: { workspaceId, plan, customerId, currency },
|
||||
})
|
||||
if (error || !data) return
|
||||
return { newPlan: plan === 'team' ? Plan.TEAM : Plan.PRO }
|
||||
}
|
||||
|
||||
const redirectToCheckout = async ({
|
||||
user,
|
||||
currency,
|
||||
plan,
|
||||
workspaceId,
|
||||
}: Omit<Props, 'customerId'>) => {
|
||||
if (isEmpty(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',
|
||||
body: { email: user.email, currency, plan, workspaceId },
|
||||
body: {
|
||||
email: user.email,
|
||||
currency,
|
||||
plan,
|
||||
workspaceId,
|
||||
href: location.origin + location.pathname,
|
||||
},
|
||||
})
|
||||
if (error || !data) return
|
||||
return stripe?.redirectToCheckout({
|
||||
const stripe = await loadStripe(process.env.NEXT_PUBLIC_STRIPE_PUBLIC_KEY)
|
||||
await stripe?.redirectToCheckout({
|
||||
sessionId: data?.sessionId,
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user