# sadja18/payment_flow_backend architecture

> Architecture diagram of the GitHub repository sadja18/payment_flow_backend, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-07-24). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/sadja18/payment_flow_backend
- Repository: https://github.com/sadja18/payment_flow_backend

## Overview

##### Project type and purpose
- This is a standalone Go‐based backend service (“payment_flow_backend”) that exposes HTTP endpoints for user and invoice management and handles payment flows. It follows a layered architecture (Router → Middleware → Controllers → Services → Models → Database/External Services).

##### File structure
- Top-level files:
- main.go – application entrypoint, starts HTTP server
- go.mod / go.sum – dependency management
- .env – environment variables (e.g., DB connection strings, API keys)
- Directories:
- config: database configuration and connection (`config/db.go`)
- routes: HTTP router setup (`routes/router.go`)
- middleware: request‐scoped logic, e.g., authentication (`middleware/auth.go`)
- controllers: HTTP handlers broken out by resource (`controllers/user.go`, `controllers/invoice.go`, plus a seed script)
- services: core business logic, e.g., payment processing (`services/payment.go`)
- models: data definitions, mapping to DB entities (`models/user.go`, `models/invoice.go`)
- utils: utility functions for external integrations (e.g., webhooks in `utils/webhooks.go`)
- Runtime artifacts:
- payments.db – SQLite (or similar) database file
- proj_dir_script.sh / run_ps.ps1 – convenience scripts for setup or run.

##### README for additional insights
- The README simply mirrors the file structure but confirms the layered design.

##### Main components and their interactions
a. HTTP Client (Postman, Web UI, Mobile App)
b. HTTP Server / API Layer
- main.go spins up the router defined in `routes/router.go`
- Routes are annotated with middleware (auth, logging, etc.)
c. Middleware Layer
- `middleware/auth.go` checks authentication/authorization tokens
d. Controller Layer
- `controllers/user.go` handles user CRUD
- `controllers/invoice.go` handles invoice CRUD
- (Optional) `controllers/seed.go` for database seeding
e. Service Layer
- `services/payment.go` contains payment‐flow logic (calling payment gateway APIs, orchestrating transactions)
f. Model / Persistence Layer
- `models/*.go`: Go structs for ORM or raw SQL mapping
- `config/db.go`: opens and configures the DB connection
- `payments.db`: on‐disk embedded database
g. External Services
- Payment gateways or webhook endpoints (abstracted by `utils/webhooks.go`)

##### Architectural patterns and principles
- Layered (n-tier) architecture: separates concerns horizontally
- Dependency injection via passing DB handle or configs down from main.go
- Single responsibility per package: controllers for HTTP, services for business logic, models for persistence
- Middleware for cross‐cutting concerns (auth, logging, metrics)

## Diagram (Mermaid)

```mermaid
flowchart TB
    Client(("Client\nPostman/Web UI/Mobile")):::external

    subgraph "payment_flow_backend"
        direction TB
        Main["main.go\n(Application Entrypoint)"]:::frontend
        Router["Router\n(routes/router.go)"]:::frontend
        Auth["Auth Middleware\n(middleware/auth.go)"]:::frontend

        subgraph "Controllers"
            direction TB
            UserController["UserController\n(controllers/user.go)"]:::frontend
            InvoiceController["InvoiceController\n(controllers/invoice.go)"]:::frontend
            SeedController["SeedController\n(controllers/seed.go)"]:::frontend
        end

        PaymentService["PaymentService\n(services/payment.go)"]:::service
        WebhookUtil["Webhook Utilities\n(utils/webhooks.go)"]:::service

        subgraph "Persistence"
            direction TB
            DBConfig["DB Connection\n(config/db.go)"]:::persistence
            UserModel["UserModel\n(models/user.go)"]:::persistence
            InvoiceModel["InvoiceModel\n(models/invoice.go)"]:::persistence
            SQLiteDB["SQLite DB\n(payments.db)"]:::persistence
        end
    end

    PaymentGateway(("Payment Gateway API")):::external_service
    WebhookEndpoint(("Webhook Endpoint")):::external_service

    Client --> Main
    Main --> Router
    Router --> Auth
    Auth --> UserController
    Auth --> InvoiceController
    Auth --> SeedController

    UserController --> UserModel
    UserController --> InvoiceModel

    InvoiceController --> InvoiceModel
    InvoiceController --> PaymentService

    PaymentService --> UserModel
    PaymentService --> InvoiceModel
    PaymentService --> WebhookUtil
    PaymentService --> DBConfig

    WebhookEndpoint --> WebhookUtil
    WebhookUtil --> PaymentService

    UserModel --> DBConfig
    InvoiceModel --> DBConfig
    DBConfig --> SQLiteDB

    click Main "https://github.com/sadja18/payment_flow_backend/blob/main/main.go"
    click DBConfig "https://github.com/sadja18/payment_flow_backend/blob/main/config/db.go"
    click Router "https://github.com/sadja18/payment_flow_backend/blob/main/routes/router.go"
    click Auth "https://github.com/sadja18/payment_flow_backend/blob/main/middleware/auth.go"
    click UserController "https://github.com/sadja18/payment_flow_backend/blob/main/controllers/user.go"
    click InvoiceController "https://github.com/sadja18/payment_flow_backend/blob/main/controllers/invoice.go"
    click SeedController "https://github.com/sadja18/payment_flow_backend/blob/main/controllers/seed.go"
    click PaymentService "https://github.com/sadja18/payment_flow_backend/blob/main/services/payment.go"
    click UserModel "https://github.com/sadja18/payment_flow_backend/blob/main/models/user.go"
    click InvoiceModel "https://github.com/sadja18/payment_flow_backend/blob/main/models/invoice.go"
    click SQLiteDB "https://github.com/sadja18/payment_flow_backend/blob/main/payments.db"
    click WebhookUtil "https://github.com/sadja18/payment_flow_backend/blob/main/utils/webhooks.go"

    classDef frontend fill:#ADD8E6,stroke:#000,stroke-width:1px
    classDef service fill:#98FB98,stroke:#000,stroke-width:1px
    classDef persistence fill:#FFA500,stroke:#000,stroke-width:1px
    classDef external fill:#D3D3D3,stroke-dasharray:5 5
    classDef external_service fill:#E0E0E0,stroke-dasharray:5 5
```
