Payment Gateway Integration: What Every Development Team Gets Wrong
Integrating a payment gateway looks simple until you hit webhook reliability, idempotency, and reconciliation. Here's what to get right the first time.
Integrating Stripe, PayPal, or any payment gateway looks straightforward in the documentation. You make an API call, get a response, store the transaction. What could go wrong?
Everything. And it all happens at 3 AM on a Saturday when your payment queue stops processing.
The gap between “basic payment integration” and “production-ready payment integration” is wider than most teams expect. It’s not about the gateway API itself — it’s about handling the dozen failure modes that the documentation glosses over.
The Three Things That Break First
1. Webhook Reliability
Your gateway sends a webhook to confirm a payment. Your server receives it, processes it, marks the order as paid. Simple.
Except your server was restarting. The webhook hit your old instance and got a 500. The gateway retried 3 times and gave up. Your customer was charged but your database still shows the order as pending.
What actually needs to happen:
Webhooks must be:
- Idempotent: Receiving the same webhook twice produces the same state. Store the webhook ID before processing. If you see it again, return 200 OK immediately without reprocessing.
- Retryable: Your endpoint might be down temporarily. Acknowledge receipt immediately (return 200 OK) before you do any heavy work. Process asynchronously in a job queue if the work takes time.
- Logged immutably: Every webhook received gets written to an append-only log before you process it. If you need to debug why a payment wasn’t recorded, the evidence is there.
GET /webhooks/{id}
{
"id": "evt_xyz",
"timestamp": "2026-11-19T14:32:15Z",
"event_type": "charge.success",
"gateway_charge_id": "ch_abc123",
"status": "RECEIVED",
"processed_at": null
}
When you’ve processed it, update the status. Never delete the record.
2. Partial Failures and Double Charges
Your customer clicks “Pay Now.” The charge succeeds at the gateway, but your server crashes before you save the transaction. Your retry logic charges them again. Now they’re charged twice.
Or worse: the charge fails at the gateway, but your error handling is broken, and you charge them three times trying to recover.
What prevents this:
Idempotency keys. Before you send a charge request to the gateway, generate a unique key tied to that customer + order + amount. Send it with the request.
POST /v1/charges
amount=1000
customer=cus_xyz
idempotency_key=order_abc123_attempt_1
The gateway stores this key. If the network fails and you retry with the same key, they return the same result without charging again. Most gateways (Stripe, PayPal, Square) support this.
Transaction state machine. Track the charge through states:
CHARGE_INITIATED— idempotency key created, request sentCHARGE_PENDING— waiting for gateway responseCHARGE_SUCCEEDED— gateway confirmed, update inventory and fulfillmentCHARGE_FAILED— gateway rejected, return error to userCHARGE_TIMEOUT— no response after 60 seconds, need manual investigation
Move through these states atomically. When you transition from one state to the next, write it to the database before you take external action.
3. Reconciliation Gaps
At the end of the day, your payment ledger should match your gateway’s ledger. It almost never does on the first check.
Money is in Stripe’s account but not in your database. Orders are in your database but Stripe has no record. Usually it’s a small number, but as you scale to thousands of transactions a day, reconciliation drift becomes a hemorrhage.
What reconciliation requires:
- Daily settlement reports: Download a full list of all charges from the past 24 hours directly from the gateway’s API. Don’t rely on webhooks alone.
- Matching by gateway ID: Every transaction you store has the gateway’s charge ID. Match against their settlement report by ID, not by amount or timestamp (which can have jitter).
- Idempotent reconciliation: If you run reconciliation twice, you should get the same results. Write reconciliation events (not mutations) so you can audit what was fixed.
- Alert on mismatches: If a charge exists in Stripe but not in your database after 10 minutes, alert. Investigate before the customer calls support.
Production-Ready Checklist
Testing in production-like environments:
- Use the gateway’s sandbox environment for integration tests
- Load test with realistic transaction patterns (not just happy path)
- Inject network failures: timeouts, 500 errors, slow responses
- Verify that failed requests don’t double-charge
- Confirm that webhook retries are idempotent
Monitoring:
- Chart: charge success rate by hour (drop = problem)
- Chart: time from charge API call to webhook received (latency = delay in fulfillment)
- Alert: if reconciliation reports missing charges (> 0 missing for >10 mins)
- Alert: if webhook processing is backlogged
Operational runbooks:
- If Stripe is down: should your orders queue up or fail fast?
- If your webhook processor is down: how do you catch up when it recovers?
- If a customer claims they were charged twice: how do you investigate?
Common Mistakes
Trusting the API docs alone. The docs show happy path. Build for the failure cases: network timeouts, partial responses, webhooks arriving out of order.
Not storing enough context. When you charge someone, store the full request you sent and the full response you got. When something breaks, that data is worth more than logs.
Assuming webhooks are “good enough.” Webhooks can be lost, delayed, or duplicated. Never rely on webhooks as your source of truth. Daily reconciliation is the source of truth.
Skipping idempotency keys. You’ll eventually retry a failed charge and charge the customer twice. Use idempotency keys from day one.
Testing only in sandbox. Sandbox gateways are slower and behave differently than production. Test your production environment’s integration early and continuously.
The Reconciliation Loop
The pattern that works:
- Charge initiated → Send to gateway with idempotency key
- Response received → Atomically update local state + store charge ID
- Webhook received → Verify against stored charge ID, mark fulfilled
- Daily reconciliation → Download settlement report, match against database, alert on gaps
- Mismatch handling → Manual investigation + fix applied only after verification
This is boring, but boring works. It’s the difference between a payment system that processes millions cleanly and one that processes millions with bleeding.
Payment gateway integration isn’t a one-day feature. It’s infrastructure. Teams that treat it like infrastructure — versioned, tested, monitored — rarely have payment incidents. Teams that treat it like a feature have recurring 3 AM pages.
At XydonTech, we’ve built payment infrastructure for platforms processing millions of transactions. The complexity isn’t in calling the API — it’s in everything around the API. If you’re building a fintech platform and need payment integration you can trust, that’s the architecture we design.