Skip to content

Zackius/Wallet-MicroService

Repository files navigation

Wallet & Settlement Microservice

A comprehensive Spring Boot microservice for managing customer wallet balances and daily reconciliation with external providers. This system enables customers to maintain credit balances for consuming third-party services like CRB, credit scoring, and KYC document verification.

πŸ“‹ Table of Contents

✨ Features

Wallet Management

  • Balance Management: Track customer wallet balances with full transaction history
  • Top-up Operations: Secure balance increase functionality
  • Consumption Control: Balance deduction with insufficient funds protection
  • Transaction Ledger: Complete audit trail of all wallet operations
  • Idempotency: Protection against double deductions on retries

Settlement & Reconciliation

  • Daily Reconciliation: Automated comparison between internal and external transactions
  • Mismatch Detection: Identifies missing transactions and amount discrepancies
  • Report Generation: CSV export capabilities for reconciliation reports
  • External Provider Integration: Support for CSV/JSON transaction reports

Infrastructure

  • Message Queuing: RabbitMQ integration for asynchronous transaction processing
  • Database: PostgreSQL with JPA/Hibernate for data persistence
  • API Documentation: Swagger/OpenAPI integration

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Spring Boot   β”‚    β”‚   PostgreSQL    β”‚    β”‚   RabbitMQ      β”‚
β”‚   Application   │◄──►│   Database      β”‚    β”‚   Message       β”‚
β”‚   (Port 8065)   β”‚    β”‚   (Port 5432)   β”‚    β”‚   Broker        β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β”‚   (Port 5672)   β”‚
                                              β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Key Components:

  • Wallet Module: Balance management and transaction processing
  • Reconciliation Module: Daily settlement and mismatch detection
  • Ledger System: Complete transaction audit trail
  • Queue Processing: Asynchronous transaction handling

πŸ› οΈ Prerequisites

  • Docker & Docker Compose
  • Git
  • Java 17+ (for local development)
  • Gradle 7+ (for local development)

πŸš€ Quick Start with Docker

1. Clone and Setup

git clone git@github.com:Zackius/Wallet-MicroService.git
cd wallet-app
chmod +x setup.sh && ./setup.sh

2. Start All Services

docker-compose up --build

3. Access the Application

πŸ“š API Documentation

All API documentation is available through Swagger UI: http://localhost:8065/swagger-ui.html

Swagger provides:

  • Complete API Reference: All endpoints with detailed descriptions
  • Interactive Testing: Test APIs directly from the browser interface
  • Request/Response Schemas: Full data models with validation rules
  • Real-time Examples: Live request/response examples
  • Authentication Details: Security requirements for each endpoint

Key API Endpoints

  • Wallet Top-up: POST /wallets/{customerId}/topup
  • Balance Consumption: POST /wallets/{customerId}/consume
  • Balance Inquiry: GET /wallets/{customerId}/balance
  • Reconciliation Report: GET /api/v1/reconciliation/report
  • Export Report: GET /api/v1/reconciliation/export

πŸ’‘ Primary Documentation: Use Swagger UI for complete API documentation, testing, and examples. All endpoint details, schemas, and validation rules are documented there.

Application Profiles

  • local: For local development
  • docker: For containerized deployment
  • test: For unit testing

πŸ”§ Development Setup

Local Development (without Docker)

  1. Start Dependencies:

    # PostgreSQL
    docker run -d --name postgres -e POSTGRES_DB=wallet -e POSTGRES_USER=zackius -e POSTGRES_PASSWORD=zackius -p 5432:5432 postgres:15-alpine
    
    # RabbitMQ
    docker run -d --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:3-management-alpine
  2. Run Application:

    ./gradlew bootRun

Building the Application

# Build JAR
./gradlew clean build

# Run tests
./gradlew test

# Build Docker image
docker build -t wallet-app .

πŸ§ͺ Testing

Unit Tests

