step() call on your success page misses users who close the tab and counts users whose payment later fails. This guide sends the payment from your Stripe webhook as a server-side step, so it appears in the same funnel as the browser steps before it, such as pricing_viewed and checkout_started.
The examples use a Next.js App Router route handler on the Node.js runtime and the stripe library. The Wendung calls work in any server runtime with fetch. On edge runtimes, verify the signature with await stripe.webhooks.constructEventAsync(body, signature, secret, undefined, Stripe.createSubtleCryptoProvider()).
Before you start
- The browser SDK is set up and calls
Wendung.identify(userId)when users sign in. See the Next.js guide. - You have a secret key in your server environment as
WENDUNG_SECRET_KEY. - Your app has a Stripe webhook endpoint, or you are about to add one. Subscribe it to
checkout.session.completed,checkout.session.async_payment_succeededandinvoice.paid.
Pass the user ID to Checkout
When you create the Checkout Session, setclient_reference_id to the same ID you pass to identify() in the browser. Stripe returns it on the session in every Checkout webhook event.
app/api/checkout/route.ts
client_reference_id holds up to 200 characters. Wendung accepts user IDs up to 255 characters, so any ID that fits in Stripe fits in Wendung.
Send the step from the webhook
Install the SDK and the Stripe library on the server:WendungServer from @wendung/sdk/server in the route handler. The browser SDK from @wendung/sdk still belongs only in client components.
app/api/stripe/webhook/route.ts
- Signature check.
constructEventneeds the raw request body, which is why the handler readsrequest.text()and notrequest.json(). Requests that fail the check never reach Wendung. - Two event types. Card payments are
paidwhencheckout.session.completedarrives. Delayed methods, such as bank debits, arrive asunpaidand sendcheckout.session.async_payment_succeededonce the money is confirmed. Checkingpayment_status === 'paid'records each payment once, at the moment it succeeds. - Free trials. A subscription that starts with a free trial can complete Checkout as
paidwith anamount_totalof0, because Stripe marks the trial’s zero-amount invoice as paid. The handler records that astrial_started, sopayment_succeededcounts only Checkouts that charged the user. Any other Checkout that totals0, such as one with a 100% discount, gets the same step. The first charge after the trial arrives asinvoice.paidwithbilling_reasonset tosubscription_cycle, so the renewal branch below records it assubscription_renewed. To measure trial conversion, build a funnel withtrial_startedfollowed bysubscription_renewed. idandtimestampfrom Stripe. Stripe retries failed deliveries for up to three days. Because the step uses the Stripe event’s ID and creation time, a retried delivery replaces the earlier copy instead of counting the payment twice. Wendung accepts steps up to 7 days old, which covers Stripe’s retry window.- No error handling around
step(). It never throws: a Wendung error returns{ ok: false }and logs a warning, so it can’t fail your webhook. It waits for ingest, including up to two retries, before your handler responds. TheWendungServerconstructor does throw if the key is missing or doesn’t start withsk_, so a missingWENDUNG_SECRET_KEYfails the route when it loads.
amount_total is in the currency’s smallest unit, for example cents.
Record subscription renewals
Renewals don’t go through Checkout, so there is noclient_reference_id. Stripe sends invoice.paid with the Stripe customer ID in invoice.customer. Map that to your user ID from your own records, for example the customer ID you saved when the first Checkout completed.
Add this branch to the same handler:
app/api/stripe/webhook/route.ts
findUserIdByStripeCustomer stands for your own lookup. The billing_reason check limits the step to renewals, including the first charge after a free trial. It skips the invoice for the subscription’s first period, which the Checkout branch handles.
Build the funnel
Under Funnels in the dashboard, create a funnel that ends with the server step, for example:pricing_viewed(browser)checkout_started(browser)payment_succeeded(server)
Test it
-
Forward test-mode webhooks to your local server with the Stripe CLI and put the signing secret it prints in
STRIPE_WEBHOOK_SECRET: - Sign in to your app and complete a Checkout with a Stripe test card.
- Open the project’s Events page. The request shows Server as its origin.
stripe trigger checkout.session.completed sends an event without client_reference_id, so the handler skips it. Use a real test-mode Checkout to see the step arrive.
Server requests don’t send an Origin header, so your allowed origins and Dev mode don’t apply to them.
Consent
Your server decides whether it may record a payment for a user. If the user declined analytics in your consent manager, skipstep() for them in the webhook too. See Consent.