stripe-webhook-handler

Builds and reviews Stripe webhook endpoints - signature verification, idempotency, event routing and retries. Use when the user adds a Stripe webhook, debugs "No signatures found matching the expected signature", or handles checkout.session.completed / invoice events.

CodingSource

Install

mkdir -p ~/.claude/skills/stripe-webhook-handler
# save the file below as ~/.claude/skills/stripe-webhook-handler/SKILL.md

SKILL.md

stripe-webhook-handler/SKILL.md
---
name: stripe-webhook-handler
description: Builds and reviews Stripe webhook endpoints - signature verification, idempotency, event routing and retries. Use when the user adds a Stripe webhook, debugs "No signatures found matching the expected signature", or handles checkout.session.completed / invoice events.
---

# Stripe Webhook Handler

## Non-negotiables

- Verify the signature against the **raw** request body with the endpoint secret (`whsec_…`). Never parse JSON first.
- Return `2xx` quickly; do slow work after acknowledging, or in a queue.
- Make every handler idempotent: store `event.id` (or the object id + state) under a unique key and skip repeats. Stripe retries for up to 3 days.
- Events can arrive out of order. Re-fetch the object from the API when the order matters.
- Check `livemode` and the account so test events never touch production data.

## Steps

1. Read the raw body (`await request.text()` in fetch handlers; `express.raw({ type: 'application/json' })` in Express).
2. `stripe.webhooks.constructEvent(body, sigHeader, secret)`; on failure return 400.
3. `switch (event.type)` over only the events the app needs; return 200 for the rest.
4. Within one database transaction: claim the event id, apply the change.
5. Log `event.id` and `event.type`, never the payload's personal data.

## Events most apps need

| Event | Do |
|---|---|
| `checkout.session.completed` | Mark the order paid (check `payment_status === 'paid'`) |
| `invoice.paid` | Extend the subscription period |
| `invoice.payment_failed` | Flag the account, email the customer |
| `customer.subscription.deleted` | End access at `current_period_end` |
| `charge.refunded` | Revoke what the payment granted |

## Testing

`stripe listen --forward-to localhost:3000/api/stripe/webhook`, then `stripe trigger checkout.session.completed`.

More Coding skills