Payment Webhooks
Handle Creem and Stripe webhook events for one-time card checkout, subscription lifecycle, invoice payment, and plan sync.
Two endpoints, two providers
Per ADR 2026-09-26, Nebutra has two payment webhook endpoints:
| Endpoint | Provider | Covers |
|---|---|---|
POST /api/webhooks/creem | Creem (current) | One-time card purchases, worldwide, merchant of record |
POST /api/webhooks/stripe | Stripe (legacy) | Subscriptions, billing portal, pre-switch checkouts |
Both are pre-built — you do not need to write the routing logic, only add handlers for new events if needed.
Creem webhook (current)
POST /api/webhooks/creem, implemented in backends/gateway/src/routes/webhooks/creem.ts. The creem-signature header is the hex HMAC-SHA256 of the raw request body, keyed by CREEM_WEBHOOK_SECRET — verify the bytes as received, before any parsing:
import { verifyCreemSignature } from "@nebutra/billing";
const rawBody = await c.req.text();
if (!verifyCreemSignature(rawBody, c.req.header("creem-signature"))) {
return c.json({ ok: false }, 401);
}
const event = JSON.parse(rawBody) as CreemWebhookEvent;
if (event.eventType === "checkout.completed" && event.object.order?.status === "paid") {
// order.amount is before tax — Creem adds tax on top as merchant of record
await settlePaymentOrder({
orderId: event.object.request_id!,
paidMinor: event.object.order.amount,
currency: event.object.order.currency,
providerRef: event.object.order.id,
});
}| Creem event | Action |
|---|---|
checkout.completed | Settles the payment order |
refund.created | Logged for visibility — a refund started elsewhere (the Creem dashboard) rather than through refundPaymentOrder |
A pending checkout can be reconciled directly with GET /v1/checkouts?checkout_id=… (getCreemCheckout) if a webhook is ever lost. See the Creem integration guide for full setup.
Stripe webhook (legacy — subscriptions)
All Stripe events are delivered to a single endpoint on the API gateway:
POST /api/webhooks/stripeThis endpoint is pre-built in backends/gateway/src/routes/webhooks/stripe.ts. It only feeds surfaces that have not yet moved to Creem: subscriptions, the billing portal, the licence webhook, and legacy credit-purchase sessions. Nothing new is built on it.
Signature verification
Always verify the Stripe webhook signature before processing any event. Skipping verification allows anyone to send fake events to your endpoint and manipulate billing state.
Stripe signs every request with the Stripe-Signature header. The handler is a Hono route in backends/gateway/src/routes/webhooks/stripe.ts — not a Next.js route — and verifies the header against STRIPE_WEBHOOK_SECRET with the stripe SDK's webhooks.constructEvent before dispatching by event.type.
Handled events
The route's handleStripeEvent switch dispatches on these event types:
| Stripe event | Handler action |
|---|---|
checkout.session.completed | If the session carries a payment-order id, settles that PaymentOrder (ADR 2026-09-25); also emits the checkout-completed analytics event and resolves the licence tier |
customer.subscription.created | Creates the local subscription record |
customer.subscription.updated | Updates the local subscription's status and period |
customer.subscription.deleted | Marks the local subscription canceled |
invoice.paid | Records the payment |
invoice.payment_failed | Marks the subscription past-due |
Any other event type is logged and ignored (log.info("Unhandled Stripe event type", …)).
Registering events in the Stripe Dashboard (legacy)
When setting up your production webhook endpoint, enable exactly these events:
checkout.session.completed
customer.subscription.created
customer.subscription.updated
customer.subscription.deleted
invoice.paid
invoice.payment_failedGo to Stripe Dashboard → Developers → Webhooks → Add endpoint, enter your API URL, and select the events above.
Local testing with Stripe CLI (legacy)
# macOS
brew install stripe/stripe-cli/stripe
# npm (cross-platform)
npm install -g stripestripe loginstripe listen --forward-to localhost:3001/api/webhooks/stripeThe CLI prints a local webhook signing secret (whsec_...). Set this as STRIPE_WEBHOOK_SECRET in your .env.local during development.
In a separate terminal, trigger individual events to test your handlers:
# Simulate a successful subscription update
stripe trigger customer.subscription.updated
# Simulate a payment failure
stripe trigger invoice.payment_failed
# Simulate a paid invoice
stripe trigger invoice.paidThe Stripe CLI signing secret is different from your production webhook signing secret. Use the CLI-provided secret in .env.local and the Dashboard secret in your production environment.
Idempotency
Both providers may deliver the same event more than once (at-least-once delivery). Make your handlers idempotent — processing the same event twice should produce the same result as processing it once.
The Creem handler records each event in a webhook inbox before any side effect, so a redelivery is a no-op. The Stripe checkout.session.completed branch that settles a PaymentOrder reuses the same order-level idempotency as Creem (settlePaymentOrder's conditional update). Subscription handlers update the local record by its Stripe subscription id, so a redelivered customer.subscription.updated/.deleted event is a no-op.
Related
How is this guide?
Last updated on