From b5a9ae05c825859a7068a9a1dedeb88407e33ea8 Mon Sep 17 00:00:00 2001 From: devansh bawari Date: Tue, 18 May 2021 17:58:24 +0530 Subject: [PATCH] Exchange Rate Key Added --- .env.example | 1 + config/services.php | 1 + .../src/Helpers/Exchange/ExchangeRates.php | 41 ++++++++++++------- 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/.env.example b/.env.example index 46f7494d8..11d047a3c 100644 --- a/.env.example +++ b/.env.example @@ -40,6 +40,7 @@ ADMIN_MAIL_TO= MAIL_FROM_NAME= FIXER_API_KEY= +EXCHANGE_RATES_API_KEY= PUSHER_APP_ID= PUSHER_APP_KEY= diff --git a/config/services.php b/config/services.php index 4c2064fd4..e24ce9ea6 100755 --- a/config/services.php +++ b/config/services.php @@ -38,6 +38,7 @@ return [ ], 'exchange_rates' => [ + 'key' => env('EXCHANGE_RATES_API_KEY'), 'class' => 'Webkul\Core\Helpers\Exchange\ExchangeRates' ], ], diff --git a/packages/Webkul/Core/src/Helpers/Exchange/ExchangeRates.php b/packages/Webkul/Core/src/Helpers/Exchange/ExchangeRates.php index 5fbeb32d3..0aa9ffb1d 100644 --- a/packages/Webkul/Core/src/Helpers/Exchange/ExchangeRates.php +++ b/packages/Webkul/Core/src/Helpers/Exchange/ExchangeRates.php @@ -9,22 +9,29 @@ use Webkul\Core\Repositories\ExchangeRateRepository; 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; @@ -32,9 +39,9 @@ class ExchangeRates extends ExchangeRate /** * 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, @@ -46,11 +53,13 @@ class ExchangeRates extends ExchangeRate $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() @@ -62,15 +71,17 @@ class ExchangeRates extends ExchangeRate 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) {