StoicBuilder
Later missionMission 14: Pro for anyone who asksYou can practise it now. It counts once you reach mission 14, after “One search, every member”.Back to HQ

DrillsPro for anyone who knows the webhook URL

Pro for anyone who knows the webhook URL

III the CastraHarbor, built with Claude CodeTrickyAbout 9 minPro

What happened

Harbor sells Pro one month at a time ($12) through Stripe Checkout, and Claude Code wrote the webhook that turns Pro on after payment. On Tuesday your email service was slow, the route timed out after adding the month, and Stripe sent the same event again, twice. Then a tester told you something worse.

A testerI POSTed some JSON I wrote by hand to /api/webhooks/stripe, with my user id in it. I have Pro until January. I never paid.
What you seeServer log
Dec 01 10:02:14 evt_DEMO_3kQ9: Pro until 2026-12-31 for 8c1e4a7b Dec 01 10:07:51 evt_DEMO_3kQ9: Pro until 2027-01-30 for 8c1e4a7b Dec 01 11:08:03 evt_DEMO_3kQ9: Pro until 2027-03-01 for 8c1e4a7b Dec 05 09:41:37 undefined: Pro until 2027-01-04 for f02a91d3

The MagisterYou don't have to fix the code yourself. Read it until you can say, in plain words, why it breaks.

import { NextResponse } from "next/server";
import { supabaseAdmin } from "@/lib/supabase/admin";
import { sendWelcomeEmail } from "@/lib/email";

// Stripe calls this when someone pays for a month of Harbor Pro.
export async function POST(req: Request) {
  const event = await req.json();

  if (event.type === "checkout.session.completed") {
    const userId = event.data.object.metadata.userId;

    const { data: profile } = await supabaseAdmin
      .from("profiles")
      .select("pro_until")
      .eq("id", userId)
      .single();

    // Add 30 days, from today or from the end of their current Pro.
    const from = Math.max(Date.now(), Date.parse(profile?.pro_until ?? "") || 0);
    const proUntil = new Date(from + 30 * 24 * 60 * 60 * 1000).toISOString();

    await supabaseAdmin.from("profiles").update({ plan: "pro", pro_until: proUntil }).eq("id", userId);
    console.log(`${event.id}: Pro until ${proUntil.slice(0, 10)} for ${userId}`);
    await sendWelcomeEmail(userId);
  }

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

This one is for reading: the code needs a real database or server to run.

Your answer

Reviewed by AI against what a good fix needs. About 10 seconds.

Sign in to get your answer reviewed

It's free. Anything you've typed here stays in this browser and is waiting when you come back.

Sign in with email