Blog Detail

07

Mar
Easily Check Exchange Rates for Any Currency in Laravel cover image

arrow_back Easily Check Exchange Rates for Any Currency in Laravel

If your app supports multi-currency, you’ll no doubt need to check exchange rates. There are many third-party services to accomplish this, but why bother reinventing the wheel when we’ve done all the hard work for you?

So Exchange is a package by
Worksome that provides an abstraction layer for exchange rate APIs, with a full suite of tools for caching, testing & local development.

Installation

You can install the package via composer.

composer require worksome/exchange

To install the exchange config file, you can use our install artisan command!

php artisan exchange:install

Exchange is now installed!

Usage

Exchange ships with a number of useful drivers for retrieving exchange rates. The default is exchange_rate, which is a free service, but you’re welcome to change that to suit your app’s requirements.

The driver can be set using the EXCHANGE_DRIVER environment variable. Supported values are null, fixer, exchange_rate, and cache. Let’s take a look at each of the options available.

Null

You can start using Exchange locally with the null driver. This will simply return 1.0 for every exchange rate, which is generally fine for local development.

use Worksome\Exchange\Facades\Exchange;

$exchangeRates = Exchange::rates('USD', ['GBP', 'EUR']);

In the example above, we are retrieving exchange rates for GBP and EUR based on USD. The rates method will return a Worksome\Exchange\Support\Rates object, which includes the base currency, retrieved rates, and the time of retrieval. Retrieved rates are an array with currency codes as keys and exchange rates as values.

$rates = $exchangeRates->getRates(); // ['GBP' => 1.0, 'EUR' => 1.0]

Fixer

Of course, the null driver isn’t very useful when you want actual exchange rates. For this, you should use the fixer driver.

In your exchange.php config file, set default to fixer, or set EXCHANGE_DRIVER to fixer in your .env file. Next, you’ll need an access key from https://fixer.io/dashboard. Set FIXER_ACCESS_KEY to your provided access key from Fixer.

That’s it! Fixer is now configured as the default driver and running Exchange::rates() again will make a request to Fixer for up-to-date exchange rates.

ExchangeRate.host

exchangerate.host is a free alternative to Fixer with an identical API spec. You don’t even need an API key!

In your exchange.php config file, set default to exchange_rate, or set EXCHANGE_DRIVER to exchange_rate in your .env file.

With that task completed, you’re ready to start using exchangerate.host for retrieving up-to-date exchange rates.

Cache

It’s unlikely that you want to make a request to a third-party service every time you call Exchange::rates(). To remedy this, we provide a cache decorator that can be used to store retrieved exchange rates for a specified period (24 hours by default).

In your exchange.php config file, set default to cache, or set EXCHANGE_DRIVER to cache in your .env file. You’ll also want to pick a strategy under services.cache.strategy. By default, this will be fixer, but you are free to change that. The strategy is the service that will be used to perform the exchange rate lookup when nothing is found in the cache.

There is also the option to override the ttl (how many seconds rates are cached for) and key for your cached rates.

Artisan

We provide an Artisan command for you to check Exchange is working correctly in your project.

php artisan exchange:rates USD GBP EUR

In the example above, exchange rates will be retrieved and displayed in the console from a base of USD to GBP and EUR respectively. You can add as many currencies as you’d like to the command.

For more details, you can visit its complete documentation and source code on Github

Published at : 07-03-2022

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project