Last active
January 16, 2023 03:26
-
-
Save dipeshhkc/26725d3c20f9976f4df99697ac3f4e2e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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