Skip to content

Commit

Permalink
Merge pull request #4877 from devansh-webkul/exchange-rate-api
Browse files Browse the repository at this point in the history
Exchange Rates Key Added #4869
  • Loading branch information
ghermans committed May 18, 2021
2 parents 90c52be + b5a9ae0 commit 94c638d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
1 change: 1 addition & 0 deletions .env.example
Expand Up @@ -40,6 +40,7 @@ ADMIN_MAIL_TO=
MAIL_FROM_NAME=

FIXER_API_KEY=
EXCHANGE_RATES_API_KEY=

PUSHER_APP_ID=
PUSHER_APP_KEY=
Expand Down
1 change: 1 addition & 0 deletions config/services.php
Expand Up @@ -38,6 +38,7 @@
],

'exchange_rates' => [
'key' => env('EXCHANGE_RATES_API_KEY'),
'class' => 'Webkul\Core\Helpers\Exchange\ExchangeRates'
],
],
Expand Down
41 changes: 26 additions & 15 deletions packages/Webkul/Core/src/Helpers/Exchange/ExchangeRates.php
Expand Up @@ -9,32 +9,39 @@
class ExchangeRates extends ExchangeRate
{
/**
* API endpoint
*
* @var string
* API key.
*
* @var string
*/
protected $apiKey;

/**
* API endpoint.
*
* @var string
*/
protected $apiEndPoint;

/**
* Holds CurrencyRepository instance
*
* CurrencyRepository $currencyRepository
*
* @var \Webkul\Core\Repositories\CurrencyRepository
*/
protected $currencyRepository;

/**
* Holds ExchangeRateRepository instance
*
* ExchangeRateRepository $exchangeRateRepository
*
* @var \Webkul\Core\Repositories\ExchangeRateRepository
*/
protected $exchangeRateRepository;

/**
* Create a new helper instance.
*
* @param \Webkul\Core\Repositories\CurrencyRepository $currencyRepository
* @param \Webkul\Core\Repositories\CurrencyRepository $currencyRepository
* @param \Webkul\Core\Repositories\ExchangeRateRepository $exchangeRateRepository
* @return void
* @return void
*/
public function __construct(
CurrencyRepository $currencyRepository,
Expand All @@ -46,11 +53,13 @@ public function __construct(
$this->exchangeRateRepository = $exchangeRateRepository;

$this->apiEndPoint = 'https://api.exchangeratesapi.io/latest';

$this->apiKey = config('services.exchange-api.exchange_rates.key');
}

/**
* Fetch rates and updates in currency_exchange_rates table
*
* Fetch rates and updates in `currency_exchange_rates` table.
*
* @return \Exception|void
*/
public function updateRates()
Expand All @@ -62,15 +71,17 @@ public function updateRates()
continue;
}

$result = $client->request('GET', $this->apiEndPoint . '?base=' . config('app.currency') . '&symbols=' . $currency->code);
$result = $client->request('GET', $this->apiEndPoint . '?access_key='. $this->apiKey . '&base=' . config('app.currency') . '&symbols=' . $currency->code);

$result = json_decode($result->getBody()->getContents(), true);

if (isset($result['success']) && ! $result['success']) {
throw new \Exception(
isset($result['error']['info'])
? $result['error']['info']
: $result['error']['type'], 1);
? $result['error']['info']
: $result['error']['type'],
1
);
}

if ($exchangeRate = $currency->exchange_rate) {
Expand Down

0 comments on commit 94c638d

Please sign in to comment.