# malwarebo/gopay architecture

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

- Interactive diagram: https://gitdiagram.com/malwarebo/gopay
- Repository: https://github.com/malwarebo/gopay

## Overview

##### Main Components
- API Layer: Represent the entry point to the system. This layer (found in the “api” directory) contains HTTP handlers – for payments, subscriptions, and disputes – that receive client requests.
- Service Layer: Illustrate the business logic layer (located in “services”). This is where core application processes reside, such as processing payments, managing subscriptions, and handling disputes.
- Repository/Data Access Layer: Include the “repositories” and “models” directories. Repositories encapsulate the data persistence logic, interacting with the database using the models.
- Payment Providers: Represent external integrations with Stripe and Xendit, as well as the “providers” directory that encapsulates provider-specific logic. Show how the service layer communicates with these external systems.
- Configuration & Cache: Display the configuration module (in “config”) that holds application settings (e.g., API keys, database credentials) and a Redis caching component (in “cache”) that can be used to store frequently accessed data to enhance performance.
- Infrastructure & Deployment: Optionally add components for Docker and docker-compose (files like Dockerfile, docker-compose.yml) to indicate how the application is containerized and deployed. Also reference the CI/CD workflows located in the “.github” directory.

##### Relationships and Interactions
- Service to Repository: Indicate data flow from services to repositories. This connection is where persistence operations (CRUD for payments, subscriptions, etc.) occur.
- Service/Repository to Database: Illustrate a connection from repositories to the Database, denoting the data storage and retrieval process.
- Configuration: Indicate that all components depend on a central configuration module for settings (database credentials, API keys, etc.).
- Deployment: Optionally represent Docker as an outer container or system boundary indicating that all these components are containerized and orchestrated via Docker Compose.

##### Architectural Patterns & Design Principles
- Dependency Inversion: Note that the service layer depends on abstractions (e.g., through interfaces for the repository and provider packages) to allow for flexibility and ease of testing.
- Configuration Management: Show that configuration is centralized and injected into components at startup.

##### Relevant Technologies & Frameworks
- Language: Go (Golang).
- Database: PostgreSQL (as seen in the README instructions and “db/schema.sql”).
- Cache: Redis (from “cache/redis.go”).
- Payment Providers: Stripe and Xendit (in “providers” folder).
- Containerization: Docker and Docker Compose for deployment.
- CI/CD: GitHub Actions workflows (as seen in the “.github/workflows” directory).

##### Final Assembly
- Follow the flow from the API to the underlying service layer.
- Split service interactions into data handling (via repositories and database) and external communication (via provider modules).
- Surround core components with ancillary functionalities like caching, configuration, and containerization.

## Diagram (Mermaid)