./gradlew test

Integration Tests

./gradlew integrationTest

API Testing Examples

For quick testing without Swagger UI, you can use these curl commands:

Test Top-up:

curl -X POST http://localhost:8065/wallets/CUST001/topup \
  -H "Content-Type: application/json" \
  -d '{"amount": 100.00, "description": "Initial top-up"}'

Test Consumption:

curl -X POST http://localhost:8065/wallets/CUST001/consume \
  -H "Content-Type: application/json" \
  -d '{"amount": 25.00, "serviceType": "KYC_VERIFICATION", "description": "KYC document verification"}'

Check Balance:

curl http://localhost:8065/wallets/CUST001/balance

πŸ—„οΈ Database Schema

Core Tables

  • customers: Customer information
  • collections: Top-up transactions
  • spending: Consumption transactions
  • reconciliation_reports: Daily reconciliation results

Key Features

  • ACID Transactions: Ensures data consistency
  • Audit Trail: Complete transaction history
  • Optimistic Locking: Prevents concurrent modification issues

πŸ”„ Message Queue Processing

Queue Configuration

  • Collection Queue: collection.ledger.request.v1
  • Spending Queue: spending.ledger.request.v1

Message Flow

  1. API receives transaction request
  2. Transaction validated and processed
  3. Message queued for asynchronous processing
  4. Ledger entries created
  5. Balance updated atomically

πŸ›‘οΈ Security Features

  • Input Validation: Comprehensive request validation
  • Transaction Integrity: ACID compliance for all operations
  • Idempotency Keys: Prevention of duplicate transactions
  • Balance Protection: Insufficient funds validation

πŸ“ Project Structure

wallet-app/
β”œβ”€β”€ src/main/java/com/example/wallet_app/
β”‚   β”œβ”€β”€ config/
β”‚   β”‚   β”œβ”€β”€ auth/WebSecurityConfiguration.java
β”‚   β”‚   β”œβ”€β”€ rabbitmq/RabbitMQConfig.java
β”‚   β”‚   └── swagger/SwaggerConfig.java
β”‚   β”œβ”€β”€ controllers/
β”‚   β”‚   β”œβ”€β”€ CustomerController.java
β”‚   β”‚   └── CollectionController.java
β”‚   β”œβ”€β”€ domain/
β”‚   β”‚   β”œβ”€β”€ customer/
β”‚   β”‚   └── wallet/
β”‚   β”œβ”€β”€ persistence/
β”‚   β”‚   β”œβ”€β”€ customer/
β”‚   β”‚   β”œβ”€β”€ collection/
β”‚   β”‚   └── spending/
β”‚   └── exceptions/
β”œβ”€β”€ src/main/resources/
β”‚   └── application.properties
β”œβ”€β”€ docker-compose.yml
β”œβ”€β”€ Dockerfile
└── README.md

πŸš€ Deployment

Production Deployment

  1. Update environment variables for production
  2. Configure external database and message broker
  3. Deploy using Docker Compose or Kubernetes

Scaling Considerations

  • Database: Configure connection pooling for high load
  • Message Processing: Scale worker instances for queue processing
  • Load Balancing: Use multiple application instances behind a load balancer

πŸ› Troubleshooting

Common Issues

Queue Declaration Failures:

# Restart with fresh volumes
docker-compose down -v
docker-compose up --build

Database Connection Issues:

  • Verify PostgreSQL is running and accessible
  • Check database credentials in environment variables

RabbitMQ Connection Issues:

  • Ensure RabbitMQ management plugin is enabled
  • Verify queue configuration in definitions.json

Logs

# View application logs
docker-compose logs wallet-app

# View all services logs
docker-compose logs

# Follow logs in real-time
docker-compose logs -f

🀝 Contributing

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

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.


Built with Spring Boot, PostgreSQL, RabbitMQ, Docker, and documented with Swagger πŸš€

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published