mirror of
https://github.com/harivansh-afk/Saas-Teamspace.git
synced 2026-04-17 03:03:48 +00:00
initial commit
This commit is contained in:
commit
ef9ccf22d3
133 changed files with 20802 additions and 0 deletions
64
app/api/checkout/route.ts
Normal file
64
app/api/checkout/route.ts
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
import { auth } from '@/auth'
|
||||
import { db } from '@/lib/db'
|
||||
import { stripe } from '@/lib/stripe'
|
||||
import { NextResponse } from 'next/server'
|
||||
|
||||
export async function POST(req: Request) {
|
||||
try {
|
||||
const user = await auth()
|
||||
|
||||
if (!user || !user.user.id) {
|
||||
return new NextResponse('Unauthorized', { status: 401 })
|
||||
}
|
||||
|
||||
const userSubscription = await db.userSubscription.findUnique({
|
||||
where: {
|
||||
userId: user.user.id
|
||||
}
|
||||
})
|
||||
|
||||
if (userSubscription && userSubscription.stripeCustomerId) {
|
||||
const stripeSession = await stripe.billingPortal.sessions.create({
|
||||
customer: userSubscription.stripeCustomerId,
|
||||
return_url: process.env.APP_URL
|
||||
})
|
||||
|
||||
return new NextResponse(JSON.stringify({ url: stripeSession.url }))
|
||||
}
|
||||
|
||||
const stripeSession = await stripe.checkout.sessions.create({
|
||||
success_url: process.env.APP_URL,
|
||||
cancel_url: process.env.APP_URL,
|
||||
payment_method_types: ['card'],
|
||||
|
||||
mode: 'subscription',
|
||||
billing_address_collection: 'auto',
|
||||
customer_email: user?.user.email!,
|
||||
|
||||
line_items: [
|
||||
{
|
||||
price_data: {
|
||||
currency: 'USD',
|
||||
product_data: {
|
||||
name: 'Your SaaS Subscription Name',
|
||||
description: 'Saas Subscription Description'
|
||||
},
|
||||
// cost (change this to the price of your product)
|
||||
unit_amount: 899,
|
||||
recurring: {
|
||||
interval: 'month'
|
||||
}
|
||||
},
|
||||
quantity: 1
|
||||
}
|
||||
],
|
||||
metadata: {
|
||||
userId: user.user.id
|
||||
}
|
||||
})
|
||||
return new NextResponse(JSON.stringify({ url: stripeSession.url }))
|
||||
} catch (error) {
|
||||
console.log('[STRIPE_GET]', error)
|
||||
return new NextResponse('Internal Error', { status: 500 })
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue