({
team,
priceId
}: {
team: Team | null;
priceId: string;
})
| 12 | }); |
| 13 | |
| 14 | export async function createCheckoutSession({ |
| 15 | team, |
| 16 | priceId |
| 17 | }: { |
| 18 | team: Team | null; |
| 19 | priceId: string; |
| 20 | }) { |
| 21 | const user = await getUser(); |
| 22 | |
| 23 | if (!team || !user) { |
| 24 | redirect(`/sign-up?redirect=checkout&priceId=${priceId}`); |
| 25 | } |
| 26 | |
| 27 | const session = await stripe.checkout.sessions.create({ |
| 28 | payment_method_types: ['card'], |
| 29 | line_items: [ |
| 30 | { |
| 31 | price: priceId, |
| 32 | quantity: 1 |
| 33 | } |
| 34 | ], |
| 35 | mode: 'subscription', |
| 36 | success_url: `${process.env.BASE_URL}/api/stripe/checkout?session_id={CHECKOUT_SESSION_ID}`, |
| 37 | cancel_url: `${process.env.BASE_URL}/pricing`, |
| 38 | customer: team.stripeCustomerId || undefined, |
| 39 | client_reference_id: user.id.toString(), |
| 40 | allow_promotion_codes: true, |
| 41 | subscription_data: { |
| 42 | trial_period_days: 14 |
| 43 | } |
| 44 | }); |
| 45 | |
| 46 | redirect(session.url!); |
| 47 | } |
| 48 | |
| 49 | export async function createCustomerPortalSession(team: Team) { |
| 50 | if (!team.stripeCustomerId || !team.stripeProductId) { |
no test coverage detected