🗃️ Add new fields to subscription (#1564)

This commit is contained in:
Luke Vella 2025-02-17 17:26:40 +07:00 committed by GitHub
parent e022c4c279
commit 7cf578bedf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 289 additions and 198 deletions

View file

@ -49,240 +49,270 @@ export async function POST(request: NextRequest) {
); );
} }
switch (event.type) { try {
case "checkout.session.completed": { switch (event.type) {
const checkoutSession = event.data.object as Stripe.Checkout.Session; case "checkout.session.completed": {
const checkoutSession = event.data.object as Stripe.Checkout.Session;
if (checkoutSession.subscription === null) { if (checkoutSession.subscription === null) {
// This is a one-time payment (probably for Rallly Self-Hosted) // This is a one-time payment (probably for Rallly Self-Hosted)
break; break;
} }
const { userId } = checkoutMetadataSchema.parse(checkoutSession.metadata); const { userId } = checkoutMetadataSchema.parse(
checkoutSession.metadata,
if (!userId) {
return NextResponse.json(
{ error: "Missing client reference ID" },
{ status: 400 },
); );
}
await prisma.user.update({ if (!userId) {
where: { return NextResponse.json(
id: userId, { error: "Missing client reference ID" },
}, { status: 400 },
data: { );
customerId: checkoutSession.customer as string, }
},
});
const subscription = await stripe.subscriptions.retrieve( await prisma.user.update({
checkoutSession.subscription as string, where: {
); id: userId,
posthog?.capture({
distinctId: userId,
event: "upgrade",
properties: {
interval: subscription.items.data[0].plan.interval,
$set: {
tier: "pro",
}, },
}, data: {
}); customerId: checkoutSession.customer as string,
},
});
break; const subscription = await stripe.subscriptions.retrieve(
} checkoutSession.subscription as string,
case "customer.subscription.deleted": {
const { id } = event.data.object as Stripe.Subscription;
const subscription = await stripe.subscriptions.retrieve(id);
// void any unpaid invoices
const invoices = await stripe.invoices.list({
subscription: subscription.id,
status: "open",
});
for (const invoice of invoices.data) {
await stripe.invoices.voidInvoice(invoice.id);
}
// delete the subscription from the database
await prisma.subscription.delete({
where: {
id: subscription.id,
},
});
try {
const { userId } = subscriptionMetadataSchema.parse(
subscription.metadata,
); );
posthog?.capture({ posthog?.capture({
distinctId: userId, distinctId: userId,
event: "subscription cancel", event: "upgrade",
properties: { properties: {
interval: subscription.items.data[0].price.recurring?.interval,
$set: { $set: {
tier: "hobby", tier: "pro",
}, },
}, },
}); });
} catch (e) {
Sentry.captureException(e); break;
} }
case "customer.subscription.deleted": {
const { id } = event.data.object as Stripe.Subscription;
const subscription = await stripe.subscriptions.retrieve(id);
break; // void any unpaid invoices
} const invoices = await stripe.invoices.list({
case "customer.subscription.updated": subscription: subscription.id,
case "customer.subscription.created": { status: "open",
const { id } = event.data.object as Stripe.Subscription; });
const subscription = await stripe.subscriptions.retrieve(id); for (const invoice of invoices.data) {
await stripe.invoices.voidInvoice(invoice.id);
}
// check if the subscription is active // delete the subscription from the database
const isActive = await prisma.subscription.delete({
subscription.status === "active" || where: {
subscription.status === "trialing" || id: subscription.id,
subscription.status === "past_due"; },
});
// get the subscription price details try {
const lineItem = subscription.items.data[0]; const { userId } = subscriptionMetadataSchema.parse(
subscription.metadata,
);
// update/create the subscription in the database posthog?.capture({
const { price } = lineItem; distinctId: userId,
event: "subscription cancel",
properties: {
$set: {
tier: "hobby",
},
},
});
} catch (e) {
Sentry.captureException(e);
}
const res = subscriptionMetadataSchema.safeParse(subscription.metadata); break;
if (!res.success) {
return NextResponse.json({ error: "Missing user ID" }, { status: 400 });
} }
case "customer.subscription.updated":
case "customer.subscription.created": {
const { id } = event.data.object as Stripe.Subscription;
// create or update the subscription in the database const subscription = await stripe.subscriptions.retrieve(id);
await prisma.subscription.upsert({
where: {
id: subscription.id,
},
create: {
id: subscription.id,
active: isActive,
priceId: price.id,
currency: subscription.currency ?? null,
createdAt: toDate(subscription.created),
periodStart: toDate(subscription.current_period_start),
periodEnd: toDate(subscription.current_period_end),
},
update: {
active: isActive,
priceId: price.id,
currency: subscription.currency ?? null,
createdAt: toDate(subscription.created),
periodStart: toDate(subscription.current_period_start),
periodEnd: toDate(subscription.current_period_end),
},
});
// update the user with the subscription id // check if the subscription is active
await prisma.user.update({ const isActive =
where: { subscription.status === "active" ||
id: res.data.userId, subscription.status === "trialing" ||
}, subscription.status === "past_due";
data: {
subscriptionId: subscription.id,
},
});
try { // get the subscription price details
posthog?.capture({ const lineItem = subscription.items.data[0];
distinctId: res.data.userId,
event: "subscription change", // update/create the subscription in the database
properties: { const { price } = lineItem;
type: event.type,
$set: { const res = subscriptionMetadataSchema.safeParse(subscription.metadata);
tier: isActive ? "pro" : "hobby",
if (!res.success) {
return NextResponse.json(
{ error: "Missing user ID" },
{ status: 400 },
);
}
const subscriptionItem = subscription.items.data[0];
const interval = subscriptionItem.price.recurring?.interval;
if (!interval) {
throw new Error(
`Missing interval in subscription ${subscription.id}`,
);
}
// create or update the subscription in the database
await prisma.subscription.upsert({
where: {
id: subscription.id,
},
create: {
id: subscription.id,
active: isActive,
priceId: price.id,
currency: subscriptionItem.price.currency,
interval,
amount: subscriptionItem.price.unit_amount,
status: subscription.status,
createdAt: toDate(subscription.created),
periodStart: toDate(subscription.current_period_start),
periodEnd: toDate(subscription.current_period_end),
},
update: {
active: isActive,
priceId: price.id,
currency: subscriptionItem.price.currency,
interval,
amount: subscriptionItem.price.unit_amount,
status: subscription.status,
createdAt: toDate(subscription.created),
periodStart: toDate(subscription.current_period_start),
periodEnd: toDate(subscription.current_period_end),
},
});
// update the user with the subscription id
await prisma.user.update({
where: {
id: res.data.userId,
},
data: {
subscriptionId: subscription.id,
},
});
try {
posthog?.capture({
distinctId: res.data.userId,
event: "subscription change",
properties: {
type: event.type,
$set: {
tier: isActive ? "pro" : "hobby",
},
},
});
} catch (e) {
Sentry.captureException(e);
}
break;
}
case "checkout.session.expired": {
console.info("Checkout session expired");
const session = event.data.object as Stripe.Checkout.Session;
// When a Checkout Session expires, the customer's email isn't returned in
// the webhook payload unless they give consent for promotional content
const email = session.customer_details?.email;
const recoveryUrl = session.after_expiration?.recovery?.url;
const userId = session.metadata?.userId;
if (!userId) {
console.info("No user ID found in Checkout Session metadata");
Sentry.captureMessage(
"No user ID found in Checkout Session metadata",
);
break;
}
// Do nothing if the Checkout Session has no email or recovery URL
if (!email || !recoveryUrl) {
console.info("No email or recovery URL found in Checkout Session");
Sentry.captureMessage(
"No email or recovery URL found in Checkout Session",
);
break;
}
const promoEmailKey = `promo_email_sent:${email}`;
// Track that a promotional email opportunity has been shown to this user
const hasReceivedPromo = await kv.get(promoEmailKey);
console.info("Has received promo", hasReceivedPromo);
const user = await prisma.user.findUnique({
where: {
id: userId,
},
select: {
locale: true,
subscription: {
select: {
active: true,
},
}, },
}, },
}); });
} catch (e) {
Sentry.captureException(e);
}
break; const isPro = !!user?.subscription?.active;
}
case "checkout.session.expired": { // Avoid spamming people who abandon Checkout multiple times
console.info("Checkout session expired"); if (user && !hasReceivedPromo && !isPro) {
const session = event.data.object as Stripe.Checkout.Session; console.info("Sending abandoned checkout email");
// When a Checkout Session expires, the customer's email isn't returned in // Set the flag with a 30-day expiration (in seconds)
// the webhook payload unless they give consent for promotional content await kv.set(promoEmailKey, 1, { ex: 30 * 24 * 60 * 60, nx: true });
const email = session.customer_details?.email; getEmailClient(user.locale ?? undefined).sendTemplate(
const recoveryUrl = session.after_expiration?.recovery?.url; "AbandonedCheckoutEmail",
const userId = session.metadata?.userId; {
if (!userId) { to: email,
console.info("No user ID found in Checkout Session metadata"); from: {
Sentry.captureMessage("No user ID found in Checkout Session metadata"); name: "Luke from Rallly",
address: "luke@rallly.co",
},
props: {
name: session.customer_details?.name ?? undefined,
discount: 20,
couponCode: "GETPRO1Y20",
recoveryUrl,
},
},
);
}
break; break;
} }
// Do nothing if the Checkout Session has no email or recovery URL default:
if (!email || !recoveryUrl) { Sentry.captureException(
console.info("No email or recovery URL found in Checkout Session"); new Error(`Unhandled event type: ${event.type}`),
Sentry.captureMessage(
"No email or recovery URL found in Checkout Session",
); );
break; // Unexpected event type
} return NextResponse.json(
const promoEmailKey = `promo_email_sent:${email}`; { error: "Unhandled event type" },
// Track that a promotional email opportunity has been shown to this user { status: 400 },
const hasReceivedPromo = await kv.get(promoEmailKey);
console.info("Has received promo", hasReceivedPromo);
const user = await prisma.user.findUnique({
where: {
id: userId,
},
select: {
locale: true,
subscription: {
select: {
active: true,
},
},
},
});
const isPro = !!user?.subscription?.active;
// Avoid spamming people who abandon Checkout multiple times
if (user && !hasReceivedPromo && !isPro) {
console.info("Sending abandoned checkout email");
// Set the flag with a 30-day expiration (in seconds)
await kv.set(promoEmailKey, 1, { ex: 30 * 24 * 60 * 60, nx: true });
getEmailClient(user.locale ?? undefined).sendTemplate(
"AbandonedCheckoutEmail",
{
to: email,
from: {
name: "Luke from Rallly",
address: "luke@rallly.co",
},
props: {
name: session.customer_details?.name ?? undefined,
discount: 20,
couponCode: "GETPRO1Y20",
recoveryUrl,
},
},
); );
}
break;
} }
default: } catch (err) {
Sentry.captureException(new Error(`Unhandled event type: ${event.type}`)); const error =
// Unexpected event type err instanceof Error ? err.message : "An unexpected error occurred";
return NextResponse.json( Sentry.captureException(err);
{ error: "Unhandled event type" }, return NextResponse.json({ error }, { status: 500 });
{ status: 400 },
);
} }
waitUntil(Promise.all([posthog?.shutdown()])); waitUntil(Promise.all([posthog?.shutdown()]));