```mermaid
graph TD
    Client["Client/User"]:::client

    subgraph "API Layer"
        Payment_API["Payment API Handler"]:::api
        Subscription_API["Subscription API Handler"]:::api
        Dispute_API["Dispute API Handler"]:::api
    end

    subgraph "Service Layer"
        Payment_Service["Payment Service"]:::service
        Subscription_Service["Subscription Service"]:::service
        Dispute_Service["Dispute Service"]:::service
    end

    subgraph "Repository/Data Access Layer"
        Payment_Repo["Payment Repository"]:::repository
        Subscription_Repo["Subscription Repository"]:::repository
        Dispute_Repo["Dispute Repository"]:::repository
        subgraph "Data Models"
            Payment_Model["Payment Model"]:::repository
            Subscription_Model["Subscription Model"]:::repository
            Dispute_Model["Dispute Model"]:::repository
        end
    end

    Database["PostgreSQL Database"]:::database

    subgraph "Payment Providers"
        Provider["Provider Abstraction"]:::provider
        Stripe_Prov["Stripe Provider"]:::provider
        Xendit_Prov["Xendit Provider"]:::provider
    end

    Config["Configuration Module"]:::config
    Redis["Redis Cache"]:::cache

    subgraph "Infrastructure & Deployment"
        Dockerfile["Dockerfile"]:::deployment
        DockerCompose["docker-compose.yml"]:::deployment
        CICD["CI/CD Workflows"]:::deployment
    end

    %% Client to API Layer
    Client -->|"HTTP_Request"| Payment_API
    Client -->|"HTTP_Request"| Subscription_API
    Client -->|"HTTP_Request"| Dispute_API

    %% API Layer to Service Layer
    Payment_API -->|"calls"| Payment_Service
    Subscription_API -->|"calls"| Subscription_Service
    Dispute_API -->|"calls"| Dispute_Service

    %% Service Layer to Repository Layer (for data persistence)
    Payment_Service -->|"persist"| Payment_Repo
    Subscription_Service -->|"persist"| Subscription_Repo
    Dispute_Service -->|"persist"| Dispute_Repo

    %% Repository uses Data Models
    Payment_Repo -->|"uses"| Payment_Model
    Subscription_Repo -->|"uses"| Subscription_Model
    Dispute_Repo -->|"uses"| Dispute_Model

    %% Repository to Database
    Payment_Repo -->|"CRUD"| Database
    Subscription_Repo -->|"CRUD"| Database
    Dispute_Repo -->|"CRUD"| Database

    %% Service Layer to Configuration Module
    Payment_Service -->|"uses"| Config
    Subscription_Service -->|"uses"| Config
    Dispute_Service -->|"uses"| Config

    %% Payment Service interacts with Payment Providers
    Payment_Service -->|"calls"| Provider
    Provider -->|"routes_to"| Stripe_Prov
    Provider -->|"routes_to"| Xendit_Prov

    %% Service Layer to Cache
    Payment_Service -->|"caches"| Redis
    Subscription_Service -->|"caches"| Redis
    Dispute_Service -->|"caches"| Redis

    %% Deployment Chain
    Config -->|"containerized_in"| Dockerfile
    Dockerfile -->|"orchestrated_by"| DockerCompose
    DockerCompose -->|"integrates"| CICD

    %% Class Definitions
    classDef client fill:#F0E68C,stroke:#B8860B,stroke-width:2px;
    classDef api fill:#AED6F1,stroke:#1B4F72,stroke-width:2px;
    classDef service fill:#ABEBC6,stroke:#1D8348,stroke-width:2px;
    classDef repository fill:#F9E79F,stroke:#B9770E,stroke-width:2px;
    classDef database fill:#F5CBA7,stroke:#873600,stroke-width:2px;
    classDef provider fill:#D6EAF8,stroke:#154360,stroke-width:2px;
    classDef config fill:#D7BDE2,stroke:#6C3483,stroke-width:2px;
    classDef cache fill:#FADBD8,stroke:#C0392B,stroke-width:2px;
    classDef deployment fill:#E8DAEF,stroke:#512E5F,stroke-width:2px;

    %% Click Events for API Layer
    click Payment_API "https://github.com/malwarebo/gopay/blob/master/api/payment_handler.go"
    click Subscription_API "https://github.com/malwarebo/gopay/blob/master/api/subscription_handler.go"
    click Dispute_API "https://github.com/malwarebo/gopay/blob/master/api/dispute_handler.go"

    %% Click Events for Service Layer
    click Payment_Service "https://github.com/malwarebo/gopay/blob/master/services/payment_service.go"
    click Subscription_Service "https://github.com/malwarebo/gopay/blob/master/services/subscription_service.go"
    click Dispute_Service "https://github.com/malwarebo/gopay/blob/master/services/dispute_service.go"

    %% Click Events for Repository/Data Access Layer
    click Payment_Repo "https://github.com/malwarebo/gopay/blob/master/repositories/payment_repository.go"
    click Subscription_Repo "https://github.com/malwarebo/gopay/blob/master/repositories/subscription_repository.go"
    click Dispute_Repo "https://github.com/malwarebo/gopay/blob/master/repositories/dispute_repository.go"
    click Payment_Model "https://github.com/malwarebo/gopay/blob/master/models/payment.go"
    click Subscription_Model "https://github.com/malwarebo/gopay/blob/master/models/subscription.go"
    click Dispute_Model "https://github.com/malwarebo/gopay/blob/master/models/dispute.go"

    %% Click Event for Database
    click Database "https://github.com/malwarebo/gopay/blob/master/db/db.go"

    %% Click Events for Payment Providers
    click Stripe_Prov "https://github.com/malwarebo/gopay/blob/master/providers/stripe.go"
    click Xendit_Prov "https://github.com/malwarebo/gopay/blob/master/providers/xendit.go"
    click Provider "https://github.com/malwarebo/gopay/blob/master/providers/provider.go"

    %% Click Event for Configuration
    click Config "https://github.com/malwarebo/gopay/blob/master/config/config.go"

    %% Click Event for Cache
    click Redis "https://github.com/malwarebo/gopay/blob/master/cache/redis.go"

    %% Click Events for Deployment & Infrastructure
    click Dockerfile "https://github.com/malwarebo/gopay/tree/master/Dockerfile"
    click DockerCompose "https://github.com/malwarebo/gopay/blob/master/docker-compose.yml"
    click CICD "https://github.com/malwarebo/gopay/blob/master/.github/workflows/go-build.yml"
```
