Git Product home page Git Product logo

currency-layer's Introduction

Currency Layer plugin for Craft CMS 3.x

API integration with https://currencylayer.com/

Requirements

This plugin requires Craft CMS 3.0.0-beta.23 or later.

Installation

To install the plugin, follow these instructions.

  1. Open your terminal and go to your Craft project:

     cd /path/to/project
    
  2. Then tell Composer to load the plugin:

     composer require ournameismud/currency-layer
    
  3. In the Control Panel, go to Settings → Plugins and click the “Install” button for Currency Layer.

Currency Layer Overview

This is a plugin layer for the Currency Layer API that lets you store and retrieve currency values locally. If you use this in conjunction with Craft Commerce it will populate the conversion rate values for corresponding Payment Currencies.

Configuring Currency Layer

To use this plugin you will require an API key from Currency Layer. A basic account is free but rate-limited to 250 Requests/mo.

Options available in the plugin can be saved in Plugin > Settings or via a config file.

The plugin settings available are:

  • apiKey the Currency Layer API key
  • primaryCurrency the primary currency you want to use as the base for conversions
  • apiCache whether to cache the response (recommended)
  • cacheDuration how long you want the cache to be stored/saved for

Using Currency Layer

You can use Currency Layer in one of two ways. The first way is via an action. This is best used in conjunction with a CRON job to fetch the results periodically. The action can be triggered via a URL as follows:

http://your.domain.com/actions/currency-layer/currency/fetch?currencies=EUR,GBP,USD with the currencies parameter being a comma-separated list of currencies you wish to retrieve. Note that the base (free) version of Currency Layer will always default to USD as the source.

If you are using Craft Commerce this action will save the conversion rates directly into your Commerce settings. Otherwise the conversion values are available via the craft.currencyLayer.fetch() variable:

craft.currencyLayer.fetch(request,type) where request is required and should be an object of key:value attributes corresponding to the endpoints for the Currency Layer API you require (please note that many of the endpoints are limited to paid-for accounts). The type parameter corresponds to the type of query you wish to make (defaults to live).

For example, {% set data = craft.currencyLayer.fetch({ currencies:'EUR,GBP,USD' }) %} would fetch the following endpoint: https://apilayer.net/api/live?currencies=EUR,GBP,USD&access_key=YOUR_ACCESS_KEY. The response will be an array of values which provide multipliers based on the primary currency defined in your plugin settings. With this in mind we can build out a macro to perform some simple currency conversion, for example:

{%- macro convert(value = 1, currency) -%}
    {% set data = craft.currencyLayer.fetch({ currencies:'EUR,GBP,USD' }) %}
    {{- data[currency] * value -}}
{%- endmacro -%}

{% set GBP = 100 %}
{% set EUR = _self.convert(100,'EUR') %}
{% set USD = _self.convert(100,'USD') %}

<dl>
    <dt>GBP</dt><dd>{{ GBP }}</dd>
    <dt>EUR</dt><dd>{{ EUR }}</dd>
    <dt>USD</dt><dd>{{ USD }}</dd>
</dl>

The plugin is designed to work closely with Craft Commerce currencies. If currencies are already defined in Craft Commerce the option to select a primary currency is restricted to those available in Craft Commerce. If a new primary currency is selected in the plugin then the primary Craft Commerce currency is updated and vice versa.

Currency Layer Roadmap

Some things to do, and ideas for potential features:

  • Release it

Brought to you by cole007

currency-layer's People

Stargazers

Sean P. Myrick V19.1.7.2 avatar François Lévesque avatar Josh Angell avatar

Watchers

James Cloos avatar Cole Henley avatar David Wood avatar Sean P. Myrick V19.1.7.2 avatar

currency-layer's Issues

Fix Composer 2 compatibility

When installing this plugin with Composer 1, the following deprecation notice is output:

Deprecation Notice: Class ournameismud\currencylayer\assetbundles\CurrencyLayer\CurrencyLayerAsset located in ./vendor/ournameismud/currency-layer/src/assetbundles/currencylayer/CurrencyLayerAsset.php does not comply with psr-4 autoloading standard. It will not autoload anymore in Composer v2.0.

