Webhook Routes
Reference for the DirectoryLaunch Stripe and PayPal webhook handlers, the events they process, local testing options, and outbound Discord and Slack webhooks.
Why this matters
Webhooks are the most common silent-failure surface in any payments-enabled directory. A user pays via Stripe Checkout, the user-facing flow returns success, but if your webhook handler crashes or rejects the event, your project never gets upgraded to premium and the user emails support an hour later. The Stripe webhook is a single endpoint, but it's the single endpoint that turns money into product state — treat it accordingly.
This page documents the inbound payment webhooks (events handled, security model, local testing) and the outbound notification helper that sends events to Slack, Discord or any URL you configure. If your directory ever charges money, read the Common pitfalls section before going to production.
Older versions of this boilerplate targeted Polar. If your deploy still has a /api/webhooks/polar route, it's legacy — keep it deleted.
POST /api/webhooks/stripe
Receives events from Stripe. Confirms payments, activates premium plans, fires email + outbound webhook notifications.
Excluded from middleware so session cookies aren't touched during the signature check.
Events handled
| Event | What it does |
|---|---|
checkout.session.completed | Mark payment paid, upgrade the related project / user |
invoice.paid | Activate recurring promotions / sponsorships |
customer.subscription.updated | Sync subscription status |
customer.subscription.deleted | Revoke promo placements when subscription ends |
Security
Every request is verified with stripe.webhooks.constructEvent():
- Read the raw request body (important: not
req.json()— signature fails) - Read the
stripe-signatureheader - Verify with
STRIPE_WEBHOOK_SECRET
If verification fails → respond 400. Legitimate Stripe requests respond 200.
Setup
See Payments for the Stripe webhook setup checklist.
POST /api/webhooks/paypal
Receives events from PayPal when a listing or a subscription is paid for with PayPal rather than a card. Also excluded from middleware.
Events handled
| Event | What it does |
|---|---|
CHECKOUT.ORDER.APPROVED | Capture an order the buyer approved and then abandoned |
PAYMENT.CAPTURE.COMPLETED | Grant the paid listing — the authoritative event |
PAYMENT.CAPTURE.DENIED | Record the failure so support can check it |
PAYMENT.CAPTURE.REFUNDED | Drop the listing back to the free plan |
PAYMENT.CAPTURE.REVERSED | The same, for a chargeback |
Seven more cover the recurring products — sponsor blocks, promotion placements and lead subscriptions — and are only needed if you sell them:
| Event | What it does |
|---|---|
BILLING.SUBSCRIPTION.ACTIVATED | Switch the sponsor block, placement or lead subscription on |
BILLING.SUBSCRIPTION.UPDATED | Apply PayPal's current status and billing period |
BILLING.SUBSCRIPTION.SUSPENDED | Pause it — repeated failed charges, among others |
BILLING.SUBSCRIPTION.CANCELLED | End it, by the subscriber or by you |
BILLING.SUBSCRIPTION.EXPIRED | It ran to its end |
BILLING.SUBSCRIPTION.PAYMENT.FAILED | Mark it past due |
PAYMENT.SALE.COMPLETED | A renewal went through; move the period forward |
Each subscription event applies PayPal's current state of the subscription rather than assuming the events arrive in order, so a renewal retried after a cancellation cannot switch a cancelled sponsor back on.
In PayPal's event picker, ticking the Checkout group selects Checkout order completed, not Checkout order approved. Subscribe to the child event explicitly and check the Events Tracked column afterwards — see PayPal Payments.
Security
Every request is verified against PAYPAL_WEBHOOK_ID. The app refuses to start in production with PayPal credentials and no webhook id, because an event that cannot be verified means money taken and nothing granted.
Both handlers end in the same fulfilment functions — one for listings, one for subscriptions — so a listing or a placement is granted, and a refund reverts it, by the same rules whichever provider took the money. Idempotency is keyed on the provider's own id (Stripe's event, PayPal's capture).
Setup
See PayPal Payments.
Testing locally
Option A — Stripe CLI (recommended)
stripe listen --forward-to localhost:3000/api/webhooks/stripeThe CLI prints a local webhook secret (whsec_...). Paste that into .env.local temporarily for local work.
Trigger a specific event:
stripe trigger checkout.session.completedOption B — built-in simulator
pnpm webhook:simulateRuns scripts/simulate-webhook.js — fakes a Stripe event payload locally. Useful when you just want to run the handler once without a full Stripe account.
Outbound webhooks
DirectoryLaunch can send webhooks too — typically to Discord or Slack. Not a route, a helper:
import { webhookEvents } from '@/lib/webhooks';
await webhookEvents.projectSubmitted(project);
await webhookEvents.projectApproved(project);
await webhookEvents.projectRejected(project);
await webhookEvents.userRegistered(user);Configure the endpoints in the admin panel → Settings → Integrations (stored in the external_webhooks table). Disable the system entirely with webhooksExternal: false in features.
A real-world walkthrough: from "Pay" click to a Discord notification
Here's the chain of events when a user buys a premium listing:
- User completes Stripe Checkout. Stripe redirects to
/checkout/successwith no special params — the success page is dumb on purpose. - Stripe sends
checkout.session.completedto/api/webhooks/stripe(this can land before, at the same time as, or after the user sees the success page — design your UX accordingly). - The handler verifies the signature, looks up the related
project_idfrom the session metadata, setsplan = 'premium'andpaid_at = now(). - Two outbound events fire: an email confirmation to the user (
/lib/email) and a Discord webhook to your community channel viawebhookEvents.projectApproved(project). - The handler responds 200 to Stripe within the 10-second window. If you exceed that, Stripe retries — and a retried event with already-processed state must be idempotent.
If anything in step 3–4 throws, respond 200 anyway and queue the failed step for retry. Returning a 5xx triggers Stripe's exponential backoff retry, and on the second retry your project will get upgraded twice (or three times, if you're unlucky). Idempotency is non-negotiable.
Common pitfalls
- Calling
req.json()before signature verification. This breaks the Stripe signature check because the raw body is consumed. Always read the raw body first usingawait req.text(), verify, then parse the JSON yourself. - Letting middleware touch the webhook route. If
middleware.tsruns the auth refresher on/api/webhooks/stripe, it touches cookies and changes the Set-Cookie header — Stripe's signature check fails. Confirm the matcher inmiddleware.tsexcludes/api/webhooks/*. - Hardcoding the webhook secret. Use
STRIPE_WEBHOOK_SECRETfrom env. The Stripe CLI prints a differentwhsec_...for local listen — keep it in.env.localand never commit it. - Skipping idempotency. Stripe retries on 5xx and on timeouts. If your handler runs DB updates that aren't keyed on a unique event ID, you'll double-charge in production once a quarter. Use the Stripe
event.idas a row in aprocessed_eventstable or rely on theidempotency_keycolumn already present in thepaymentstable.
FAQ
My webhook works locally with the Stripe CLI but fails in production.
Three usual culprits: (a) the production STRIPE_WEBHOOK_SECRET is the local CLI secret, not the one Stripe shows in the dashboard for your production endpoint; (b) middleware is intercepting the route; (c) the request is being routed through Cloudflare or a CDN that buffers and changes the body. Fix in that order.
Can I add a Polar fallback alongside Stripe?
The legacy Polar route (/api/webhooks/polar) is intentionally orphaned — keep it deleted. Running two providers at once is already supported for listing payments: Stripe and PayPal keep separate routes and separate verification, then hand off to one shared "grant the listing" function. If you add a third, do the same — don't double-write status logic.
How do I retry a webhook that failed silently?
Re-trigger via the Stripe dashboard (Events → click the event → "Resend"). For development, stripe trigger checkout.session.completed --add checkout_session:metadata.project_id=<id>. For full audit, every event handled writes to webhook_events table — query for status = 'failed' to backfill.
See also
- Payments
- PayPal Payments
- Stripe webhook docs
- API Reference Overview — request/response conventions
- Email & Notifications — what fires on payment events