# gaurav8299/payment-gateway-backend architecture

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

- Interactive diagram: https://gitdiagram.com/gaurav8299/payment-gateway-backend
- Repository: https://github.com/gaurav8299/payment-gateway-backend

## Overview

This is a Django REST payment-gateway backend for merchants and customers. Its principal flow is a merchant request to create a payment: the API delegates payment processing to a gateway adapter, records the result and ledger, and updates the associated order; asynchronous webhook delivery is a separate follow-on capability. The README also documents authentication and RBAC, refunds, wallets, analytics, audit, and notifications. The sampled source confirms adapter dispatch, repository writes, refund processing, and webhook delivery; some API routing and cross-domain wiring are represented only at the documented subsystem level.

Main entry points: [`apps/payments/services/payment_service.py`](https://github.com/gaurav8299/payment-gateway-backend/blob/main/apps/payments/services/payment_service.py) (Payment Service), [`apps/refunds/tasks.py`](https://github.com/gaurav8299/payment-gateway-backend/blob/main/apps/refunds/tasks.py) (Refund Tasks), [`apps/accounts/services/auth_service.py`](https://github.com/gaurav8299/payment-gateway-backend/blob/main/apps/accounts/services/auth_service.py) (Auth Service), [`apps/analytics/views.py`](https://github.com/gaurav8299/payment-gateway-backend/blob/main/apps/analytics/views.py) (Analytics API)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_identity["Identity and Tenancy"]
  node_accounts_api["Accounts API<br/>[views.py]"]
  node_auth_service["Auth Service<br/>[auth_service.py]"]
end

subgraph group_commerce["Commerce Lifecycle"]
  node_payment_api["Payment API<br/>[views.py]"]
  node_payment_service["Payment Service<br/>[payment_service.py]"]
  node_gateway_factory["Gateway Factory<br/>[factories.py]"]
  node_gateway_adapters["Gateway Adapters"]
  node_payment_repository["Payment Repository"]
  node_payment_records[("Payment Records<br/>[models.py]")]
  node_order_service["Order Service<br/>[order_service.py]"]
  node_order_records[("Orders<br/>[models.py]")]
  node_refund_service["Refund Service<br/>[refund_service.py]"]
  node_refund_repository["Refund Repository"]
  node_refund_records[("Refund Records<br/>[models.py]")]
  node_wallet_service["Wallet Service<br/>[wallet_service.py]"]
end

subgraph group_operations["Async Operations"]
  node_refund_tasks["Refund Tasks<br/>[tasks.py]"]
  node_account_tasks["Account Email Tasks<br/>[tasks.py]"]
  node_analytics_api["Analytics API<br/>[views.py]"]
  node_analytics_service["Analytics Service"]
  node_invoice_service["Invoice Service<br/>[invoice_service.py]"]
  node_webhook_tasks["Webhook Tasks<br/>[tasks.py]"]
  node_notification_tasks["Notification Tasks<br/>[tasks.py]"]
end

subgraph group_platform["Shared Platform"]
  node_redis[("Redis")]
  node_audit_middleware["Audit Middleware<br/>[middleware.py]"]
  node_audit_service["Audit Service<br/>[audit_service.py]"]
  node_audit_records[("Audit Records<br/>[models.py]")]
end

node_merchant(("Merchant"))
node_customer(("Customer"))
node_external_gateways["External Gateways"]
node_webhook_endpoint["Merchant Webhook"]

node_merchant -->|"submits payment"| node_payment_api
node_payment_api -->|"delegates"| node_payment_service
node_payment_service -->|"selects adapter"| node_gateway_factory
node_gateway_factory -->|"returns adapter"| node_gateway_adapters
node_gateway_adapters -.->|"processes charge"| node_external_gateways
node_payment_service -->|"records payment"| node_payment_repository
node_payment_repository -->|"writes ledger"| node_payment_records
node_payment_service -->|"reads order"| node_order_records
node_payment_service -->|"updates order"| node_order_service
node_order_service -->|"writes status"| node_order_records
node_payment_service --> node_refund_service
node_payment_service -->|"publishes outbox event"| node_webhook_tasks
node_webhook_tasks -->|"delivers signed event"| node_webhook_endpoint
node_customer -->|"manages account"| node_accounts_api
node_accounts_api -->|"delegates"| node_auth_service
node_auth_service -->|"stores OTP tokens"| node_redis
node_auth_service -->|"dispatches email"| node_account_tasks
node_refund_service -->|"persists refund"| node_refund_repository
node_refund_repository -->|"writes ledger"| node_refund_records
node_refund_tasks -->|"updates refund"| node_refund_repository
node_refund_tasks -->|"selects adapter"| node_gateway_factory
node_refund_tasks -->|"credits wallet"| node_wallet_service
node_merchant -->|"requests reports"| node_analytics_api
node_analytics_api -->|"delegates"| node_analytics_service
node_analytics_api -->|"generates invoices"| node_invoice_service
node_audit_middleware -->|"records activity"| node_audit_service
node_audit_service -->|"writes audit log"| node_audit_records
node_notification_tasks --> node_webhook_endpoint

click node_payment_api "https://github.com/gaurav8299/payment-gateway-backend/blob/main/apps/payments/views.py"
click node_payment_service "https://github.com/gaurav8299/payment-gateway-backend/blob/main/apps/payments/services/payment_service.py"
click node_gateway_factory "https://github.com/gaurav8299/payment-gateway-backend/blob/main/apps/payments/factories.py"
click node_gateway_adapters "https://github.com/gaurav8299/payment-gateway-backend/tree/main/apps/payments/adapters"
click node_payment_repository "https://github.com/gaurav8299/payment-gateway-backend/blob/main/apps/payments/repositories/payment_repository.py"
click node_payment_records "https://github.com/gaurav8299/payment-gateway-backend/blob/main/apps/payments/models.py"
click node_order_service "https://github.com/gaurav8299/payment-gateway-backend/blob/main/apps/orders/services/order_service.py"
click node_order_records "https://github.com/gaurav8299/payment-gateway-backend/blob/main/apps/orders/models.py"
click node_refund_service "https://github.com/gaurav8299/payment-gateway-backend/blob/main/apps/refunds/services/refund_service.py"
click node_refund_tasks "https://github.com/gaurav8299/payment-gateway-backend/blob/main/apps/refunds/tasks.py"
click node_refund_repository "https://github.com/gaurav8299/payment-gateway-backend/blob/main/apps/refunds/repositories/refund_repository.py"
click node_refund_records "https://github.com/gaurav8299/payment-gateway-backend/blob/main/apps/refunds/models.py"
click node_wallet_service "https://github.com/gaurav8299/payment-gateway-backend/blob/main/apps/wallet/services/wallet_service.py"
click node_accounts_api "https://github.com/gaurav8299/payment-gateway-backend/blob/main/apps/accounts/views.py"
click node_auth_service "https://github.com/gaurav8299/payment-gateway-backend/blob/main/apps/accounts/services/auth_service.py"
click node_account_tasks "https://github.com/gaurav8299/payment-gateway-backend/blob/main/apps/accounts/tasks.py"
click node_analytics_api "https://github.com/gaurav8299/payment-gateway-backend/blob/main/apps/analytics/views.py"
click node_analytics_service "https://github.com/gaurav8299/payment-gateway-backend/tree/main/apps/analytics/services"
click node_invoice_service "https://github.com/gaurav8299/payment-gateway-backend/blob/main/apps/analytics/services/invoice_service.py"
click node_webhook_tasks "https://github.com/gaurav8299/payment-gateway-backend/blob/main/apps/webhooks/tasks.py"
click node_notification_tasks "https://github.com/gaurav8299/payment-gateway-backend/blob/main/apps/notifications/tasks.py"
click node_audit_middleware "https://github.com/gaurav8299/payment-gateway-backend/blob/main/apps/audit_logs/middleware.py"
click node_audit_service "https://github.com/gaurav8299/payment-gateway-backend/blob/main/apps/audit_logs/services/audit_service.py"
click node_audit_records "https://github.com/gaurav8299/payment-gateway-backend/blob/main/apps/audit_logs/models.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_accounts_api,node_auth_service toneBlue
class node_payment_api,node_payment_service,node_gateway_factory,node_gateway_adapters,node_payment_repository,node_payment_records,node_order_service,node_order_records,node_refund_service,node_refund_repository,node_refund_records,node_wallet_service toneAmber
class node_refund_tasks,node_account_tasks,node_analytics_api,node_analytics_service,node_invoice_service,node_webhook_tasks,node_notification_tasks,node_webhook_endpoint toneMint
class node_redis,node_audit_middleware,node_audit_service,node_audit_records toneRose
class node_merchant,node_customer,node_external_gateways toneIndigo
```

## Components

### Identity and Tenancy

- [Accounts API](https://github.com/gaurav8299/payment-gateway-backend/blob/main/apps/accounts/views.py): `apps/accounts/views.py`
- [Auth Service](https://github.com/gaurav8299/payment-gateway-backend/blob/main/apps/accounts/services/auth_service.py): `apps/accounts/services/auth_service.py`

### Commerce Lifecycle

- [Payment API](https://github.com/gaurav8299/payment-gateway-backend/blob/main/apps/payments/views.py): `apps/payments/views.py`
- [Payment Service](https://github.com/gaurav8299/payment-gateway-backend/blob/main/apps/payments/services/payment_service.py): `apps/payments/services/payment_service.py`
- [Gateway Factory](https://github.com/gaurav8299/payment-gateway-backend/blob/main/apps/payments/factories.py): `apps/payments/factories.py`
- [Gateway Adapters](https://github.com/gaurav8299/payment-gateway-backend/tree/main/apps/payments/adapters): `apps/payments/adapters`
- [Payment Repository](https://github.com/gaurav8299/payment-gateway-backend/blob/main/apps/payments/repositories/payment_repository.py): `apps/payments/repositories/payment_repository.py`
- [Payment Records](https://github.com/gaurav8299/payment-gateway-backend/blob/main/apps/payments/models.py): `apps/payments/models.py`
- [Order Service](https://github.com/gaurav8299/payment-gateway-backend/blob/main/apps/orders/services/order_service.py): `apps/orders/services/order_service.py`
- [Orders](https://github.com/gaurav8299/payment-gateway-backend/blob/main/apps/orders/models.py): `apps/orders/models.py`
- [Refund Service](https://github.com/gaurav8299/payment-gateway-backend/blob/main/apps/refunds/services/refund_service.py): `apps/refunds/services/refund_service.py`
- [Refund Repository](https://github.com/gaurav8299/payment-gateway-backend/blob/main/apps/refunds/repositories/refund_repository.py): `apps/refunds/repositories/refund_repository.py`
- [Refund Records](https://github.com/gaurav8299/payment-gateway-backend/blob/main/apps/refunds/models.py): `apps/refunds/models.py`
- [Wallet Service](https://github.com/gaurav8299/payment-gateway-backend/blob/main/apps/wallet/services/wallet_service.py): `apps/wallet/services/wallet_service.py`

### Async Operations

- [Refund Tasks](https://github.com/gaurav8299/payment-gateway-backend/blob/main/apps/refunds/tasks.py): `apps/refunds/tasks.py`
- [Account Email Tasks](https://github.com/gaurav8299/payment-gateway-backend/blob/main/apps/accounts/tasks.py): `apps/accounts/tasks.py`
- [Analytics API](https://github.com/gaurav8299/payment-gateway-backend/blob/main/apps/analytics/views.py): `apps/analytics/views.py`
- [Analytics Service](https://github.com/gaurav8299/payment-gateway-backend/tree/main/apps/analytics/services): `apps/analytics/services`
- [Invoice Service](https://github.com/gaurav8299/payment-gateway-backend/blob/main/apps/analytics/services/invoice_service.py): `apps/analytics/services/invoice_service.py`
- [Webhook Tasks](https://github.com/gaurav8299/payment-gateway-backend/blob/main/apps/webhooks/tasks.py): `apps/webhooks/tasks.py`
- [Notification Tasks](https://github.com/gaurav8299/payment-gateway-backend/blob/main/apps/notifications/tasks.py): `apps/notifications/tasks.py`

### Shared Platform

- **Redis**
- [Audit Middleware](https://github.com/gaurav8299/payment-gateway-backend/blob/main/apps/audit_logs/middleware.py): `apps/audit_logs/middleware.py`
- [Audit Service](https://github.com/gaurav8299/payment-gateway-backend/blob/main/apps/audit_logs/services/audit_service.py): `apps/audit_logs/services/audit_service.py`
- [Audit Records](https://github.com/gaurav8299/payment-gateway-backend/blob/main/apps/audit_logs/models.py): `apps/audit_logs/models.py`

### Other components

- **Merchant**
- **Customer**
- **External Gateways**
- **Merchant Webhook**

## Connections

- Merchant → Payment API: submits payment
- Payment API → Payment Service: delegates
- Payment Service → Gateway Factory: selects adapter
- Gateway Factory → Gateway Adapters: returns adapter
- Gateway Adapters → External Gateways: processes charge
- Payment Service → Payment Repository: records payment
- Payment Repository → Payment Records: writes ledger
- Payment Service → Orders: reads order
- Payment Service → Order Service: updates order
- Order Service → Orders: writes status
- Payment Service → Refund Service
- Payment Service → Webhook Tasks: publishes outbox event
- Webhook Tasks → Merchant Webhook: delivers signed event
- Customer → Accounts API: manages account
- Accounts API → Auth Service: delegates
- Auth Service → Redis: stores OTP tokens
- Auth Service → Account Email Tasks: dispatches email
- Refund Service → Refund Repository: persists refund
- Refund Repository → Refund Records: writes ledger
- Refund Tasks → Refund Repository: updates refund
- Refund Tasks → Gateway Factory: selects adapter
- Refund Tasks → Wallet Service: credits wallet
- Merchant → Analytics API: requests reports
- Analytics API → Analytics Service: delegates
- Analytics API → Invoice Service: generates invoices
- Audit Middleware → Audit Service: records activity
- Audit Service → Audit Records: writes audit log
- Notification Tasks → Merchant Webhook
