From 1319cb2e4e583fb62b63a12b9a27c3006f199520 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sun, 27 Aug 2017 08:54:58 +0200 Subject: [PATCH] Improving bunq import. --- .../Controllers/Import/BankController.php | 48 ++++++++- app/Services/Bunq/Object/Alias.php | 1 - app/Services/Bunq/Object/Amount.php | 1 - .../Bunq/Object/MonetaryAccountBank.php | 98 +++++++++---------- .../Bunq/Object/MonetaryAccountProfile.php | 1 + .../Bunq/Object/MonetaryAccountSetting.php | 1 + .../Bunq/Object/NotificationFilter.php | 3 +- app/Services/Bunq/Object/ServerPublicKey.php | 19 ++-- app/Services/Bunq/Object/UserCompany.php | 1 - app/Services/Bunq/Object/UserLight.php | 2 +- app/Services/Bunq/Request/BunqRequest.php | 9 +- .../Import/Information/BunqInformation.php | 21 ++-- .../Prerequisites/BunqPrerequisites.php | 3 - config/firefly.php | 3 + resources/views/import/bank/form.twig | 69 +++++++++++++ .../views/import/bunq/prerequisites.twig | 2 +- 16 files changed, 195 insertions(+), 87 deletions(-) create mode 100644 resources/views/import/bank/form.twig diff --git a/app/Http/Controllers/Import/BankController.php b/app/Http/Controllers/Import/BankController.php index d8a0279063..1e853620b5 100644 --- a/app/Http/Controllers/Import/BankController.php +++ b/app/Http/Controllers/Import/BankController.php @@ -21,6 +21,7 @@ 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, @@ -42,6 +43,49 @@ class BankController extends Controller $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. + * + * @param Request $request + * @param string $bank + * + * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector + */ + public function postForm(Request $request, string $bank) + { + + $class = config(sprintf('firefly.import_pre.%s', $bank)); + /** @var PrerequisitesInterface $object */ + $object = app($class); + $object->setUser(auth()->user()); + + if ($object->hasPrerequisites()) { + return redirect(route('import.bank.prerequisites', [$bank])); + } + $remoteAccounts = $request->get('do_import'); + if (!is_array($remoteAccounts) || count($remoteAccounts) === 0) { + 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)); + + var_dump($remoteAccounts);exit; + + // get import file + + // get import config + + + } /** @@ -66,6 +110,7 @@ class BankController extends Controller $object->setUser(auth()->user()); if (!$object->hasPrerequisites()) { Log::debug(sprintf('No more prerequisites for %s, move to form.', $bank)); + return redirect(route('import.bank.form', [$bank])); } Log::debug('Going to store entered preprerequisites.'); @@ -102,7 +147,8 @@ class BankController extends Controller return view($view, $parameters); } - return redirect(route('import.bank.form', [$bank])); + + return redirect(route('import.bank.form', [$bank])); } } diff --git a/app/Services/Bunq/Object/Alias.php b/app/Services/Bunq/Object/Alias.php index 7d1703eabc..7b0ed84ea3 100644 --- a/app/Services/Bunq/Object/Alias.php +++ b/app/Services/Bunq/Object/Alias.php @@ -65,5 +65,4 @@ class Alias extends BunqObject } - } \ No newline at end of file diff --git a/app/Services/Bunq/Object/Amount.php b/app/Services/Bunq/Object/Amount.php index 857e85f304..cd7782a0ad 100644 --- a/app/Services/Bunq/Object/Amount.php +++ b/app/Services/Bunq/Object/Amount.php @@ -54,5 +54,4 @@ class Amount extends BunqObject } - } \ No newline at end of file diff --git a/app/Services/Bunq/Object/MonetaryAccountBank.php b/app/Services/Bunq/Object/MonetaryAccountBank.php index 0295983581..5082c858e0 100644 --- a/app/Services/Bunq/Object/MonetaryAccountBank.php +++ b/app/Services/Bunq/Object/MonetaryAccountBank.php @@ -62,56 +62,6 @@ class MonetaryAccountBank extends BunqObject /** @var int */ private $userId = 0; - /** - * @return string - */ - public function getDescription(): string - { - return $this->description; - } - - /** - * @return int - */ - public function getId(): int - { - return $this->id; - } - - /** - * @return array - */ - public function getAliases(): array - { - return $this->aliases; - } - - /** - * @return string - */ - public function getCurrency(): string - { - return $this->currency; - } - - /** - * @return Amount - */ - public function getBalance(): Amount - { - return $this->balance; - } - - /** - * @return MonetaryAccountSetting - */ - public function getSetting(): MonetaryAccountSetting - { - return $this->setting; - } - - - /** * MonetaryAccountBank constructor. * @@ -150,4 +100,52 @@ class MonetaryAccountBank extends BunqObject return; } + /** + * @return array + */ + public function getAliases(): array + { + return $this->aliases; + } + + /** + * @return Amount + */ + public function getBalance(): Amount + { + return $this->balance; + } + + /** + * @return string + */ + public function getCurrency(): string + { + return $this->currency; + } + + /** + * @return string + */ + public function getDescription(): string + { + return $this->description; + } + + /** + * @return int + */ + public function getId(): int + { + return $this->id; + } + + /** + * @return MonetaryAccountSetting + */ + public function getSetting(): MonetaryAccountSetting + { + return $this->setting; + } + } \ No newline at end of file diff --git a/app/Services/Bunq/Object/MonetaryAccountProfile.php b/app/Services/Bunq/Object/MonetaryAccountProfile.php index e4995ea19c..a19d3403ee 100644 --- a/app/Services/Bunq/Object/MonetaryAccountProfile.php +++ b/app/Services/Bunq/Object/MonetaryAccountProfile.php @@ -37,6 +37,7 @@ class MonetaryAccountProfile extends BunqObject $this->profileFill = null; $this->profileActionRequired = $data['profile_action_required']; $this->profileAmountRequired = new Amount($data['profile_amount_required']); + return; } diff --git a/app/Services/Bunq/Object/MonetaryAccountSetting.php b/app/Services/Bunq/Object/MonetaryAccountSetting.php index e1277c3dbc..38fe8db75d 100644 --- a/app/Services/Bunq/Object/MonetaryAccountSetting.php +++ b/app/Services/Bunq/Object/MonetaryAccountSetting.php @@ -36,6 +36,7 @@ class MonetaryAccountSetting extends BunqObject $this->color = $data['color']; $this->defaultAvatarStatus = $data['default_avatar_status']; $this->restrictionChat = $data['restriction_chat']; + return; } diff --git a/app/Services/Bunq/Object/NotificationFilter.php b/app/Services/Bunq/Object/NotificationFilter.php index f12843c159..06bf96b01e 100644 --- a/app/Services/Bunq/Object/NotificationFilter.php +++ b/app/Services/Bunq/Object/NotificationFilter.php @@ -24,7 +24,8 @@ class NotificationFilter extends BunqObject * * @param array $data */ - public function __construct(array $data) { + public function __construct(array $data) + { var_dump($data); exit; } diff --git a/app/Services/Bunq/Object/ServerPublicKey.php b/app/Services/Bunq/Object/ServerPublicKey.php index 78426d34d1..56aa17654b 100644 --- a/app/Services/Bunq/Object/ServerPublicKey.php +++ b/app/Services/Bunq/Object/ServerPublicKey.php @@ -27,16 +27,9 @@ class ServerPublicKey extends BunqObject * * @param array $response */ - public function __construct(array $response) { - $this->publicKey = $response['server_public_key']; - } - - /** - * @param string $publicKey - */ - public function setPublicKey(string $publicKey) + public function __construct(array $response) { - $this->publicKey = $publicKey; + $this->publicKey = $response['server_public_key']; } /** @@ -47,5 +40,13 @@ class ServerPublicKey extends BunqObject return $this->publicKey; } + /** + * @param string $publicKey + */ + public function setPublicKey(string $publicKey) + { + $this->publicKey = $publicKey; + } + } \ No newline at end of file diff --git a/app/Services/Bunq/Object/UserCompany.php b/app/Services/Bunq/Object/UserCompany.php index 57c250773a..a0efcaf876 100644 --- a/app/Services/Bunq/Object/UserCompany.php +++ b/app/Services/Bunq/Object/UserCompany.php @@ -103,5 +103,4 @@ class UserCompany extends BunqObject } - } \ No newline at end of file diff --git a/app/Services/Bunq/Object/UserLight.php b/app/Services/Bunq/Object/UserLight.php index fe47df54a7..baed9d9620 100644 --- a/app/Services/Bunq/Object/UserLight.php +++ b/app/Services/Bunq/Object/UserLight.php @@ -51,7 +51,7 @@ class UserLight extends BunqObject */ public function __construct(array $data) { - if(count($data) === 0) { + if (count($data) === 0) { return; } $this->id = intval($data['id']); diff --git a/app/Services/Bunq/Request/BunqRequest.php b/app/Services/Bunq/Request/BunqRequest.php index 629378377f..0046d2f35b 100644 --- a/app/Services/Bunq/Request/BunqRequest.php +++ b/app/Services/Bunq/Request/BunqRequest.php @@ -45,6 +45,7 @@ abstract class BunqRequest */ public function __construct() { + $this->server = config('firefly.bunq.server'); } /** @@ -60,14 +61,6 @@ abstract class BunqRequest return $this->server; } - /** - * @param string $server - */ - public function setServer(string $server) - { - $this->server = $server; - } - /** * @param string $privateKey */ diff --git a/app/Support/Import/Information/BunqInformation.php b/app/Support/Import/Information/BunqInformation.php index 9acf363f2e..6d0c955280 100644 --- a/app/Support/Import/Information/BunqInformation.php +++ b/app/Support/Import/Information/BunqInformation.php @@ -13,6 +13,7 @@ declare(strict_types=1); namespace FireflyIII\Support\Import\Information; use FireflyIII\Exceptions\FireflyException; +use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; use FireflyIII\Services\Bunq\Object\Alias; use FireflyIII\Services\Bunq\Object\MonetaryAccountBank; use FireflyIII\Services\Bunq\Request\DeleteDeviceSessionRequest; @@ -20,6 +21,7 @@ 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; @@ -56,22 +58,28 @@ class BunqInformation implements InformationInterface */ 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(); $id = $this->getUserInformation($sessionToken); - // get list of Bunq accounts: $accounts = $this->getMonetaryAccounts($sessionToken, $id); $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) { @@ -81,6 +89,7 @@ class BunqInformation implements InformationInterface } $return[] = $current; } + $cache->store($return); $this->closeSession($sessionToken); @@ -105,11 +114,9 @@ class BunqInformation implements InformationInterface 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; - $server = config('firefly.bunq.server'); $privateKey = Preferences::getForUser($this->user, 'bunq_private_key')->data; $request = new DeleteDeviceSessionRequest(); $request->setSecret($apiKey); - $request->setServer($server); $request->setPrivateKey($privateKey); $request->setServerPublicKey($serverPublicKey); $request->setSessionToken($sessionToken); @@ -128,14 +135,12 @@ class BunqInformation implements InformationInterface { $apiKey = Preferences::getForUser($this->user, 'bunq_api_key')->data; $serverPublicKey = Preferences::getForUser($this->user, 'bunq_server_public_key')->data; - $server = config('firefly.bunq.server'); $privateKey = Preferences::getForUser($this->user, 'bunq_private_key')->data; $request = new ListMonetaryAccountRequest; $request->setSessionToken($sessionToken); $request->setSecret($apiKey); $request->setServerPublicKey($serverPublicKey); - $request->setServer($server); $request->setPrivateKey($privateKey); $request->setUserId($userId); $request->call(); @@ -154,13 +159,11 @@ class BunqInformation implements InformationInterface { $apiKey = Preferences::getForUser($this->user, 'bunq_api_key')->data; $serverPublicKey = Preferences::getForUser($this->user, 'bunq_server_public_key')->data; - $server = config('firefly.bunq.server'); $privateKey = Preferences::getForUser($this->user, 'bunq_private_key')->data; $request = new ListUserRequest; $request->setSessionToken($sessionToken); $request->setSecret($apiKey); $request->setServerPublicKey($serverPublicKey); - $request->setServer($server); $request->setPrivateKey($privateKey); $request->call(); // return the first that isn't null? @@ -183,13 +186,11 @@ class BunqInformation implements InformationInterface Log::debug('Now in startSession.'); $apiKey = Preferences::getForUser($this->user, 'bunq_api_key')->data; $serverPublicKey = Preferences::getForUser($this->user, 'bunq_server_public_key')->data; - $server = config('firefly.bunq.server'); $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->setServer($server); $request->setPrivateKey($privateKey); $request->setInstallationToken($installationToken); $request->call(); diff --git a/app/Support/Import/Prerequisites/BunqPrerequisites.php b/app/Support/Import/Prerequisites/BunqPrerequisites.php index 022d1e44b9..a83d4ec4c8 100644 --- a/app/Support/Import/Prerequisites/BunqPrerequisites.php +++ b/app/Support/Import/Prerequisites/BunqPrerequisites.php @@ -157,7 +157,6 @@ class BunqPrerequisites implements PrerequisitesInterface $request->setInstallationToken($installationToken); $request->setServerPublicKey($serverPublicKey); $request->setPrivateKey($this->getPrivateKey()); - $request->setServer(config('firefly.bunq.server')); $request->call(); $devices = $request->getDevices(); /** @var DeviceServer $device */ @@ -186,7 +185,6 @@ class BunqPrerequisites implements PrerequisitesInterface // verify bunq api code: $publicKey = $this->getPublicKey(); $request = new InstallationTokenRequest; - $request->setServer(strval(config('firefly.bunq.server'))); $request->setPublicKey($publicKey); $request->call(); Log::debug('Sent request'); @@ -301,7 +299,6 @@ class BunqPrerequisites implements PrerequisitesInterface $serverPublicKey = $this->getServerPublicKey(); $apiKey = Preferences::getForUser($this->user, 'bunq_api_key', ''); $request = new DeviceServerRequest; - $request->setServer(strval(config('firefly.bunq.server'))); $request->setPrivateKey($this->getPrivateKey()); $request->setDescription('Firefly III v' . config('firefly.version') . ' for ' . $this->user->email); $request->setSecret($apiKey->data); diff --git a/config/firefly.php b/config/firefly.php index 51c26ecb91..2ecb8a3782 100644 --- a/config/firefly.php +++ b/config/firefly.php @@ -45,6 +45,9 @@ return [ 'import_info' => [ 'bunq' => 'FireflyIII\Support\Import\Information\BunqInformation', ], + 'import_transactions' => [ + 'bunq' => 'FireflyIII\Support\Import\Transactions\BunqTransactions', + ], 'bunq' => [ 'server' => 'https://sandbox.public.api.bunq.com', ], diff --git a/resources/views/import/bank/form.twig b/resources/views/import/bank/form.twig new file mode 100644 index 0000000000..17bd9ac029 --- /dev/null +++ b/resources/views/import/bank/form.twig @@ -0,0 +1,69 @@ +{% extends "./layout/default" %} + +{% block breadcrumbs %} + {{ Breadcrumbs.renderIfExists }} +{% endblock %} +{% block content %} +
+
+ +
+
+
+

{{ trans('bank.bank_form_title') }}

+
+
+
+
+

+ {{ trans('bank.bank_form_text') }} +

+
+
+
+
+ + + + + + + + + + {% for remoteAccount in remoteAccounts %} + + + + + + {% endfor %} + +
AccountCurrent balance
+ + + + {{ remoteAccount.name }} + +
{{ remoteAccount.number }} +
+ {{ remoteAccount.currency }} + {{ remoteAccount.balance }} +
+
+
+
+ +
+
+
+
+{% endblock %} +{% block scripts %} +{% endblock %} +{% block styles %} +{% endblock %} diff --git a/resources/views/import/bunq/prerequisites.twig b/resources/views/import/bunq/prerequisites.twig index aafe01c93d..a70da151d1 100644 --- a/resources/views/import/bunq/prerequisites.twig +++ b/resources/views/import/bunq/prerequisites.twig @@ -5,7 +5,7 @@ {% endblock %} {% block content %}
-
+