Skip to content
All insights

Payment API integrations: webhooks, reconciliation and failure handling

The happy path is the easy part of a payment integration. Idempotency, webhook verification, event ordering, ledgers and daily reconciliation are what keep the money correct.

Payments6 min readREVANTO MANAGEMENT - FZCO

Parallel threads of light running through a lattice of small glass nodes, with a few threads looping back to rejoin the main flow further along.

Most payment integrations work in the demo. A test card goes through, the success page loads, the order is marked paid. The problems appear later: a customer charged twice because a timed-out request was retried, an order marked paid when the payment actually failed, a refund that never reached the accounting system, a payout that does not match the day's sales.

At real volume these are not edge cases; they are certainties. This piece covers the patterns that keep a payment integration correct when networks fail, events arrive out of order, and money moves in more directions than "charge succeeded."

Idempotency and retries

Network calls fail in ambiguous ways. If a request to create or capture a payment times out, you do not know whether the provider processed it. Retrying blindly can charge the customer twice; not retrying can lose the sale.

Idempotency keys solve this. Most payment APIs accept a unique key on write requests such as creating a payment, capturing it or issuing a refund. If the same key arrives again, the provider returns the original result instead of repeating the operation. Check your provider's documentation for which endpoints support keys and how long they are retained.

  • Derive the key from your own domain object, such as the order ID plus the operation, rather than generating a random value at retry time.
  • Persist the key before sending the request, so a crash mid-request can be retried safely with the same key.
  • Retry only errors that are safe to retry: timeouts, connection errors, rate limits and server errors. Card declines and validation errors will fail again, so do not retry them.
  • Use exponential backoff with jitter and a maximum number of attempts, and respect any retry-after guidance the API returns.
  • Treat a decline (a message for the customer) differently from a system error (an alert for your team).

Verify and process webhooks safely

Webhooks tell you what happened after the fact: payment succeeded or failed, refund processed, dispute opened, payout sent. Treat them as untrusted input until they are verified.

Signature verification

  • Verify the signature on every webhook using the secret your provider issues, commonly an HMAC computed over the request body.
  • Compute it over the raw bytes, before any JSON parsing or reformatting, or verification will fail intermittently.
  • Compare signatures with a constant-time function.
  • Where the signature includes a timestamp, reject messages outside a short tolerance window to limit replay attacks.
  • Rotate webhook secrets periodically, accepting both old and new secrets during the changeover.

Duplicates and event ordering

Assume at-least-once delivery. You will receive duplicates, and you will receive events out of order: a success event can arrive before the creation event, and a refund event can arrive while you are still processing the original payment.

  • Store each event ID and skip events already processed.
  • Acknowledge quickly with a 2xx response and do the work asynchronously from a queue. Slow handlers cause timeouts, and timeouts cause redelivery.
  • Model payments as a state machine with allowed transitions, and ignore any event that would move an object backward.
  • When in doubt, fetch the object's current state from the provider's API rather than trusting the event payload.
  • Run a periodic sweep that lists recent objects from the API and compares them with your records, to catch webhooks that never arrived.
  • Send events that fail processing to a dead-letter queue with the error attached, and make replay a supported tool rather than a manual database edit.

Model the full lifecycle in a ledger

A payment is not a single event. Plan for partial and full refunds, including refunds that fail; authorization expiry if you authorize and capture separately; disputes moving from opened to evidence submitted to won or lost, each with its own money movement and fees; asynchronous methods such as bank transfers that stay pending for days and can fail late; and payouts, payout failures, and reserve holds and releases where your provider reports them.

Each of these should update both the order status your operations team sees and the financial records your finance team relies on. That second part needs a ledger.

Ledger design basics

  • Keep money separate from order status. An order can be "shipped"; money is "captured," "refunded," "disputed" or "paid out."
  • Use double-entry principles. Every movement debits one account and credits another, so balances always sum and errors surface instead of hiding.
  • Make entries immutable. Corrections are new entries that reverse or adjust earlier ones, never edits.
  • Store amounts as integers in minor units, with an explicit currency on every entry. Never use floating-point numbers for money, and remember that not every currency has two decimal places.
  • Record provider IDs for the payment, refund, dispute and payout on each entry, so any number can be traced back to its source.
  • Book fees and FX separately from gross amounts.

Reconcile daily against payouts

The provider's payout is the money that actually reaches your bank, and it is the final test of whether your records are right. Daily reconciliation compares three sources:

  1. Your ledger: what you believe was captured, refunded and disputed.
  2. The provider's payout or settlement report: the transactions in each payout, with fees, FX, adjustments and reserve movements.
  3. The bank statement: the payout amount that actually arrived.

Match at transaction level between the ledger and the provider report, then match each payout total to the bank credit. Classify every difference as timing (it will appear in the next payout), fees, FX, reserve movements, dispute debits, or a genuine error. Keep an exceptions queue with a named owner and clear it within days; unexplained differences that pile up become very hard to untangle at month-end. Automate the matching and keep people for the exceptions.

If you cannot explain every payout down to the last cent, you do not yet know whether your integration is correct.

Monitoring and alerting

Payment failures are often silent. Nothing crashes; money just stops moving correctly. Alert on:

  • Payment success rate falling outside its normal range, broken down by payment method and region. A sudden drop usually points to an integration or provider issue rather than customer behavior.
  • Webhook signature failures, processing errors and growth in the dead-letter queue.
  • Gaps in webhook delivery, such as no events during hours when you normally receive them.
  • API error rates and latency to the provider.
  • Reconciliation exceptions above an agreed level, or a payout that did not arrive when expected.
  • Unusual spikes in refunds or disputes.

Route alerts to people who can act on them, with a short runbook for each, and subscribe to your provider's status page and incident notices.

Reduce PCI scope with hosted fields and tokenization

If raw card numbers pass through your servers, your PCI DSS obligations grow substantially. Most businesses have no reason to handle them at all.

  • Use the provider's hosted payment page, or hosted fields and embedded components that send card data directly from the customer's browser to the provider.
  • Store and reuse the tokens or payment method IDs the provider returns, so card data stays with the provider.
  • Scrub request and response logs on payment flows, and keep analytics and session-recording scripts off checkout pages.
  • Control which scripts can run on payment pages, since a compromised script can capture what customers type.
  • Your remaining validation obligations depend on the integration method. Confirm them with your provider and, where needed, a qualified security assessor.

This is general, educational information, not financial, legal or compliance advice; confirm the specifics with your payment provider, your bank and qualified advisers.

How we approach this

We review payment integrations end to end: how writes are retried, how webhooks are verified and processed, how the ledger is structured, and whether payouts reconcile without manual effort. Gaps are documented in priority order, and we work through fixes with the client's own engineers. See our payment operations consulting.

This article is general information, not legal, tax or financial advice. Platform features and policies change; check the current documentation of each platform or provider. See our Disclaimer.

Keep reading

Tell us what you want to scale next.

Start with a consultation. We listen, ask the questions that matter and propose a written scope with a clear fee.