mirror of
https://github.com/marcogll/TaxHacker_s23.git
synced 2026-01-13 13:25:18 +00:00
fix: webhook events
This commit is contained in:
@@ -31,13 +31,47 @@ export async function POST(request: Request) {
|
||||
// Handle the event
|
||||
try {
|
||||
switch (event.type) {
|
||||
case "checkout.session.completed": {
|
||||
const session = event.data.object as Stripe.Checkout.Session
|
||||
const customerId = session.customer as string
|
||||
const subscriptionId = session.subscription as string
|
||||
const subscription = await stripeClient.subscriptions.retrieve(subscriptionId)
|
||||
const item = subscription.items.data[0]
|
||||
|
||||
await handleUserSubscriptionUpdate(customerId, item)
|
||||
break
|
||||
}
|
||||
|
||||
case "customer.subscription.created":
|
||||
case "customer.subscription.updated": {
|
||||
const subscription = event.data.object as Stripe.Subscription
|
||||
const customerId = subscription.customer as string
|
||||
const item = subscription.items.data[0]
|
||||
|
||||
// Get the plan from our plans configuration
|
||||
await handleUserSubscriptionUpdate(customerId, item)
|
||||
break
|
||||
}
|
||||
|
||||
default:
|
||||
console.log(`Unhandled event type ${event.type}`)
|
||||
return new NextResponse("No handler for event type", { status: 200 })
|
||||
}
|
||||
|
||||
return new NextResponse("Webhook processed successfully", { status: 200 })
|
||||
} catch (error) {
|
||||
console.error("Error processing webhook:", error)
|
||||
return new NextResponse("Webhook processing failed", { status: 500 })
|
||||
}
|
||||
}
|
||||
|
||||
async function handleUserSubscriptionUpdate(
|
||||
customerId: string,
|
||||
item: Stripe.SubscriptionItem
|
||||
) {
|
||||
if (!stripeClient) {
|
||||
return new NextResponse("Stripe client is not initialized", { status: 500 })
|
||||
}
|
||||
|
||||
const plan = Object.values(PLANS).find((p) => p.stripePriceId === item.price.id)
|
||||
if (!plan) {
|
||||
throw new Error(`Plan not found for price ID: ${item.price.id}`)
|
||||
@@ -64,17 +98,4 @@ export async function POST(request: Request) {
|
||||
aiBalance: plan.limits.ai,
|
||||
updatedAt: new Date(),
|
||||
})
|
||||
|
||||
break
|
||||
}
|
||||
|
||||
default:
|
||||
console.log(`Unhandled event type ${event.type}`)
|
||||
}
|
||||
|
||||
return new NextResponse("Webhook processed successfully", { status: 200 })
|
||||
} catch (error) {
|
||||
console.error("Error processing webhook:", error)
|
||||
return new NextResponse("Webhook processing failed", { status: 500 })
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user