Webhook handlers fail in two ways that look identical in your logs but corrupt billing in opposite directions: events that arrive out of order, and events that arrive twice. Paddle and Stripe both retry, both batch, and neither promises ordering. If your handler assumes tidy sequences, a retry grants the same credits twice and a late subscription.updated overwrites a newer cancellation.
This post is the design we ship for that, from working Laravel code: three rules that make subscription sync order-independent and the money-moving side effects exactly-once in effect, without a queue of specialist infrastructure.
Rule 1: Sync the subscription from the event payload, never from sequence assumptions
A webhook handler that branches on event type ("created means insert, updated means update") breaks the day subscription.canceled arrives before the subscription.updated that was sent earlier. The fix is to treat every subscription event the same way: take the payload's current state and reconcile the local row to it.
In practice that means one sync path for all subscription events. The handler extracts the provider's subscription id, the status, the current period end, and the price, then upserts. A late, stale event still writes, so the reconciliation compares the provider's updated_at (or the event's occurred_at) against the stored timestamp and refuses to move the local record backwards:
if ($eventOccurredAt->lessThanOrEqualTo($subscription->provider_synced_at)) { return; // stale event, newer state already stored } $subscription->fill($mappedState); $subscription->provider_synced_at = $eventOccurredAt; $subscription->save(); The result: events can arrive in any order, and the local subscription converges to the newest state the provider has actually reported, not the newest event that happened to arrive.
Rule 2: Key side effects to the invoice, not to the event
The dangerous side effect in a credit-based product is "first payment succeeded, grant the signup credits." Providers send transaction.completed, then send it again on retry, and overlapping events can both look like the first payment if you key the check to "does the user have credits yet."
The fix is to make the grant a function of the invoice id, which is stable across retries:
DB::transaction(function () use ($invoiceId, $user, $amount) { $alreadyGranted = CreditGrant::where('invoice_id', $invoiceId)->lockForUpdate()->exists(); if ($alreadyGranted) { return; } CreditGrant::create(['invoice_id' => $invoiceId, 'user_id' => $user->id, 'credits' => $amount]); $user->creditBalance()->increment('balance', $amount); }); Two properties do the work: a unique constraint on credit_grants.invoice_id, and the whole check-and-grant inside one transaction. A retried webhook now re-runs harmlessly - the row already exists, the handler returns 200, and the provider stops retrying. The user gets exactly one grant per paid invoice no matter how many times the event arrives.
Rule 3: Persist every processed event as a durable receipt
The last piece is operational: you cannot debug what you cannot replay. Every incoming webhook is stored before processing - provider, event id, event type, signature-verification result, and the processing outcome:
WebhookReceipt::create([ 'provider' => 'paddle', 'event_id' => $eventId, 'event_type' => $eventType, 'verified' => $signatureOk, 'processed' => false, 'payload' => $payload, ]); Processing flips processed to true on success. The event_id gets a unique index too, which gives you a second idempotency layer at the door: a retried delivery of the exact same event can be acknowledged without re-entering business logic at all.
When a customer writes in saying their credits are wrong, the receipts table is the difference between "let me read the provider's event log and reconcile by hand" and "here is every event, in order, with what we did about each."
What this costs
One extra table (webhook_receipts), one unique index per side-effect table, and a timestamp comparison in the sync path. No queue topology, no event-sourcing framework, no locking service. The handlers get simpler, not more clever, because each rule removes a case the code no longer has to imagine.
This article was written by an AI assistant.



