- Add Stripe SDK and subscription management - Create checkout, webhook, and portal API routes - Add pricing page with plan cards - Create subscriptions table in Supabase - Update database types for subscriptions - Configure n8n webhook for AI chat responses 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
39 lines
1.3 KiB
TypeScript
39 lines
1.3 KiB
TypeScript
import { NextResponse } from 'next/server'
|
|
import { createClient } from '@/lib/supabase/server'
|
|
import { stripe } from '@/lib/stripe/config'
|
|
import type { Database } from '@/types/database'
|
|
|
|
type Subscription = Database['public']['Tables']['subscriptions']['Row']
|
|
|
|
export async function POST() {
|
|
try {
|
|
const supabase = await createClient()
|
|
const { data: { user } } = await supabase.auth.getUser()
|
|
|
|
if (!user) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
const { data: subscription } = await (supabase as any)
|
|
.from('subscriptions')
|
|
.select('stripe_customer_id')
|
|
.eq('user_id', user.id)
|
|
.single() as { data: Pick<Subscription, 'stripe_customer_id'> | null }
|
|
|
|
if (!subscription?.stripe_customer_id) {
|
|
return NextResponse.json({ error: 'No subscription found' }, { status: 404 })
|
|
}
|
|
|
|
const session = await stripe.billingPortal.sessions.create({
|
|
customer: subscription.stripe_customer_id,
|
|
return_url: `${process.env.NEXT_PUBLIC_SITE_URL}/dashboard/settings`,
|
|
})
|
|
|
|
return NextResponse.json({ url: session.url })
|
|
} catch (error) {
|
|
console.error('Portal error:', error)
|
|
return NextResponse.json({ error: 'Failed to create portal session' }, { status: 500 })
|
|
}
|
|
}
|