Added support for fixer for fetching rates

This commit is contained in:
Prashant Singh 2019-07-15 18:24:36 +05:30
parent d0b8a1f50f
commit c6bc5ba1c3
8 changed files with 127 additions and 13 deletions

View File

@ -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",

View File

@ -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' => [

View File

@ -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');

View File

@ -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.',

View File

@ -15,6 +15,16 @@
<a href="{{ route('admin.exchange_rates.create') }}" class="btn btn-lg btn-primary">
{{ __('admin::app.settings.exchange_rates.add-title') }}
</a>
@php
$defaultService = config('services.exchange-api.default');
@endphp
<a href="{{ route('admin.exchange_rates.update-rates', $defaultService) }}" class="btn btn-lg btn-primary">
{{ __('admin::app.settings.exchange_rates.update-rates', [
'service' => $defaultService
]) }}
</a>
</div>
</div>

View File

@ -0,0 +1,10 @@
<?php
namespace Webkul\Core\Helpers\Exchange;
abstract class ExchangeRate
{
abstract public function fetchRates();
abstract public function UpdateRates();
}

View File

@ -0,0 +1,56 @@
<?php
namespace Webkul\Core\Helpers\Exchange;
use Webkul\Core\Helpers\Exchange\ExchangeRate;
use Webkul\Core\Repositories\ExchangeRateRepository;
class FixerExchange extends ExchangeRate
{
/**
* API key
*/
protected $apiKey;
/**
* API endpoint
*/
protected $apiEndPoint;
/**
* Holds ExchangeRateRepository instance
*/
protected $exchangeRate;
public function __construct()
{
$this->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()
{
}
}

View File

@ -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.
*