agentleFS
charanjit-singhcharanjit-singh/lovable-skills/skills/stripe-payment-webhooks/SKILL.md

stripe-payment-webhooks

Skill10 starsChanged 6 months agoMITView on GitHub

Use when implementing or fixing Stripe payment webhooks: signature verification, event handlers, syncing subscriptions or Checkout to Supabase, webhook endpoint setup. Not for pricing page UI only, non-Stripe providers, or generic edge functions without payment events (see edge-functions-and-webhooks).

  • Reads credentials

1 other project uses a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

5 rules. About 0 are common, 3 are distinctive, and 2 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈3

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Use raw body for signature verification (do not parse JSON first).

    line 20
  2. Distinctive:

    Always set on Checkout Session creation (server or Stripe Dashboard template):

    line 116
  3. Distinctive:

    Never trust webhook to grant access without resolving user from metadata you control at session creation.

    line 121
  4. Very specific:

    After DB upsert, app reads subscriptions.status:

    line 125
  5. Very specific:

    Not pro: canceled, unpaid, past_due, incomplete, missing row

    line 128

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Stripe payment webhooks

Webhooks are the source of truth for paid state. Checkout success URLs and client callbacks are not sufficient alone.

Related: edge-functions-and-webhooks (hosting), payment-webhook-idempotency, payment-webhook-testing, payment-failed-and-recovery, pricing-and-billing.

Endpoint

  • Supabase Edge Function: supabase/functions/stripe-webhook/index.ts
  • Stripe Dashboard → Webhooks → endpoint URL → signing secret in STRIPE_WEBHOOK_SECRET
  • Use raw body for signature verification (do not parse JSON first).
import Stripe from "npm:stripe@17";

const stripe = new Stripe(Deno.env.get("STRIPE_SECRET_KEY")!, {
  apiVersion: "2024-11-20.acacia",
  httpClient: Stripe.createFetchHttpClient(),
});
const cryptoProvider = Stripe.createSubtleCryptoProvider();

const signature = req.headers.get("stripe-signature");
const body = await req.text();

let event: Stripe.Event;
try {
  event = await stripe.webhooks.constructEventAsync(
    body,
    signature!,
    Deno.env.get("STRIPE_WEBHOOK_SECRET")!,
    undefined,
    cryptoProvider
  );
} catch {
  return new Response("Invalid signature", { status: 400 });
}

Return 2xx quickly after enqueue or DB write; Stripe retries on non-2xx.

Database (service role inside function)

-- Processed events (idempotency) — see payment-webhook-idempotency skill
create table public.stripe_webhook_events (
  id text primary key,  -- Stripe event id evt_...
  type text not null,
  processed_at timestamptz default now()
);

create table public.subscriptions (
  user_id uuid primary key references auth.users(id),
  stripe_customer_id text unique,
  stripe_subscription_id text unique,
  status text not null,
  price_id text,
  current_period_end timestamptz,
  cancel_at_period_end boolean default false,
  updated_at timestamptz default now()
);

Map user_id via client_reference_id on Checkout or metadata.user_id on Customer/Subscription.

Event handlers (subscriptions)

EventAction
checkout.session.completedRead subscription or mode; link customer to user_id; upsert subscription row
customer.subscription.createdUpsert status, price_id, period end
customer.subscription.updatedSame; handle plan change, cancel_at_period_end
customer.subscription.deletedSet status canceled; revoke entitlements
invoice.paidConfirm period; optional receipt email
invoice.payment_failedSee payment-failed-and-recovery
switch (event.type) {
  case "checkout.session.completed": {
    const session = event.data.object as Stripe.Checkout.Session;
    const userId = session.client_reference_id ?? session.metadata?.user_id;
    if (!userId) break;
    // retrieve subscription if mode=subscription
    break;
  }
  case "customer.subscription.updated":
  case "customer.subscription.deleted": {
    const sub = event.data.object as Stripe.Subscription;
    await upsertSubscriptionFromStripe(sub);
    break;
  }
  default:
    // unhandled — still return 200 if intentionally ignored
}

One-time payments

For mode: "payment" Checkout or Payment Links, handle:

  • checkout.session.completed (check mode === "payment")
  • payment_intent.succeeded

Store orders or purchases table; do not reuse subscription row without clear schema.

Metadata contract

Always set on Checkout Session creation (server or Stripe Dashboard template):

  • client_reference_id: Supabase auth.users.id
  • metadata.user_id: same (backup)

Never trust webhook to grant access without resolving user from metadata you control at session creation.

Entitlements

After DB upsert, app reads subscriptions.status:

  • Pro: active, trialing
  • Not pro: canceled, unpaid, past_due, incomplete, missing row

Avoid

  • Skipping signature verification in production.
  • Using Checkout success_url alone to set is_pro.
  • Logging full card or customer PII.
  • 4xx on duplicate events (use idempotency — return 200 if already processed).

Checklist

  • Raw body + constructEvent / constructEventAsync
  • stripe_webhook_events insert before side effects
  • Service role client only inside function
  • Test mode vs live mode secrets separated

Templates: examples.md.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.