How achyuthnamburi/paycore works
Purpose and Entry Points
PayCore is a payment-processing backend with guarantees for request idempotency, double-entry balance accounting, and durable ledger writes. The Python API starts at app/main.py; the durable ledger daemon starts at engine/src/main.cpp. A separate Vite frontend is rooted at frontend/src/main.jsx and calls the API through frontend/src/api/client.js.
API and Application Boundary
FastAPI route modules expose account, transaction, ledger-consistency, and health operations:
• app/api/transactions.py
• app/api/accounts.py
• app/api/ledger.py
Routes use Pydantic request/response schemas and service-layer logic rather than directly owning payment rules. Infrastructure clients centralize PostgreSQL, Redis, and C++ engine connectivity:
• app/core/database.py
• app/core/redis_client.py
• app/core/engine_client.py
Transaction Orchestration
The transaction endpoint is the primary control flow. It first checks a Redis idempotency key, validates account state and funds, creates a pending transaction, then drives permitted status changes through authorization and settlement.
• app/services/idempotency.py
• app/services/authorization.py
• app/services/state_machine.py
The state-machine boundary prevents skipped or reversed lifecycle transitions. The intended path is PENDING → AUTHORIZED → CAPTURED → SETTLED; failures and reversals are controlled alternatives.
Relational Ledger and System of Record
PostgreSQL, accessed asynchronously through SQLAlchemy, persists accounts, transactions, and immutable debit/credit ledger entries:
• app/models/account.py
• app/models/transaction.py
• app/models/ledger_entry.py
app/services/ledger_service.py coordinates creation of paired ledger entries and database balance updates atomically. Alembic establishes the persisted schema through app/migrations/versions/0001_initial.py.
Durable C++ Ledger Engine
The Python service sends ledger writes across a Unix-socket client boundary to the C++17 engine. The engine parses commands, appends and fsyncs the WAL before acknowledgement, then updates its concurrent in-memory ledger store:
• engine/src/server.cpp
• engine/src/wal.cpp
• engine/src/ledger.cpp
On process startup, WAL replay restores engine state. The ledger API compares engine and database balances, making divergence a visible operational concern.
Asynchronous Post-Settlement Work
Once settlement completes, Celery workers receive non-blocking fraud-scoring and webhook-delivery jobs using Redis as task broker/cache:
• app/tasks/celery_app.py
• app/tasks/fraud_task.py
• app/tasks/webhook_task.py
Fraud processing may score/account-suspend after settlement; webhook delivery is intentionally outside the synchronous payment response path and retries failures.
Runtime and Deployment Topology
Docker Compose is the primary local/deployed integration unit:
• docker-compose.yml
• app/Dockerfile
• engine/Dockerfile
Material services are Python 3.11/FastAPI, C++17/POSIX sockets, PostgreSQL 15, Redis 7, Celery, and scikit-learn fraud detection. The frontend is a separately built React/Vite static application, commonly served via its Nginx container:
• frontend/Dockerfile
• frontend/nginx.conf
• frontend/src/App.jsx
Main entry points: transactions.py Payment routes · celery_app.py Celery workers · state_machine.py Transaction state machine · ledger_service.py Ledger service
Components 19
Frontend
- React/Vite UI
frontend/src/main.jsx - API client
frontend/src/api/client.js
Python API
- FastAPI application
app/main.py - Payment routes · Accounts, transactions, ledger consistency, and health endpoints.
app/api/transactions.py - Idempotency service
app/services/idempotency.py - Authorization service · Validates account state and available funds.
app/services/authorization.py - Transaction state machine · Controls PENDING → AUTHORIZED → CAPTURED → SETTLED transitions.
app/services/state_machine.py - Ledger service · Atomically creates paired entries and updates database balances.
app/services/ledger_service.py - Async database client
app/core/database.py - Engine socket client
app/core/engine_client.py
Durable Ledger Engine
- Ledger daemon
engine/src/main.cpp - Command server
engine/src/server.cpp - Write-ahead log · Appends and fsyncs each write before acknowledgement.
engine/src/wal.cpp - In-memory ledger · Restored from WAL replay at startup.
engine/src/ledger.cpp
Post-settlement Workers
- Celery workers
app/tasks/celery_app.py - Fraud scoring · May suspend an account after settlement.
app/tasks/fraud_task.py - Webhook delivery · Retries delivery outside the synchronous payment response.
app/tasks/webhook_task.py
Other components
- PostgreSQL · Persists accounts, transactions, and immutable debit/credit entries.
- Redis · Stores idempotency keys and brokers Celery work.
app/core/redis_client.py
Connections 21
- React/Vite UI → API client: uses
- API client → FastAPI application: HTTP requests
- FastAPI application → Payment routes: mounts
- Payment routes → Idempotency service: checks request key
- Idempotency service → Redis: reads/writes keys
- Payment routes → Authorization service: validates payment
- Authorization service → Transaction state machine: authorizes transition
- Transaction state machine → Ledger service: settles payment
- Ledger service → Async database client: atomic database write
- Async database client → PostgreSQL: persists ledger
- Ledger service → Engine socket client: sends durable write
- Engine socket client → Ledger daemon: Unix socket
- Ledger daemon → Command server: runs
- Command server → Write-ahead log: append then fsync
- Write-ahead log → In-memory ledger: replay and apply
- Command server → In-memory ledger: applies acknowledged write
- Transaction state machine → Celery workers: after settlement
- Celery workers → Redis: task broker
- Celery workers → Fraud scoring: executes
- Celery workers → Webhook delivery: executes
- Payment routes → In-memory ledger: compares balances · Ledger endpoint exposes engine/database divergence.