A Laravel-based currency exchange API that handles currency conversions and order management.
- PHP >= 8.1
- MySQL >= 5.7
- Composer
- Node.js & NPM (for frontend assets)
- Currency listing and details
- Currency conversion (USD to foreign currency and vice versa)
- Order management with surcharge and discount calculations
- RESTful API endpoints
- Clone the repository:
git clone https://github.com/tzone85/mukuru-api.git
cd mukuru-api- Install PHP dependencies:
composer install- Environment Setup:
cp .env.example .env
php artisan key:generate- Configure your
.envfile with your database settings:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=mukurudb
DB_USERNAME=root
DB_PASSWORD=- Create the database:
mysql -u root -e "CREATE DATABASE mukurudb"- Run migrations and seed the database:
php artisan migrate --seedStart the development server:
php artisan serveThe API will be available at http://127.0.0.1:8000
The API documentation is available through Scribe, which provides detailed information about all endpoints, request/response formats, and example usage.
- Generate the documentation (if not already generated):
php artisan scribe:generate- Visit the documentation at:
http://localhost:8000/docs
The documentation includes:
- Detailed API endpoint descriptions
- Request/response examples
- Interactive API testing interface
- Authentication details
- OpenAPI/Swagger specification
- Postman collection
You can also find the following files:
- OpenAPI specification:
public/docs/openapi.yaml - Postman collection:
public/docs/collection.json
All API endpoints are versioned and prefixed with /api/v1.
GET /api/v1/currencies- List all currenciesGET /api/v1/currencies/{id}- Get specific currency detailsPOST /api/v1/currencies/get-foreign-currency-amount- Convert USD to foreign currencyPOST /api/v1/currencies/get-total-amount- Convert foreign currency to USD
GET /api/v1/orders- List all ordersPOST /api/v1/orders- Create a new currency exchange order
# Request
curl -X GET http://127.0.0.1:8000/api/v1/currencies
# Response
{
"data": [
{
"id": 1,
"code": "ZAR",
"name": "South African Rand",
"symbol": "R",
"rate": 18.65,
"surcharge_percentage": 7.5,
"discount_percentage": 0,
"created_at": "2024-12-12T14:20:53.000000Z",
"updated_at": "2024-12-12T14:20:53.000000Z"
}
// ... other currencies
]
}# Request
curl -X GET http://127.0.0.1:8000/api/v1/currencies/1
# Response
{
"data": {
"id": 1,
"code": "ZAR",
"name": "South African Rand",
"symbol": "R",
"rate": 18.65,
"surcharge_percentage": 7.5,
"discount_percentage": 0,
"created_at": "2024-12-12T14:20:53.000000Z",
"updated_at": "2024-12-12T14:20:53.000000Z"
}
}# Request
curl -X POST http://127.0.0.1:8000/api/v1/currencies/get-foreign-currency-amount \
-H "Content-Type: application/json" \
-d '{
"currency": "ZAR",
"amount": 100
}'
# Response
{
"data": {
"amount": 1865.00,
"exchange_rate": 18.65,
"surcharge_rate": 7.5
}
}# Request
curl -X POST http://127.0.0.1:8000/api/v1/currencies/get-total-amount \
-H "Content-Type: application/json" \
-d '{
"currency": "ZAR",
"amount": 1000
}'
# Response
{
"data": {
"amount": 53.62,
"exchange_rate": 18.65,
"surcharge_rate": 7.5
}
}# Request
curl -X POST http://127.0.0.1:8000/api/v1/orders \
-H "Content-Type: application/json" \
-d '{
"currency": "ZAR",
"foreign_currency_amount": 1000,
"total_amount": 53.62
}'
# Response
{
"data": {
"id": 1,
"currency": "ZAR",
"foreign_currency_amount": 1000,
"total_amount": 53.62,
"created_at": "2024-12-12T14:30:00.000000Z",
"updated_at": "2024-12-12T14:30:00.000000Z"
}
}- ZAR (South African Rand)
- GBP (British Pound)
- EUR (Euro)
- KES (Kenyan Shilling)
The project includes comprehensive unit and feature tests for all API endpoints. To run the tests:
- Set up the testing environment:
cp .env.example .env.testing- Configure your testing database in
.env.testing:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=mukurudb_testing
DB_USERNAME=root
DB_PASSWORD=- Create the testing database:
mysql -u root -e "CREATE DATABASE mukurudb_testing"- Run all tests:
php artisan testOr run specific test suites:
# Run only currency-related tests
php artisan test --filter=CurrencyTest
# Run only order-related tests
php artisan test --filter=OrderTestThe test suite covers:
- Currency listing and retrieval
- Currency details validation
- Non-existent currency handling
- USD to foreign currency conversion
- Foreign currency to USD conversion
- Exchange rate calculations
- Surcharge and discount applications
- Required fields validation
- Numeric input validation
- Positive amount validation
- Invalid currency handling
- 404 responses for non-existent resources
- 422 responses for validation errors
- Proper error message formatting
The test suite is automatically run on every push to the repository using GitHub Actions.
.
├── app/
│ ├── Http/Controllers/ # API Controllers
│ ├── Model/ # Database Models
│ ├── Repository/ # Repository Pattern Classes
│ └── Service/ # Business Logic Services
├── database/
│ ├── migrations/ # Database Migrations
│ └── seeds/ # Database Seeders
└── routes/
└── api.php # API Routes2024-12-12
This project is open-sourced software licensed under the MIT license.