DevPath
Mid lesson · 12 min

Verify webhooks and test failure paths

Treat repeated and forged events as normal design cases.

Authenticate the event

Verify the provider's signature against the raw request body before trusting any fields. Apply timestamp tolerance where the provider's scheme requires it, and do not log secrets. A valid signature proves the event came through the configured channel; it does not prove the event has not already been processed.

Deduplicate the business effect

Store the provider event ID under a unique constraint. Process the state transition in a transaction with the record of that ID. A repeated delivery should return success without granting a second credit or sending a second fulfillment. If an external side effect is required, use a separate idempotent worker.

if (WebhookEvent::where('provider_id', $eventId)->exists()) {
    return response()->noContent();
}
// The unique index still guards a concurrent duplicate.

Build a threat-shaped test set

Test a valid event, changed body with the old signature, missing signature, duplicate event, and two deliveries with the same ID. Assert the final ledger or entitlement state, not only response codes. Keep clock behavior deterministic so timestamp checks do not make tests flaky.

Check your understanding

Does a valid webhook signature make duplicate delivery harmless?

Show explanation

No. Signatures authenticate the payload; a unique event ID and idempotent processing prevent duplicate effects.

Put this lesson into practice

A practice activity at this level in the same module.

Try: Deduplicate a payment webhook

Further reading

Primary references for this module. Check the current version when behavior or framework guidance may have changed.