diff --git a/.env.example b/.env.example index 46f7494d83b..11d047a3c67 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 4c2064fd4b7..e24ce9ea6de 100755 --- a/config/services.php +++ b/config/services.php @@ -38,6 +38,7 @@ ], '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 5fbeb32d3d0..0aa9ffb1df7 100644 --- a/packages/Webkul/Core/src/Helpers/Exchange/ExchangeRates.php +++ b/packages/Webkul/Core/src/Helpers/Exchange/ExchangeRates.php @@ -9,22 +9,29 @@ 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 @@ 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() @@ -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) {