Webhooks are the one part of your app where the failure mode is silence. A normal endpoint that breaks throws 500s you can see; a webhook endpoint that breaks just stops mattering, because the only thing watching it is a provider's retry queue that gives up after a while. By then you've missed a day of Stripe events, and the first you hear of it is a customer asking why their subscription never activated. This guide covers both halves: writing the handler correctly, and noticing when deliveries stop arriving.
The 4-part pattern
Every webhook endpoint should follow the same skeleton:
app/Http/Controllers/StripeWebhookController.php
use App\Jobs\ProcessStripeWebhook;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Stripe\Exception\SignatureVerificationException;
use Stripe\Webhook;
public function __invoke(Request $request)
{
// 1. Verify signature on the RAW body (Stripe signs "timestamp.body")
$body = $request->getContent();
try {
$event = Webhook::constructEvent(
$body,
(string) $request->header('Stripe-Signature'),
config('services.stripe.webhook_secret'),
);
} catch (SignatureVerificationException | \UnexpectedValueException $e) {
abort(401, 'Invalid signature');
}
$eventId = $event->id;
// 2. Persist raw payload for audit and replay
// (needs a unique index on provider + event_id)
$stored = DB::table('webhook_deliveries')->insertOrIgnore([
'provider' => 'stripe',
'event_id' => $eventId,
'event_type' => $event->type,
'payload' => $body,
'received_at' => now(),
]);
// 3. Idempotency: only queue if this is a fresh delivery
if ($stored === 0) {
// Duplicate retry: we already have it. ACK and bail.
return response()->json(['status' => 'duplicate']);
}
// 4. Process async: return 200 fast
ProcessStripeWebhook::dispatch($eventId);
return response()->json(['status' => 'queued']);
}
Signature first: if we can't trust the payload, nothing else matters. Raw body: request()->all() re-serializes and breaks HMAC. Unique event ID insert: duplicate retries hit the constraint. Async dispatch: the controller returns in under a second.
Once you return 200, it's your problem
There's a subtlety in accept-and-queue worth being explicit about: the moment you return 200, the provider considers the event delivered and stops retrying. If your queued job then fails (a processing bug, a downstream outage, a poison payload), the provider won't help you. The event is gone from their side. That's exactly why persisting the raw payload before you return 200 matters: it's the only copy you control, and it's what lets you replay a delivery your own processing dropped. Treat the webhook_deliveries table as the source of truth, not the provider's dashboard.
Dead-man's-switch monitoring
The scariest webhook bug: no webhooks at all. Someone rotates the signing secret in the provider's dashboard without updating your .env, every delivery now fails with 401, and nobody notices until business metrics drop.
Alert on absence, not on errors:
app/Console/Commands/CheckWebhookHeartbeat.php
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
class CheckWebhookHeartbeat extends Command
{
protected $signature = 'webhooks:heartbeat';
public function handle()
{
$providers = ['stripe', 'shopify', 'sendgrid'];
foreach ($providers as $provider) {
$recentCount = DB::table('webhook_deliveries')
->where('provider', $provider)
->where('received_at', '>=', now()->subMinutes(30))
->count();
if ($recentCount === 0) {
logger()->alert("No {$provider} webhooks in 30 minutes");
// Send Slack alert via a real channel
}
}
}
}
Schedule it every 5 minutes in routes/console.php with Schedule::command('webhooks:heartbeat')->everyFiveMinutes(), and tune the window per provider. A high-volume provider (Stripe at scale) should alert after 5 silent minutes; a low-volume one can go longer.
Metrics that matter
- Delivery rate per provider: time-series of webhooks received per minute. Drops alert.
- Signature failure rate: > 0 is a red flag.
- Receive-to-ACK latency: should be under 1 second. Longer means the controller is doing too much synchronously.
- Processing job failure rate: async processing is where real business logic lives and breaks.
- Duplicate rate: measure how often you hit the idempotency constraint. Some duplicates are normal; a spike usually means the provider thinks you're timing out.
Spatie's laravel-webhook-client
If you don't want to hand-roll the pattern, Spatie's package does most of it: signature verification, persistence to a webhook_calls table, and async job dispatch with the stored call. Recommended for multi-provider webhook setups.