Skip to content

ddias00/Wallet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Digital Wallet API

A complete REST API for a Digital Wallet platform built with Golang, following Clean Architecture principles.

πŸš€ Features

User Management

  • User registration with CPF, email, phone validation
  • JWT-based authentication with refresh tokens
  • Account blocking/unblocking

Wallet Operations

  • Get wallet balance
  • Credit wallet
  • Debit wallet

Transactions

  • Transfer money between users
  • Transaction history with pagination
  • Transaction status tracking (pending, confirmed, cancelled)

Payments

  • Generate Boleto
  • PIX payment support
  • QR Code payment generation
  • Payment processing

Notifications

  • Transaction notifications (console-based, ready for email/SMS integration)

πŸ› οΈ Tech Stack

  • Language: Go 1.21
  • Framework: Gin
  • Database: PostgreSQL
  • ORM: GORM
  • Authentication: JWT with refresh tokens
  • Password Hashing: bcrypt
  • API Documentation: Swagger/OpenAPI
  • Containerization: Docker & Docker Compose
  • Migrations: golang-migrate compatible

πŸ“ Project Structure

Wallet/
β”œβ”€β”€ cmd/
β”‚   └── api/
β”‚       └── main.go              # Application entry point
β”œβ”€β”€ internal/
β”‚   β”œβ”€β”€ config/
β”‚   β”‚   └── config.go            # Configuration management
β”‚   β”œβ”€β”€ domain/                  # Domain models/entities
β”‚   β”‚   β”œβ”€β”€ user.go
β”‚   β”‚   β”œβ”€β”€ wallet.go
β”‚   β”‚   β”œβ”€β”€ transaction.go
β”‚   β”‚   └── payment.go
β”‚   β”œβ”€β”€ handler/                 # HTTP handlers
β”‚   β”‚   β”œβ”€β”€ auth_handler.go
β”‚   β”‚   β”œβ”€β”€ user_handler.go
β”‚   β”‚   β”œβ”€β”€ wallet_handler.go
β”‚   β”‚   β”œβ”€β”€ transaction_handler.go
β”‚   β”‚   └── payment_handler.go
β”‚   β”œβ”€β”€ service/                 # Business logic
β”‚   β”‚   β”œβ”€β”€ auth_service.go
β”‚   β”‚   β”œβ”€β”€ user_service.go
β”‚   β”‚   β”œβ”€β”€ wallet_service.go
β”‚   β”‚   β”œβ”€β”€ transaction_service.go
β”‚   β”‚   β”œβ”€β”€ payment_service.go
β”‚   β”‚   └── notification_service.go
β”‚   β”œβ”€β”€ repository/              # Data access layer
β”‚   β”‚   β”œβ”€β”€ user_repository.go
β”‚   β”‚   β”œβ”€β”€ wallet_repository.go
β”‚   β”‚   β”œβ”€β”€ transaction_repository.go
β”‚   β”‚   β”œβ”€β”€ payment_repository.go
β”‚   β”‚   └── refresh_token_repository.go
β”‚   β”œβ”€β”€ middleware/              # HTTP middleware
β”‚   β”‚   β”œβ”€β”€ auth.go
β”‚   β”‚   └── logging.go
β”‚   └── utils/
β”‚       └── response.go          # Response helpers
β”œβ”€β”€ pkg/
β”‚   β”œβ”€β”€ jwt/
β”‚   β”‚   └── jwt.go               # JWT utilities
β”‚   └── validator/
β”‚       └── validator.go         # CPF, email, phone validators
β”œβ”€β”€ migrations/                  # Database migrations
β”‚   β”œβ”€β”€ 000001_init_schema.up.sql
β”‚   └── 000001_init_schema.down.sql
β”œβ”€β”€ docs/                        # Swagger documentation (generated)
β”œβ”€β”€ docker-compose.yml
β”œβ”€β”€ Dockerfile
β”œβ”€β”€ Makefile
β”œβ”€β”€ go.mod
β”œβ”€β”€ go.sum
β”œβ”€β”€ .env.example
β”œβ”€β”€ .gitignore
└── README.md

πŸ”§ Prerequisites

  • Go 1.21 or higher
  • Docker and Docker Compose
  • PostgreSQL 15 (if running locally without Docker)
  • golang-migrate (optional, for manual migrations)

πŸš€ Quick Start

1. Clone the repository

git clone <repository-url>
cd Wallet

2. Set up environment variables

cp .env.example .env

Edit .env with your configuration if needed.

3. Run with Docker Compose (Recommended)

docker-compose up -d

This will start:

  • PostgreSQL database on port 5432
  • API server on port 8080

4. Access the API

  • API Base URL: http://localhost:8080
  • Swagger Documentation: http://localhost:8080/swagger/index.html
  • Health Check: http://localhost:8080/health

