|
| 1 | +import { headers } from 'next/headers'; |
| 2 | +import { NextRequest } from 'next/server'; |
| 3 | +import Stripe from 'stripe'; |
| 4 | +import { prisma } from '@/prisma'; |
| 5 | +import { STRIPE_WEBHOOK_SECRET } from '@/lib/environment'; |
| 6 | +import { getStripe } from '@/lib/stripe'; |
| 7 | +import { ConnectionSyncStatus, StripeSubscriptionStatus } from '@sourcebot/db'; |
| 8 | +export async function POST(req: NextRequest) { |
| 9 | + const body = await req.text(); |
| 10 | + const signature = headers().get('stripe-signature'); |
| 11 | + |
| 12 | + if (!signature) { |
| 13 | + return new Response('No signature', { status: 400 }); |
| 14 | + } |
| 15 | + |
| 16 | + try { |
| 17 | + const stripe = getStripe(); |
| 18 | + const event = stripe.webhooks.constructEvent( |
| 19 | + body, |
| 20 | + signature, |
| 21 | + STRIPE_WEBHOOK_SECRET! |
| 22 | + ); |
| 23 | + |
| 24 | + if (event.type === 'customer.subscription.deleted') { |
| 25 | + const subscription = event.data.object as Stripe.Subscription; |
| 26 | + const customerId = subscription.customer as string; |
| 27 | + |
| 28 | + const org = await prisma.org.findFirst({ |
| 29 | + where: { |
| 30 | + stripeCustomerId: customerId |
| 31 | + } |
| 32 | + }); |
| 33 | + |
| 34 | + if (!org) { |
| 35 | + return new Response('Org not found', { status: 404 }); |
| 36 | + } |
| 37 | + |
| 38 | + await prisma.org.update({ |
| 39 | + where: { |
| 40 | + id: org.id |
| 41 | + }, |
| 42 | + data: { |
| 43 | + stripeSubscriptionStatus: StripeSubscriptionStatus.INACTIVE, |
| 44 | + stripeLastUpdatedAt: new Date() |
| 45 | + } |
| 46 | + }); |
| 47 | + console.log(`Org ${org.id} subscription status updated to INACTIVE`); |
| 48 | + |
| 49 | + return new Response(JSON.stringify({ received: true }), { |
| 50 | + status: 200 |
| 51 | + }); |
| 52 | + } else if (event.type === 'customer.subscription.created') { |
| 53 | + const subscription = event.data.object as Stripe.Subscription; |
| 54 | + const customerId = subscription.customer as string; |
| 55 | + |
| 56 | + const org = await prisma.org.findFirst({ |
| 57 | + where: { |
| 58 | + stripeCustomerId: customerId |
| 59 | + } |
| 60 | + }); |
| 61 | + |
| 62 | + if (!org) { |
| 63 | + return new Response('Org not found', { status: 404 }); |
| 64 | + } |
| 65 | + |
| 66 | + await prisma.org.update({ |
| 67 | + where: { |
| 68 | + id: org.id |
| 69 | + }, |
| 70 | + data: { |
| 71 | + stripeSubscriptionStatus: StripeSubscriptionStatus.ACTIVE, |
| 72 | + stripeLastUpdatedAt: new Date() |
| 73 | + } |
| 74 | + }); |
| 75 | + console.log(`Org ${org.id} subscription status updated to ACTIVE`); |
| 76 | + |
| 77 | + // mark all of this org's connections for sync, since their repos may have been previously garbage collected |
| 78 | + await prisma.connection.updateMany({ |
| 79 | + where: { |
| 80 | + orgId: org.id |
| 81 | + }, |
| 82 | + data: { |
| 83 | + syncStatus: ConnectionSyncStatus.SYNC_NEEDED |
| 84 | + } |
| 85 | + }); |
| 86 | + |
| 87 | + return new Response(JSON.stringify({ received: true }), { |
| 88 | + status: 200 |
| 89 | + }); |
| 90 | + } else { |
| 91 | + console.log(`Received unknown event type: ${event.type}`); |
| 92 | + return new Response(JSON.stringify({ received: true }), { |
| 93 | + status: 202 |
| 94 | + }); |
| 95 | + } |
| 96 | + |
| 97 | + } catch (err) { |
| 98 | + console.error('Error processing webhook:', err); |
| 99 | + return new Response( |
| 100 | + 'Webhook error: ' + (err as Error).message, |
| 101 | + { status: 400 } |
| 102 | + ); |
| 103 | + } |
| 104 | +} |
0 commit comments