Composer 2 was released on October 24, and is now the default version that will be installed, so it’s critical that this gets resolved ASAP, or people will start getting Class not found errors.

To fix, run the following command:

> git mv src/assetbundles/currencylayer src/assetbundles/CurrencyLayer

Then commit your change and tag a new release.

(Note that you must use the git mv command, as Git tends to not notice case-sensitive file renames otherwise.)

Plugin settings ENV vars

It would be great if you could support ENV vars in the plugin settings - specifically with the API key.

Rapid API Support (FR)

Would be so nice if there was a free option that was more robust. I've been using this particular api for a long time now and it's been 100% reliable. Unfortunately the Craft Plugin that make use of it is not well maintained.

https://rapidapi.com/natkapral/api/currency-converter5

I know it doesn't make sense that you would add support for another service to a plugin aimed specifically at currency layer, and that is why I won't begrudge you closing and ignoring this. But maybe one day you decide to expand this to a more agnostic service that can allow different providers to slot in. :)

Error when API cache results is turned off

I had initially had some results cached, but wanted to test something with caching turned off. However, I got the following error:

Error
Cannot use object of type stdClass as array

which reported ournameismud/currency-layer/src/services/Currency.php at line 46 as the culprit.

Craft Version: 3.4.18
Commerce Version: 3.1.3

Currencies in Craft Commerce not updating?

Hi there, mud!

I'm really sorry. Not sure if I'm doing something wrong or not, but I have Craft Commerce installed, a paid currency layer account, but the plugin's action is not updating the payment currencies in Commerce.

I'm not sure if it's a problem or not, but I don't use USD at all; just THB and AUD. The primary currency in both Commerce and Currency Layer is set to THB.

I have tried both of these, with no luck:

http://my.domain/actions/currency-layer/currency/fetch?currencies=THB,AUD
which returns:
{"source":"From Cache","cacheDuration":60,"data":{"success":true,"terms":"https://currencylayer.com/terms","privacy":"https://currencylayer.com/privacy","timestamp":1581742145,"source":"USD","quotes":{"USDTHB":31.21037,"USDAUD":1.489203}}}

http://my.domain/actions/currency-layer/currency/fetch?currencies=THB,AUD?source=THB
which returns:
{"source":"From Cache","cacheDuration":60,"data":{"success":true,"terms":"https://currencylayer.com/terms","privacy":"https://currencylayer.com/privacy","timestamp":1581738846,"source":"THB","quotes":{"THBTHB":1,"THBAUD":0.047715}}}

Neither of those actions updates the currencies in Commerce.

Any idea what I might be doing wrong?

Thanks in advance for any help or advice you may be able to give with this.

Andrew Macpherson
Sydney, Australia

Craft 4 Compatibility

Hi Cole

I know you're usually really busy, but I was just wondering if you had any plans for updating the plugin to add Craft 4 compatibility?

Totally understand if you don't, but if I know the situation I can start looking at alternatives if necessary.

Thanks heaps, Andrew

Error loading Craft dashboard

Hello again, and thanks making those updates recently. Things are again working great, except for one thing. Although everything works fine on my local development site, I get an error on the main Dashboard page on my production site. All of the other pages I've checked so far seem to work just fine, so it's just the dashboard page (site.url/admin).

I tracked the problem down to capitalisation in the src/assetbundles directory. If the 'CurrencyLayer' folder is set to lowercase ('currencylayer') my dashboard works again. But obviously, (a) I don't know if this would cause issues for anyone else, and (b) I don't want to keep having to change things manually in a composer-based deployment system.

I really do hope your health is back on track again, and if it is then I hope you don't mind taking a teensy little bit more time to look into this, to see if it's a change you could make.

Thanks again for all your help, Andrew

Here's the error, by the way:

ReflectionException: Class ournameismud\currencylayer\assetbundles\currencylayer\CurrencyLayerAsset does not exist in /home/forge/website.com/releases/20201108223808/vendor/yiisoft/yii2/di/Container.php:453
Stack trace:
#0 /home/forge/website.com/releases/20201108223808/vendor/yiisoft/yii2/di/Container.php(453): ReflectionClass->__construct()
#1 /home/forge/website.com/releases/20201108223808/vendor/yiisoft/yii2/di/Container.php(380): yii\di\Container->getDependencies()
#2 /home/forge/website.com/releases/20201108223808/vendor/yiisoft/yii2/di/Container.php(165): yii\di\Container->build()
#3 /home/forge/website.com/releases/20201108223808/vendor/yiisoft/yii2/BaseYii.php(365): yii\di\Container->get()
#4 /home/forge/website.com/releases/20201108223808/vendor/yiisoft/yii2/web/AssetManager.php(264): yii\BaseYii::createObject()
#5 /home/forge/website.com/releases/20201108223808/vendor/yiisoft/yii2/web/AssetManager.php(237): yii\web\AssetManager->loadBundle()
#6 /home/forge/website.com/releases/20201108223808/vendor/yiisoft/yii2/web/View.php(287): yii\web\AssetManager->getBundle()
#7 /home/forge/website.com/releases/20201108223808/vendor/twig/twig/src/Extension/CoreExtension.php(1507): yii\web\View->registerAssetBundle()
#8 /home/forge/website.com/releases/20201108223808/vendor/craftcms/cms/src/helpers/Template.php(92): twig_get_attribute()
#9 /home/forge/website.com/releases/20201108223808/storage/runtime/compiled_templates/58/586cb5751c2bed0476779e665cb4700342ec6c3f67efcc062709f9b488c13946.php(46): craft\helpers\Template::attribute()
#10 /home/forge/website.com/releases/20201108223808/vendor/twig/twig/src/Template.php(405): __TwigTemplate_399a49d6d9221ec986bfc0211d3e197261bebde53f8e868305196b72de65efc0->doDisplay()
#11 /home/forge/website.com/releases/20201108223808/vendor/twig/twig/src/Template.php(378): Twig\Template->displayWithErrorHandling()
#12 /home/forge/website.com/releases/20201108223808/vendor/twig/twig/src/Template.php(390): Twig\Template->display()
#13 /home/forge/website.com/releases/20201108223808/vendor/twig/twig/src/TemplateWrapper.php(45): Twig\Template->render()
#14 /home/forge/website.com/releases/20201108223808/vendor/twig/twig/src/Environment.php(318): Twig\TemplateWrapper->render()
#15 /home/forge/website.com/releases/20201108223808/vendor/craftcms/cms/src/web/View.php(389): Twig\Environment->render()
#16 /home/forge/website.com/releases/20201108223808/vendor/ournameismud/currency-layer/src/widgets/Currency.php(91): craft\web\View->renderTemplate()
#17 /home/forge/website.com/releases/20201108223808/vendor/craftcms/cms/src/controllers/DashboardController.php(80): ournameismud\currencylayer\widgets\Currency->getSettingsHtml()
#18 [internal function]: craft\controllers\DashboardController->actionIndex()
#19 /home/forge/website.com/releases/20201108223808/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array()
#20 /home/forge/website.com/releases/20201108223808/vendor/yiisoft/yii2/base/Controller.php(180): yii\base\InlineAction->runWithParams()
#21 /home/forge/website.com/releases/20201108223808/vendor/craftcms/cms/src/web/Controller.php(190): yii\base\Controller->runAction()
#22 /home/forge/website.com/releases/20201108223808/vendor/yiisoft/yii2/base/Module.php(528): craft\web\Controller->runAction()
#23 /home/forge/website.com/releases/20201108223808/vendor/craftcms/cms/src/web/Application.php(274): yii\base\Module->runAction()
#24 /home/forge/website.com/releases/20201108223808/vendor/yiisoft/yii2/web/Application.php(103): craft\web\Application->runAction()
#25 /home/forge/website.com/releases/20201108223808/vendor/craftcms/cms/src/web/Application.php(259): yii\web\Application->handleRequest()
#26 /home/forge/website.com/releases/20201108223808/vendor/yiisoft/yii2/base/Application.php(386): craft\web\Application->handleRequest()
#27 /home/forge/website.com/releases/20201108223808/web/index.php(21): yii\base\Application->run()
#28 {main}

