[Go to site: main page, start]

Skip to main contentBlack Friday40% off

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.

Not Polar

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

EventWhat it does
checkout.session.completedMark payment paid, upgrade the related project / user
invoice.paidActivate recurring promotions / sponsorships
customer.subscription.updatedSync subscription status
customer.subscription.deletedRevoke promo placements when subscription ends

Security

Every request is verified with stripe.webhooks.constructEvent():

  1. Read the raw request body (important: not req.json() — signature fails)
  2. Read the stripe-signature header
  3. 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

EventWhat it does
CHECKOUT.ORDER.APPROVEDCapture an order the buyer approved and then abandoned
PAYMENT.CAPTURE.COMPLETEDGrant the paid listing — the authoritative event
PAYMENT.CAPTURE.DENIEDRecord the failure so support can check it
PAYMENT.CAPTURE.REFUNDEDDrop the listing back to the free plan
PAYMENT.CAPTURE.REVERSEDThe 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:

EventWhat it does
BILLING.SUBSCRIPTION.ACTIVATEDSwitch the sponsor block, placement or lead subscription on
BILLING.SUBSCRIPTION.UPDATEDApply PayPal's current status and billing period
BILLING.SUBSCRIPTION.SUSPENDEDPause it — repeated failed charges, among others
BILLING.SUBSCRIPTION.CANCELLEDEnd it, by the subscriber or by you
BILLING.SUBSCRIPTION.EXPIREDIt ran to its end
BILLING.SUBSCRIPTION.PAYMENT.FAILEDMark it past due
PAYMENT.SALE.COMPLETEDA 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.

CHECKOUT.ORDER.APPROVED is easy to miss

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

stripe listen --forward-to localhost:3000/api/webhooks/stripe

The CLI prints a local webhook secret (whsec_...). Paste that into .env.local temporarily for local work.

Trigger a specific event:

stripe trigger checkout.session.completed

Option B — built-in simulator

pnpm webhook:simulate

Runs 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:

  1. User completes Stripe Checkout. Stripe redirects to /checkout/success with no special params — the success page is dumb on purpose.
  2. Stripe sends checkout.session.completed to /api/webhooks/stripe (this can land before, at the same time as, or after the user sees the success page — design your UX accordingly).
  3. The handler verifies the signature, looks up the related project_id from the session metadata, sets plan = 'premium' and paid_at = now().
  4. Two outbound events fire: an email confirmation to the user (/lib/email) and a Discord webhook to your community channel via webhookEvents.projectApproved(project).
  5. 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 using await req.text(), verify, then parse the JSON yourself.
  • Letting middleware touch the webhook route. If middleware.ts runs the auth refresher on /api/webhooks/stripe, it touches cookies and changes the Set-Cookie header — Stripe's signature check fails. Confirm the matcher in middleware.ts excludes /api/webhooks/*.
  • Hardcoding the webhook secret. Use STRIPE_WEBHOOK_SECRET from env. The Stripe CLI prints a different whsec_... for local listen — keep it in .env.local and 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.id as a row in a processed_events table or rely on the idempotency_key column already present in the payments table.

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