Source of file ExchangeRateProvider.php
Size: 3,849 Bytes - Last Modified: 2021-12-23T10:39:35+00:00
/var/www/docs.ssmods.com/process/src/src/Money/ExchangeRateProvider.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 | <?php namespace Sunnysideup\Ecommerce\Money; /* * the sole purpose of this class is to provide an exchange rate * from currency 1 to currency 2. * It can provide number that reads as follows: * * If I exchange 1 USD I will get EUR 0.8 * This is the exchange rate. * * So, how many do I get of the "to" currency * when I have one "from" currency. * * * @authors: Nicolaas [at] Sunny Side Up .co.nz * @package: ecommerce * @sub-package: money */ use SilverStripe\Control\Controller; use SilverStripe\Core\Config\Configurable; use SilverStripe\Core\Extensible; use SilverStripe\Core\Injector\Injectable; /* * the sole purpose of this class is to provide an exchange rate * from currency 1 to currency 2. * It can provide number that reads as follows: * * If I exchange 1 USD I will get EUR 0.8 * This is the exchange rate. * * So, how many do I get of the "to" currency * when I have one "from" currency. * * * @authors: Nicolaas [at] Sunny Side Up .co.nz * @package: ecommerce * @sub-package: money */ class ExchangeRateProvider { use Extensible; use Injectable; use Configurable; /** * adds a bit of additional cost to account for the exchange cost. * * @var float */ protected $exchangeCostMultiplier = 1.05; /** * cache of exchange rates. * * @var array */ private static $_memory_cache = []; /** * Get the exchange rate. * * @param string $fromCode e.g. NZD * @param string $toCode e.g. USD * * @return float * @return float */ public function ExchangeRate($fromCode, $toCode) { $fromCode = strtoupper($fromCode); $toCode = strtoupper($toCode); $cacheCode = $fromCode . '_' . $toCode; if (isset(self::$_memory_cache[$cacheCode])) { return self::$_memory_cache[$cacheCode]; } $value = Controller::curr()->getRequest()->getSession()->get($cacheCode); if ($value) { self::$_memory_cache[$cacheCode] = $value; } else { $value = $this->getRate($fromCode, $toCode); self::$_memory_cache[$cacheCode] = $value; Controller::curr()->getRequest()->getSession()->set($cacheCode, $value); } return self::$_memory_cache[$cacheCode]; } /** * gets a rate from a FROM and a TO currency. * see https://free.currencyconverterapi.com/ for limitations. * * @param string $fromCode - UPPERCASE Code, e.g. NZD * @param string $toCode - UPPERCASE Code, e.g. EUR * * @return float - returns exchange rate */ protected function getRate($fromCode, $toCode) { if ($fromCode === $toCode) { return 1; } $rate = 1; $reference = $fromCode . '_' . $toCode; $url = 'http://free.currencyconverterapi.com/api/v5/convert?q=' . $reference . '&compact=y'; $ch = @curl_init(); if ($ch) { $timeout = 5; // set to zero for no timeout curl_setopt($ch, CURLOPT_URL, "{$url}"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $record = curl_exec($ch); curl_close($ch); } if (! $record) { $record = file_get_contents($url); } if ($record) { $currencyData = json_decode($record); if (property_exists($currencyData, $reference)) { $rate = $currencyData->{$reference}->val; if (! $rate) { user_error('There was a problem retrieving the exchange rate.'); } } } if (1 !== $rate) { $rate *= $this->exchangeCostMultiplier; } return $rate; } } |