PayFlow is a production-grade, portfolio-quality backend system built from scratch using .NET 10, ASP.NET Core Web API, C#, Entity Framework Core 10, PostgreSQL, Redis, RabbitMQ, and Docker.
It demonstrates clean modular architecture, financial transfer consistency, double-entry accounting ledgering, database-level idempotency protection, outbox/inbox message processing, JWT refresh token rotation, and OpenTelemetry observability.
- Double-Entry Accounting Ledger:
- Every financial transaction (Deposit, Withdrawal, Transfer) creates balanced
DebitandCreditledger entries. - Utilizes a dedicated System Treasury Clearing Account so that
Total Debit == Total Creditacross all system transactions.
- Every financial transaction (Deposit, Withdrawal, Transfer) creates balanced
- Safe Money Transfers & Concurrency:
- Transfers acquire wallet row locks in deterministic ID order (
min(SenderId, ReceiverId)thenmax(SenderId, ReceiverId)) using PostgreSQLSELECT ... FOR UPDATErow locks to prevent lock-order inversion and reduce deadlock risk. - Zero double-spending and non-negative wallet balance policy enforced via PostgreSQL
CHECK ("Balance" >= 0)database constraints.
- Transfers acquire wallet row locks in deterministic ID order (
- Database-Level Idempotency:
- Mandatory
Idempotency-KeyHTTP header for transfers. - Composite unique database constraint
(UserId, Key)preventing duplicate money movement across concurrent requests or retries.
- Mandatory
- Transactional Outbox & Durable Inbox Processing:
OutboxMessageinserted atomically within the financial database transaction.OutboxBackgroundWorkerdispatches events asynchronously to RabbitMQ.NotificationConsumerutilizes a durableInboxMessagetable in PostgreSQL for idempotent consumer processing across app restarts.
- Authentication & Security:
- JWT Bearer Authentication with short-lived access tokens (15 mins).
- Cryptographically secure Refresh Tokens stored as SHA-256 hashes in database.
- Refresh Token Rotation, old token revocation, and reuse rejection.
- BCrypt password hashing (Work Factor 11).
- Rate Limiting on authentication endpoints (
FixedWindowpolicy).
- Observability & Health Checks:
- Centralized RFC 7807
ProblemDetailserror handling middleware. - Serilog structured logging with request correlation IDs (
X-Correlation-ID). - OpenTelemetry tracing and metrics for ASP.NET Core, HttpClient, and EF Core.
- Health check endpoints (
/health,/readiness,/liveness).
- Centralized RFC 7807
src/
├── PayFlow.Domain/ # Pure Domain Entities, System Constants, Enums, Rules & Exceptions
├── PayFlow.Application/ # Use Cases, DTOs, FluentValidators, Service Interfaces
├── PayFlow.Infrastructure/ # EF Core DbContext, PostgreSQL Configurations, Redis, RabbitMQ, Outbox/Inbox Workers
└── PayFlow.Api/ # ASP.NET Core Controllers, Middleware, JWT Setup, OpenTelemetry, Swagger
tests/
├── PayFlow.UnitTests/ # Domain Invariants, Ledger Balance Checks, AuthService & TransferService Unit Tests
└── PayFlow.IntegrationTests/# Integration Tests (Testcontainers PostgreSQL, Concurrency, Idempotency, Deadlock Tests)
docs/
└── adr/ # Architecture Decision Records (ADR-001 to ADR-004)
graph TD
Client[HTTP Client / Mobile App / API Client] -->|HTTPS REST| API[PayFlow.Api]
subgraph Core Application
API -->|Dependency Injection| App[PayFlow.Application]
App -->|Pure Entities & Rules| Domain[PayFlow.Domain]
end
subgraph Infrastructure Layer
API -->|Infrastructure Services| Infra[PayFlow.Infrastructure]
Infra -->|EF Core Npgsql| Postgres[(PostgreSQL 16\nSource of Truth)]
Infra -->|Cache Reads| Redis[(Redis Cache)]
Infra -->|Outbox Background Worker| RabbitMQ[RabbitMQ Broker]
end
subgraph Asynchronous Event Processing
RabbitMQ -->|Deliver Event| Consumer[NotificationConsumer Worker]
Consumer -->|Durable Inbox Check| Postgres
Consumer -->|Log Notification| AuditLog[Structured Logs]
end
sequenceDiagram
autonumber
actor Client
participant API as PayFlow.Api
participant DB as PostgreSQL DB
participant Redis as Redis Cache
participant Outbox as Outbox Worker
participant Rabbit as RabbitMQ
participant Consumer as Notification Consumer
Client->>API: POST /api/transfers (Idempotency-Key: XYZ, Receiver, Amount)
API->>DB: Check Idempotency Record (UserId, Key)
alt Key Exists
DB-->>API: Return Cached Response Payload
API-->>Client: 200 OK (Original Result)
else Key Is New
API->>DB: Begin DB Transaction
API->>DB: Lock Wallets in Deterministic Order (SELECT FOR UPDATE)
API->>DB: Deduct Sender Balance & Credit Receiver Balance
API->>DB: Insert Transaction & Balanced Double-Entry Ledger (Debit/Credit)
API->>DB: Insert Outbox Message (TransferCompleted)
API->>DB: Insert Idempotency Record (UserId, Key)
API->>DB: Commit DB Transaction
API->>Redis: Invalidate Cached Sender & Receiver Wallets
API-->>Client: 200 OK (TransactionResponse)
loop Every 5 Seconds
Outbox->>DB: Query Pending Outbox Messages
Outbox->>Rabbit: Publish TransferCompleted Event
Outbox->>DB: Mark Message ProcessedAtUtc
end
Rabbit->>Consumer: Deliver Event (transfer.completed)
Consumer->>DB: Check & Record Inbox Message (MessageId)
Consumer->>Consumer: Process Notification Log
end
- How is Double-Spending Prevented?
- Money transfers acquire wallet row locks in deterministic ascending ID order (
min(A,B)thenmax(A,B)) using PostgreSQLSELECT ... FOR UPDATErow locks to prevent lock-order inversion and reduce deadlock risk. Additionally, PostgreSQLCHECK ("Balance" >= 0)constraints enforce balance non-negativity at the database engine level.
- Money transfers acquire wallet row locks in deterministic ascending ID order (
- How does Database-Level Idempotency Work?
- PayFlow enforces idempotency via a composite unique database index
IX_IdempotencyRecords_UserId_Key. If two duplicate requests hit the server concurrently, PostgreSQL throws a unique constraint violation (DbUpdateException) on the second thread, causing it to catch the exception and return the original transaction response cleanly.
- PayFlow enforces idempotency via a composite unique database index
- Why use the Outbox & Inbox Patterns?
- Publishing events directly to RabbitMQ inside HTTP controllers causes dual-write inconsistencies if network drops occur before DB commit. PayFlow persists the
OutboxMessagein the same PostgreSQL transaction. A background worker dispatches outbox events asynchronously, and the consumer recordsInboxMessageentries for durable inbox-based duplicate processing protection.
- Publishing events directly to RabbitMQ inside HTTP controllers causes dual-write inconsistencies if network drops occur before DB commit. PayFlow persists the
- How is Double-Entry Accounting Enforced?
- Single-sided ledger entries violate accounting invariants. PayFlow introduces a System Treasury Clearing Account (
SystemConstants.SystemWalletId). Deposits debit System Treasury and credit User Wallet; withdrawals debit User Wallet and credit System Treasury; transfers debit Sender and credit Receiver. Every transaction produces balanced entries whereSUM(Debit) == SUM(Credit).
- Single-sided ledger entries violate accounting invariants. PayFlow introduces a System Treasury Clearing Account (
- How is Refresh Token Security Handled?
- Refresh tokens are stored only as SHA-256 hashes. Upon refresh, the used token is marked revoked. Attempting to reuse an already revoked refresh token results in immediate rejection (
UnauthorizedDomainException).
- Refresh tokens are stored only as SHA-256 hashes. Upon refresh, the used token is marked revoked. Attempting to reuse an already revoked refresh token results in immediate rejection (
# Clone the repository
git clone https://github.com/Jaclyn25/PayFlow.git
cd PayFlow
# Launch full stack via Docker Compose
docker compose up --build- Swagger UI: http://localhost:5000/swagger
- Health Endpoint: http://localhost:5000/health
- RabbitMQ Management: http://localhost:15672 (
guest/guest)
- Alice Smith:
alice@payflow.local/Password123!(Balance: 1,000 EGP) - Bob Jones:
bob@payflow.local/Password123!(Balance: 500 EGP)
Validated by 31 automated unit and integration tests:
dotnet test- ADR-001: Modular Monolith Architecture
- ADR-002: PostgreSQL Concurrency Control
- ADR-003: Database-Level Idempotency Strategy
- ADR-004: Transactional Outbox & Inbox Pattern
MIT License - see LICENSE for details.