diff --git a/composer.json b/composer.json index e4e05d0e3..833dd322a 100755 --- a/composer.json +++ b/composer.json @@ -21,6 +21,7 @@ "doctrine/dbal": "^2.9@dev", "fideloper/proxy": "^4.0", "flynsarmy/db-blade-compiler": "*", + "guzzlehttp/guzzle": "~6.0", "intervention/image": "^2.4", "intervention/imagecache": "^2.3", "kalnoy/nestedset": "^4.3", diff --git a/config/services.php b/config/services.php index aabaf2f3c..456421a1a 100755 --- a/config/services.php +++ b/config/services.php @@ -29,8 +29,13 @@ return [ 'secret' => env('SPARKPOST_SECRET'), ], - 'fixer' => [ - 'key' => env('fixer_api_key') + 'exchange-api' => [ + 'default' => 'fixer', + 'fixer' => [ + 'paid_account' => false, + 'key' => env('fixer_api_key'), + 'class' => 'Webkul\Core\Helpers\Exchange\FixerExchange' + ] ], 'stripe' => [ diff --git a/packages/Webkul/Admin/src/Http/routes.php b/packages/Webkul/Admin/src/Http/routes.php index 0331e1d5a..6af64c9b1 100755 --- a/packages/Webkul/Admin/src/Http/routes.php +++ b/packages/Webkul/Admin/src/Http/routes.php @@ -445,6 +445,8 @@ Route::group(['middleware' => ['web']], function () { 'view' => 'admin::settings.exchange_rates.edit' ])->name('admin.exchange_rates.edit'); + Route::get('/exchange_rates/update-rates/{service}', 'Webkul\Core\Http\Controllers\ExchangeRateController@updateRates')->name('admin.exchange_rates.update-rates'); + Route::put('/exchange_rates/edit/{id}', 'Webkul\Core\Http\Controllers\ExchangeRateController@update')->defaults('_config', [ 'redirect' => 'admin.exchange_rates.index' ])->name('admin.exchange_rates.update'); diff --git a/packages/Webkul/Admin/src/Resources/lang/en/app.php b/packages/Webkul/Admin/src/Resources/lang/en/app.php index 78de14d6d..2a3fb1937 100755 --- a/packages/Webkul/Admin/src/Resources/lang/en/app.php +++ b/packages/Webkul/Admin/src/Resources/lang/en/app.php @@ -596,6 +596,8 @@ return [ 'source_currency' => 'Source Currency', 'target_currency' => 'Target Currency', 'rate' => 'Rate', + 'exchange-class-not-found' => ':service exchange rate class not found', + 'update-rates' => 'Update rates using :service', 'create-success' => 'Exchange Rate created successfully.', 'update-success' => 'Exchange Rate updated successfully.', 'delete-success' => 'Exchange Rate deleted successfully.', diff --git a/packages/Webkul/Admin/src/Resources/views/settings/exchange_rates/index.blade.php b/packages/Webkul/Admin/src/Resources/views/settings/exchange_rates/index.blade.php index e42db46f0..38e6c9269 100755 --- a/packages/Webkul/Admin/src/Resources/views/settings/exchange_rates/index.blade.php +++ b/packages/Webkul/Admin/src/Resources/views/settings/exchange_rates/index.blade.php @@ -15,6 +15,16 @@ {{ __('admin::app.settings.exchange_rates.add-title') }} + + @php + $defaultService = config('services.exchange-api.default'); + @endphp + + + {{ __('admin::app.settings.exchange_rates.update-rates', [ + 'service' => $defaultService + ]) }} + diff --git a/packages/Webkul/Core/src/Helpers/Exchange/ExchangeRate.php b/packages/Webkul/Core/src/Helpers/Exchange/ExchangeRate.php new file mode 100644 index 000000000..4d0612399 --- /dev/null +++ b/packages/Webkul/Core/src/Helpers/Exchange/ExchangeRate.php @@ -0,0 +1,10 @@ +apiKey = config('services.exchange-api')['fixer']['key']; + + $this->apiEndPoint = 'http://data.fixer.io/api/latest?access_key='.$this->apiKey; + } + + public function fetchRates() + { + $rates = array(); + + $this->exchangeRate = app('Webkul\Core\Repositories\ExchangeRateRepository'); + + // dummy api call + $client = new \GuzzleHttp\Client(); + + if (config('services.exchange-api')['fixer']['paid_account']) { + $result = $client->request('GET', 'http://data.fixer.io/api/'.date('Y-m-d').'?access_key='.$this->apiKey.'&base='.core()->getBaseCurrency()->code.'&symbols=INR'); + + } else { + $result = $client->request('GET', 'http://data.fixer.io/api/'.date('Y-m-d').'?access_key='.$this->apiKey.'&symbols=USD'); + } + + $result = json_decode($result->getBody()->getContents()); + + return $result; + } + + public function updateRates() + { + } +} \ No newline at end of file diff --git a/packages/Webkul/Core/src/Http/Controllers/ExchangeRateController.php b/packages/Webkul/Core/src/Http/Controllers/ExchangeRateController.php index b10b1c315..96d6caebf 100755 --- a/packages/Webkul/Core/src/Http/Controllers/ExchangeRateController.php +++ b/packages/Webkul/Core/src/Http/Controllers/ExchangeRateController.php @@ -5,7 +5,6 @@ namespace Webkul\Core\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Http\Response; use Illuminate\Support\Facades\Event; -use Webkul\Core\Repositories\ExchangeRateRepository as ExchangeRate; use Webkul\Core\Repositories\CurrencyRepository as Currency; /** @@ -23,13 +22,6 @@ class ExchangeRateController extends Controller */ protected $_config; - /** - * ExchangeRateRepository object - * - * @var array - */ - protected $exchangeRate; - /** * CurrencyRepository object * @@ -44,10 +36,8 @@ class ExchangeRateController extends Controller * @param \Webkul\Core\Repositories\CurrencyRepository $currency * @return void */ - public function __construct(ExchangeRate $exchangeRate, Currency $currency) + public function __construct(Currency $currency) { - $this->exchangeRate = $exchangeRate; - $this->currency = $currency; $this->_config = request('_config'); @@ -139,6 +129,44 @@ class ExchangeRateController extends Controller return redirect()->route($this->_config['redirect']); } + /** + * Update Rates Using Exchange Rates API + * + * @return void + */ + public function updateRates($service) + { + $exchangeService = config('services.exchange-api')[$service]; + + if (is_array($exchangeService)) { + if (! array_key_exists('class', $exchangeService)) { + return response()->json([ + 'success' => false, + 'rates' => null, + 'error' => trans('admin::app.exchange-rate.exchange-class-not-found', [ + 'service' => $service + ]) + ], 400); + } + + $exchangeServiceInstance = new $exchangeService['class']; + $updatedRates = $exchangeServiceInstance->fetchRates(); + + dd($updatedRates); + + return response()->json([ + 'success' => true, + 'rates' => 'rates' + ], 200); + } else { + return response()->json([ + 'success' => false, + 'rates' => null, + 'error' => trans('admin::app.exchange-rate.invalid-config') + ], 400); + } + } + /** * Remove the specified resource from storage. *