Skip to content

Instantly share code, notes, and snippets.

@dipeshhkc
Last active January 16, 2023 03:26
Show Gist options
  • Save dipeshhkc/26725d3c20f9976f4df99697ac3f4e2e to your computer and use it in GitHub Desktop.
Save dipeshhkc/26725d3c20f9976f4df99697ac3f4e2e to your computer and use it in GitHub Desktop.
const handleInvoicePaymentFailed = async (data: Stripe.Event.Data.Object) => {
const invoice = data as Stripe.Invoice;
const stripeSub = invoice.subscription;
const subId = typeof stripeSub === 'string' ? stripeSub : stripeSub?.id;
const subscription = await db.subscription.findFirst({
where: {
stripe_subscription_id: subId,
},
});
if (!subscription) {
console.log({
message: `Received invoice payment failed event but there is no subscription ${subId} with us`,
});
return;
}
//notify user of invoice failed
console.log({ message: 'Invoice payment failed' });
};
const handleCustomerSubscriptionDeleted = async (
data: Stripe.Event.Data.Object
) => {
const stripeSub = data as Stripe.Subscription;
const subscription = await db.subscription.findFirst({
where: {
stripe_subscription_id: stripeSub.id,
},
});
await db.subscription.update({
where: { id: subscription!.id },
data: {
ends_at: add(new Date(), { days: 1 }),
stripe_status: stripeSub.status,
},
});
console.log({
message: `Subscription deleted ${subscription!.stripe_subscription_id}>`,
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment