Skip to content

Instantly share code, notes, and snippets.

@dipeshhkc
Created January 16, 2023 03:24
Show Gist options
  • Save dipeshhkc/c3ee8e6054a357bc67e682a1ba1f3f4a to your computer and use it in GitHub Desktop.
Save dipeshhkc/c3ee8e6054a357bc67e682a1ba1f3f4a to your computer and use it in GitHub Desktop.
const handleInvoicePaid = async (data: Stripe.Event.Data.Object) => {
const invoice = data as Stripe.Invoice;
const stripeSubId = invoice.subscription?.toString();
if (
invoice.billing_reason === 'subscription_create' ||
invoice.billing_reason === 'subscription_update'
) {
// this should already be handled with 'checkout.session.completed' or with 'customer.subscription.updated'
return;
}
const subscription = await db.subscription.findFirst({
where: {
stripe_subscription_id: stripeSubId,
},
});
await db.subscription.update({
where: { id: subscription!.id },
data: { ends_at: null, stripe_status: 'active' },
});
console.log({ message: 'Invoice paid' });
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment