• Stars
    star
    106
  • Rank 325,871 (Top 7 %)
  • Language
    PHP
  • License
    MIT License
  • Created almost 3 years ago
  • Updated 9 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Check Exchange Rates for any currency in Laravel.

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.

Frankfurter.app

frankfurter.app is an open-source API for current and historical foreign exchange rates published by the European Central Bank, which can be used without an API key.

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

With that task completed, you're ready to start using frankfurter.app 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.

More Repositories

1

envy

Keep your .env.example file up to date
PHP
664
star
2

request-factories

Test requests in Laravel without all the boilerplate.
PHP
598
star
3

verify-by-phone

A package for verifying a user via call or SMS
PHP
138
star
4

coding-style

Worksomes coding style
PHP
46
star
5

company-info

Lookup company information
PHP
32
star
6

foggy

Foggy is a tool for making database dumps with some data removed/changed.
PHP
26
star
7

code-sniffer

Worksome's preferences and custom sniffers for phpcs
PHP
15
star
8

graphlint

A static analysis tool for GraphQL
PHP
12
star
9

jira-branch-name-validator-action

A GitHub action for validating if the branch name is contains JIRA id
TypeScript
12
star
10

feature-flags

A package to manage feature flags in your application.
PHP
9
star
11

uk-tax-code-validator

A PHP package for validating uk tax codes
PHP
9
star
12

router-checker

Checks if a laravel route is valid
PHP
7
star
13

laravel-mfa

A driver-based multifactor authentication package for Laravel
PHP
6
star
14

pest-graphql-coverage

A plugin for PestPHP to add GraphQL coverage via LighthousePHP
PHP
6
star
15

number

A package for handling numbers
PHP
6
star
16

horizon-telemetry

An Opentelemetry implementation for Laravel Horizon and queues
PHP
6
star
17

pretty-pest

A set of PHP CS rules for formatting Pest PHP tests.
PHP
6
star
18

pest-plugin-silence

Prevent none-test output in your Pest tests.
PHP
6
star
19

rover

A docker image for apollo rover
Dockerfile
5
star
20

phpinsights-app

(IN-DEVELOPMENT) The must have GitHub action for PHP Insights
PHP
5
star
21

foggy-laravel

A Laravel wrapper for Foggy
PHP
5
star
22

laravel-telemetry

A package for adding Telemetry in Laravel
PHP
4
star
23

model-attributes

PHP
4
star
24

graphql-standards

A repository to include our GraphQL API standards
4
star
25

cdn-headers

A package that wraps around CDN headers.
PHP
3
star
26

laravel-direct-vue-routing

Add support for mapping Laravel routes directly to Vue components.
PHP
2
star
27

translator

A driver-based translation package for Laravel.
PHP
2
star
28

laravel-saml2

PHP
2
star
29

graphql-helpers

A collection of GraphQL helpers for GraphQL PHP.
PHP
2
star
30

coding-style-generator

Generate a coding style from a php insights configuration file
PHP
2
star
31

laravel-onfido

The Onfido Laravel Package
PHP
1
star
32

data-export

Worksome's data export package
PHP
1
star
33

vscode-extension-pack

Collection of recommended extensions used at Worksome.
1
star
34

phpstan-request-factories

A PHPStan rule for enforcing that every request has a corresponding factory.
PHP
1
star
35

ceevee

Simple CV and Rèsumè parsing for Laravel applications.
PHP
1
star