View file

@ -9,6 +9,7 @@
"scripts": { "scripts": {
"normalize-subscription-metadata": "dotenv -e ../../.env -- tsx ./src/scripts/normalize-metadata.ts", "normalize-subscription-metadata": "dotenv -e ../../.env -- tsx ./src/scripts/normalize-metadata.ts",
"checkout-expiry": "dotenv -e ../../.env -- tsx ./src/scripts/checkout-expiry.ts", "checkout-expiry": "dotenv -e ../../.env -- tsx ./src/scripts/checkout-expiry.ts",
"subscription-data-sync": "dotenv -e ../../.env -- tsx ./src/scripts/subscription-data-sync.ts",
"type-check": "tsc --pretty --noEmit", "type-check": "tsc --pretty --noEmit",
"lint": "eslint ./src" "lint": "eslint ./src"
}, },

View file

@ -0,0 +1,55 @@
import { prisma } from "@rallly/database";
import { stripe } from "../lib/stripe";
(async function syncSubscriptionData() {
const BATCH_SIZE = 10;
let processed = 0;
let failed = 0;
const userSubscriptions = await prisma.subscription.findMany({
select: {
id: true,
},
take: BATCH_SIZE,
});
console.info(`🚀 Syncing ${userSubscriptions.length} subscriptions...`)
for (const userSubscription of userSubscriptions) {
try {
const subscription = await stripe.subscriptions.retrieve(
userSubscription.id,
);
const subscriptionItem = subscription.items.data[0];
const interval = subscriptionItem.price.recurring?.interval;
if (!interval) {
console.info(`🚨 Missing interval in subscription ${subscription.id}`);
+ failed++;
continue;
}
await prisma.subscription.update({
where: {
id: subscription.id,
},
data: {
amount: subscriptionItem.price.unit_amount,
currency: subscriptionItem.price.currency,
interval: subscriptionItem.price.recurring?.interval,
status: subscription.status,
},
});
console.info(`✅ Subscription ${subscription.id} synced`);
processed++;
} catch (error) {
console.error(`❌ Failed to sync subscription ${userSubscription.id}:`, error);
failed++;
}
}
console.info(`📊 Sync complete: ${processed} processed, ${failed} failed`);
})();

View file

@ -0,0 +1,3 @@
-- AlterTable
ALTER TABLE "subscriptions" ADD COLUMN "amount" INTEGER,
ADD COLUMN "status" TEXT;

View file

@ -88,6 +88,8 @@ model UserPaymentData {
model Subscription { model Subscription {
id String @id id String @id
priceId String @map("price_id") priceId String @map("price_id")
amount Int?
status String?
active Boolean active Boolean
currency String? currency String?
interval String? interval String?