mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
More code for Spectre import.
This commit is contained in:
@@ -24,7 +24,7 @@ namespace FireflyIII\Http\Controllers\Import;
|
||||
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Http\Controllers\Controller;
|
||||
use FireflyIII\Support\Import\Information\InformationInterface;
|
||||
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
|
||||
use FireflyIII\Support\Import\Prerequisites\PrerequisitesInterface;
|
||||
use Illuminate\Http\Request;
|
||||
use Log;
|
||||
@@ -32,72 +32,27 @@ use Session;
|
||||
|
||||
class BankController extends Controller
|
||||
{
|
||||
/**
|
||||
* This method must ask the user all parameters necessary to start importing data. This may not be enough
|
||||
* to finish the import itself (ie. mapping) but it should be enough to begin: accounts to import from,
|
||||
* accounts to import into, data ranges, etc.
|
||||
*
|
||||
* @param string $bank
|
||||
*
|
||||
* @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View
|
||||
*/
|
||||
public function form(string $bank)
|
||||
{
|
||||
$class = config(sprintf('firefly.import_pre.%s', $bank));
|
||||
if(!class_exists($class)) {
|
||||
throw new FireflyException(sprintf('Cannot find class %s', $class));
|
||||
}
|
||||
/** @var PrerequisitesInterface $object */
|
||||
$object = app($class);
|
||||
$object->setUser(auth()->user());
|
||||
|
||||
if ($object->hasPrerequisites()) {
|
||||
return redirect(route('import.bank.prerequisites', [$bank]));
|
||||
}
|
||||
$class = config(sprintf('firefly.import_info.%s', $bank));
|
||||
/** @var InformationInterface $object */
|
||||
$object = app($class);
|
||||
$object->setUser(auth()->user());
|
||||
$remoteAccounts = $object->getAccounts();
|
||||
|
||||
return view('import.bank.form', compact('remoteAccounts', 'bank'));
|
||||
}
|
||||
|
||||
/**
|
||||
* With the information given in the submitted form Firefly III will call upon the bank's classes to return transaction
|
||||
* information as requested. The user will be able to map unknown data and continue. Or maybe, it's put into some kind of
|
||||
* fake CSV file and forwarded to the import routine.
|
||||
* Once there are no prerequisites, this method will create an importjob object and
|
||||
* redirect the user to a view where this object can be used by a bank specific
|
||||
* class to process.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param string $bank
|
||||
* @param ImportJobRepositoryInterface $repository
|
||||
* @param string $bank
|
||||
*
|
||||
* @return \Illuminate\Http\RedirectResponse|null
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function postForm(Request $request, string $bank)
|
||||
public function createJob(ImportJobRepositoryInterface $repository, string $bank)
|
||||
{
|
||||
$class = config(sprintf('firefly.import_pre.%s', $bank));
|
||||
if(!class_exists($class)) {
|
||||
if (!class_exists($class)) {
|
||||
throw new FireflyException(sprintf('Cannot find class %s', $class));
|
||||
}
|
||||
/** @var PrerequisitesInterface $object */
|
||||
$object = app($class);
|
||||
$object->setUser(auth()->user());
|
||||
$importJob = $repository->create($bank);
|
||||
|
||||
if ($object->hasPrerequisites()) {
|
||||
return redirect(route('import.bank.prerequisites', [$bank]));
|
||||
}
|
||||
$remoteAccounts = $request->get('do_import');
|
||||
if (!is_array($remoteAccounts) || 0 === count($remoteAccounts)) {
|
||||
Session::flash('error', 'Must select accounts');
|
||||
|
||||
return redirect(route('import.bank.form', [$bank]));
|
||||
}
|
||||
$remoteAccounts = array_keys($remoteAccounts);
|
||||
$class = config(sprintf('firefly.import_pre.%s', $bank));
|
||||
// get import file
|
||||
unset($remoteAccounts, $class);
|
||||
|
||||
// get import config
|
||||
return redirect(route('import.bank.configure', [$bank, $importJob->key]));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -112,12 +67,13 @@ class BankController extends Controller
|
||||
* @param string $bank
|
||||
*
|
||||
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function postPrerequisites(Request $request, string $bank)
|
||||
{
|
||||
Log::debug(sprintf('Now in postPrerequisites for %s', $bank));
|
||||
$class = config(sprintf('firefly.import_pre.%s', $bank));
|
||||
if(!class_exists($class)) {
|
||||
if (!class_exists($class)) {
|
||||
throw new FireflyException(sprintf('Cannot find class %s', $class));
|
||||
}
|
||||
/** @var PrerequisitesInterface $object */
|
||||
@@ -126,7 +82,7 @@ class BankController extends Controller
|
||||
if (!$object->hasPrerequisites()) {
|
||||
Log::debug(sprintf('No more prerequisites for %s, move to form.', $bank));
|
||||
|
||||
return redirect(route('import.bank.form', [$bank]));
|
||||
return redirect(route('import.bank.create-job', [$bank]));
|
||||
}
|
||||
Log::debug('Going to store entered preprerequisites.');
|
||||
// store post data
|
||||
@@ -138,7 +94,7 @@ class BankController extends Controller
|
||||
return redirect(route('import.bank.prerequisites', [$bank]));
|
||||
}
|
||||
|
||||
return redirect(route('import.bank.form', [$bank]));
|
||||
return redirect(route('import.bank.create-job', [$bank]));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -148,11 +104,12 @@ class BankController extends Controller
|
||||
* @param string $bank
|
||||
*
|
||||
* @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function prerequisites(string $bank)
|
||||
{
|
||||
$class = config(sprintf('firefly.import_pre.%s', $bank));
|
||||
if(!class_exists($class)) {
|
||||
if (!class_exists($class)) {
|
||||
throw new FireflyException(sprintf('Cannot find class %s', $class));
|
||||
}
|
||||
/** @var PrerequisitesInterface $object */
|
||||
@@ -161,12 +118,12 @@ class BankController extends Controller
|
||||
|
||||
if ($object->hasPrerequisites()) {
|
||||
$view = $object->getView();
|
||||
$parameters = ['title' => strval(trans('firefly.import_index_title')),'mainTitleIcon' => 'fa-archive'];
|
||||
$parameters = ['title' => strval(trans('firefly.import_index_title')), 'mainTitleIcon' => 'fa-archive'];
|
||||
$parameters = $object->getViewParameters() + $parameters;
|
||||
|
||||
return view($view, $parameters);
|
||||
}
|
||||
|
||||
return redirect(route('import.bank.form', [$bank]));
|
||||
return redirect(route('import.bank.create-job', [$bank]));
|
||||
}
|
||||
}
|
||||
|
||||
178
app/Import/Configurator/SpectreConfigurator.php
Normal file
178
app/Import/Configurator/SpectreConfigurator.php
Normal file
@@ -0,0 +1,178 @@
|
||||
<?php
|
||||
/**
|
||||
* SpectreConfigurator.php
|
||||
* Copyright (c) 2017 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\Import\Configurator;
|
||||
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Models\ImportJob;
|
||||
use FireflyIII\Support\Import\Configuration\ConfigurationInterface;
|
||||
use FireflyIII\Support\Import\Configuration\Spectre\SelectBank;
|
||||
use FireflyIII\Support\Import\Configuration\Spectre\SelectCountry;
|
||||
use Log;
|
||||
|
||||
/**
|
||||
* Class SpectreConfigurator.
|
||||
*/
|
||||
class SpectreConfigurator implements ConfiguratorInterface
|
||||
{
|
||||
/** @var ImportJob */
|
||||
private $job;
|
||||
|
||||
/** @var string */
|
||||
private $warning = '';
|
||||
|
||||
/**
|
||||
* ConfiguratorInterface constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Store any data from the $data array into the job.
|
||||
*
|
||||
* @param array $data
|
||||
*
|
||||
* @return bool
|
||||
*
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function configureJob(array $data): bool
|
||||
{
|
||||
$class = $this->getConfigurationClass();
|
||||
$job = $this->job;
|
||||
/** @var ConfigurationInterface $object */
|
||||
$object = new $class($this->job);
|
||||
$object->setJob($job);
|
||||
$result = $object->storeConfiguration($data);
|
||||
$this->warning = $object->getWarningMessage();
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the data required for the next step in the job configuration.
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function getNextData(): array
|
||||
{
|
||||
$class = $this->getConfigurationClass();
|
||||
$job = $this->job;
|
||||
/** @var ConfigurationInterface $object */
|
||||
$object = app($class);
|
||||
$object->setJob($job);
|
||||
|
||||
return $object->getData();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function getNextView(): string
|
||||
{
|
||||
if (!$this->job->configuration['selected-country']) {
|
||||
return 'import.spectre.select-country';
|
||||
}
|
||||
if (!$this->job->configuration['selected-bank']) {
|
||||
return 'import.spectre.select-bank';
|
||||
}
|
||||
|
||||
throw new FireflyException('No view for state');
|
||||
}
|
||||
|
||||
/**
|
||||
* Return possible warning to user.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getWarningMessage(): string
|
||||
{
|
||||
return $this->warning;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isJobConfigured(): bool
|
||||
{
|
||||
$config = $this->job->configuration;
|
||||
$config['selected-country'] = $config['selected-country'] ?? false;
|
||||
$config['selected-bank'] = $config['selected-bank'] ?? false;
|
||||
$this->job->configuration = $config;
|
||||
$this->job->save();
|
||||
|
||||
if ($config['selected-country'] && $config['selected-bank'] && false) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ImportJob $job
|
||||
*/
|
||||
public function setJob(ImportJob $job)
|
||||
{
|
||||
$this->job = $job;
|
||||
if (null === $this->job->configuration || 0 === count($this->job->configuration)) {
|
||||
Log::debug(sprintf('Gave import job %s initial configuration.', $this->job->key));
|
||||
$this->job->configuration = [
|
||||
'selected-country' => false,
|
||||
];
|
||||
$this->job->save();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function getConfigurationClass(): string
|
||||
{
|
||||
$class = false;
|
||||
switch (true) {
|
||||
case !$this->job->configuration['selected-country']:
|
||||
$class = SelectCountry::class;
|
||||
break;
|
||||
case !$this->job->configuration['selected-bank']:
|
||||
$class = SelectBank::class;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (false === $class || 0 === strlen($class)) {
|
||||
throw new FireflyException('Cannot handle current job state in getConfigurationClass().');
|
||||
}
|
||||
if (!class_exists($class)) {
|
||||
throw new FireflyException(sprintf('Class %s does not exist in getConfigurationClass().', $class));
|
||||
}
|
||||
|
||||
return $class;
|
||||
}
|
||||
}
|
||||
76
app/Support/Import/Configuration/Spectre/SelectBank.php
Normal file
76
app/Support/Import/Configuration/Spectre/SelectBank.php
Normal file
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Support\Import\Configuration\Spectre;
|
||||
|
||||
|
||||
use FireflyIII\Models\ImportJob;
|
||||
use FireflyIII\Models\SpectreProvider;
|
||||
use FireflyIII\Support\Import\Configuration\ConfigurationInterface;
|
||||
|
||||
/**
|
||||
* Class SelectBank
|
||||
*/
|
||||
class SelectBank implements ConfigurationInterface
|
||||
{
|
||||
/** @var ImportJob */
|
||||
private $job;
|
||||
|
||||
/**
|
||||
* Get the data necessary to show the configuration screen.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getData(): array
|
||||
{
|
||||
$config = $this->job->configuration;
|
||||
$selection = SpectreProvider::where('country_code', $config['country'])->where('status', 'active')->get();
|
||||
$providers = [];
|
||||
/** @var SpectreProvider $provider */
|
||||
foreach ($selection as $provider) {
|
||||
$providerId = $provider->spectre_id;
|
||||
$name = $provider->data['name'];
|
||||
$providers[$providerId] = $name;
|
||||
}
|
||||
|
||||
return compact('providers');
|
||||
}
|
||||
|
||||
/**
|
||||
* Return possible warning to user.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getWarningMessage(): string
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ImportJob $job
|
||||
*
|
||||
* @return ConfigurationInterface
|
||||
*/
|
||||
public function setJob(ImportJob $job)
|
||||
{
|
||||
$this->job = $job;
|
||||
}
|
||||
|
||||
/**
|
||||
* Store the result.
|
||||
*
|
||||
* @param array $data
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function storeConfiguration(array $data): bool
|
||||
{
|
||||
$config = $this->job->configuration;
|
||||
$config['bank'] = intval($data['bank_code']) ?? 0; // default to fake country.
|
||||
$config['selected-bank'] = true;
|
||||
$this->job->configuration = $config;
|
||||
$this->job->save();
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
329
app/Support/Import/Configuration/Spectre/SelectCountry.php
Normal file
329
app/Support/Import/Configuration/Spectre/SelectCountry.php
Normal file
@@ -0,0 +1,329 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Support\Import\Configuration\Spectre;
|
||||
|
||||
|
||||
use FireflyIII\Models\ImportJob;
|
||||
use FireflyIII\Models\SpectreProvider;
|
||||
use FireflyIII\Support\Import\Configuration\ConfigurationInterface;
|
||||
|
||||
/**
|
||||
* Class SelectCountry
|
||||
*/
|
||||
class SelectCountry implements ConfigurationInterface
|
||||
{
|
||||
private $allCountries
|
||||
= [
|
||||
'AF' => 'Afghanistan',
|
||||
'AX' => 'Aland Islands',
|
||||
'AL' => 'Albania',
|
||||
'DZ' => 'Algeria',
|
||||
'AS' => 'American Samoa',
|
||||
'AD' => 'Andorra',
|
||||
'AO' => 'Angola',
|
||||
'AI' => 'Anguilla',
|
||||
'AQ' => 'Antarctica',
|
||||
'AG' => 'Antigua and Barbuda',
|
||||
'AR' => 'Argentina',
|
||||
'AM' => 'Armenia',
|
||||
'AW' => 'Aruba',
|
||||
'AU' => 'Australia',
|
||||
'AT' => 'Austria',
|
||||
'AZ' => 'Azerbaijan',
|
||||
'BS' => 'Bahamas',
|
||||
'BH' => 'Bahrain',
|
||||
'BD' => 'Bangladesh',
|
||||
'BB' => 'Barbados',
|
||||
'BY' => 'Belarus',
|
||||
'BE' => 'Belgium',
|
||||
'BZ' => 'Belize',
|
||||
'BJ' => 'Benin',
|
||||
'BM' => 'Bermuda',
|
||||
'BT' => 'Bhutan',
|
||||
'BO' => 'Bolivia',
|
||||
'BQ' => 'Bonaire, Saint Eustatius and Saba',
|
||||
'BA' => 'Bosnia and Herzegovina',
|
||||
'BW' => 'Botswana',
|
||||
'BV' => 'Bouvet Island',
|
||||
'BR' => 'Brazil',
|
||||
'IO' => 'British Indian Ocean Territory',
|
||||
'VG' => 'British Virgin Islands',
|
||||
'BN' => 'Brunei',
|
||||
'BG' => 'Bulgaria',
|
||||
'BF' => 'Burkina Faso',
|
||||
'BI' => 'Burundi',
|
||||
'KH' => 'Cambodia',
|
||||
'CM' => 'Cameroon',
|
||||
'CA' => 'Canada',
|
||||
'CV' => 'Cape Verde',
|
||||
'KY' => 'Cayman Islands',
|
||||
'CF' => 'Central African Republic',
|
||||
'TD' => 'Chad',
|
||||
'CL' => 'Chile',
|
||||
'CN' => 'China',
|
||||
'CX' => 'Christmas Island',
|
||||
'CC' => 'Cocos Islands',
|
||||
'CO' => 'Colombia',
|
||||
'KM' => 'Comoros',
|
||||
'CK' => 'Cook Islands',
|
||||
'CR' => 'Costa Rica',
|
||||
'HR' => 'Croatia',
|
||||
'CU' => 'Cuba',
|
||||
'CW' => 'Curacao',
|
||||
'CY' => 'Cyprus',
|
||||
'CZ' => 'Czech Republic',
|
||||
'CD' => 'Democratic Republic of the Congo',
|
||||
'DK' => 'Denmark',
|
||||
'DJ' => 'Djibouti',
|
||||
'DM' => 'Dominica',
|
||||
'DO' => 'Dominican Republic',
|
||||
'TL' => 'East Timor',
|
||||
'EC' => 'Ecuador',
|
||||
'EG' => 'Egypt',
|
||||
'SV' => 'El Salvador',
|
||||
'GQ' => 'Equatorial Guinea',
|
||||
'ER' => 'Eritrea',
|
||||
'EE' => 'Estonia',
|
||||
'ET' => 'Ethiopia',
|
||||
'FK' => 'Falkland Islands',
|
||||
'FO' => 'Faroe Islands',
|
||||
'FJ' => 'Fiji',
|
||||
'FI' => 'Finland',
|
||||
'FR' => 'France',
|
||||
'GF' => 'French Guiana',
|
||||
'PF' => 'French Polynesia',
|
||||
'TF' => 'French Southern Territories',
|
||||
'GA' => 'Gabon',
|
||||
'GM' => 'Gambia',
|
||||
'GE' => 'Georgia',
|
||||
'DE' => 'Germany',
|
||||
'GH' => 'Ghana',
|
||||
'GI' => 'Gibraltar',
|
||||
'GR' => 'Greece',
|
||||
'GL' => 'Greenland',
|
||||
'GD' => 'Grenada',
|
||||
'GP' => 'Guadeloupe',
|
||||
'GU' => 'Guam',
|
||||
'GT' => 'Guatemala',
|
||||
'GG' => 'Guernsey',
|
||||
'GN' => 'Guinea',
|
||||
'GW' => 'Guinea-Bissau',
|
||||
'GY' => 'Guyana',
|
||||
'HT' => 'Haiti',
|
||||
'HM' => 'Heard Island and McDonald Islands',
|
||||
'HN' => 'Honduras',
|
||||
'HK' => 'Hong Kong',
|
||||
'HU' => 'Hungary',
|
||||
'IS' => 'Iceland',
|
||||
'IN' => 'India',
|
||||
'ID' => 'Indonesia',
|
||||
'IR' => 'Iran',
|
||||
'IQ' => 'Iraq',
|
||||
'IE' => 'Ireland',
|
||||
'IM' => 'Isle of Man',
|
||||
'IL' => 'Israel',
|
||||
'IT' => 'Italy',
|
||||
'CI' => 'Ivory Coast',
|
||||
'JM' => 'Jamaica',
|
||||
'JP' => 'Japan',
|
||||
'JE' => 'Jersey',
|
||||
'JO' => 'Jordan',
|
||||
'KZ' => 'Kazakhstan',
|
||||
'KE' => 'Kenya',
|
||||
'KI' => 'Kiribati',
|
||||
'XK' => 'Kosovo',
|
||||
'KW' => 'Kuwait',
|
||||
'KG' => 'Kyrgyzstan',
|
||||
'LA' => 'Laos',
|
||||
'LV' => 'Latvia',
|
||||
'LB' => 'Lebanon',
|
||||
'LS' => 'Lesotho',
|
||||
'LR' => 'Liberia',
|
||||
'LY' => 'Libya',
|
||||
'LI' => 'Liechtenstein',
|
||||
'LT' => 'Lithuania',
|
||||
'LU' => 'Luxembourg',
|
||||
'MO' => 'Macao',
|
||||
'MK' => 'Macedonia',
|
||||
'MG' => 'Madagascar',
|
||||
'MW' => 'Malawi',
|
||||
'MY' => 'Malaysia',
|
||||
'MV' => 'Maldives',
|
||||
'ML' => 'Mali',
|
||||
'MT' => 'Malta',
|
||||
'MH' => 'Marshall Islands',
|
||||
'MQ' => 'Martinique',
|
||||
'MR' => 'Mauritania',
|
||||
'MU' => 'Mauritius',
|
||||
'YT' => 'Mayotte',
|
||||
'MX' => 'Mexico',
|
||||
'FM' => 'Micronesia',
|
||||
'MD' => 'Moldova',
|
||||
'MC' => 'Monaco',
|
||||
'MN' => 'Mongolia',
|
||||
'ME' => 'Montenegro',
|
||||
'MS' => 'Montserrat',
|
||||
'MA' => 'Morocco',
|
||||
'MZ' => 'Mozambique',
|
||||
'MM' => 'Myanmar',
|
||||
'NA' => 'Namibia',
|
||||
'NR' => 'Nauru',
|
||||
'NP' => 'Nepal',
|
||||
'NL' => 'Netherlands',
|
||||
'NC' => 'New Caledonia',
|
||||
'NZ' => 'New Zealand',
|
||||
'NI' => 'Nicaragua',
|
||||
'NE' => 'Niger',
|
||||
'NG' => 'Nigeria',
|
||||
'NU' => 'Niue',
|
||||
'NF' => 'Norfolk Island',
|
||||
'KP' => 'North Korea',
|
||||
'MP' => 'Northern Mariana Islands',
|
||||
'NO' => 'Norway',
|
||||
'OM' => 'Oman',
|
||||
'PK' => 'Pakistan',
|
||||
'PW' => 'Palau',
|
||||
'PS' => 'Palestinian Territory',
|
||||
'PA' => 'Panama',
|
||||
'PG' => 'Papua New Guinea',
|
||||
'PY' => 'Paraguay',
|
||||
'PE' => 'Peru',
|
||||
'PH' => 'Philippines',
|
||||
'PN' => 'Pitcairn',
|
||||
'PL' => 'Poland',
|
||||
'PT' => 'Portugal',
|
||||
'PR' => 'Puerto Rico',
|
||||
'QA' => 'Qatar',
|
||||
'CG' => 'Republic of the Congo',
|
||||
'RE' => 'Reunion',
|
||||
'RO' => 'Romania',
|
||||
'RU' => 'Russia',
|
||||
'RW' => 'Rwanda',
|
||||
'BL' => 'Saint Barthelemy',
|
||||
'SH' => 'Saint Helena',
|
||||
'KN' => 'Saint Kitts and Nevis',
|
||||
'LC' => 'Saint Lucia',
|
||||
'MF' => 'Saint Martin',
|
||||
'PM' => 'Saint Pierre and Miquelon',
|
||||
'VC' => 'Saint Vincent and the Grenadines',
|
||||
'WS' => 'Samoa',
|
||||
'SM' => 'San Marino',
|
||||
'ST' => 'Sao Tome and Principe',
|
||||
'SA' => 'Saudi Arabia',
|
||||
'SN' => 'Senegal',
|
||||
'RS' => 'Serbia',
|
||||
'SC' => 'Seychelles',
|
||||
'SL' => 'Sierra Leone',
|
||||
'SG' => 'Singapore',
|
||||
'SX' => 'Sint Maarten',
|
||||
'SK' => 'Slovakia',
|
||||
'SI' => 'Slovenia',
|
||||
'SB' => 'Solomon Islands',
|
||||
'SO' => 'Somalia',
|
||||
'ZA' => 'South Africa',
|
||||
'GS' => 'South Georgia and the South Sandwich Islands',
|
||||
'KR' => 'South Korea',
|
||||
'SS' => 'South Sudan',
|
||||
'ES' => 'Spain',
|
||||
'LK' => 'Sri Lanka',
|
||||
'SD' => 'Sudan',
|
||||
'SR' => 'Suriname',
|
||||
'SJ' => 'Svalbard and Jan Mayen',
|
||||
'SZ' => 'Swaziland',
|
||||
'SE' => 'Sweden',
|
||||
'CH' => 'Switzerland',
|
||||
'SY' => 'Syria',
|
||||
'TW' => 'Taiwan',
|
||||
'TJ' => 'Tajikistan',
|
||||
'TZ' => 'Tanzania',
|
||||
'TH' => 'Thailand',
|
||||
'TG' => 'Togo',
|
||||
'TK' => 'Tokelau',
|
||||
'TO' => 'Tonga',
|
||||
'TT' => 'Trinidad and Tobago',
|
||||
'TN' => 'Tunisia',
|
||||
'TR' => 'Turkey',
|
||||
'TM' => 'Turkmenistan',
|
||||
'TC' => 'Turks and Caicos Islands',
|
||||
'TV' => 'Tuvalu',
|
||||
'VI' => 'U.S. Virgin Islands',
|
||||
'UG' => 'Uganda',
|
||||
'UA' => 'Ukraine',
|
||||
'AE' => 'United Arab Emirates',
|
||||
'GB' => 'United Kingdom',
|
||||
'US' => 'United States',
|
||||
'UM' => 'United States Minor Outlying Islands',
|
||||
'UY' => 'Uruguay',
|
||||
'UZ' => 'Uzbekistan',
|
||||
'VU' => 'Vanuatu',
|
||||
'VA' => 'Vatican',
|
||||
'VE' => 'Venezuela',
|
||||
'VN' => 'Vietnam',
|
||||
'WF' => 'Wallis and Futuna',
|
||||
'EH' => 'Western Sahara',
|
||||
'YE' => 'Yemen',
|
||||
'ZM' => 'Zambia',
|
||||
'ZW' => 'Zimbabwe',
|
||||
'XF' => 'Fake Country (for testing)',
|
||||
'XO' => 'Other financial applications',
|
||||
];
|
||||
/** @var ImportJob */
|
||||
private $job;
|
||||
|
||||
/**
|
||||
* Get the data necessary to show the configuration screen.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getData(): array
|
||||
{
|
||||
$providers = SpectreProvider::get();
|
||||
$countries = [];
|
||||
/** @var SpectreProvider $provider */
|
||||
foreach ($providers as $provider) {
|
||||
$countries[$provider->country_code] = $this->allCountries[$provider->country_code] ?? $provider->country_code;
|
||||
}
|
||||
asort($countries);
|
||||
|
||||
return compact('countries');
|
||||
}
|
||||
|
||||
/**
|
||||
* Return possible warning to user.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getWarningMessage(): string
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ImportJob $job
|
||||
*
|
||||
* @return ConfigurationInterface
|
||||
*/
|
||||
public function setJob(ImportJob $job)
|
||||
{
|
||||
$this->job = $job;
|
||||
}
|
||||
|
||||
/**
|
||||
* Store the result.
|
||||
*
|
||||
* @param array $data
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function storeConfiguration(array $data): bool
|
||||
{
|
||||
$config = $this->job->configuration;
|
||||
$config['country'] = $data['country_code'] ?? 'XF'; // default to fake country.
|
||||
$config['selected-country'] = true;
|
||||
$this->job->configuration = $config;
|
||||
$this->job->save();
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
207
app/Support/Import/Information/SpectreInformation.php
Normal file
207
app/Support/Import/Information/SpectreInformation.php
Normal file
@@ -0,0 +1,207 @@
|
||||
<?php
|
||||
/**
|
||||
* SpectreInformation.php
|
||||
* Copyright (c) 2017 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\Support\Import\Information;
|
||||
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Services\Bunq\Object\Alias;
|
||||
use FireflyIII\Services\Bunq\Object\MonetaryAccountBank;
|
||||
use FireflyIII\Services\Bunq\Request\DeleteDeviceSessionRequest;
|
||||
use FireflyIII\Services\Bunq\Request\DeviceSessionRequest;
|
||||
use FireflyIII\Services\Bunq\Request\ListMonetaryAccountRequest;
|
||||
use FireflyIII\Services\Bunq\Request\ListUserRequest;
|
||||
use FireflyIII\Services\Bunq\Token\SessionToken;
|
||||
use FireflyIII\Support\CacheProperties;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Support\Collection;
|
||||
use Log;
|
||||
use Preferences;
|
||||
|
||||
/**
|
||||
* Class SpectreInformation
|
||||
*/
|
||||
class SpectreInformation implements InformationInterface
|
||||
{
|
||||
/** @var User */
|
||||
private $user;
|
||||
|
||||
/**
|
||||
* Returns a collection of accounts. Preferrably, these follow a uniform Firefly III format so they can be managed over banks.
|
||||
*
|
||||
* The format for these bank accounts is basically this:
|
||||
*
|
||||
* id: bank specific id
|
||||
* name: bank appointed name
|
||||
* number: account number (usually IBAN)
|
||||
* currency: ISO code of currency
|
||||
* balance: current balance
|
||||
*
|
||||
*
|
||||
* any other fields are optional but can be useful:
|
||||
* image: logo or account specific thing
|
||||
* color: any associated color.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getAccounts(): array
|
||||
{
|
||||
// cache for an hour:
|
||||
$cache = new CacheProperties;
|
||||
$cache->addProperty('bunq.get-accounts');
|
||||
$cache->addProperty(date('dmy h'));
|
||||
if ($cache->has()) {
|
||||
return $cache->get(); // @codeCoverageIgnore
|
||||
}
|
||||
Log::debug('Now in getAccounts()');
|
||||
$sessionToken = $this->startSession();
|
||||
$userId = $this->getUserInformation($sessionToken);
|
||||
// get list of Bunq accounts:
|
||||
$accounts = $this->getMonetaryAccounts($sessionToken, $userId);
|
||||
$return = [];
|
||||
/** @var MonetaryAccountBank $account */
|
||||
foreach ($accounts as $account) {
|
||||
$current = [
|
||||
'id' => $account->getId(),
|
||||
'name' => $account->getDescription(),
|
||||
'currency' => $account->getCurrency(),
|
||||
'balance' => $account->getBalance()->getValue(),
|
||||
'color' => $account->getSetting()->getColor(),
|
||||
];
|
||||
/** @var Alias $alias */
|
||||
foreach ($account->getAliases() as $alias) {
|
||||
if ('IBAN' === $alias->getType()) {
|
||||
$current['number'] = $alias->getValue();
|
||||
}
|
||||
}
|
||||
$return[] = $current;
|
||||
}
|
||||
$cache->store($return);
|
||||
|
||||
$this->closeSession($sessionToken);
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the user for this Prerequisites-routine. Class is expected to implement and save this.
|
||||
*
|
||||
* @param User $user
|
||||
*/
|
||||
public function setUser(User $user): void
|
||||
{
|
||||
$this->user = $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param SessionToken $sessionToken
|
||||
*/
|
||||
private function closeSession(SessionToken $sessionToken): void
|
||||
{
|
||||
Log::debug('Going to close session');
|
||||
$apiKey = Preferences::getForUser($this->user, 'bunq_api_key')->data;
|
||||
$serverPublicKey = Preferences::getForUser($this->user, 'bunq_server_public_key')->data;
|
||||
$privateKey = Preferences::getForUser($this->user, 'bunq_private_key')->data;
|
||||
$request = new DeleteDeviceSessionRequest();
|
||||
$request->setSecret($apiKey);
|
||||
$request->setPrivateKey($privateKey);
|
||||
$request->setServerPublicKey($serverPublicKey);
|
||||
$request->setSessionToken($sessionToken);
|
||||
$request->call();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param SessionToken $sessionToken
|
||||
* @param int $userId
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
private function getMonetaryAccounts(SessionToken $sessionToken, int $userId): Collection
|
||||
{
|
||||
$apiKey = Preferences::getForUser($this->user, 'bunq_api_key')->data;
|
||||
$serverPublicKey = Preferences::getForUser($this->user, 'bunq_server_public_key')->data;
|
||||
$privateKey = Preferences::getForUser($this->user, 'bunq_private_key')->data;
|
||||
$request = new ListMonetaryAccountRequest;
|
||||
|
||||
$request->setSessionToken($sessionToken);
|
||||
$request->setSecret($apiKey);
|
||||
$request->setServerPublicKey($serverPublicKey);
|
||||
$request->setPrivateKey($privateKey);
|
||||
$request->setUserId($userId);
|
||||
$request->call();
|
||||
|
||||
return $request->getMonetaryAccounts();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param SessionToken $sessionToken
|
||||
*
|
||||
* @return int
|
||||
*
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function getUserInformation(SessionToken $sessionToken): int
|
||||
{
|
||||
$apiKey = Preferences::getForUser($this->user, 'bunq_api_key')->data;
|
||||
$serverPublicKey = Preferences::getForUser($this->user, 'bunq_server_public_key')->data;
|
||||
$privateKey = Preferences::getForUser($this->user, 'bunq_private_key')->data;
|
||||
$request = new ListUserRequest;
|
||||
$request->setSessionToken($sessionToken);
|
||||
$request->setSecret($apiKey);
|
||||
$request->setServerPublicKey($serverPublicKey);
|
||||
$request->setPrivateKey($privateKey);
|
||||
$request->call();
|
||||
// return the first that isn't null?
|
||||
$company = $request->getUserCompany();
|
||||
if ($company->getId() > 0) {
|
||||
return $company->getId();
|
||||
}
|
||||
$user = $request->getUserPerson();
|
||||
if ($user->getId() > 0) {
|
||||
return $user->getId();
|
||||
}
|
||||
throw new FireflyException('Expected user or company from Bunq, but got neither.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return SessionToken
|
||||
*/
|
||||
private function startSession(): SessionToken
|
||||
{
|
||||
Log::debug('Now in startSession.');
|
||||
$apiKey = Preferences::getForUser($this->user, 'bunq_api_key')->data;
|
||||
$serverPublicKey = Preferences::getForUser($this->user, 'bunq_server_public_key')->data;
|
||||
$privateKey = Preferences::getForUser($this->user, 'bunq_private_key')->data;
|
||||
$installationToken = Preferences::getForUser($this->user, 'bunq_installation_token')->data;
|
||||
$request = new DeviceSessionRequest();
|
||||
$request->setSecret($apiKey);
|
||||
$request->setServerPublicKey($serverPublicKey);
|
||||
$request->setPrivateKey($privateKey);
|
||||
$request->setInstallationToken($installationToken);
|
||||
$request->call();
|
||||
$sessionToken = $request->getSessionToken();
|
||||
Log::debug(sprintf('Now have got session token: %s', serialize($sessionToken)));
|
||||
|
||||
return $sessionToken;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
/**
|
||||
* BunqPrerequisites.php
|
||||
* SpectrePrerequisites.php
|
||||
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
||||
*
|
||||
* This file is part of Firefly III.
|
||||
@@ -22,20 +22,16 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Support\Import\Prerequisites;
|
||||
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Jobs\GetSpectreProviders;
|
||||
use FireflyIII\Models\Configuration;
|
||||
use FireflyIII\Models\Preference;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\MessageBag;
|
||||
use Log;
|
||||
use Preferences;
|
||||
use Requests;
|
||||
use Requests_Exception;
|
||||
|
||||
/**
|
||||
* This class contains all the routines necessary to connect to Bunq.
|
||||
* This class contains all the routines necessary to connect to Spectre.
|
||||
*/
|
||||
class SpectrePrerequisites implements PrerequisitesInterface
|
||||
{
|
||||
@@ -158,26 +154,6 @@ class SpectrePrerequisites implements PrerequisitesInterface
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the private key from the users preferences.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function getPrivateKey(): string
|
||||
{
|
||||
Log::debug('get private key');
|
||||
$preference = Preferences::getForUser($this->user, 'spectre_private_key', null);
|
||||
if (null === $preference) {
|
||||
Log::debug('private key is null');
|
||||
// create key pair
|
||||
$this->createKeyPair();
|
||||
}
|
||||
$preference = Preferences::getForUser($this->user, 'spectre_private_key', null);
|
||||
Log::debug('Return private key for user');
|
||||
|
||||
return $preference->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a public key from the users preferences.
|
||||
*
|
||||
@@ -197,33 +173,4 @@ class SpectrePrerequisites implements PrerequisitesInterface
|
||||
|
||||
return $preference->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Request users server remote IP. Let's assume this value will not change any time soon.
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function getRemoteIp(): string
|
||||
{
|
||||
$preference = Preferences::getForUser($this->user, 'external_ip', null);
|
||||
if (null === $preference) {
|
||||
try {
|
||||
$response = Requests::get('https://api.ipify.org');
|
||||
} catch (Requests_Exception $e) {
|
||||
throw new FireflyException(sprintf('Could not retrieve external IP: %s', $e->getMessage()));
|
||||
}
|
||||
if (200 !== $response->status_code) {
|
||||
throw new FireflyException(sprintf('Could not retrieve external IP: %d %s', $response->status_code, $response->body));
|
||||
}
|
||||
$serverIp = $response->body;
|
||||
Preferences::setForUser($this->user, 'external_ip', $serverIp);
|
||||
|
||||
return $serverIp;
|
||||
}
|
||||
|
||||
return $preference->data;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user