TransferWiseExchanger.php in Commerce Exchanger 8
File
src/Plugin/Commerce/ExchangerProvider/TransferWiseExchanger.php
View source
<?php
namespace Drupal\commerce_exchanger\Plugin\Commerce\ExchangerProvider;
use Drupal\Component\Serialization\Json;
use GuzzleHttp\RequestOptions;
class TransferWiseExchanger extends ExchangerProviderRemoteBase {
public function apiUrl() {
return 'https://api.transferwise.com/v1/rates';
}
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;
}
}