Building a Payment Gateway API from Scratch

Khimananda Oli 7 min read Programming and Languages
Building a Payment Gateway API from Scratch

By Khimananda Oli | Last reviewed: August 2026

Building a Payment Gateway API from Scratch is fundamentally an exercise in data integrity and security, not just web development. Unlike standard CRUD applications, financial systems cannot tolerate eventual consistency, lost requests, or silent failures; every transaction must be atomic, traceable, and cryptographically verified. This guide outlines the architectural patterns, database schemas, and operational safeguards required to build a production-grade payment platform that meets global compliance standards while serving local markets like Nepal.

What core architecture is needed when building a payment gateway API from scratch?

The most common mistake engineers make when building a payment gateway API from scratch is treating money as a mutable integer in a single column. In production fintech systems, you must adopt a double-entry ledger model where every transaction creates at least two opposing entries. This ensures the system can always self-audit: the sum of all debits must equal the sum of all credits. Before writing a single line of API code, you need to understand how data flows through your system securely.

Merchant AppClient SDKAPI GatewayRate Limit / WAFIdempotency CheckAuth / mTLSCore LedgerACID TransactionsBank RailsNRB / SWIFTAudit LogImmutable Events
Secure request flow when building a payment gateway API from scratch: validation precedes ledger writes

This architecture separates concerns strictly. The API Gateway handles authentication, rate limiting, and idempotency checks before any request touches the Core Ledger. For teams in Nepal integrating with local wallets like eSewa or Khalti alongside international cards, this abstraction layer is vital. It allows you to normalize disparate upstream responses into a unified internal state machine. If you are also managing high-volume transactional data, reviewing PostgreSQL administration essentials will help ensure your ledger database remains performant under load.

How do you implement idempotency and prevent double-spending?

Networks are unreliable. Clients retry. When building a payment gateway API from scratch, you must assume every POST request could arrive twice. Without idempotency, a retried charge results in double billing. The solution is an idempotency key pattern enforced at the database level, not just the application cache.

Designing the Idempotency Schema

Never rely solely on Redis for idempotency in financial systems. Cache evictions can lead to duplicate processing. Instead, use a dedicated PostgreSQL table with a unique constraint. This guarantees atomicity even during failovers.

CREATE TABLE idempotency_keys (
    key_hash BYTEA PRIMARY KEY,      -- SHA-256 hash of client key
    endpoint TEXT NOT NULL,
    request_body_hash BYTEA NOT NULL,
    response_code INT,
    response_body JSONB,
    created_at TIMESTAMPTZ DEFAULT NOW(),
    locked_at TIMESTAMPTZ            -- Prevents concurrent processing
);

-- Index for cleanup jobs
CREATE INDEX idx_idempotency_created ON idempotency_keys(created_at);

Handling Concurrent Requests Safely

A common race condition occurs when two identical requests hit different pods simultaneously. You must use row-level locking. The workflow should be:

  1. Hash the incoming Idempotency-Key header combined with the endpoint path.
  2. Attempt an INSERT with locked_at = NOW(). If it fails due to constraint violation, another request is active.
  3. If the row exists but response_code is NULL and locked_at is recent, return 409 Conflict or wait briefly.
  4. If the row exists with a completed response, return the cached response immediately without re-processing.
  5. After processing, update the row with the actual response and clear locked_at.

This pattern ensures exactly-once semantics for side effects. For deeper insights into handling concurrency and replication lag in your ledger backend, see our guide on PostgreSQL replication and high availability.

What security standards apply when building a payment gateway API from scratch?

Security in payments is non-negotiable. When building a payment gateway API from scratch, you inherit the responsibility of protecting cardholder data (CHD) and sensitive authentication data (SAD). Even if you offload raw card handling to a processor, your API metadata and user PII remain targets.

PCI-DSS v4.0 Compliance BoundaryRaw Card DataPAN / CVV / ExpiryNEVER STORETokenize ImmediatelyVault / TokenizerHSM BackedAES-256-GCMFormat PreservingInternal LedgerStores ONLY TokensReference IDsSafe for AnalyticsKey Rotation Policy
Tokenization architecture ensuring PCI compliance when building a payment gateway API from scratch

Tokenization Over Encryption

Do not encrypt PANs in your application database. Use a dedicated vault service (like AWS Payment Cryptography or a FIPS 140-2 validated HSM) to exchange raw card data for format-preserving tokens. Your ledger should only ever see these tokens. This drastically reduces your PCI-DSS scope from Level 1 to potentially Level 4, saving hundreds of thousands in annual audit costs.

Secrets Management and Signing

All API responses containing financial data must be signed. Use HMAC-SHA256 with rotating keys stored in a secrets manager like HashiCorp Vault or AWS Secrets Manager. Never hardcode keys. For webhook notifications to merchants, implement a signature verification header so they can trust the payload originated from you. If you are deploying this infrastructure on Kubernetes, follow Kubernetes secrets management done right to avoid leaking credentials via environment variables or logs.

How do you choose between SQL and NoSQL for payment ledgers?

When building a payment gateway API from scratch, the choice of database dictates your reliability ceiling. While NoSQL offers horizontal scale, financial ledgers demand strong consistency and complex joins for reconciliation. Here is a practical comparison based on production experience:

CriteriaPostgreSQL (Recommended)MongoDB / DynamoDB
Consistency ModelStrong ACID by default; serializable isolation availableEventual consistency default; read-after-write requires tuning
Transaction SupportMulti-table, multi-row atomic transactions nativeMulti-document transactions supported but performance-heavy
Schema EnforcementStrict typing prevents invalid ledger statesFlexible schema risks inconsistent historical records
ReconciliationComplex aggregations and window functions optimizedAggregation pipeline powerful but less mature for finance
Compliance AuditsWidely accepted by QSA auditors; mature toolingRequires additional justification and controls
Best Use CaseCore ledger, user balances, settlementMetadata, audit logs, session state, analytics

In practice, use PostgreSQL for the ledger and MongoDB for unstructured metadata. If you are new to managing document stores alongside your relational ledger, our MongoDB administration basics guide covers essential operational patterns.

What observability practices are critical for payment APIs?

You cannot fix what you cannot see. When building a payment gateway API from scratch, traditional metrics like CPU and memory are insufficient. You need business-level observability that tracks money movement in real-time.

The Four Golden Signals for Payments

  • Latency: Distinguish between successful and failed request latency. A slow failure is worse than a fast one because it holds resources and frustrates users.
  • Traffic: Monitor transactions per second (TPS) broken down by merchant, payment method, and region. Sudden drops often indicate upstream bank outages, not your code.
  • Errors: Track explicit error codes (e.g., INSUFFICIENT_FUNDS, CARD_DECLINED) separately from system errors (500, TIMEOUT). Alert on system errors; dashboard business errors.
  • Saturation: Measure connection pool usage, queue depth, and HSM throughput. Financial systems fail catastrophically when saturated, not gracefully.

Distributed Tracing for Reconciliation

Every transaction must carry a correlation ID from ingress to bank response. When a merchant claims "I was charged but got no confirmation," you need to trace that specific request across services in seconds. Implement OpenTelemetry early. Retrofitting tracing into a live payment system is painful and error-prone. Structured logging with context fields (merchant_id, txn_id, idempotency_key) is mandatory for forensic analysis.

Payment APIOTel InstrumentedMetrics StorePrometheus / VictoriaMetricsTPS / Latency / Error RateTrace BackendTempo / JaegerEnd-to-End Txn FlowLog AggregatorLoki / ELKAudit & ForensicsGrafana DashboardReal-Time Settlement ViewFraud Alerts & Anomaly DetectionMerchant Success Rate Panels
Unified observability pipeline essential for debugging and compliance when building a payment gateway API from scratch

Next Steps for Your Payment Platform

Building a Payment Gateway API from Scratch is a marathon that demands discipline in data modeling, security, and operational excellence. Start with a robust double-entry ledger and idempotency layer before adding features. Prioritize PCI compliance and observability from day one; retrofitting them later is exponentially harder. Whether you are serving Kathmandu merchants or global SaaS platforms, the principles of atomicity and auditability remain universal. If your team needs architectural review or compliance guidance for your payment infrastructure, contact me to discuss your specific requirements.

Frequently Asked Questions

You need a secure tokenization service, PCI-compliant database schema, idempotent transaction ledger, webhook dispatcher, and fraud detection engine. Most teams use PostgreSQL for ACID compliance and Redis for session management. Never store raw card data directly in your application database under any circumstances.

Achieve PCI DSS Level 1 certification by implementing network segmentation, encrypting cardholder data at rest and in transit, and maintaining strict access controls. Hire a Qualified Security Assessor early. Most startups reduce scope using tokenization providers like VGS or Stripe Elements instead of handling raw PANs directly.

Expect six to twelve months for a production-ready system with full PCI compliance. Core transaction processing takes two to three months, but security audits, bank integrations, and reconciliation tooling consume most time. Rushing compliance testing inevitably causes costly rework during certification phases.

Go or Rust offer optimal performance and memory safety for high-throughput transaction processing. Java remains standard for enterprise banking integrations due to mature libraries. PHP Laravel works for merchant-facing dashboards but avoid it for core payment routing. Prioritize strong typing and comprehensive test coverage over language popularity.

Initial development costs range from $150,000 to $500,000 depending on compliance scope and integration complexity. Annual PCI audit fees start at $30,000. Ongoing infrastructure, security monitoring, and bank certification maintenance add significant operational expenses. Compare this against white-label solutions before committing to custom development.

Implement idempotency keys as unique constraints on transaction tables. Use optimistic locking with version columns for balance updates. Store all state transitions in an immutable ledger table. Never rely solely on application-level checks. Database-level constraints provide the final safety net against race conditions during concurrent requests.

Use exponential backoff with jitter for retries, signing payloads with HMAC-SHA256 for verification. Deliver webhooks asynchronously via message queues like RabbitMQ or SQS. Maintain a delivery log with status tracking. Set reasonable timeouts and circuit breakers to prevent merchant endpoint failures from blocking your entire notification pipeline.

Use TLS 1.3 for all API communications and AES-256-GCM for data at rest. Implement HSM-backed key management for cryptographic operations. Rotate encryption keys quarterly minimum. Never use deprecated algorithms like SHA-1 or DES. Follow NIST SP 800-57 guidelines for key lifecycle management throughout your system.

Contact bank partnership teams directly or work through ISO sponsors. Each bank requires separate certification testing lasting four to eight weeks. Prepare technical documentation, security assessments, and business justification. Start with one processor to validate your architecture before expanding. Bank integrations are contractual relationships, not just technical connections.

Build comprehensive sandbox environments mirroring production bank simulators. Implement contract testing for all external integrations. Run chaos engineering tests against failure scenarios. Maintain separate test card ranges for each processor. Automated regression suites must cover every edge case including timeouts, partial captures, and refund reversals before deployment.

Integrate real-time FX rate feeds from providers like Open Exchange Rates. Lock exchange rates at transaction initiation with configurable validity windows. Store both original and converted amounts with applied rates. Reconcile daily against settlement files. Never calculate conversions client-side. Display clear disclaimers about rate fluctuations to merchants and end users.

Implement velocity checks, BIN validation, AVS matching, and 3D Secure authentication. Use machine learning models trained on historical chargeback data. Integrate third-party services like Sift or Forter for enhanced scoring. Create configurable rule engines allowing merchants to customize thresholds. Monitor false positive rates continuously to balance security with approval rates.

Generate unique idempotency keys per operation and persist them before processing. Return cached responses for duplicate keys within defined TTL windows. Use distributed locks only when necessary. Design all downstream operations to be safely retryable. Document idempotency behavior clearly in API specifications so integrators understand guaranteed semantics.

Track authorization success rates, latency percentiles, error codes by processor, and webhook delivery failures. Alert on approval rate drops exceeding baseline thresholds. Monitor queue depths and database connection pools. Correlate metrics with deployment events. Financial systems require observability beyond standard application monitoring to detect subtle processing anomalies quickly.

Choose white-label if you lack PCI expertise, need market entry under six months, or cannot justify $300K+ investment. Custom builds make sense only for unique processing logic, proprietary risk models, or strategic control requirements. Most businesses overestimate differentiation needs. Validate assumptions with potential customers before committing to ground-up development.