CurrencylayerExchanger.php in Commerce Exchanger 8
File
src/Plugin/Commerce/ExchangerProvider/CurrencylayerExchanger.php
View source
<?php
namespace Drupal\commerce_exchanger\Plugin\Commerce\ExchangerProvider;
use Drupal\Component\Serialization\Json;
class CurrencylayerExchanger extends ExchangerProviderRemoteBase {
public function apiUrl() {
if ($this
->isEnterprise()) {
return 'https://api.currencylayer.com/live';
}
return 'http://api.currencylayer.com/live';
}
public function getRemoteData($base_currency = NULL) {
$data = NULL;
$options = [
'query' => [
'access_key' => $this
->getApiKey(),
],
];
if ($this
->isEnterprise()) {
$options['query']['base'] = $base_currency;
}
$request = $this
->apiClient($options);
if ($request) {
$json = Json::decode($request);
if (!empty($json['success'])) {
$data['base'] = $json['source'];
foreach ($json['quotes'] as $code => $rate) {
$data['rates'][str_replace($json['source'], '', $code)] = $rate;
}
}
}
return $data;
}
}