Next yii\base\InvalidConfigException: Failed to instantiate component or class "ournameismud\currencylayer\assetbundles\currencylayer\CurrencyLayerAsset". in /home/forge/website.com/releases/20201108223808/vendor/yiisoft/yii2/di/Container.php:455
Stack trace:
#0 /home/forge/website.com/releases/20201108223808/vendor/yiisoft/yii2/di/Container.php(380): yii\di\Container->getDependencies()
#1 /home/forge/website.com/releases/20201108223808/vendor/yiisoft/yii2/di/Container.php(165): yii\di\Container->build()
#2 /home/forge/website.com/releases/20201108223808/vendor/yiisoft/yii2/BaseYii.php(365): yii\di\Container->get()
#3 /home/forge/website.com/releases/20201108223808/vendor/yiisoft/yii2/web/AssetManager.php(264): yii\BaseYii::createObject()
#4 /home/forge/website.com/releases/20201108223808/vendor/yiisoft/yii2/web/AssetManager.php(237): yii\web\AssetManager->loadBundle()
#5 /home/forge/website.com/releases/20201108223808/vendor/yiisoft/yii2/web/View.php(287): yii\web\AssetManager->getBundle()
#6 /home/forge/website.com/releases/20201108223808/vendor/twig/twig/src/Extension/CoreExtension.php(1507): yii\web\View->registerAssetBundle()
#7 /home/forge/website.com/releases/20201108223808/vendor/craftcms/cms/src/helpers/Template.php(92): twig_get_attribute()
#8 /home/forge/website.com/releases/20201108223808/storage/runtime/compiled_templates/58/586cb5751c2bed0476779e665cb4700342ec6c3f67efcc062709f9b488c13946.php(46): craft\helpers\Template::attribute()
#9 /home/forge/website.com/releases/20201108223808/vendor/twig/twig/src/Template.php(405): __TwigTemplate_399a49d6d9221ec986bfc0211d3e197261bebde53f8e868305196b72de65efc0->doDisplay()
#10 /home/forge/website.com/releases/20201108223808/vendor/twig/twig/src/Template.php(378): Twig\Template->displayWithErrorHandling()
#11 /home/forge/website.com/releases/20201108223808/vendor/twig/twig/src/Template.php(390): Twig\Template->display()
#12 /home/forge/website.com/releases/20201108223808/vendor/twig/twig/src/TemplateWrapper.php(45): Twig\Template->render()
#13 /home/forge/website.com/releases/20201108223808/vendor/twig/twig/src/Environment.php(318): Twig\TemplateWrapper->render()
#14 /home/forge/website.com/releases/20201108223808/vendor/craftcms/cms/src/web/View.php(389): Twig\Environment->render()
#15 /home/forge/website.com/releases/20201108223808/vendor/ournameismud/currency-layer/src/widgets/Currency.php(91): craft\web\View->renderTemplate()
#16 /home/forge/website.com/releases/20201108223808/vendor/craftcms/cms/src/controllers/DashboardController.php(80): ournameismud\currencylayer\widgets\Currency->getSettingsHtml()
#17 [internal function]: craft\controllers\DashboardController->actionIndex()
#18 /home/forge/website.com/releases/20201108223808/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array()
#19 /home/forge/website.com/releases/20201108223808/vendor/yiisoft/yii2/base/Controller.php(180): yii\base\InlineAction->runWithParams()
#20 /home/forge/website.com/releases/20201108223808/vendor/craftcms/cms/src/web/Controller.php(190): yii\base\Controller->runAction()
#21 /home/forge/website.com/releases/20201108223808/vendor/yiisoft/yii2/base/Module.php(528): craft\web\Controller->runAction()
#22 /home/forge/website.com/releases/20201108223808/vendor/craftcms/cms/src/web/Application.php(274): yii\base\Module->runAction()
#23 /home/forge/website.com/releases/20201108223808/vendor/yiisoft/yii2/web/Application.php(103): craft\web\Application->runAction()
#24 /home/forge/website.com/releases/20201108223808/vendor/craftcms/cms/src/web/Application.php(259): yii\web\Application->handleRequest()
#25 /home/forge/website.com/releases/20201108223808/vendor/yiisoft/yii2/base/Application.php(386): craft\web\Application->handleRequest()
#26 /home/forge/website.com/releases/20201108223808/web/index.php(21): yii\base\Application->run()
#27 {main}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.