Skip to content

About

Laravel 11 + Sanctum transactions API with concurrency handling and tests

Resources

Stars

0 stars

Watchers

1 watching

Forks

Repository files navigation

Transaction Processing System

A secure and scalable transaction processing system built with Laravel, featuring concurrent operation handling, data integrity protection, and comprehensive testing.

Features

  • RESTful API for transaction processing
  • Real-time balance tracking
  • Concurrent transaction handling
  • Data integrity protection
  • Comprehensive testing suite
  • API Authentication
  • Transaction history and filtering
  • Balance monitoring

Requirements

  • PHP 8.1 or higher
  • Composer
  • SQLite (for testing)
  • Laravel 10.x

Installation

  1. Clone the repository:
git clone https://github.com/brian-chebon/transaction-processing-system.git
cd transaction-processing-system
  1. Install dependencies:
composer install
  1. Set up environment:
cp .env.example .env
php artisan key:generate
  1. Configure database in .env:
DB_CONNECTION=sqlite
DB_DATABASE=database/database.sqlite
  1. Run migrations:
touch database/database.sqlite
php artisan migrate
  1. Seed the database:
php artisan db:seed
  1. Install Sanctum:
php artisan sanctum:install

Running Tests

Run the comprehensive test suite:

php artisan test

Run specific test categories:

php artisan test --testsuite=Feature
php artisan test --testsuite=Unit

API Documentation

Authentication

All API endpoints require authentication using Laravel Sanctum. Include the bearer token in your request headers:

Authorization: Bearer <your-token>

Endpoints

Transactions

  • Create Transaction

    POST /api/v1/transactions
    

    Body:

    {
        "amount": 100.0,
        "type": "credit",
        "description": "Optional description"
    }
  • Get Transaction History

    GET /api/v1/transactions
    

    Query Parameters:

    • type: credit/debit
    • date_from: YYYY-MM-DD
    • date_to: YYYY-MM-DD

Balance

  • Get Current Balance

    GET /api/v1/balance
    
  • Get Detailed Balance

    GET /api/v1/balance/details
    

Error Handling

The API returns appropriate HTTP status codes and JSON responses:

{
    "status": "error",
    "message": "Error description",
    "errors": {
        "field": ["Error details"]
    }
}

Project Structure

transaction-system/
├── app/
│   ├── Exceptions/
│   │   ├── AccountNotFoundException.php
│   │   ├── InsufficientFundsException.php
│   │   ├── InvalidTransactionException.php
│   │   └── Handler.php
│   ├── Http/
│   │   ├── Controllers/
│   │   ├── Middleware/
│   │   └── Requests/
│   ├── Models/
│   ├── Providers/
│   ├── Repositories/
│   └── Services/
├── database/
│   ├── factories/
│   ├── migrations/
│   └── seeders/
└── tests/
    ├── Feature/
    └── Unit/

Architecture

Key Components

  • Controllers: Handle HTTP requests and responses
  • Services: Implement business logic
  • Repositories: Manage data access
  • Models: Define database structure and relationships
  • Middleware: Handle authentication and request processing

Concurrency Handling

  • Database transactions for atomicity
  • Pessimistic locking for balance updates
  • Idempotency support via transaction references

Security Features

  • API Authentication using Sanctum
  • Request validation
  • SQL injection protection
  • Rate limiting
  • Safe balance calculations

Monitoring

Logging Channels

Configure logging channels in config/logging.php:

'channels' => [
    'transactions' => [
        'driver' => 'daily',
        'path' => storage_path('logs/transactions.log'),
        'level' => env('LOG_LEVEL', 'debug'),
        'days' => 14,
    ],
    'security' => [
        'driver' => 'daily',
        'path' => storage_path('logs/security.log'),
        'level' => env('LOG_LEVEL', 'debug'),
        'days' => 30,
    ],
    'metrics' => [
        'driver' => 'daily',
        'path' => storage_path('logs/metrics.log'),
        'level' => env('LOG_LEVEL', 'debug'),
        'days' => 7,
    ],
],

View Logs

# View transaction logs
tail -f storage/logs/transactions.log

# View security logs
tail -f storage/logs/security.log

# View metrics logs
tail -f storage/logs/metrics.log

Scaling Considerations

Current Implementation

  • Database transaction isolation
  • Efficient indexing
  • Cache implementation
  • Request validation

Future Improvements

  1. High Availability

    • Load balancing
    • Database replication
    • Cache clustering
  2. Performance

    • Queue implementation for async processing
    • Read replicas for balance queries
    • Horizontal scaling
  3. Monitoring

    • Transaction logging
    • Performance metrics
    • Error tracking

Troubleshooting

Common commands for troubleshooting:

# Clear cache
php artisan cache:clear

# Clear config
php artisan config:clear

# Rebuild autoload files
composer dump-autoload

# Reset database
php artisan migrate:fresh --seed

Development Commands

Creating new components:

# Create controller
php artisan make:controller TransactionController

# Create model
php artisan make:model Transaction -mf

# Create test
php artisan make:test TransactionTest

# Create seeder
php artisan make:seeder TransactionSeeder

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Commit your changes
  4. Push to the branch
  5. Create a Pull Request

About

Laravel 11 + Sanctum transactions API with concurrency handling and tests

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages