fix: better webhook error handling

This commit is contained in:
vas3k
2025-05-22 17:04:19 +02:00
parent da16558298
commit 3976db1114
4 changed files with 26 additions and 11 deletions

View File

@@ -36,25 +36,28 @@ export async function POST(request: Request) {
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)
for (const item of subscription.items.data) {
await handleUserSubscriptionUpdate(customerId, item)
}
break
}
case "customer.subscription.created":
case "customer.subscription.updated": {
case "customer.subscription.updated":
case "customer.subscription.deleted": {
const subscription = event.data.object as Stripe.Subscription
const customerId = subscription.customer as string
const item = subscription.items.data[0]
await handleUserSubscriptionUpdate(customerId, item)
for (const item of subscription.items.data) {
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("No handler for event type", { status: 400 })
}
return new NextResponse("Webhook processed successfully", { status: 200 })
@@ -91,11 +94,18 @@ async function handleUserSubscriptionUpdate(
}
}
const newMembershipExpiresAt = new Date(item.current_period_end * 1000)
await updateUser(user.id, {
membershipPlan: plan.code,
membershipExpiresAt: new Date(item.current_period_end * 1000),
membershipExpiresAt:
user.membershipExpiresAt && user.membershipExpiresAt > newMembershipExpiresAt
? user.membershipExpiresAt
: newMembershipExpiresAt,
storageLimit: plan.limits.storage,
aiBalance: plan.limits.ai,
updatedAt: new Date(),
})
console.log(`Updated user ${user.id} with plan ${plan.code} and expires at ${newMembershipExpiresAt}`)
}