Skip to content

Lesson 4 of 9 · 28m

Webhook Verification and Event Ordering

Verify provider signatures, tolerate retries and out-of-order events, and use webhooks as asynchronous state evidence rather than trusted commands.

If you have never run payments in production, webhooks feel redundant: the API already returned success. If you have run payments in production, webhooks are the part that tells you what happened after the customer closed the browser, the bank finished checking, or a dispute appeared three weeks later. Payment truth is asynchronous. Webhooks are how that truth arrives.

Verify before parsing business meaning

Stripe signs webhook payloads with a timestamped signature. Verify the exact raw request body; do not parse and re-serialize JSON first:

event = stripe.Webhook.construct_event(
    payload=request.body,
    sig_header=request.headers["Stripe-Signature"],
    secret=webhook_signing_secret,
)

PayPal webhook verification is a separate API-mediated process using the webhook ID, transmission headers, certificate chain, and event body. In both cases, unverified events are untrusted input. An attacker who can post payment_succeeded to your endpoint should not be able to unlock anything.

Use a raw-body endpoint

Framework middleware often destroys the exact bytes needed for signature verification:

# Django-style example: bypass JSON parsing before verification.
def stripe_webhook(request):
    payload = request.body
    sig = request.META["HTTP_STRIPE_SIGNATURE"]
    event = stripe.Webhook.construct_event(payload, sig, secret)
    return process_event(event)

Compression, JSON body parsers, proxy rewriting, and test-client conveniences can all break verification. If signatures pass in curl but fail in the app, suspect middleware before suspecting the provider.

Event ordering is not promised

You may receive:

payment_intent.processing
payment_intent.succeeded
charge.updated
payment_intent.created

in an order that does not match your mental timeline. Design handlers to be monotonic where possible. A terminal success should not be overwritten by an older pending event. A cancellation after success should trigger investigation rather than blind status regression.

RANK = {
    "created": 10,
    "requires_action": 20,
    "processing": 30,
    "succeeded": 90,
    "canceled": 95,
}

Store provider_event_id, event_created_at, observed status, and processing time. You will need it when reconciling weird sequences.

Acknowledge quickly, process safely

Providers expect a timely 2xx. Do not run slow fulfillment work inline. The production pattern is:

verify signature -> persist event -> enqueue job -> return 200

Return non-2xx only when you want the provider to retry because your system genuinely could not durably accept the event. Returning 500 for a business-logic duplicate poisons retry behavior and creates alert noise.

Webhooks are evidence, not commands

After receiving payment_intent.succeeded, fetch or otherwise confirm the current object when the action is high risk. Event payloads can be delayed, replayed legitimately, or missing fields depending on API versions. The event tells you something changed; the current object state tells you what is true now.

Field notes

  • Rotate webhook secrets like credentials and scope them per environment.
  • Never log full webhook payloads blindly; they can contain customer PII and payment metadata.
  • Use separate endpoint secrets for live and test modes. Accidentally mixing them causes baffling verification failures.
  • checkout.session.completed is not always interchangeable with payment_intent.succeeded; understand which object model your flow uses.
  • Monitor webhook delivery failures in the provider dashboard; silence usually means your integration is blind, not healthy.

Was this lesson helpful?

Sign in to rate