A production-grade, fault-tolerant financial payment engine built with Python 3, Django REST Framework (DRF), PostgreSQL, and Redis. Designed to guarantee exactly-once processing under high-concurrency multi-threaded load, coupled with an Asynchronous AI Intelligence Layer that reads its own audit trail, explains duplicate requests in plain English, scores fraud/risk dynamically, safely translates natural language to SQL queries, and automatically drafts SRE incident postmortems.
| Resource | URL / Endpoint | Details |
|---|---|---|
| π’ Live Interactive Demo | https://payment-api-3fi4.onrender.com/ |
Editorial Landing Page & Double-POST Proof Test (Hosted 24/7 on Render Cloud) |
| Admin Dashboard | https://payment-api-3fi4.onrender.com/admin/ |
(Login: Ayush / Tl02xd1@3140) |
| API Root Status | GET https://payment-api-3fi4.onrender.com/api/ |
Structured JSON status & API gateway health |
The single most critical architectural decision in this project:
The AI layer NEVER sits in the critical path of
process-payment/.
Your idempotency guarantee comes from deterministic, high-speed PostgreSQL row-level locks (SELECT ... FOR UPDATE), SHA-256 request body fingerprinting, and atomic database transactions. That logic is deterministic and sub-millisecond fast. An LLM API call is slow (200msβ3,000ms), non-deterministic, and a dangerous external dependency inside a locked database transaction. If OpenAI, Claude, or any LLM provider experiences an outage, financial payments must still process without delay.
Therefore, our system architecture is event-driven, asynchronous, and read-only by design:
graph TD
Client["Client App / Browser / cURL"] -->|1. POST process-payment/| API["Core Payment API Engine"]
subgraph CoreEngine ["Core Payment Engine (Synchronous)"]
API -->|"2. SHA-256 Hash + UUID Check"| DBLock["PostgreSQL Row-Level Lock (SELECT ... FOR UPDATE)"]
DBLock -->|"3a. Fresh Request"| Gateway["Simulate Payment Processor"]
DBLock -->|"3b. Duplicate Replay"| Cache["Return Cached Response (Zero Double-Charging)"]
Gateway -->|"4. Save Payment & Lock Status"| DB[("PostgreSQL Database")]
end
API -.->|"5. Emit Async Signal (django.dispatch.Signal)"| Redis["Redis Stream / Broker"]
subgraph AILayer ["Asynchronous AI Intelligence Layer"]
Redis -->|"6. Dequeue Task"| Celery["Celery Worker Pool"]
Celery -->|"7. Scrub PII Cards / CVV"| Redact["Redaction Service"]
Redact -->|"8. Render Prompt Template"| LLM["LLM Gateway w/ Fallback Rule Engine"]
LLM -->|"9. Persist AI Explanation & Score"| AIDB[("AI Event & Risk Tables")]
end
Every incoming charge to POST /api/payments/process-payment/ must supply a unique Idempotency-Key (UUIDv4) header alongside the request payload. The core engine immediately computes a SHA-256 cryptographic hash of the exact raw bytes (_hash_body()).
- Payload Mismatch Interception: If a client reuses an existing
Idempotency-Key(demo-key-01) but changes the body (e.g., changing"amount": "125.50"to"amount": "900.00"), the engine intercepts the hash mismatch instantly and rejects the request with422 Unprocessable Entity. - Zero Payload Tampering: Prevents buggy retries or malicious interception attacks from modifying critical transaction details mid-flight.
To eliminate race conditions when dozens of concurrent worker threads or rapid client retry loops hit the server with the same key simultaneously:
- The transaction engine acquires an exclusive PostgreSQL row lock inside an atomic transaction using
IdempotencyRecord.objects.select_for_update(nowait=False).filter(idempotency_key=key). - Any concurrent thread attempting to process the same key blocks cleanly until the active transaction finishes or releases the lock, guaranteeing zero double-charging under heavy concurrent load.
To fulfill core reliability principles (build self-healing systems), the API features an autonomous dead-lock recovery engine (_handle_existing_record):
- If a server node crashes, loses power, or drops its DB connection mid-processing while an idempotency record is locked (
status = 'processing'), the lock would traditionally remain orphaned forever. - Automated Stale-Lock Detection: The engine continuously tracks the
locked_attimestamp. If aprocessinglock exceeds the 60-second threshold without completing, the system automatically deletes the orphaned record, emits astale_lock_recovereddomain signal, and returns409 Conflict (retry: true). - Client applications can safely retry immediately without manual DBA intervention or database row cleanup.
- Custom DRF permission (
HasAPIKey) enforces header-based authentication across all secure endpoints (-H "X-API-KEY: pay_prefix.secret"). - Zero Plaintext Storage: API secrets (
pay_prefix.secret) are shown to the administrator or developer exactly once upon generation. They are stored in the database strictly as SHA-256 hashes (hashed_key).
The AI Intelligence Layer runs entirely decoupled from the transactional payment path via Django Signals (django.dispatch.Signal) and Celery asynchronous background task queues.
Whenever the core payment API executes an event, it broadcasts a lightweight memory signal (payment_processed) with a meta dictionary containing latency, status codes, and request hashes:
payment_created: A successful new charge.payment_failed: A gateway decline or processing exception.duplicate_detected: A cached replay where an idempotency lock successfully prevented a double charge.payload_mismatch: An attempt to reuse an idempotency key with different data.stale_lock_recovered: A recovered dead-lock.
The signal receiver intercepts these events, writes an initial PaymentEvent record (status='pending_ai'), and dispatches an asynchronous background job (analyze_event_task.delay()) without delaying the HTTP response sent back to the customer.
Before any audit data or payload log is formatted into an LLM prompt, it passes through a rigorous PII Redaction Engine:
- Credit Card Numbers: All 13β19 digit card sequences (
4111222233334444) are automatically replaced with[REDACTED_CARD: ****-4444]. - CVV / CVC Codes: 3β4 digit security codes are scrubbed (
[REDACTED_CVV]). - Email Addresses: Payer emails are anonymized (
live.reviewer@example.comβl***r@example.com).
Our LLM Gateway formats prompts cleanly from external Markdown templates stored inside prompts/ (duplicate_analysis.md, failure_analysis.md, risk_scoring.md, nl2sql.md, incident_postmortem.md, log_summary.md).
- Resilience & Fallback Architecture: If the configured LLM provider (
OpenAI/Anthropic/Mock) experiences an outage, network timeout (timeout=10s), or rate limit (429), the gateway automatically falls back to deterministic Rule-Based Mock Adapters (MockLLMAdapter). - Zero AI Downtime: The intelligence layer always produces structured, accurate explanations, risk scores, and incident logs even without live external internet access.
- Duplicate Analyzer (
duplicate_analyzer.py): Examines duplicate requests to distinguish benign client retry loops (network timeouts causing exact replays) from malicious replay attacks or bad client state logic, generating a concise, plain-English summary (AIExplanation). - Failure Analyzer (
failure_analyzer.py): Classifies gateway declines and backend exceptions into standardized error taxonomies (insufficient_funds,card_expired,fraud_suspected,system_error) and suggests concrete remediation steps. - Risk Scoring Engine (
risk_engine.py): Implements a Hybrid Risk Score:- Rule-Based Base Score (
base_score): Assigns deterministic weights (+0.60for mismatched payload,+0.40for failed status,+0.30for repeated duplicates within 60s). - LLM Adjustment (
llm_adjustment): Allows the LLM to adjust the score based on contextual anomalies, clamped strictly to[-0.1, +0.1]to prevent hallucinations from wildly altering fraud metrics. - Risk Tiering: Final score
[0.0 to 1.0]classified asLOW(< 0.35),MEDIUM(0.35 to 0.70), orHIGH(>= 0.70), complete with a written justification (RiskScore).
- Rule-Based Base Score (
Allows engineers or product managers to ask complex analytical questions in plain English ("Show me all duplicate payments from the last hour where the amount exceeded $100").
- Strict SQL Guardrails:
- AST Token & Regex Parsing: Rejects any query containing modifying or dangerous keywords (
INSERT,UPDATE,DELETE,DROP,ALTER,TRUNCATE,GRANT,EXEC,--,;). - SELECT-Only Enforcement: Queries MUST begin with
SELECTand access only Whitelisted Tables (payments_payment,payments_idempotencyrecord,ai_intelligence_paymentevent,ai_intelligence_aiexplanation,ai_intelligence_riskscore). - LIMIT Clamping: Automatically enforces
LIMIT 500(or clamps higher limits down to 500) to prevent memory exhaustion or table scans.
- AST Token & Regex Parsing: Rejects any query containing modifying or dangerous keywords (
- Human-in-the-Loop (
auto_execute): Ifauto_execute=false, the engine returns the validated SQL query and explanation for human review and approval before execution (status='pending_approval').
During API disruptions or error spikes (POST /api/ai/incident/postmortem/), the service aggregates failure logs across a specified time window (time_window_minutes) and automatically drafts a Markdown SRE Incident Postmortem complete with:
- Timeline Table: Chronological breakdown of events and error counts.
- Root Cause Analysis: AI-synthesized explanation of the underlying failure mechanism.
- Actionable Remediation Plan: Concrete engineering steps to prevent recurrence (
status='draft').
Instead of returning raw JSON at the root URL (/), visiting the application in any web browser renders an ultra-clean, glassmorphic Editorial Web Application (Newsreader serif display font + IBM Plex Mono monospace data styling):
-
One-Paragraph Pitch & Live Status Badge:
"Idempotent by proof, not by promise. A payment engine that guarantees exactly-once processing under concurrent load β with an AI layer that reads its own audit trail and explains, in plain language, why every duplicate was caught." (Accompanying a glowing
Online Β· Live demo serverstatus dot indicator). -
β‘ Interactive Double-POST Live Verification Ticket:
- Reviewers can click
β» Fire duplicate requestdirectly inside the live receipt section. - The browser generates a fresh
UUIDv4(demo-xxxxxxxx-...), fires Request #1 and Request #2 (POST /api/payments/process-payment/) sequentially using a live reviewer API key, and renders both exact JSON responses side-by-side in real time:- Request #1:
201 Createdβ Initial charge processed ("id": "31c4116a-...", "amount": "125.50"). - Request #2:
200 OK (replay)β Intercepted by PostgreSQL row-level locks and served cleanly from the idempotency cache without hitting external gateways or double-charging the customer!
- Request #1:
- Displays verification proof confirming identical payment IDs and timestamps.
- Reviewers can click
-
π Compact Flat-Row Endpoint Ledger with Inline cURL Expansion & Copy:
- Every endpoint (
/api/payments/process-payment/,/api/ai/events/,/api/ai/risk-scores/,/api/ai/query/) is laid out in a compact 4-column flat grid ([Verb Badge] [Path & Description] [Core / AI Badge] [π Copy cURL Button]). - Click any row to toggle open an inline multiline
cURLcode box for deep inspection. - Click
π Copy cURLto copy the exact terminal command (-H "X-API-KEY: {{ demo_api_key }}") directly to your clipboard (β Copiedvisual confirmation).
- Every endpoint (
-
100% Backward Compatibility:
- Programmatic API clients or evaluation bots accessing
/with anAccept: application/jsonheader still receive the structuredJsonResponsedictionary seamlessly.
- Programmatic API clients or evaluation bots accessing
Executes an atomic charge with mandatory SHA-256 fingerprinting and row-level locking.
- Headers Required:
Content-Type: application/jsonX-API-KEY: <your-api-key>Idempotency-Key: <unique-uuidv4>
- Request Payload:
{ "amount": "125.50", "currency": "USD", "payer_email": "live.reviewer@example.com", "description": "Live Idempotent Verification" } - Responses:
201 Createdβ Initial payment processed successfully.200 OKβ Cached replay returned (idempotent intercept).409 Conflictβ Lock currently processing or stale lock recovered ({"retry": true}).422 Unprocessable Entityβ Payload mismatch detected for reused key.400 Bad Requestβ Missing headers or invalid serializer payload.403 Forbiddenβ Missing or invalidX-API-KEY.
Returns a paginated list of all completed payments.
- Headers Required:
X-API-KEY: <your-api-key>
Returns exact details for a specific payment UUID.
- Headers Required:
X-API-KEY: <your-api-key>
Retrieve all captured domain events (payment_created, duplicate_detected, payload_mismatch) along with their nested explanations and risk_scores.
- Headers Required:
X-API-KEY: <your-api-key> - Optional Query Params:
?event_type=duplicate_detected&risk_level=HIGH
Fetch full audit trail, redacted payload, and AI explanations for a specific event.
- Headers Required:
X-API-KEY: <your-api-key>
View calculated risk scores, hybrid weights, and LLM justifications across transactions.
- Headers Required:
X-API-KEY: <your-api-key> - Optional Query Params:
?risk_level=HIGH
Execute plain-English analytical queries against whitelisted database tables.
- Headers Required:
X-API-KEY: <your-api-key> - Request Payload:
{ "question": "How many duplicate payments occurred in the last 24 hours?", "auto_execute": true } - Response (
200 OK):{ "query_id": "81f1837a-...", "question": "How many duplicate payments occurred in the last 24 hours?", "sql_generated": "SELECT COUNT(*) FROM ai_intelligence_paymentevent WHERE event_type = 'duplicate_detected';", "is_valid_sql": true, "status": "executed", "row_count": 1, "rows": [{"count": 14}], "explanation": "Query counted all records where event_type matches 'duplicate_detected'." }
Trigger automated SRE postmortem drafting across a failure window.
- Headers Required:
X-API-KEY: <your-api-key> - Request Payload:
{ "title": "Payment Gateway Latency Spike", "time_window_minutes": 60 } - Response (
201 Created):{ "incident_id": "c9a0112e-...", "title": "Payment Gateway Latency Spike", "status": "draft", "markdown_report": "# Incident Postmortem: Payment Gateway Latency Spike\n\n## Timeline...\n\n## Root Cause Analysis...\n\n## Actionable Remediation Plan..." }
The project features a complete 46-test regression suite spanning across both synchronous payment logic and asynchronous AI intelligence services, boasting 100% pass rate with zero flaky tests.
To execute the entire regression suite across all apps:
python manage.py test --verbosity=2To run individual test modules:
# Run Core Payment & Idempotency Tests (21 Tests)
python manage.py test payments.tests --verbosity=2
# Run AI Intelligence Layer & API Tests (25 Tests)
python manage.py test ai_intelligence.tests --verbosity=2PaymentIdempotencyTests(14 Tests):test_first_request_creates_payment: Fresh key creates payment (201 Created).test_retry_returns_same_response: Replayed key returns cached response (200 OK) without double-charging.test_reused_key_with_different_body_returns_422: SHA-256 fingerprint mismatch returns422 Unprocessable Entity.test_different_keys_create_different_payments: Unique keys create independent payments.test_stale_processing_record_returns_409_with_retry: Automated 60s stale lock self-healing recovery (409 Conflict).test_expired_key_returns_409_with_retry: TTL expiration recovery (409 Conflict).test_missing_idempotency_key_returns_400: Missing header validation.test_missing_api_key_returns_403: API key enforcement.test_invalid_api_key_returns_403: Malformed API key rejection.test_inactive_api_key_returns_403: Deactivated API key rejection.test_missing_payer_email_returns_400: Payload validation.test_invalid_amount_returns_400: Negative or zero amount check.test_unsupported_currency_returns_400: Currency whitelist check (USD,EUR,GBP,INR).test_payment_detail_view/test_payment_detail_404/test_payment_list_view: Read views verification.
IdempotencyRecordModelTests(5 Tests):test_is_locked_true/test_is_locked_false_when_completed/test_is_locked_false_when_stale: Property verification.test_is_expired_false/test_is_expired_true: TTL verification.
APIKeyAndManagementTests(2 Tests):test_api_key_creation_and_hashing: SHA-256 storage verification.test_cleanup_idempotency_keys_command: Background management task cleanup (24 hours).
AIEndpointsAPITests(8 Tests):test_landing_page_html: Verifies HTML/JS root rendering and editorial strings (Idempotent by proof).test_api_root_json: Verifies JSON fallback when hitting/api/or with JSON headers.test_get_events_list_authorized/test_get_events_list_unauthorized/test_get_event_detail_authorized: API event access control.test_get_risk_scores_authorized: Risk engine endpoint verification.test_nl2sql_query_endpoint: Plain-English SQL execution.test_draft_incident_endpoint: SRE Postmortem creation.
EventCaptureTests(3 Tests):test_payment_created_emits_event/test_duplicate_detected_emits_event/test_payload_mismatch_emits_event: Signal interception & DB record creation.
NL2SQLServiceTests(6 Tests):test_validate_sql_valid_select: WhitelistedSELECTapproval.test_validate_sql_rejects_forbidden_tokens: SQL injection/mutation token rejection (DROP,DELETE,;).test_validate_sql_rejects_non_whitelisted_table: Unauthorized table access prevention (auth_user).test_validate_sql_limit_clamping: EnforcesLIMIT 500.test_execute_nl_query_auto_execute/test_execute_nl_query_pending_approval: Execution flows.
CoreAnalyzersTests&RedactionServiceTests(5 Tests):test_redact_credit_card/test_redact_cvv_and_email: 100% PII scrubbing verification.test_analyze_duplicate_service/test_analyze_failure_service: Explanation generation.test_compute_risk_score_hybrid_clamping: Verifiesllm_adjustment[-0.1, +0.1]clamping and base score calculation.
LLMGatewayTests&PostmortemServiceTests(3 Tests):test_run_mock_adapter_duplicate: Fallback resilience check.test_missing_template_raises_error: Prompt template check.test_draft_incident_postmortem: Markdown SRE postmortem structure check.
- Python 3.10+ (tested on Python 3.11)
- PostgreSQL 14+ (or SQLite for local zero-config quick testing)
- Redis 6+ (required for Celery task worker queues)
git clone https://github.com/Ayush678-hue/payment_api.git
cd payment_api
python -m venv venv
# On Windows PowerShell:
.\venv\Scripts\Activate.ps1
# On macOS/Linux:
source venv/bin/activatepip install --upgrade pip
pip install -r requirements.txtCreate a .env file inside c:\Users\ayush\django\payment_api\ (or root directory):
DJANGO_SECRET_KEY="django-insecure-local-dev-secret-key"
DEBUG=True
DATABASE_URL="sqlite:///db.sqlite3" # Or postgresql://user:password@localhost:5432/payment_db
CELERY_BROKER_URL="redis://localhost:6379/0"
CELERY_RESULT_BACKEND="redis://localhost:6379/0"
# Optional: Configure LLM Provider (Defaults to robust mock adapter if omitted/mock)
AI_LLM_PROVIDER="mock"
# OPENAI_API_KEY="sk-..."If using Docker:
docker run -d -p 6379:6379 --name redis-worker redis:alpinepython manage.py makemigrations
python manage.py migratepython manage.py createsuperuser(Note: When the application boots, payment_api/urls.py automatically checks and provisions the live reviewer demo key pay_demo.live_reviewer_test_key_2026 so the interactive landing page double-POST button works immediately out of the box without manual administration).
To process AI Intelligence background tasks asynchronously:
# On Windows PowerShell (using solo or pool=threads):
celery -A payment_api worker -l info -P solo
# On macOS/Linux:
celery -A payment_api worker -l infopython manage.py runserverVisit http://127.0.0.1:8000/ in your browser to experience the interactive editorial landing page and trigger the live double-POST idempotency proof!
This repository is completely pre-configured for instant 1-Click Production Cloud Deployment:
Procfile: Configured for robust WSGI production server execution:web: gunicorn payment_api.wsgi:applicationrequirements.txt: Includesgunicorn,whitenoise,psycopg2-binary,redis,celery, anddjango-cors-headers.- WhiteNoise Static File Compression (
settings.py): Automatic compression and caching of Django admin and landing page static assets (MIDDLEWARE = ['whitenoise.middleware.WhiteNoiseMiddleware', ...]). - Demo Environment Auto-Provisioning:
Even on clean PostgreSQL cloud deployments, accessing
/automatically verifies or creates thepay_demoAPI key viaget_or_create_demo_api_key()insideurls.py, ensuring live cloud reviewers can test idempotency with zero setup.
Built by Ayush Sharma, with AI-assisted development. Licensed under the MIT License. Designed for fault-tolerant, high-concurrency payment reliability and AI-assisted observability.