Initial commit: PodcastYes — AI podcast platform

This commit is contained in:
Leon Serfaty
2026-06-07 03:58:32 -04:00
commit 155507f21a
151 changed files with 19826 additions and 0 deletions
+69
View File
@@ -0,0 +1,69 @@
import { upsertSubscription } from "../subscription";
import { planFromPaypalPlan } from "../catalog";
import type { PlanKey } from "../plans";
interface PaypalResource {
id?: string;
plan_id?: string;
custom_id?: string;
billing_info?: { next_billing_time?: string };
}
interface PaypalEvent {
event_type: string;
resource: PaypalResource;
}
function parseCustom(
custom?: string
): { subjectId: string; subjectType: "user" | "organization"; plan: PlanKey } | null {
if (!custom) return null;
try {
return JSON.parse(custom);
} catch {
return null;
}
}
async function sync(resource: PaypalResource, status: string) {
const subId = resource.id;
if (!subId) return;
const custom = parseCustom(resource.custom_id);
const planFromId = resource.plan_id ? planFromPaypalPlan(resource.plan_id) : null;
const plan = (custom?.plan || planFromId || "free") as PlanKey;
const referenceId = custom?.subjectId;
if (!referenceId) {
console.warn("[paypal] subscription without custom subjectId, skipping", subId);
return;
}
await upsertSubscription({
provider: "paypal",
referenceId,
plan,
status,
paypalSubscriptionId: subId,
paypalPlanId: resource.plan_id ?? null,
periodEnd: resource.billing_info?.next_billing_time
? new Date(resource.billing_info.next_billing_time)
: null,
});
}
export async function handlePaypalEvent(event: PaypalEvent): Promise<void> {
switch (event.event_type) {
case "BILLING.SUBSCRIPTION.ACTIVATED":
case "BILLING.SUBSCRIPTION.UPDATED":
case "BILLING.SUBSCRIPTION.RE-ACTIVATED":
await sync(event.resource, "active");
break;
case "BILLING.SUBSCRIPTION.SUSPENDED":
await sync(event.resource, "paused");
break;
case "BILLING.SUBSCRIPTION.CANCELLED":
case "BILLING.SUBSCRIPTION.EXPIRED":
await sync(event.resource, "canceled");
break;
default:
break;
}
}
+71
View File
@@ -0,0 +1,71 @@
import type Stripe from "stripe";
import { stripe } from "../stripe";
import { upsertSubscription } from "../subscription";
import { planFromStripePrice } from "../catalog";
import type { PlanKey } from "../plans";
function normalizeStatus(status: Stripe.Subscription.Status): string {
switch (status) {
case "active":
case "trialing":
case "past_due":
case "paused":
return status;
default:
return "canceled"; // canceled | incomplete | incomplete_expired | unpaid
}
}
async function syncStripeSubscription(
sub: Stripe.Subscription,
metadata?: Stripe.Metadata | null
) {
const item = sub.items.data[0];
const priceId = item?.price?.id;
const mapped = priceId ? planFromStripePrice(priceId) : null;
const plan = ((metadata?.plan as PlanKey) || mapped?.plan || "free") as PlanKey;
const referenceId = metadata?.subjectId || sub.metadata?.subjectId;
if (!referenceId) {
console.warn("[stripe] subscription without subjectId metadata, skipping", sub.id);
return;
}
const interval =
mapped?.interval ?? (item?.price?.recurring?.interval === "year" ? "year" : "month");
await upsertSubscription({
provider: "stripe",
referenceId,
plan,
status: normalizeStatus(sub.status),
billingInterval: interval,
stripeCustomerId: typeof sub.customer === "string" ? sub.customer : sub.customer.id,
stripeSubscriptionId: sub.id,
periodStart: sub.current_period_start ? new Date(sub.current_period_start * 1000) : null,
periodEnd: sub.current_period_end ? new Date(sub.current_period_end * 1000) : null,
cancelAtPeriodEnd: sub.cancel_at_period_end,
});
}
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;
if (session.mode !== "subscription" || !session.subscription) break;
const subId =
typeof session.subscription === "string" ? session.subscription : session.subscription.id;
const sub = await stripe().subscriptions.retrieve(subId);
await syncStripeSubscription(sub, session.metadata);
break;
}
case "customer.subscription.created":
case "customer.subscription.updated":
case "customer.subscription.deleted": {
const sub = event.data.object as Stripe.Subscription;
await syncStripeSubscription(sub, sub.metadata);
break;
}
default:
break;
}
}