定制 worksome/exchange 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

worksome/exchange

Composer 安装命令:

composer require worksome/exchange

包简介

Check Exchange Rates for any currency in Laravel.

README 文档

README

Check exchange rates for any currency in Laravel

Latest Version on Packagist GitHub Tests Action Status GitHub Static Analysis Action Status Total Downloads

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.

/** @var \Worksome\Exchange\Support\Rates $exchangeRates */
$rates = $exchangeRates->rates; // ['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 an alternative to Fixer with an identical API spec.

In your exchange.php config file, set default to exchange_rate, or set EXCHANGE_DRIVER to exchange_rate in your .env file. Set EXCHANGE_RATE_ACCESS_KEY to your provided access key from exchangerate.host.

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

Currency.GetGeoApi.com

Currency.GetGeoApi.com is an alternative option you can use with a free quota.

In your exchange.php config file, set default to currency_geo, or set EXCHANGE_DRIVER to currency_geo in your .env file. Set CURRENCY_GEO_ACCESS_KEY to your provided access key from currency.getgeoapi.com.

With that task completed, you're ready to start using Currency.GetGeoApi.com for retrieving up-to-date exchange rates.

Frankfurter.dev

frankfurter.dev is an open-source API for current and historical foreign exchange rates, which can be used without an API key.

The driver targets the v2 API, which covers a much larger currency set than the ECB-only v1 (for example AED and MAD).

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

If you are self-hosting Frankfurter or need to use a different endpoint, you can set FRANKFURTER_BASE_URL in your .env file. It defaults to https://api.frankfurter.dev/v2.

Note

Prior to v3 of this package, the frankfurter driver targeted the v1 API. If you need to keep using the v1 API, stay on v2 of this package.

With that task completed, you're ready to start using frankfurter.dev 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), key for your cached rates, and the store.

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 GitHub Releases for more information on what has changed recently.

Credits

License

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

worksome/exchange 适用场景与选型建议

worksome/exchange 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 597.1k 次下载、GitHub Stars 达 124, 最近一次更新时间为 2022 年 02 月 23 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「exchange」 「worksome」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 worksome/exchange 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 worksome/exchange 我们能提供哪些服务?
定制开发 / 二次开发

基于 worksome/exchange 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 597.1k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 124
  • 点击次数: 24
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 124
  • Watchers: 8
  • Forks: 11
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-02-23