# amarkumar66428/payment-withdrawal architecture

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

- Interactive diagram: https://gitdiagram.com/amarkumar66428/payment-withdrawal
- Repository: https://github.com/amarkumar66428/payment-withdrawal

## Overview

This is a Node.js payment-withdrawal API: users register or log in, then authenticated clients inspect wallets and submit withdrawal requests. Requests are validated and idempotent, atomically debit wallet funds and record a ledger entry; a background worker sends pending payouts to a mock gateway, records success or failure, and refunds failures. MongoDB transactions keep balance, withdrawal, and ledger changes consistent. The README also documents concurrency protection, blocked-user checks, and reconciliation. Worker and withdrawal internals are mapped from the source index; edges without direct excerpt evidence are omitted.

Main entry points: [`src/routes/index.js`](https://github.com/amarkumar66428/payment-withdrawal/blob/main/src/routes/index.js) (API routes), [`src/services/withdrawal.service.js`](https://github.com/amarkumar66428/payment-withdrawal/blob/main/src/services/withdrawal.service.js) (Withdrawal service), [`src/services/withdrawal.processor.js`](https://github.com/amarkumar66428/payment-withdrawal/blob/main/src/services/withdrawal.processor.js) (Payout processor), [`src/services/auth.service.js`](https://github.com/amarkumar66428/payment-withdrawal/blob/main/src/services/auth.service.js) (Authentication service)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["API access"]
  node_app["HTTP application<br/>[app.js]"]
  node_routes["API routes<br/>[index.js]"]
  node_authctl["Auth controller<br/>[auth.controller.js]"]
  node_walletctl["Wallet controller"]
  node_withdrawctl["Withdrawal controller"]
  node_authmw["JWT middleware<br/>[auth.js]"]
  node_security["Request security<br/>[security.js]"]
end

subgraph group_domain["Wallet and withdrawal"]
  node_authsvc["Authentication service<br/>[auth.service.js]"]
  node_walletsvc["Wallet service<br/>[wallet.service.js]"]
  node_withdrawsvc["Withdrawal service"]
end

subgraph group_storage["MongoDB persistence"]
  node_userrepo["User repository<br/>[user.repository.js]"]
  node_walletrepo["Wallet repository"]
  node_withdrawrepo["Withdrawal repository"]
  node_ledgerrepo["Ledger repository"]
  node_user[("User records<br/>[User.js]")]
  node_wallet[("Wallet balances<br/>[Wallet.js]")]
  node_withdrawal[("Withdrawal records<br/>[Withdrawal.js]")]
  node_ledger[("Transaction ledger<br/>[TransactionLog.js]")]
end

subgraph group_processing["Payout processing"]
  node_worker["Withdrawal worker"]
  node_processor["Payout processor"]
  node_gateway["Mock payout gateway<br/>[mockGateway.js]"]
end

subgraph group_shared["Security and utilities"]
  node_crypto["Password and token crypto<br/>[crypto.js]"]
  node_transaction["Transaction helper<br/>[transaction.js]"]
  node_validator["Withdrawal validation"]
  node_response["API responses<br/>[response.js]"]
  node_databaseconfig["Database connection<br/>[database.js]"]
end

node_client(("API client"))
node_db[("MongoDB")]

node_client -->|"sends requests"| node_app
node_app -->|"mounts routes"| node_routes
node_routes -->|"routes auth"| node_authctl
node_routes -->|"routes wallet"| node_walletctl
node_routes -->|"routes withdrawals"| node_withdrawctl
node_routes -->|"protects routes"| node_authmw
node_routes -->|"applies security"| node_security
node_authctl -->|"calls"| node_authsvc
node_walletctl -->|"calls"| node_walletsvc
node_withdrawctl -->|"calls"| node_withdrawsvc
node_authctl -->|"formats response"| node_response
node_walletctl -->|"formats response"| node_response
node_withdrawctl -->|"validates request"| node_validator
node_withdrawctl -->|"formats response"| node_response
node_authmw -->|"verifies token"| node_crypto
node_authsvc -->|"hashes and signs"| node_crypto
node_authsvc -->|"reads and creates"| node_userrepo
node_authsvc -->|"creates wallet"| node_walletrepo
node_authsvc -->|"runs transaction"| node_transaction
node_walletsvc -->|"reads and credits"| node_walletrepo
node_walletsvc -->|"appends ledger"| node_ledgerrepo
node_walletsvc -->|"runs transaction"| node_transaction
node_withdrawsvc -->|"checks user"| node_userrepo
node_withdrawsvc -->|"debits and refunds"| node_walletrepo
node_withdrawsvc -->|"creates request"| node_withdrawrepo
node_withdrawsvc -->|"records changes"| node_ledgerrepo
node_withdrawsvc -->|"runs transaction"| node_transaction
node_worker -->|"processes next"| node_processor
node_processor -->|"submits payout"| node_gateway
node_processor -->|"updates balance"| node_walletrepo
node_processor -->|"claims and updates"| node_withdrawrepo
node_processor -->|"records outcome"| node_ledgerrepo
node_processor -->|"runs transaction"| node_transaction
node_userrepo -->|"queries"| node_user
node_walletrepo -->|"updates and reads"| node_wallet
node_withdrawrepo -->|"persists requests"| node_withdrawal
node_ledgerrepo -->|"appends entries"| node_ledger
node_databaseconfig -->|"connects"| node_db

click node_app "https://github.com/amarkumar66428/payment-withdrawal/blob/main/src/app.js"
click node_routes "https://github.com/amarkumar66428/payment-withdrawal/blob/main/src/routes/index.js"
click node_authctl "https://github.com/amarkumar66428/payment-withdrawal/blob/main/src/controllers/auth.controller.js"
click node_walletctl "https://github.com/amarkumar66428/payment-withdrawal/blob/main/src/controllers/wallet.controller.js"
click node_withdrawctl "https://github.com/amarkumar66428/payment-withdrawal/blob/main/src/controllers/withdrawal.controller.js"
click node_authmw "https://github.com/amarkumar66428/payment-withdrawal/blob/main/src/middlewares/auth.js"
click node_security "https://github.com/amarkumar66428/payment-withdrawal/blob/main/src/middlewares/security.js"
click node_authsvc "https://github.com/amarkumar66428/payment-withdrawal/blob/main/src/services/auth.service.js"
click node_walletsvc "https://github.com/amarkumar66428/payment-withdrawal/blob/main/src/services/wallet.service.js"
click node_withdrawsvc "https://github.com/amarkumar66428/payment-withdrawal/blob/main/src/services/withdrawal.service.js"
click node_worker "https://github.com/amarkumar66428/payment-withdrawal/blob/main/src/workers/withdrawal.worker.js"
click node_processor "https://github.com/amarkumar66428/payment-withdrawal/blob/main/src/services/withdrawal.processor.js"
click node_gateway "https://github.com/amarkumar66428/payment-withdrawal/blob/main/src/services/mockGateway.js"
click node_userrepo "https://github.com/amarkumar66428/payment-withdrawal/blob/main/src/repositories/user.repository.js"
click node_walletrepo "https://github.com/amarkumar66428/payment-withdrawal/blob/main/src/repositories/wallet.repository.js"
click node_withdrawrepo "https://github.com/amarkumar66428/payment-withdrawal/blob/main/src/repositories/withdrawal.repository.js"
click node_ledgerrepo "https://github.com/amarkumar66428/payment-withdrawal/blob/main/src/repositories/transactionLog.repository.js"
click node_user "https://github.com/amarkumar66428/payment-withdrawal/blob/main/src/models/User.js"
click node_wallet "https://github.com/amarkumar66428/payment-withdrawal/blob/main/src/models/Wallet.js"
click node_withdrawal "https://github.com/amarkumar66428/payment-withdrawal/blob/main/src/models/Withdrawal.js"
click node_ledger "https://github.com/amarkumar66428/payment-withdrawal/blob/main/src/models/TransactionLog.js"
click node_crypto "https://github.com/amarkumar66428/payment-withdrawal/blob/main/src/utils/crypto.js"
click node_transaction "https://github.com/amarkumar66428/payment-withdrawal/blob/main/src/utils/transaction.js"
click node_validator "https://github.com/amarkumar66428/payment-withdrawal/blob/main/src/validators/withdrawal.validator.js"
click node_response "https://github.com/amarkumar66428/payment-withdrawal/blob/main/src/utils/response.js"
click node_databaseconfig "https://github.com/amarkumar66428/payment-withdrawal/blob/main/config/database.js"

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_app,node_routes,node_authctl,node_walletctl,node_withdrawctl,node_authmw,node_security,node_client toneBlue
class node_authsvc,node_walletsvc,node_withdrawsvc,node_db toneAmber
class node_userrepo,node_walletrepo,node_withdrawrepo,node_ledgerrepo,node_user,node_wallet,node_withdrawal,node_ledger toneMint
class node_worker,node_processor,node_gateway toneRose
class node_crypto,node_transaction,node_validator,node_response,node_databaseconfig toneIndigo
```

## Components

### API access

- [HTTP application](https://github.com/amarkumar66428/payment-withdrawal/blob/main/src/app.js): `src/app.js`
- [API routes](https://github.com/amarkumar66428/payment-withdrawal/blob/main/src/routes/index.js): `src/routes/index.js`
- [Auth controller](https://github.com/amarkumar66428/payment-withdrawal/blob/main/src/controllers/auth.controller.js): `src/controllers/auth.controller.js`
- [Wallet controller](https://github.com/amarkumar66428/payment-withdrawal/blob/main/src/controllers/wallet.controller.js): `src/controllers/wallet.controller.js`
- [Withdrawal controller](https://github.com/amarkumar66428/payment-withdrawal/blob/main/src/controllers/withdrawal.controller.js): `src/controllers/withdrawal.controller.js`
- [JWT middleware](https://github.com/amarkumar66428/payment-withdrawal/blob/main/src/middlewares/auth.js): `src/middlewares/auth.js`
- [Request security](https://github.com/amarkumar66428/payment-withdrawal/blob/main/src/middlewares/security.js): `src/middlewares/security.js`

### Wallet and withdrawal

- [Authentication service](https://github.com/amarkumar66428/payment-withdrawal/blob/main/src/services/auth.service.js): `src/services/auth.service.js`
- [Wallet service](https://github.com/amarkumar66428/payment-withdrawal/blob/main/src/services/wallet.service.js): `src/services/wallet.service.js`
- [Withdrawal service](https://github.com/amarkumar66428/payment-withdrawal/blob/main/src/services/withdrawal.service.js): `src/services/withdrawal.service.js`

### MongoDB persistence

- [User repository](https://github.com/amarkumar66428/payment-withdrawal/blob/main/src/repositories/user.repository.js): `src/repositories/user.repository.js`
- [Wallet repository](https://github.com/amarkumar66428/payment-withdrawal/blob/main/src/repositories/wallet.repository.js): `src/repositories/wallet.repository.js`
- [Withdrawal repository](https://github.com/amarkumar66428/payment-withdrawal/blob/main/src/repositories/withdrawal.repository.js): `src/repositories/withdrawal.repository.js`
- [Ledger repository](https://github.com/amarkumar66428/payment-withdrawal/blob/main/src/repositories/transactionLog.repository.js): `src/repositories/transactionLog.repository.js`
- [User records](https://github.com/amarkumar66428/payment-withdrawal/blob/main/src/models/User.js): `src/models/User.js`
- [Wallet balances](https://github.com/amarkumar66428/payment-withdrawal/blob/main/src/models/Wallet.js): `src/models/Wallet.js`
- [Withdrawal records](https://github.com/amarkumar66428/payment-withdrawal/blob/main/src/models/Withdrawal.js): `src/models/Withdrawal.js`
- [Transaction ledger](https://github.com/amarkumar66428/payment-withdrawal/blob/main/src/models/TransactionLog.js): `src/models/TransactionLog.js`

### Payout processing

- [Withdrawal worker](https://github.com/amarkumar66428/payment-withdrawal/blob/main/src/workers/withdrawal.worker.js): `src/workers/withdrawal.worker.js`
- [Payout processor](https://github.com/amarkumar66428/payment-withdrawal/blob/main/src/services/withdrawal.processor.js): `src/services/withdrawal.processor.js`
- [Mock payout gateway](https://github.com/amarkumar66428/payment-withdrawal/blob/main/src/services/mockGateway.js): `src/services/mockGateway.js`

### Security and utilities

- [Password and token crypto](https://github.com/amarkumar66428/payment-withdrawal/blob/main/src/utils/crypto.js): `src/utils/crypto.js`
- [Transaction helper](https://github.com/amarkumar66428/payment-withdrawal/blob/main/src/utils/transaction.js): `src/utils/transaction.js`
- [Withdrawal validation](https://github.com/amarkumar66428/payment-withdrawal/blob/main/src/validators/withdrawal.validator.js): `src/validators/withdrawal.validator.js`
- [API responses](https://github.com/amarkumar66428/payment-withdrawal/blob/main/src/utils/response.js): `src/utils/response.js`
- [Database connection](https://github.com/amarkumar66428/payment-withdrawal/blob/main/config/database.js): `config/database.js`

### Other components

- **API client**
- **MongoDB**

## Connections

- API client → HTTP application: sends requests (evidence: `src/app.js`)
- HTTP application → API routes: mounts routes (evidence: `src/app.js`)
- API routes → Auth controller: routes auth (evidence: `src/routes/index.js`)
- API routes → Wallet controller: routes wallet (evidence: `src/routes/index.js`)
- API routes → Withdrawal controller: routes withdrawals (evidence: `src/routes/index.js`)
- API routes → JWT middleware: protects routes (evidence: `src/routes/index.js`)
- API routes → Request security: applies security (evidence: `src/routes/index.js`)
- Auth controller → Authentication service: calls (evidence: `src/controllers/auth.controller.js`)
- Wallet controller → Wallet service: calls (evidence: `src/controllers/wallet.controller.js`)
- Withdrawal controller → Withdrawal service: calls (evidence: `src/controllers/withdrawal.controller.js`)
- Auth controller → API responses: formats response (evidence: `src/controllers/auth.controller.js`)
- Wallet controller → API responses: formats response (evidence: `src/controllers/wallet.controller.js`)
- Withdrawal controller → Withdrawal validation: validates request (evidence: `src/controllers/withdrawal.controller.js`)
- Withdrawal controller → API responses: formats response (evidence: `src/controllers/withdrawal.controller.js`)
- JWT middleware → Password and token crypto: verifies token (evidence: `src/middlewares/auth.js`)
- Authentication service → Password and token crypto: hashes and signs (evidence: `src/services/auth.service.js`)
- Authentication service → User repository: reads and creates (evidence: `src/services/auth.service.js`)
- Authentication service → Wallet repository: creates wallet (evidence: `src/services/auth.service.js`)
- Authentication service → Transaction helper: runs transaction (evidence: `src/services/auth.service.js`)
- Wallet service → Wallet repository: reads and credits (evidence: `src/services/wallet.service.js`)
- Wallet service → Ledger repository: appends ledger (evidence: `src/services/wallet.service.js`)
- Wallet service → Transaction helper: runs transaction (evidence: `src/services/wallet.service.js`)
- Withdrawal service → User repository: checks user (evidence: `src/services/withdrawal.service.js`)
- Withdrawal service → Wallet repository: debits and refunds (evidence: `src/services/withdrawal.service.js`)
- Withdrawal service → Withdrawal repository: creates request (evidence: `src/services/withdrawal.service.js`)
- Withdrawal service → Ledger repository: records changes (evidence: `src/services/withdrawal.service.js`)
- Withdrawal service → Transaction helper: runs transaction (evidence: `src/services/withdrawal.service.js`)
- Withdrawal worker → Payout processor: processes next (evidence: `src/workers/withdrawal.worker.js`)
- Payout processor → Mock payout gateway: submits payout (evidence: `src/services/withdrawal.processor.js`)
- Payout processor → Wallet repository: updates balance (evidence: `src/services/withdrawal.processor.js`)
- Payout processor → Withdrawal repository: claims and updates (evidence: `src/services/withdrawal.processor.js`)
- Payout processor → Ledger repository: records outcome (evidence: `src/services/withdrawal.processor.js`)
- Payout processor → Transaction helper: runs transaction (evidence: `src/services/withdrawal.processor.js`)
- User repository → User records: queries (evidence: `src/repositories/user.repository.js`)
- Wallet repository → Wallet balances: updates and reads (evidence: `src/repositories/wallet.repository.js`)
- Withdrawal repository → Withdrawal records: persists requests (evidence: `src/repositories/withdrawal.repository.js`)
- Ledger repository → Transaction ledger: appends entries (evidence: `src/repositories/transactionLog.repository.js`)
- Database connection → MongoDB: connects (evidence: `config/database.js`)
