mirror of
https://github.com/lukevella/rallly.git
synced 2025-04-29 18:26:34 +02:00
♻️ Handle failed payments in webhook (#1443)
This commit is contained in:
parent
b161ea0be3
commit
c2e1c289eb
1 changed files with 50 additions and 1 deletions
|
@ -91,7 +91,56 @@ export async function POST(request: NextRequest) {
|
|||
|
||||
break;
|
||||
}
|
||||
case "customer.subscription.deleted":
|
||||
case "customer.subscription.deleted": {
|
||||
const { id } = event.data.object as Stripe.Subscription;
|
||||
const subscription = await stripe.subscriptions.retrieve(id);
|
||||
|
||||
// void any unpaid invoices
|
||||
const invoices = await stripe.invoices.list({
|
||||
subscription: subscription.id,
|
||||
status: "open",
|
||||
});
|
||||
|
||||
for (const invoice of invoices.data) {
|
||||
await stripe.invoices.voidInvoice(invoice.id);
|
||||
}
|
||||
|
||||
// remove the subscription from the user
|
||||
await prisma.user.update({
|
||||
where: {
|
||||
subscriptionId: subscription.id,
|
||||
},
|
||||
data: {
|
||||
subscriptionId: null,
|
||||
},
|
||||
});
|
||||
// delete the subscription from the database
|
||||
await prisma.subscription.delete({
|
||||
where: {
|
||||
id: subscription.id,
|
||||
},
|
||||
});
|
||||
|
||||
try {
|
||||
const { userId } = subscriptionMetadataSchema.parse(
|
||||
subscription.metadata,
|
||||
);
|
||||
|
||||
posthog?.capture({
|
||||
distinctId: userId,
|
||||
event: "subscription cancel",
|
||||
properties: {
|
||||
$set: {
|
||||
tier: "hobby",
|
||||
},
|
||||
},
|
||||
});
|
||||
} catch (e) {
|
||||
Sentry.captureException(e);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case "customer.subscription.updated":
|
||||
case "customer.subscription.created": {
|
||||
const { id } = event.data.object as Stripe.Subscription;
|
||||
|
|
Loading…
Add table
Reference in a new issue