# achyuthnamburi/paycore architecture

> Architecture diagram of the GitHub repository achyuthnamburi/paycore, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/achyuthnamburi/paycore
- Repository: https://github.com/achyuthnamburi/paycore

## Overview

#### 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 `fsync`s 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: [`app/api/transactions.py`](https://github.com/achyuthnamburi/paycore/blob/main/app/api/transactions.py) (Payment routes), [`app/tasks/celery_app.py`](https://github.com/achyuthnamburi/paycore/blob/main/app/tasks/celery_app.py) (Celery workers), [`app/services/state_machine.py`](https://github.com/achyuthnamburi/paycore/blob/main/app/services/state_machine.py) (Transaction state machine), [`app/services/ledger_service.py`](https://github.com/achyuthnamburi/paycore/blob/main/app/services/ledger_service.py) (Ledger service)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Frontend"]
  node_web["React/Vite UI<br/>frontend<br/>[main.jsx]"]
  node_api_client["API client<br/>frontend client<br/>[client.js]"]
end

subgraph group_api["Python API"]
  node_fastapi["FastAPI application<br/>API runtime<br/>[main.py]"]
  node_routes["Payment routes<br/>route modules<br/>[transactions.py]"]
  node_idempotency["Idempotency service<br/>[idempotency.py]"]
  node_authorization["Authorization service<br/>[authorization.py]"]
  node_state_machine["Transaction state machine<br/>lifecycle control<br/>[state_machine.py]"]
  node_ledger_service["Ledger service<br/>accounting service<br/>[ledger_service.py]"]
  node_db_client["Async database client<br/>infrastructure client<br/>[database.py]"]
  node_engine_client["Engine socket client<br/>infrastructure client<br/>[engine_client.py]"]
end

subgraph group_engine["Durable Ledger Engine"]
  node_engine_daemon["Ledger daemon<br/>C++ runtime<br/>[main.cpp]"]
  node_engine_server["Command server<br/>Unix-socket server<br/>[server.cpp]"]
  node_wal["Write-ahead log<br/>durable log<br/>[wal.cpp]"]
  node_engine_ledger["In-memory ledger<br/>concurrent ledger store<br/>[ledger.cpp]"]
end

subgraph group_async["Post-settlement Workers"]
  node_celery["Celery workers<br/>async worker runtime<br/>[celery_app.py]"]
  node_fraud["Fraud scoring<br/>background task<br/>[fraud_task.py]"]
  node_webhooks["Webhook delivery<br/>background task<br/>[webhook_task.py]"]
end

node_postgres[("PostgreSQL<br/>system of record")]
node_redis[("Redis<br/>cache and broker<br/>[redis_client.py]")]

node_web -->|"uses"| node_api_client
node_api_client -->|"HTTP requests"| node_fastapi
node_fastapi -->|"mounts"| node_routes
node_routes -->|"checks request key"| node_idempotency
node_idempotency -->|"reads/writes keys"| node_redis
node_routes -->|"validates payment"| node_authorization
node_authorization -->|"authorizes transition"| node_state_machine
node_state_machine -->|"settles payment"| node_ledger_service
node_ledger_service -->|"atomic database write"| node_db_client
node_db_client -->|"persists ledger"| node_postgres
node_ledger_service -->|"sends durable write"| node_engine_client
node_engine_client -->|"Unix socket"| node_engine_daemon
node_engine_daemon -->|"runs"| node_engine_server
node_engine_server -->|"append then fsync"| node_wal
node_wal -->|"replay and apply"| node_engine_ledger
node_engine_server -->|"applies acknowledged write"| node_engine_ledger
node_state_machine -.->|"after settlement"| node_celery
node_celery -->|"task broker"| node_redis
node_celery -->|"executes"| node_fraud
node_celery -->|"executes"| node_webhooks
node_routes -.->|"compares balances"| node_engine_ledger

click node_web "https://github.com/achyuthnamburi/paycore/blob/main/frontend/src/main.jsx"
click node_api_client "https://github.com/achyuthnamburi/paycore/blob/main/frontend/src/api/client.js"
click node_fastapi "https://github.com/achyuthnamburi/paycore/blob/main/app/main.py"
click node_routes "https://github.com/achyuthnamburi/paycore/blob/main/app/api/transactions.py"
click node_idempotency "https://github.com/achyuthnamburi/paycore/blob/main/app/services/idempotency.py"
click node_authorization "https://github.com/achyuthnamburi/paycore/blob/main/app/services/authorization.py"
click node_state_machine "https://github.com/achyuthnamburi/paycore/blob/main/app/services/state_machine.py"
click node_ledger_service "https://github.com/achyuthnamburi/paycore/blob/main/app/services/ledger_service.py"
click node_db_client "https://github.com/achyuthnamburi/paycore/blob/main/app/core/database.py"
click node_engine_client "https://github.com/achyuthnamburi/paycore/blob/main/app/core/engine_client.py"
click node_redis "https://github.com/achyuthnamburi/paycore/blob/main/app/core/redis_client.py"
click node_engine_daemon "https://github.com/achyuthnamburi/paycore/blob/main/engine/src/main.cpp"
click node_engine_server "https://github.com/achyuthnamburi/paycore/blob/main/engine/src/server.cpp"
click node_wal "https://github.com/achyuthnamburi/paycore/blob/main/engine/src/wal.cpp"
click node_engine_ledger "https://github.com/achyuthnamburi/paycore/blob/main/engine/src/ledger.cpp"
click node_celery "https://github.com/achyuthnamburi/paycore/blob/main/app/tasks/celery_app.py"
click node_fraud "https://github.com/achyuthnamburi/paycore/blob/main/app/tasks/fraud_task.py"
click node_webhooks "https://github.com/achyuthnamburi/paycore/blob/main/app/tasks/webhook_task.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_web,node_api_client toneBlue
class node_fastapi,node_routes,node_idempotency,node_authorization,node_state_machine,node_ledger_service,node_db_client,node_engine_client toneAmber
class node_engine_daemon,node_engine_server,node_wal,node_engine_ledger toneMint
class node_celery,node_fraud,node_webhooks toneRose
class node_postgres,node_redis toneNeutral
```

## Components

### Frontend

- [React/Vite UI](https://github.com/achyuthnamburi/paycore/blob/main/frontend/src/main.jsx): frontend · `frontend/src/main.jsx`
- [API client](https://github.com/achyuthnamburi/paycore/blob/main/frontend/src/api/client.js): frontend client · `frontend/src/api/client.js`

### Python API

- [FastAPI application](https://github.com/achyuthnamburi/paycore/blob/main/app/main.py): API runtime · `app/main.py`
- [Payment routes](https://github.com/achyuthnamburi/paycore/blob/main/app/api/transactions.py): route modules · Accounts, transactions, ledger consistency, and health endpoints. · `app/api/transactions.py`
- [Idempotency service](https://github.com/achyuthnamburi/paycore/blob/main/app/services/idempotency.py): service · `app/services/idempotency.py`
- [Authorization service](https://github.com/achyuthnamburi/paycore/blob/main/app/services/authorization.py): service · Validates account state and available funds. · `app/services/authorization.py`
- [Transaction state machine](https://github.com/achyuthnamburi/paycore/blob/main/app/services/state_machine.py): lifecycle control · Controls PENDING → AUTHORIZED → CAPTURED → SETTLED transitions. · `app/services/state_machine.py`
- [Ledger service](https://github.com/achyuthnamburi/paycore/blob/main/app/services/ledger_service.py): accounting service · Atomically creates paired entries and updates database balances. · `app/services/ledger_service.py`
- [Async database client](https://github.com/achyuthnamburi/paycore/blob/main/app/core/database.py): infrastructure client · `app/core/database.py`
- [Engine socket client](https://github.com/achyuthnamburi/paycore/blob/main/app/core/engine_client.py): infrastructure client · `app/core/engine_client.py`

### Durable Ledger Engine

- [Ledger daemon](https://github.com/achyuthnamburi/paycore/blob/main/engine/src/main.cpp): C++ runtime · `engine/src/main.cpp`
- [Command server](https://github.com/achyuthnamburi/paycore/blob/main/engine/src/server.cpp): Unix-socket server · `engine/src/server.cpp`
- [Write-ahead log](https://github.com/achyuthnamburi/paycore/blob/main/engine/src/wal.cpp): durable log · Appends and fsyncs each write before acknowledgement. · `engine/src/wal.cpp`
- [In-memory ledger](https://github.com/achyuthnamburi/paycore/blob/main/engine/src/ledger.cpp): concurrent ledger store · Restored from WAL replay at startup. · `engine/src/ledger.cpp`

### Post-settlement Workers

- [Celery workers](https://github.com/achyuthnamburi/paycore/blob/main/app/tasks/celery_app.py): async worker runtime · `app/tasks/celery_app.py`
- [Fraud scoring](https://github.com/achyuthnamburi/paycore/blob/main/app/tasks/fraud_task.py): background task · May suspend an account after settlement. · `app/tasks/fraud_task.py`
- [Webhook delivery](https://github.com/achyuthnamburi/paycore/blob/main/app/tasks/webhook_task.py): background task · Retries delivery outside the synchronous payment response. · `app/tasks/webhook_task.py`

### Other components

- **PostgreSQL**: system of record · Persists accounts, transactions, and immutable debit/credit entries.
- [Redis](https://github.com/achyuthnamburi/paycore/blob/main/app/core/redis_client.py): cache and broker · Stores idempotency keys and brokers Celery work. · `app/core/redis_client.py`

## Connections

- 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.
