mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-18 18:44:16 +00:00
Code for #1291
This commit is contained in:
@@ -65,6 +65,9 @@ SEND_ERROR_MESSAGE=false
|
||||
# Set a Mapbox API key here (see mapbox.com) so there might be a map available at various places.
|
||||
MAPBOX_API_KEY=${MAPBOX_API_KEY}
|
||||
|
||||
# Set a Fixer IO API key here (see https://fixer.io) to enable live currency exchange rates.
|
||||
FIXER_API_KEY=${FIXER_API_KEY}
|
||||
|
||||
# If you wish to track your own behavior over Firefly III, set a valid analytics tracker ID here.
|
||||
ANALYTICS_ID=${ANALYTICS_ID}
|
||||
|
||||
@@ -82,7 +85,6 @@ REDIS_PASSWORD=null
|
||||
REDIS_PORT=6379
|
||||
CACHE_PREFIX=firefly
|
||||
SEARCH_RESULT_LIMIT=50
|
||||
EXCHANGE_RATE_SERVICE=fixerio
|
||||
PUSHER_KEY=
|
||||
PUSHER_SECRET=
|
||||
PUSHER_ID=
|
||||
|
@@ -69,6 +69,9 @@ SEND_ERROR_MESSAGE=true
|
||||
# Set a Mapbox API key here (see mapbox.com) so there might be a map available at various places.
|
||||
MAPBOX_API_KEY=
|
||||
|
||||
# Set a Fixer IO API key here (see https://fixer.io) to enable live currency exchange rates.
|
||||
FIXER_API_KEY=
|
||||
|
||||
# If you wish to track your own behavior over Firefly III, set a valid analytics tracker ID here.
|
||||
ANALYTICS_ID=
|
||||
|
||||
@@ -86,7 +89,6 @@ REDIS_PASSWORD=null
|
||||
REDIS_PORT=6379
|
||||
CACHE_PREFIX=firefly
|
||||
SEARCH_RESULT_LIMIT=50
|
||||
EXCHANGE_RATE_SERVICE=fixerio
|
||||
PUSHER_KEY=
|
||||
PUSHER_SECRET=
|
||||
PUSHER_ID=
|
||||
|
@@ -69,6 +69,9 @@ SEND_ERROR_MESSAGE=true
|
||||
# Set a Mapbox API key here (see mapbox.com) so there might be a map available at various places.
|
||||
MAPBOX_API_KEY=
|
||||
|
||||
# Set a Fixer IO API key here (see https://fixer.io) to enable live currency exchange rates.
|
||||
FIXER_API_KEY=
|
||||
|
||||
# If you wish to track your own behavior over Firefly III, set a valid analytics tracker ID here.
|
||||
ANALYTICS_ID=
|
||||
|
||||
@@ -86,7 +89,6 @@ REDIS_PASSWORD=null
|
||||
REDIS_PORT=6379
|
||||
CACHE_PREFIX=firefly
|
||||
SEARCH_RESULT_LIMIT=50
|
||||
EXCHANGE_RATE_SERVICE=fixerio
|
||||
PUSHER_KEY=
|
||||
PUSHER_SECRET=
|
||||
PUSHER_ID=
|
||||
|
@@ -69,6 +69,9 @@ SEND_ERROR_MESSAGE=true
|
||||
# Set a Mapbox API key here (see mapbox.com) so there might be a map available at various places.
|
||||
MAPBOX_API_KEY=
|
||||
|
||||
# Set a Fixer IO API key here (see https://fixer.io) to enable live currency exchange rates.
|
||||
FIXER_API_KEY=
|
||||
|
||||
# If you wish to track your own behavior over Firefly III, set a valid analytics tracker ID here.
|
||||
ANALYTICS_ID=
|
||||
|
||||
@@ -86,7 +89,6 @@ REDIS_PASSWORD=null
|
||||
REDIS_PORT=6379
|
||||
CACHE_PREFIX=firefly
|
||||
SEARCH_RESULT_LIMIT=50
|
||||
EXCHANGE_RATE_SERVICE=fixerio
|
||||
PUSHER_KEY=
|
||||
PUSHER_SECRET=
|
||||
PUSHER_ID=
|
||||
|
@@ -45,9 +45,9 @@ SEND_ERROR_MESSAGE=false
|
||||
CACHE_PREFIX=firefly
|
||||
|
||||
SEARCH_RESULT_LIMIT=50
|
||||
EXCHANGE_RATE_SERVICE=fixerio
|
||||
|
||||
MAPBOX_API_KEY=
|
||||
FIXER_API_KEY=
|
||||
ANALYTICS_ID=
|
||||
SITE_OWNER=mail@example.com
|
||||
USE_ENCRYPTION=true
|
||||
|
@@ -48,15 +48,23 @@ class ExchangeController extends Controller
|
||||
/** @var CurrencyRepositoryInterface $repository */
|
||||
$repository = app(CurrencyRepositoryInterface::class);
|
||||
$rate = $repository->getExchangeRate($fromCurrency, $toCurrency, $date);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (null === $rate->id) {
|
||||
Log::debug(sprintf('No cached exchange rate in database for %s to %s on %s', $fromCurrency->code, $toCurrency->code, $date->format('Y-m-d')));
|
||||
$preferred = env('EXCHANGE_RATE_SERVICE', config('firefly.preferred_exchange_service'));
|
||||
$class = config('firefly.currency_exchange_services.' . $preferred);
|
||||
/** @var ExchangeRateInterface $object */
|
||||
$object = app($class);
|
||||
$object->setUser(auth()->user());
|
||||
$rate = $object->getRate($fromCurrency, $toCurrency, $date);
|
||||
|
||||
// create service:
|
||||
/** @var ExchangeRateInterface $service */
|
||||
$service = app(ExchangeRateInterface::class);
|
||||
$service->setUser(auth()->user());
|
||||
|
||||
// get rate:
|
||||
$rate = $service->getRate($fromCurrency, $toCurrency, $date);
|
||||
}
|
||||
|
||||
$return = $rate->toArray();
|
||||
$return['amount'] = null;
|
||||
if (null !== $request->get('amount')) {
|
||||
|
@@ -46,6 +46,8 @@ use FireflyIII\Repositories\TransactionType\TransactionTypeRepository;
|
||||
use FireflyIII\Repositories\TransactionType\TransactionTypeRepositoryInterface;
|
||||
use FireflyIII\Repositories\User\UserRepository;
|
||||
use FireflyIII\Repositories\User\UserRepositoryInterface;
|
||||
use FireflyIII\Services\Currency\ExchangeRateInterface;
|
||||
use FireflyIII\Services\Currency\FixerIOv2;
|
||||
use FireflyIII\Services\Password\PwndVerifierV2;
|
||||
use FireflyIII\Services\Password\Verifier;
|
||||
use FireflyIII\Support\Amount;
|
||||
@@ -174,6 +176,7 @@ class FireflyServiceProvider extends ServiceProvider
|
||||
$this->app->bind(FiscalHelperInterface::class, FiscalHelper::class);
|
||||
$this->app->bind(BalanceReportHelperInterface::class, BalanceReportHelper::class);
|
||||
$this->app->bind(BudgetReportHelperInterface::class, BudgetReportHelper::class);
|
||||
$this->app->bind(ExchangeRateInterface::class, FixerIOv2::class);
|
||||
|
||||
// password verifier thing
|
||||
$this->app->bind(Verifier::class, PwndVerifierV2::class);
|
||||
|
113
app/Services/Currency/FixerIOv2.php
Normal file
113
app/Services/Currency/FixerIOv2.php
Normal file
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
/**
|
||||
* FixerIOv2.php
|
||||
* Copyright (c) 2018 thegrumpydictator@gmail.com
|
||||
*
|
||||
* This file is part of Firefly III.
|
||||
*
|
||||
* Firefly III is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Firefly III is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Services\Currency;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use FireflyIII\Models\CurrencyExchangeRate;
|
||||
use FireflyIII\Models\TransactionCurrency;
|
||||
use FireflyIII\User;
|
||||
use Log;
|
||||
use Requests;
|
||||
use Requests_Exception;
|
||||
|
||||
/**
|
||||
* Class FixerIOv2.
|
||||
*/
|
||||
class FixerIOv2 implements ExchangeRateInterface
|
||||
{
|
||||
/** @var User */
|
||||
protected $user;
|
||||
|
||||
/**
|
||||
* @param TransactionCurrency $fromCurrency
|
||||
* @param TransactionCurrency $toCurrency
|
||||
* @param Carbon $date
|
||||
*
|
||||
* @return CurrencyExchangeRate
|
||||
*/
|
||||
public function getRate(TransactionCurrency $fromCurrency, TransactionCurrency $toCurrency, Carbon $date): CurrencyExchangeRate
|
||||
{
|
||||
// create new exchange rate with default values.
|
||||
// create new currency exchange rate object:
|
||||
$exchangeRate = new CurrencyExchangeRate;
|
||||
$exchangeRate->user()->associate($this->user);
|
||||
$exchangeRate->fromCurrency()->associate($fromCurrency);
|
||||
$exchangeRate->toCurrency()->associate($toCurrency);
|
||||
$exchangeRate->date = $date;
|
||||
$exchangeRate->rate = 0;
|
||||
|
||||
// get API key
|
||||
$apiKey = env('FIXER_API_KEY', '');
|
||||
|
||||
// if no API key, return unsaved exchange rate.
|
||||
if (strlen($apiKey) === 0) {
|
||||
return $exchangeRate;
|
||||
}
|
||||
|
||||
// build URI
|
||||
$uri = sprintf(
|
||||
'http://data.fixer.io/api/%s?access_key=%s&base=%s&symbols=%s',
|
||||
$date->format('Y-m-d'), $apiKey, $fromCurrency->code, $toCurrency->code
|
||||
);
|
||||
$statusCode = -1;
|
||||
Log::debug(sprintf('Going to request exchange rate using URI %s', str_replace($apiKey, 'xxxx', $uri)));
|
||||
try {
|
||||
$result = Requests::get($uri);
|
||||
$statusCode = $result->status_code;
|
||||
$body = $result->body;
|
||||
Log::debug(sprintf('Result status code is %d', $statusCode));
|
||||
} catch (Requests_Exception $e) {
|
||||
// don't care about error
|
||||
$body = sprintf('Requests_Exception: %s', $e->getMessage());
|
||||
}
|
||||
|
||||
// Requests_Exception
|
||||
$content = null;
|
||||
if (200 !== $statusCode) {
|
||||
Log::error(sprintf('Something went wrong. Received error code %d and body "%s" from FixerIO.', $statusCode, $body));
|
||||
}
|
||||
// get rate from body:
|
||||
if (200 === $statusCode) {
|
||||
$content = json_decode($body, true);
|
||||
}
|
||||
if (null !== $content) {
|
||||
$code = $toCurrency->code;
|
||||
$rate = isset($content['rates'][$code]) ? $content['rates'][$code] : '0';
|
||||
}
|
||||
Log::debug('Got the following rates from Fixer: ', $content['rates'] ?? []);
|
||||
$exchangeRate->rate = $rate;
|
||||
$exchangeRate->save();
|
||||
|
||||
return $exchangeRate;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
*
|
||||
* @return mixed|void
|
||||
*/
|
||||
public function setUser(User $user)
|
||||
{
|
||||
$this->user = $user;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user