You are here

public function TransferWiseExchanger::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/TransferWiseExchanger.php, line 33

Class

TransferWiseExchanger
Provides the transferwise.com exchange rates.

Namespace

Drupal\commerce_exchanger\Plugin\Commerce\ExchangerProvider

Code

public function getRemoteData($base_currency = NULL) {
  $data = NULL;
  $source = $this
    ->isEnterprise() ? $base_currency : $this
    ->getBaseCurrency();
  $options = [
    RequestOptions::QUERY => [
      'source' => $source,
    ],
    RequestOptions::HEADERS => [
      'Authorization' => 'Bearer ' . $this
        ->getApiKey(),
    ],
  ];
  $request = $this
    ->apiClient($options);
  if ($request) {
    $rates = Json::decode($request);
    $data['base'] = $source;
    foreach ($rates as $rate) {
      $data['rates'][$rate['target']] = $rate['rate'];
    }
  }
  return $data;
}