Theo's Typesafe CultTTC
Theo's Typesafe Cult9mo ago
1 reply
Jakov

How to store user's tier?

I use Theo's recommendation for Stripe integration.

However, I have a question.
I don't know how to store the tier of the user (free,plus or pro).

Here's the webhook code:
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!, {
  apiVersion: "2025-03-31.basil",
});

const allowedEvents: Stripe.Event.Type[] = [
  "checkout.session.completed",
  .........
  "payment_intent.canceled",
];

async function processEvent(event: Stripe.Event) {
  // Skip processing if the event isn't one I'm tracking (list of all events below)
  if (!allowedEvents.includes(event.type)) return;
  console.log("[STRIPE WEBHOOKS] Processing event", event.type);
  // All the events I track have a customerId
  const { customer: customerId } = event?.data?.object as {
    customer: string; // Sadly TypeScript does not know this
  };
  // This helps make it typesafe and also lets me know if my assumption is wrong
  if (typeof customerId !== "string") {
    throw new Error(
      `[STRIPE HOOK][CANCER] ID isn't string.\nEvent type: ${event.type}`,
    );
  }

  return await syncStripeDataToKV(customerId);
}

export async function POST(req: NextRequest) {
  const body = await req.text();
  const signature = (await headers()).get("Stripe-Signature");

  if (!signature) return NextResponse.json({}, { status: 400 });

........

  return NextResponse.json({ received: true });
}


I was thinking of storing the tier in my db, but I don't know where in this code I can get:
- the tier that user subscribed to
- user's id: I only have customer id, but I stored a relation of user's id and customer id in the way where I can only get customer's id by user's id (not reverse)
Was this page helpful?