You are here

public function CurrencylayerExchanger::getRemoteData in Commerce Exchanger 8

Fetch external data.

Parameters

string|null $base_currency: If we fetch data based on specific currency.

Overrides ExchangerProviderRemoteInterface::getRemoteData

File

src/Plugin/Commerce/ExchangerProvider/CurrencylayerExchanger.php, line 34

Class

CurrencylayerExchanger
Provides the Currencylayer.com exchange rates.

Namespace

Drupal\commerce_exchanger\Plugin\Commerce\ExchangerProvider

Code

public function getRemoteData($base_currency = NULL) {
  $data = NULL;
  $options = [
    'query' => [
      'access_key' => $this
        ->getApiKey(),
    ],
  ];

  // Add base currency if we use enterprise model.
  if ($this
    ->isEnterprise()) {
    $options['query']['base'] = $base_currency;
  }
  $request = $this
    ->apiClient($options);
  if ($request) {
    $json = Json::decode($request);
    if (!empty($json['success'])) {

      // Leave base currency. In some cases we don't know base currency.
      // Currenclayer on free plan uses your address for base currency, and in
      // Drupal you could have different default value.
      $data['base'] = $json['source'];
      foreach ($json['quotes'] as $code => $rate) {
        $data['rates'][str_replace($json['source'], '', $code)] = $rate;
      }
    }
  }
  return $data;
}