import { verifyWebhook, handleSubscriptionEvent } from '@adrper79-dot/stripe';
app.post('/webhook/stripe', async (c) => {
const event = await verifyWebhook(c.req.raw, c.env.STRIPE_WEBHOOK_SECRET);
await handleSubscriptionEvent(event, {
onCreated: async (sub) => {
await db.execute(
'UPDATE users SET stripe_subscription_id = $1 WHERE stripe_customer_id = $2',
[sub.id, sub.customer],
);
},
onDeleted: async (sub) => {
await db.execute(
'UPDATE users SET subscription_status = $1 WHERE stripe_customer_id = $2',
['canceled', sub.customer],
);
},
});
return c.json({ received: true });
});