πŸ”¨ Development Setup

Install dependencies

go mod download

Run PostgreSQL locally

docker run --name wallet_postgres \
  -e POSTGRES_USER=wallet_user \
  -e POSTGRES_PASSWORD=wallet_pass \
  -e POSTGRES_DB=wallet_db \
  -p 5432:5432 \
  -d postgres:15-alpine

Run migrations (optional - auto-migrated on startup)

make migrate-up

Run the application

make run
# or
go run cmd/api/main.go

Generate Swagger documentation

make swagger

Run tests

make test

πŸ“š API Documentation

Authentication

All protected endpoints require a Bearer token in the Authorization header:

Authorization: Bearer <access_token>

API Endpoints

Auth

  • POST /api/v1/auth/register - Register new user
  • POST /api/v1/auth/login - Login user
  • POST /api/v1/auth/refresh - Refresh access token
  • GET /api/v1/auth/me - Get current user profile (protected)

Users

  • GET /api/v1/users/:id - Get user by ID (protected)
  • POST /api/v1/users/block - Block user account (protected)
  • POST /api/v1/users/unblock - Unblock user account (protected)

Wallet

  • GET /api/v1/wallet/balance - Get wallet balance (protected)
  • POST /api/v1/wallet/credit - Credit wallet (protected)
  • POST /api/v1/wallet/debit - Debit wallet (protected)

Transactions

  • POST /api/v1/transactions/transfer - Transfer money (protected)
  • GET /api/v1/transactions/history - Get transaction history (protected)
  • GET /api/v1/transactions/:id - Get transaction by ID (protected)

Payments

  • POST /api/v1/payments/boleto - Generate boleto (protected)
  • POST /api/v1/payments/pix - Generate PIX payment (protected)
  • POST /api/v1/payments/qrcode - Generate QR code payment (protected)
  • POST /api/v1/payments/process - Process payment (public - webhook)

Example Requests

Register User

curl -X POST http://localhost:8080/api/v1/auth/register \
  -H "Content-Type: application/json" \
  -d '{
    "name": "John Doe",
    "cpf": "12345678909",
    "email": "john@example.com",
    "phone": "11987654321",
    "password": "password123"
  }'

Login

curl -X POST http://localhost:8080/api/v1/auth/login \
  -H "Content-Type: application/json" \
  -d '{
    "email": "john@example.com",
    "password": "password123"
  }'

Get Balance

curl -X GET http://localhost:8080/api/v1/wallet/balance \
  -H "Authorization: Bearer <access_token>"

Transfer Money

curl -X POST http://localhost:8080/api/v1/transactions/transfer \
  -H "Authorization: Bearer <access_token>" \
  -H "Content-Type: application/json" \
  -d '{
    "to_user_id": "uuid-here",
    "amount": 100.50,
    "description": "Payment for services"
  }'

πŸ§ͺ Testing

Run all tests:

make test

Run tests with coverage:

go test -v -cover ./...

🐳 Docker Commands

Build and start containers:

make docker-up

Stop containers:

make docker-down

View logs:

docker-compose logs -f api

πŸ“ Environment Variables

Variable Description Default
APP_ENV Application environment development
APP_PORT Server port 8080
DB_HOST Database host localhost
DB_PORT Database port 5432
DB_USER Database user wallet_user
DB_PASSWORD Database password wallet_pass
DB_NAME Database name wallet_db
DB_SSLMODE Database SSL mode disable
JWT_SECRET JWT secret key your-secret-key
JWT_ACCESS_EXPIRY Access token expiry 15m
JWT_REFRESH_EXPIRY Refresh token expiry 7d
BCRYPT_COST Bcrypt hashing cost 10

πŸ—οΈ Architecture

This project follows Clean Architecture principles:

  • Domain Layer: Core business entities and rules
  • Repository Layer: Data access abstraction
  • Service Layer: Business logic implementation
  • Handler Layer: HTTP request/response handling
  • Middleware: Cross-cutting concerns (auth, logging)

Key Design Patterns

  • Repository Pattern
  • Dependency Injection
  • Interface-based design
  • Separation of Concerns

πŸ”’ Security Features

  • Password hashing with bcrypt
  • JWT-based authentication
  • Refresh token rotation
  • CPF, email, and phone validation
  • Account blocking mechanism
  • SQL injection prevention (GORM)

🚧 Future Enhancements

  • Email/SMS notification integration
  • Rate limiting
  • API key authentication for webhooks
  • Transaction rollback mechanism
  • Audit logging
  • Multi-currency support
  • KYC integration
  • 2FA authentication

πŸ“„ License

MIT License

πŸ‘₯ Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“ž Support

For support, email support@wallet.com or open an issue in the repository.


Built with ❀️ using Go and Clean Architecture

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors