Skip to content

CurrencyExchangeService ​

File: src/Services/CurrencyExchangeService.php
Facade: Unusualify\Modularous\Facades\CurrencyExchange

Fetches live currency exchange rates from a configurable external API, caches the result for one hour, and provides amount conversion helpers.

Configuration ​

All options live under config/modularous.php → services.currency_exchange:

php
'services' => [
    'currency_exchange' => [
        'endpoint'      => 'https://api.freecurrencyapi.com/v1/latest',
        'api_key'       => env('CURRENCY_EXCHANGE_API_KEY'),
        'base_currency' => 'EUR',

        // Map of service property names → query parameter names
        'parameters' => [
            'apiKey'       => 'apikey',
            'baseCurrency' => 'base_currency',
        ],

        // JSON key in the API response that holds the rates object
        'rates_key' => 'data',
    ],
],

Key Methods ​

MethodSignatureDescription
fetchExchangeRatesfetchExchangeRates(): arrayFetch rates from the API and cache for 1 hour. Returns [currency => rate]. Throws on API failure.
convertToconvertTo(float $amount, string $currency, int $decimals, string $round): floatConvert $amount from the base currency to $currency.
getExchangeRategetExchangeRate(string $currency, int $decimals, string $round): floatReturn the exchange rate for a single currency.

Rounding ​

The $round parameter on convertTo() and getExchangeRate() controls how the result is rounded:

ValueBehaviour
'round' (default)round($value, $decimals)
'ceil'ceil($value)
'floor'floor($value)

Cache ​

Rates are cached using the key exchange_rates for 1 hour via Cache::remember. To force a refresh, clear this key:

bash
php artisan cache:forget exchange_rates

Facade Usage ​

php
use Unusualify\Modularous\Facades\CurrencyExchange;

// Convert 100 EUR to USD
$usd = CurrencyExchange::convertTo(100, 'USD');

// Get the raw rate for TRY
$rate = CurrencyExchange::getExchangeRate('TRY', decimals: 4);

// Convert, ceiling the result
$price = CurrencyExchange::convertTo($euroPrice, 'GBP', decimals: 2, round: 'ceil');

Currency Providers ​

The CurrencyExchangeService is supplemented by currency provider classes used in the pricing module:

ClassDescription
Currency/SystemPricingCurrencyProviderFetches the configured currency from the system pricing module
Currency/NullCurrencyProviderNo-op provider returned when no pricing module is active

The active provider is resolved via modularous.currency_provider config key.