Webhooks will arrive twice. Plan for it.
Every payment platform retries webhooks. An architecture that processes invoice.paid twice grants two credits, ships two orders, or marks a milestone paid that isn't. Idempotency is not an optimization — it is the baseline.
The processing contract
- Verify the signature first. Stripe: HMAC-SHA256 against the raw body with the endpoint secret. PayPal: verify the webhook transmission against registered webhook IDs. Reject before parsing.
- Dedupe on the provider event ID. A unique constraint makes duplicates a no-op:
CREATE TABLE payment_events (
provider TEXT NOT NULL, -- 'stripe' | 'paypal'
event_id TEXT NOT NULL,
event_type TEXT NOT NULL,
processed_at TIMESTAMPTZ,
payload_hash TEXT,
PRIMARY KEY (provider, event_id)
);
- Insert-then-process. Insert the event row inside the same transaction as the ledger effect. If the insert conflicts, return 200 and stop.
- Return 2xx fast. Do heavy work (emails, provisioning) in a queue keyed off the committed event, so slow downstream services never cause provider retries of the money path.
Out-of-order events
payment_intent.succeeded can arrive after invoice.paid; refunds can arrive before your system has seen the charge on a replayed day. Model state transitions, not event order: each handler reads current ledger state, applies a guarded transition, and writes an audit row. A transition that does not apply (refund for an unknown charge) goes to an exception queue — never silently dropped.
The ledger is the source of truth
Keep a double-entry-style ledger table separate from provider objects:
CREATE TABLE ledger_entries (
id BIGSERIAL PRIMARY KEY,
client_id TEXT NOT NULL,
invoice_no TEXT,
direction TEXT NOT NULL CHECK (direction IN ('DEBIT','CREDIT')),
amount_usd NUMERIC(12,2) NOT NULL,
reason TEXT NOT NULL, -- 'milestone_2_payment' | 'refund' | 'dispute_hold'
provider_ref TEXT,
created_at TIMESTAMPTZ DEFAULT now()
);
Nightly reconciliation diffs the ledger against the provider's balance-transaction API. Mismatches page a human. This is exactly the discipline payment underwriters (Stripe, PayPal, Mercury, Wise Business) look for when reviewing a merchant.
Refunds and disputes
- Refunds are ledger events with provider references, issued within a published SLA (we commit 5–10 business days to the original payment method).
- Disputes freeze the associated ledger lines and gather the evidence bundle automatically: approved SOW, milestone approvals, HTML preview sign-off, delivery records, and communication logs — all retrievable by permanent Client ID.
Marketplaces and split payouts
For marketplaces, model the platform as its own ledger participant: buyer pays → platform holds → split on fulfillment → provider payout reconciled. Stripe Connect (or PayPal for Business) handles rails, but your ledger must still record every leg, or reconciliation becomes archaeology.
The payoff
Idempotent, ledger-first billing produces three visible outcomes: zero double-grants, a finance close that takes minutes instead of days, and an underwriting file that answers every risk question before it is asked.
Put this into practice
Get a written Scope of Work with USD milestone pricing — or estimate your build first with our free interactive calculators.