The CurrencyExchange
package provides a simple and efficient way to retrieve currency exchange rates and perform currency conversions in your Laravel applications. Integrates with the ExchangeRate API for reliable and up-to-date data.
- Retrieve real-time exchange rates for any support currency.
- Perform currency conversions between two currencies.
- Easily configurable with a default base currency and API key.
- Includes a frontend UI for fetching rates and performing conversions.
- Lightweight and extensible, suitable for any Laravel application.
composer require mattyeend/currency-exchange
Publish the package configuration file to customise settings like the API key and base currency:
php artisan vendor:publish --tag=config --provider="MattYeend\CurrencyExchange\CurrencyExchangeServiceProvider"
This will create a currencyexchange.php
configuration file in your config
directory.
Add your ExchangeRate API key to your .env
file:
EXCHANGERATE_API_KEY=your_api_key
You can fetch exchange rates programmatically using the CurrencyExchangeService
:
use MattYeend\CurrencyExchange\Services\CurrencyExchangeService;
$service = app(CurrencyExchangeService::class);
$rates = $service->getRates('GBP'); // Replace 'GBP' with your base currency
dd($rates); // Outputs an array of exchange rates
Perform a currency conversion between two currencies:
$convertedAmount = $service->convert(100, 'GBP', 'EUR');
echo "Converted Amount: $convertedAmount";
The package includes predefined routes for fetching exchange rates and performing conversions:
- View Exchange Rates:
GET
/currency-exchange
Displays a basic UI for fetching and displaying rates. - Get Rates (API);
GET
/currency-exchange/rates
Returns exchange rates for a specific base currency. - Convert Currency (API):
POST
/currency-exchange/convert
Acceptsamount
,from
, andto
parameters and returns the converted amount.
A basic UI is included for quick usage. Visit /currency-exchange
to view rates and use the conversion form.
To customise the view, edit the Blade template at:
resources/views/vendor/currencyexchange/index.blade.php
The package includes unit and feature tests. Run the tests using PHPUnit:
php artisan test
The following configuration options are available in config/currencyexchange.php
:
return [
'api_key' -> env('EXCHANGERATE_API_KEY', 'your_api_key'),
'base_currency' => 'GBP'
];
This package is open-source software licensed under the MIT license.
Feel free to fork the repository and submit pull requests for improvements or new features!