# singhlalit755/distributed-payment-system architecture

> Architecture diagram of the GitHub repository singhlalit755/distributed-payment-system, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/singhlalit755/distributed-payment-system
- Repository: https://github.com/singhlalit755/distributed-payment-system

## Overview

This platform accepts authenticated merchant payment and refund requests, stores workflow state, and dispatches asynchronous work to provider and webhook processing. Separate reconciliation and operations paths compare provider state, expose failures, and support retries; ledger records preserve financial history. The README also describes state-machine enforcement, multiple provider adapters, rate limiting, and observability. Edges use the sampled source or explicit README statements; some asynchronous worker wiring is documented rather than directly visible in the excerpts.

Main entry points: [`apps/api/src/payments/payments.service.ts`](https://github.com/singhlalit755/distributed-payment-system/blob/main/apps/api/src/payments/payments.service.ts) (Payment service), [`apps/webhook-worker/src/webhook.consumer.ts`](https://github.com/singhlalit755/distributed-payment-system/blob/main/apps/webhook-worker/src/webhook.consumer.ts) (Webhook worker), [`libs/messaging/src/kafka.service.ts`](https://github.com/singhlalit755/distributed-payment-system/blob/main/libs/messaging/src/kafka.service.ts) (Kafka messaging), [`libs/database/src/prisma.service.ts`](https://github.com/singhlalit755/distributed-payment-system/blob/main/libs/database/src/prisma.service.ts) (PostgreSQL state)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["API and access"]
  node_api["API application<br/>[app.module.ts]"]
  node_auth["Auth and RBAC"]
  node_payments["Payments API"]
end

subgraph group_processing["Payment processing"]
  node_payment_service["Payment service"]
  node_idempotency["Idempotency"]
  node_provider_adapters["Provider adapters"]
  node_payment_worker["Payment worker"]
  node_webhook_api["Webhook intake"]
  node_webhook_worker["Webhook worker"]
  node_reconciliation["Reconciliation"]
end

subgraph group_state["State and messaging"]
  node_database[("PostgreSQL state<br/>[prisma.service.ts]")]
  node_ledger["Double-entry ledger<br/>[ledger.service.ts]"]
  node_domain["Payment rules<br/>[state-machine.ts]"]
  node_kafka["Kafka messaging<br/>[kafka.service.ts]"]
  node_redis[("Redis controls<br/>[redis.service.ts]")]
end

subgraph group_ops["Operations and telemetry"]
  node_admin["Admin operations"]
  node_metrics["Metrics endpoint"]
  node_observability["Metrics and tracing"]
end

node_clients(("Client apps"))
node_provider["Payment providers"]
node_operators(("Operators"))

node_clients -->|"submit requests"| node_api
node_api -->|"configures"| node_auth
node_api -->|"routes to"| node_payments
node_payments -->|"calls"| node_payment_service
node_payment_service -->|"checks keys"| node_idempotency
node_payment_service -->|"reads and writes"| node_database
node_payment_service -->|"limits and locks"| node_redis
node_payment_service -->|"enforces rules"| node_domain
node_payment_service -->|"posts entries"| node_ledger
node_payment_service -->|"publishes commands"| node_kafka
node_payment_worker -->|"consumes commands"| node_kafka
node_payment_worker -->|"invokes"| node_provider_adapters
node_provider_adapters -.->|"charges and queries"| node_provider
node_webhook_api -->|"verifies signature"| node_provider_adapters
node_webhook_api -->|"stores event"| node_database
node_webhook_api -->|"publishes event"| node_kafka
node_webhook_worker -->|"consumes events"| node_kafka
node_webhook_worker -->|"updates payment"| node_database
node_webhook_worker -->|"checks transition"| node_domain
node_webhook_worker -->|"posts capture"| node_ledger
node_reconciliation -->|"consumes and publishes"| node_kafka
node_reconciliation -->|"compares and records"| node_database
node_reconciliation -->|"queries provider"| node_provider_adapters
node_operators -->|"inspect and retry"| node_admin
node_admin -->|"reads and audits"| node_database
node_admin -->|"requeues payments"| node_kafka
node_api -->|"exposes"| node_metrics
node_metrics -->|"reads registry"| node_observability
node_payment_worker -.->|"records telemetry"| node_observability
node_reconciliation -->|"records metrics"| node_observability
node_webhook_worker -->|"records duration"| node_observability

click node_api "https://github.com/singhlalit755/distributed-payment-system/blob/main/apps/api/src/app.module.ts"
click node_auth "https://github.com/singhlalit755/distributed-payment-system/tree/main/apps/api/src/auth"
click node_payments "https://github.com/singhlalit755/distributed-payment-system/blob/main/apps/api/src/payments/payments.controller.ts"
click node_payment_service "https://github.com/singhlalit755/distributed-payment-system/blob/main/apps/api/src/payments/payments.service.ts"
click node_idempotency "https://github.com/singhlalit755/distributed-payment-system/blob/main/apps/api/src/payments/idempotency.service.ts"
click node_provider_adapters "https://github.com/singhlalit755/distributed-payment-system/tree/main/libs/providers/src"
click node_payment_worker "https://github.com/singhlalit755/distributed-payment-system/blob/main/apps/payment-worker/src/payment.processor.ts"
click node_webhook_api "https://github.com/singhlalit755/distributed-payment-system/blob/main/apps/api/src/webhooks/webhooks.controller.ts"
click node_webhook_worker "https://github.com/singhlalit755/distributed-payment-system/blob/main/apps/webhook-worker/src/webhook.consumer.ts"
click node_reconciliation "https://github.com/singhlalit755/distributed-payment-system/blob/main/apps/reconciliation-worker/src/reconciliation.service.ts"
click node_database "https://github.com/singhlalit755/distributed-payment-system/blob/main/libs/database/src/prisma.service.ts"
click node_ledger "https://github.com/singhlalit755/distributed-payment-system/blob/main/libs/database/src/ledger.service.ts"
click node_domain "https://github.com/singhlalit755/distributed-payment-system/blob/main/libs/domain/src/state-machine.ts"
click node_kafka "https://github.com/singhlalit755/distributed-payment-system/blob/main/libs/messaging/src/kafka.service.ts"
click node_redis "https://github.com/singhlalit755/distributed-payment-system/blob/main/libs/messaging/src/redis.service.ts"
click node_admin "https://github.com/singhlalit755/distributed-payment-system/blob/main/apps/api/src/admin/admin.controller.ts"
click node_metrics "https://github.com/singhlalit755/distributed-payment-system/blob/main/apps/api/src/metrics.controller.ts"
click node_observability "https://github.com/singhlalit755/distributed-payment-system/tree/main/libs/observability/src"

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_api,node_auth,node_payments,node_clients toneBlue
class node_payment_service,node_idempotency,node_provider_adapters,node_payment_worker,node_webhook_api,node_webhook_worker,node_reconciliation toneAmber
class node_database,node_ledger,node_domain,node_kafka,node_redis toneMint
class node_admin,node_metrics,node_observability toneRose
class node_provider,node_operators toneIndigo
```

## Components

### API and access

- [API application](https://github.com/singhlalit755/distributed-payment-system/blob/main/apps/api/src/app.module.ts): `apps/api/src/app.module.ts`
- [Auth and RBAC](https://github.com/singhlalit755/distributed-payment-system/tree/main/apps/api/src/auth): `apps/api/src/auth`
- [Payments API](https://github.com/singhlalit755/distributed-payment-system/blob/main/apps/api/src/payments/payments.controller.ts): `apps/api/src/payments/payments.controller.ts`

### Payment processing

- [Payment service](https://github.com/singhlalit755/distributed-payment-system/blob/main/apps/api/src/payments/payments.service.ts): `apps/api/src/payments/payments.service.ts`
- [Idempotency](https://github.com/singhlalit755/distributed-payment-system/blob/main/apps/api/src/payments/idempotency.service.ts): `apps/api/src/payments/idempotency.service.ts`
- [Provider adapters](https://github.com/singhlalit755/distributed-payment-system/tree/main/libs/providers/src): `libs/providers/src`
- [Payment worker](https://github.com/singhlalit755/distributed-payment-system/blob/main/apps/payment-worker/src/payment.processor.ts): `apps/payment-worker/src/payment.processor.ts`
- [Webhook intake](https://github.com/singhlalit755/distributed-payment-system/blob/main/apps/api/src/webhooks/webhooks.controller.ts): `apps/api/src/webhooks/webhooks.controller.ts`
- [Webhook worker](https://github.com/singhlalit755/distributed-payment-system/blob/main/apps/webhook-worker/src/webhook.consumer.ts): `apps/webhook-worker/src/webhook.consumer.ts`
- [Reconciliation](https://github.com/singhlalit755/distributed-payment-system/blob/main/apps/reconciliation-worker/src/reconciliation.service.ts): `apps/reconciliation-worker/src/reconciliation.service.ts`

### State and messaging

- [PostgreSQL state](https://github.com/singhlalit755/distributed-payment-system/blob/main/libs/database/src/prisma.service.ts): `libs/database/src/prisma.service.ts`
- [Double-entry ledger](https://github.com/singhlalit755/distributed-payment-system/blob/main/libs/database/src/ledger.service.ts): `libs/database/src/ledger.service.ts`
- [Payment rules](https://github.com/singhlalit755/distributed-payment-system/blob/main/libs/domain/src/state-machine.ts): `libs/domain/src/state-machine.ts`
- [Kafka messaging](https://github.com/singhlalit755/distributed-payment-system/blob/main/libs/messaging/src/kafka.service.ts): `libs/messaging/src/kafka.service.ts`
- [Redis controls](https://github.com/singhlalit755/distributed-payment-system/blob/main/libs/messaging/src/redis.service.ts): `libs/messaging/src/redis.service.ts`

### Operations and telemetry

- [Admin operations](https://github.com/singhlalit755/distributed-payment-system/blob/main/apps/api/src/admin/admin.controller.ts): `apps/api/src/admin/admin.controller.ts`
- [Metrics endpoint](https://github.com/singhlalit755/distributed-payment-system/blob/main/apps/api/src/metrics.controller.ts): `apps/api/src/metrics.controller.ts`
- [Metrics and tracing](https://github.com/singhlalit755/distributed-payment-system/tree/main/libs/observability/src): `libs/observability/src`

### Other components

- **Client apps**
- **Payment providers**
- **Operators**

## Connections

- Client apps → API application: submit requests
- API application → Auth and RBAC: configures (evidence: `apps/api/src/app.module.ts`)
- API application → Payments API: routes to (evidence: `apps/api/src/app.module.ts`)
- Payments API → Payment service: calls (evidence: `apps/api/src/payments/payments.controller.ts`)
- Payment service → Idempotency: checks keys (evidence: `apps/api/src/payments/payments.service.ts`)
- Payment service → PostgreSQL state: reads and writes (evidence: `apps/api/src/payments/payments.service.ts`)
- Payment service → Redis controls: limits and locks (evidence: `apps/api/src/payments/payments.service.ts`)
- Payment service → Payment rules: enforces rules (evidence: `apps/api/src/payments/payments.service.ts`)
- Payment service → Double-entry ledger: posts entries (evidence: `apps/api/src/payments/payments.service.ts`)
- Payment service → Kafka messaging: publishes commands (evidence: `apps/api/src/payments/payments.service.ts`)
- Payment worker → Kafka messaging: consumes commands
- Payment worker → Provider adapters: invokes
- Provider adapters → Payment providers: charges and queries
- Webhook intake → Provider adapters: verifies signature (evidence: `apps/api/src/webhooks/webhooks.controller.ts`)
- Webhook intake → PostgreSQL state: stores event (evidence: `apps/api/src/webhooks/webhooks.controller.ts`)
- Webhook intake → Kafka messaging: publishes event (evidence: `apps/api/src/webhooks/webhooks.controller.ts`)
- Webhook worker → Kafka messaging: consumes events (evidence: `apps/webhook-worker/src/webhook.consumer.ts`)
- Webhook worker → PostgreSQL state: updates payment (evidence: `apps/webhook-worker/src/webhook.consumer.ts`)
- Webhook worker → Payment rules: checks transition (evidence: `apps/webhook-worker/src/webhook.consumer.ts`)
- Webhook worker → Double-entry ledger: posts capture (evidence: `apps/webhook-worker/src/webhook.consumer.ts`)
- Reconciliation → Kafka messaging: consumes and publishes (evidence: `apps/reconciliation-worker/src/reconciliation.service.ts`)
- Reconciliation → PostgreSQL state: compares and records (evidence: `apps/reconciliation-worker/src/reconciliation.service.ts`)
- Reconciliation → Provider adapters: queries provider (evidence: `apps/reconciliation-worker/src/reconciliation.service.ts`)
- Operators → Admin operations: inspect and retry
- Admin operations → PostgreSQL state: reads and audits (evidence: `apps/api/src/admin/admin.controller.ts`)
- Admin operations → Kafka messaging: requeues payments (evidence: `apps/api/src/admin/admin.controller.ts`)
- API application → Metrics endpoint: exposes (evidence: `apps/api/src/app.module.ts`)
- Metrics endpoint → Metrics and tracing: reads registry (evidence: `apps/api/src/metrics.controller.ts`)
- Payment worker → Metrics and tracing: records telemetry
- Reconciliation → Metrics and tracing: records metrics (evidence: `apps/reconciliation-worker/src/reconciliation.service.ts`)
- Webhook worker → Metrics and tracing: records duration (evidence: `apps/webhook-worker/src/webhook.consumer.ts`)
