add embedded checkout

This commit is contained in:
JCEEE
2026-08-20 07:35:31 +01:00
parent c6e987a852
commit 397543c880
45 changed files with 5731 additions and 334 deletions
+41
View File
@@ -0,0 +1,41 @@
import { pbAdmin } from '$lib/server/pocketbase';
import type Stripe from 'stripe';
// Shared Stripe event handling. Both the real webhook (/account/webhook) and
// the dev-only simulator (/account/webhook/simulate) route through here so the
// DB effects are identical.
export async function handleStripeEvent(event: Stripe.Event): Promise<void> {
switch (event.type) {
case 'checkout.session.completed': {
const session = event.data.object as Stripe.Checkout.Session;
const famId = session.metadata?.famId;
if (famId && session.customer) {
await pbAdmin.update('fams', famId, {
stripeCustomerId: String(session.customer),
active: true
});
}
break;
}
case 'customer.subscription.created':
case 'customer.subscription.updated':
case 'customer.subscription.deleted':
case 'customer.subscription.paused': {
const sub = event.data.object as Stripe.Subscription;
await setActiveFromSubscription(sub);
break;
}
}
}
async function setActiveFromSubscription(sub: Stripe.Subscription) {
// The fam that owns this subscription — look up by the customer id stored
// on the fam (set at checkout). Stripe customer id is unique per fam.
const fams = await pbAdmin.getList('fams', `stripeCustomerId = '${sub.customer}'`);
const fam = fams[0];
if (!fam) return;
// Active only while the sub is trialing/active (not past_due/canceled/paused).
const active =
sub.status === 'trialing' || sub.status === 'active' || sub.status === 'past_due';
await pbAdmin.update('fams', fam.id, { active });
}