Technical article
Process webhooks securely with signatures and idempotency
Verify provider signatures against the raw body, protect against replay and process repeated events idempotently.
- API & integrations
- Process
- webhooks
- securely
- signatures
- idempotency

In this article
Process a webhook only after provider-specific signature verification against the unchanged body. Then make its business effect idempotent. A valid signature establishes origin under the provider contract but does not prevent duplicate or out-of-order delivery.
Use the provider verification contract
$secret = (string) config('services.stripe.webhook_secret');
abort_if($secret === '', 503);
try {
$event = \Stripe\Webhook::constructEvent(
$request->getContent(),
(string) $request->header('Stripe-Signature'),
$secret,
);
} catch (\UnexpectedValueException | \Stripe\Exception\SignatureVerificationException $error) {
return response()->noContent(400);
}
// Continue with durable receipt handling for verified events only.Illustrative Stripe fragment: the endpoint secret belongs to this webhook destination. The SDK checks the signed format and timing; do not replace it with an arbitrary X-Signature HMAC. Missing configuration is a configuration failure. Invalid payloads or signatures must produce no business processing.
A receipt completes after successful processing
Cache::add before the business write is insufficient: after failure, the marker can remain and discard a legitimate retry. Use durable receipts with a unique provider/event-ID key. Couple local business writes and completion in the same database transaction.
BEGIN TRANSACTION
claim (provider, event_id) using a UNIQUE constraint
if a completed receipt already exists: return its saved outcome
apply the local business changes
mark this receipt completed
COMMIT
on failure: ROLLBACK, leave the event retryable
for external effects: record a durable outbox item, do not call inlineThis transaction outline is pseudocode, not a drop-in handler. A unique constraint and safe concurrency handling are required. External effects need a recoverable outbox or provider idempotency contract; a local transaction cannot roll back an external API call.
Illustrative delivery sequence
- Event E is verified, processed and completed: one business change.
- E arrives again: its completed receipt returns 2xx without repetition.
- Processing new event F fails: no completed receipt, so retry remains possible.
- Two deliveries arrive together: uniqueness and the transaction prevent duplicate local writes.
Ordering and recovery
Do not assume delivery order. Retrieve current provider state where an old event could overwrite a newer status. Respond quickly after safe durable acceptance; run lengthy work through an actually available processor. Without reliable background processing, the design must accommodate processing time and recovery.
Test synthetic or provider-test events: modified body, old signed request, duplicate, concurrent delivery and failure between write and completion. These are recommended scenarios, not an executed payment test.
Code examples are illustrative. They have not been executed in a complete Laravel project; adapt imports, relationships, routes and configuration to your project and test actual behaviour.
Last updated:


