Phases 1–4 & 6 — Live

Payment orchestration,
done right

Idempotent API, double-entry ledger, saga orchestrator with Kafka, HMAC-signed webhooks, settlement reconciliation, ML fraud scoring, and an LLM ops assistant — the full machinery underneath a payment gateway.

Journal Entry
DEBIT
₹1,500.00
CREDIT
₹1,500.00
Design principles

Three decisions drive everything

Each is a deliberate correctness choice — the kind payment teams actually screen for.

Integer money

All amounts are integer minor units (paise / cents) stored as BIGINT. 0.1 + 0.2 ≠ 0.3 has no place in a ledger. No floating-point, no rounding errors, no surprises.

Derived balances

An account's balance is always computed by summing its ledger lines — never stored. A stored balance can drift; a derived one is correct by construction. Every write posts a balanced journal entry.

Idempotent writes

Networks retry. The same Idempotency-Key with the same body replays the original response — charged once. The unique constraint in PostgreSQL is the source of truth, not application memory.


Architecture

How a payment moves

A capture posts one balanced journal entry. A refund posts the exact reverse. Because both sides always move together, the books are always balanced.

Client
POST /v1/payments
PaymentController
API layer
PaymentService
Orchestrator
Ledger core
LedgerService
Posts entries
IdempotencyService
Dedup guard
PostgreSQL

Capture entry

Account Type Direction Amount Meaning
ACQUIRER_CASH Asset DEBIT 150000 Cash received from acquirer
MERCHANT_PAYABLE Liability CREDIT 150000 Amount owed to merchant

API Reference

Fourteen endpoints, zero ambiguity

Every write requires an Idempotency-Key header. Retries are safe by design.

Payments & Ledger

POST /v1/payments Create + capture. Requires Idempotency-Key.
GET /v1/payments/{id} Fetch a payment by ID.
POST /v1/payments/{id}/refund Refund a captured payment.
GET /v1/accounts/{code}/balance Live, derived account balance.

Orchestration & Risk

POST /v1/orchestrated-payments Saga-based authorize → risk-check → capture flow.
POST /v1/risk/analyze/{paymentId} ML fraud score with SHAP explanations.
GET /v1/risk/explanation/{paymentId} Human-readable risk narrative.

Webhooks

POST /v1/webhooks Register a webhook subscription.
GET /v1/webhooks List all subscriptions.
DEL /v1/webhooks/{id} Deactivate a subscription.

Reconciliation

POST /v1/reconciliation/upload Upload a settlement CSV and reconcile.
GET /v1/reconciliation/runs/{runId} Fetch reconciliation results.
↻
Same key + same body

Replays the original response. No new payment created.

✕
Same key + different body

Returns 409 Conflict. Prevents accidental mutations.

!
Missing key

Returns 400 Bad Request. Every write must be idempotent.


Quickstart

Run it locally

One command to start, one curl to test. Docker Compose spins up PostgreSQL and the application together.

terminal
# Start Postgres + the app
docker compose up --build

# Create a payment (idempotent)
curl -s -X POST http://localhost:8080/v1/payments \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: demo-001" \
  -d '{"merchantReference":"order-42","amountMinor":150000,"currency":"INR"}'

# Retry the SAME request — same key → same result, charged once
curl -s -X POST http://localhost:8080/v1/payments \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: demo-001" \
  -d '{"merchantReference":"order-42","amountMinor":150000,"currency":"INR"}'

# Verify the ledger is balanced
curl -s http://localhost:8080/v1/accounts/ACQUIRER_CASH/balance
curl -s http://localhost:8080/v1/accounts/MERCHANT_PAYABLE/balance
# Both return 150000 ✓

Tech stack

Built on proven infrastructure

21
Java 21Records, sealed types, virtual threads
SB
Spring Boot 3.3Web, Data JPA, Validation, Actuator
PG
PostgreSQL 16CHECK constraints, unique keys, FKs
FW
FlywayVersioned migrations, validate mode
TC
TestcontainersIntegration tests on real Postgres
OA
springdoc / SwaggerLive API documentation
🐳
Docker ComposeOne-command local environment
OL
Optimistic Locking@Version guards on state transitions
KF
Apache KafkaEvent bus with transactional outbox
WH
HMAC WebhooksSHA-256 signed, exponential backoff
XG
XGBoostML fraud scoring with SHAP explanations

Roadmap

The wider platform

Five of six phases are live. Each phase adds a production concern.

1

Financial Core

Idempotent payment API, double-entry ledger, integer money, derived balances, Flyway migrations, Testcontainers integration tests.

Live
2

Orchestrator

Explicit state machine (INITIATED → AUTHORIZED → CAPTURED → SETTLED) with the saga pattern and compensating transactions over a Kafka event bus.

Live
3

Webhooks + Reconciliation

HMAC-SHA256 signed outbound events with exponential-backoff retries and dead-letter. Settlement CSV reconciliation with auto-settle on match.

Live
4

AI Fraud Scoring

XGBoost model with SHAP explanations in the decline response — every "declined: fraud" says why. Dockerized Python scoring service.

Live
5

Smart Payment Routing

Predict success probability per acquiring route and route to maximise expected success at minimum cost.

Planned
6

LLM Ops Assistant

Natural-language queries over payment and risk data, powered by Anthropic Claude with template-based fallback for local dev.

Live