Check Exchange Rates for any currency in Laravel.

Related tags

Laravel exchange
Overview

Exchange

Check exchange rates for any currency in Laravel

Tests PHPStan

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?

Exchange provides an abstraction layer for exchange rate APIs, with a full suite of tools for caching, testing and 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 you 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.

CleanShot 2022-02-23 at 13 10 55@2x

Testing

To help you write tests using Exchange, we provide a fake implementation via the Exchange::fake() method.

it('retrieves exchange rates', function () {
    Exchange::fake(['GBP' => 1.25, 'USD' => 1.105]);
    
    $this->get(route('my-app-route'))
        ->assertOk();
        
    Exchange::assertRetrievedRates();
});

The assertRetrievedRates method will cause your test to fail if no exchange rates were ever retrieved.

Internally, Exchange prides itself on a thorough test suite written in Pest, strict static analysis, and a very high level of code coverage. You may run these tests yourself by cloning the project and running our test script:

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Credits

License

The MIT License (MIT). Please see License File for more information.

Comments
Releases(v1.0.0)
  • v1.0.0(Mar 3, 2022)

    What's Changed

    • fix: FlatCurrencyCodeProvider to support all currencies by @sweptsquash in https://github.com/worksome/exchange/pull/5

    New Contributors

    • @sweptsquash made their first contribution in https://github.com/worksome/exchange/pull/5

    Full Changelog: https://github.com/worksome/exchange/compare/v0.2.0...v1.0.0

    Source code(tar.gz)
    Source code(zip)
Owner
Worksome
Building a future platform for flexible employment.
Worksome
Tiny Laravel worldwide currency formatter

Laravel Currency Formatter Tiny Laravel worldwide currency formatter Install composer require magarrent/laravel-currency-formatter Usage use Magarrent

Marc Garcia Torrent 17 Oct 5, 2022
A Laravel 8 Project Implement with GraphQL With Sanctum APIs Authentications Which utilized in Any Frontend or Any Mobile Application Programs.

A Laravel 8 Project Implement with GraphQL With Sanctum APIs Authentications Which utilized in Any Frontend or Any Mobile Application Programs.

Vikas Ukani 3 Jan 6, 2022
GeoLocation-Package - This package helps you to know the current language of the user, the country from which he is browsing, the currency of his country, and also whether he is using it vpn

GeoLocation in PHP (API) ?? ?? ?? This package helps you to know a lot of information about the current user by his ip address ?? ?? ?? This package h

Abdullah Karam 4 Dec 8, 2022
Check the health of your Laravel app

Check the health of your Laravel app This repo can be used to scaffold a Laravel package. Follow these steps to get started: Press the "Use template"

Spatie 512 Jan 4, 2023
.env vars check for Spatie's Laravel Health

Custom check for Spatie's Laravel Health - Ensure every .env variable you need has a value

Encodia 4 Nov 7, 2022
A simple validator package to check if the given zipcode has a valid Dutch zipcode format

Laravel Dutch Zipcode Validator A simple validator package to check if the given zipcode has a valid Dutch zipcode format Table of Contents Installati

Tim Wassenburg 0 May 30, 2022
Library that offers Input Filtering based on Annotations for use with Objects. Check out 2.dev for 2.0 pre-release.

DMS Filter Component This library provides a service that can be used to filter object values based on annotations Install Use composer to add DMS\Fil

Rafael Dohms 89 Nov 28, 2022
A Laravel package that adds a simple image functionality to any Laravel model

Laraimage A Laravel package that adds a simple image functionality to any Laravel model Introduction Laraimage served four use cases when using images

Hussein Feras 52 Jul 17, 2022
Laravel 2-Step Verification is a package to add 2-Step user authentication to any Laravel project easily.

Laravel 2-Step verification is a package to add 2-Step user authentication to any Laravel project easily. It is configurable and customizable. It uses notifications to send the user an email with a 4-digit verification code. Laravel 2-Step Authentication Verification for Laravel. Can be used in out the box with Laravel's authentication scaffolding or integrated into other projects.

Jeremy Kenedy 204 Dec 23, 2022
A Laravel Wrapper for the CoinDCX API. Now easily connect and consume the CoinDCX Public API in your Laravel apps without any hassle.

This package provides a Laravel Wrapper for the CoinDCX API and allows you to easily communicate with it. Important Note This package is in early deve

Moinuddin S. Khaja 2 Feb 16, 2022
Laravel comments - This package enables to easily associate comments to any Eloquent model in your Laravel application

Laravel comments - This package enables to easily associate comments to any Eloquent model in your Laravel application

Rubik 4 May 12, 2022
Easily create a revision history for any laravel model

Wouldn't it be nice to have a revision history for any model in your project, without having to do any work for it. By simply adding the RevisionableT

Venture Craft 2.4k Jan 6, 2023
cybercog 996 Dec 28, 2022
Make any class queue aware in a Laravel app

Make any class queue aware This is where your description should go. Limit it to a paragraph or two. Consider adding a small example. Support us We in

Spatie 8 Jun 17, 2022
Add settings to any Laravel model.

Laravel Property Bag Simple settings for Laravel apps. Easily give multiple resources settings Simple to add additional settings as your app grows Set

Zach Leigh 80 Aug 8, 2022
🖖Repository Pattern in Laravel. The package allows to filter by request out-of-the-box, as well as to integrate customized criteria and any kind of filters.

Repository Repository Pattern in Laravel. The package allows to filter by request out-of-the-box, as well as to integrate customized criteria and any

Awes.io 160 Dec 26, 2022
A package to handle the SEO in any Laravel application, big or small.

Never worry about SEO in Laravel again! Currently there aren't that many SEO-packages for Laravel and the available ones are quite complex to set up a

Ralph J. Smit 267 Jan 2, 2023
Turn any Eloquent model into a list!

Listify Turn any Eloquent model into a list! Description Listify provides the capabilities for sorting and reordering a number of objects in a list. T

Travis Vignon 138 Nov 28, 2022
This package provides a trait that will generate a unique uuid when saving any Eloquent model.

Generate slugs when saving Eloquent models This package provides a trait that will generate a unique uuid when saving any Eloquent model. $model = new

Abdul Kudus 2 Oct 14, 2021