mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-25 21:16:47 +00:00
Expand bunq related code.
This commit is contained in:
23
app/Services/Bunq/Object/MonetaryAccountBank.php
Normal file
23
app/Services/Bunq/Object/MonetaryAccountBank.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
/**
|
||||
* MonetaryAccountBank.php
|
||||
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
||||
* This software may be modified and distributed under the terms of the
|
||||
* Creative Commons Attribution-ShareAlike 4.0 International License.
|
||||
*
|
||||
* See the LICENSE file for details.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Services\Bunq\Object;
|
||||
|
||||
/**
|
||||
* Class MonetaryAccountBank
|
||||
*
|
||||
* @package FireflyIII\Services\Bunq\Object
|
||||
*/
|
||||
class MonetaryAccountBank extends BunqObject
|
||||
{
|
||||
|
||||
}
|
||||
34
app/Services/Bunq/Object/UserCompany.php
Normal file
34
app/Services/Bunq/Object/UserCompany.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
/**
|
||||
* UserCompany.php
|
||||
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
||||
* This software may be modified and distributed under the terms of the
|
||||
* Creative Commons Attribution-ShareAlike 4.0 International License.
|
||||
*
|
||||
* See the LICENSE file for details.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Services\Bunq\Object;
|
||||
|
||||
use Carbon\Carbon;
|
||||
|
||||
/**
|
||||
* Class UserCompany
|
||||
*
|
||||
* @package FireflyIII\Services\Bunq\Object
|
||||
*/
|
||||
class UserCompany extends BunqObject
|
||||
{
|
||||
/**
|
||||
* UserCompany constructor.
|
||||
*
|
||||
* @param array $data
|
||||
*/
|
||||
public function __construct(array $data)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -90,6 +90,9 @@ class UserPerson extends BunqObject
|
||||
*/
|
||||
public function __construct(array $data)
|
||||
{
|
||||
if (count($data) === 0) {
|
||||
return;
|
||||
}
|
||||
$this->id = intval($data['id']);
|
||||
$this->created = Carbon::createFromFormat('Y-m-d H:i:s.u', $data['created']);
|
||||
$this->updated = Carbon::createFromFormat('Y-m-d H:i:s.u', $data['updated']);
|
||||
|
||||
@@ -99,23 +99,24 @@ abstract class BunqRequest
|
||||
* @param string $data
|
||||
*
|
||||
* @return string
|
||||
* @throws FireflyException
|
||||
*/
|
||||
protected function generateSignature(string $method, string $uri, array $headers, string $data): string
|
||||
{
|
||||
if (strlen($this->privateKey) === 0) {
|
||||
throw new Exception('No private key present.');
|
||||
throw new FireflyException('No private key present.');
|
||||
}
|
||||
if (strtolower($method) === 'get') {
|
||||
if (strtolower($method) === 'get' || strtolower($method) === 'delete') {
|
||||
$data = '';
|
||||
}
|
||||
|
||||
$uri = str_replace(['https://api.bunq.com', 'https://sandbox.public.api.bunq.com'], '', $uri);
|
||||
$toSign = strtoupper($method) . ' ' . $uri . "\n";
|
||||
$toSign = sprintf("%s %s\n", strtoupper($method), $uri);
|
||||
$headersToSign = ['Cache-Control', 'User-Agent'];
|
||||
ksort($headers);
|
||||
foreach ($headers as $name => $value) {
|
||||
if (in_array($name, $headersToSign) || substr($name, 0, 7) === 'X-Bunq-') {
|
||||
$toSign .= $name . ': ' . $value . "\n";
|
||||
$toSign .= sprintf("%s: %s\n", $name, $value);
|
||||
}
|
||||
}
|
||||
$toSign .= "\n" . $data;
|
||||
@@ -184,6 +185,48 @@ abstract class BunqRequest
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $uri
|
||||
* @param array $headers
|
||||
*
|
||||
* @return array
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function sendSignedBunqDelete(string $uri, array $headers): array
|
||||
{
|
||||
if (strlen($this->server) === 0) {
|
||||
throw new FireflyException('No bunq server defined');
|
||||
}
|
||||
|
||||
$fullUri = $this->server . $uri;
|
||||
$signature = $this->generateSignature('delete', $uri, $headers, '');
|
||||
$headers['X-Bunq-Client-Signature'] = $signature;
|
||||
try {
|
||||
$response = Requests::delete($fullUri, $headers);
|
||||
} catch (Requests_Exception $e) {
|
||||
return ['Error' => [0 => ['error_description' => $e->getMessage(), 'error_description_translated' => $e->getMessage()],]];
|
||||
}
|
||||
|
||||
$body = $response->body;
|
||||
$array = json_decode($body, true);
|
||||
$responseHeaders = $response->headers->getAll();
|
||||
$statusCode = $response->status_code;
|
||||
$array['ResponseHeaders'] = $responseHeaders;
|
||||
$array['ResponseStatusCode'] = $statusCode;
|
||||
|
||||
Log::debug(sprintf('Response to DELETE %s is %s', $fullUri, $body));
|
||||
if ($this->isErrorResponse($array)) {
|
||||
$this->throwResponseError($array);
|
||||
}
|
||||
|
||||
if (!$this->verifyServerSignature($body, $responseHeaders, $statusCode)) {
|
||||
throw new FireflyException(sprintf('Could not verify signature for request to "%s"', $uri));
|
||||
}
|
||||
|
||||
|
||||
return $array;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $uri
|
||||
* @param array $data
|
||||
@@ -208,17 +251,20 @@ abstract class BunqRequest
|
||||
return ['Error' => [0 => ['error_description' => $e->getMessage(), 'error_description_translated' => $e->getMessage()],]];
|
||||
}
|
||||
|
||||
$body = $response->body;
|
||||
$array = json_decode($body, true);
|
||||
$body = $response->body;
|
||||
$array = json_decode($body, true);
|
||||
$responseHeaders = $response->headers->getAll();
|
||||
$statusCode = $response->status_code;
|
||||
$array['ResponseHeaders'] = $responseHeaders;
|
||||
$array['ResponseStatusCode'] = $statusCode;
|
||||
|
||||
if ($this->isErrorResponse($array)) {
|
||||
$this->throwResponseError($array);
|
||||
}
|
||||
$responseHeaders = $response->headers->getAll();
|
||||
$statusCode = $response->status_code;
|
||||
|
||||
if (!$this->verifyServerSignature($body, $responseHeaders, $statusCode)) {
|
||||
throw new FireflyException(sprintf('Could not verify signature for request to "%s"', $uri));
|
||||
}
|
||||
$array['ResponseHeaders'] = $responseHeaders;
|
||||
|
||||
return $array;
|
||||
}
|
||||
@@ -243,17 +289,49 @@ abstract class BunqRequest
|
||||
return ['Error' => [0 => ['error_description' => $e->getMessage(), 'error_description_translated' => $e->getMessage()],]];
|
||||
}
|
||||
|
||||
$body = $response->body;
|
||||
$array = json_decode($body, true);
|
||||
$body = $response->body;
|
||||
$array = json_decode($body, true);
|
||||
$responseHeaders = $response->headers->getAll();
|
||||
$statusCode = $response->status_code;
|
||||
$array['ResponseHeaders'] = $responseHeaders;
|
||||
$array['ResponseStatusCode'] = $statusCode;
|
||||
|
||||
if ($this->isErrorResponse($array)) {
|
||||
$this->throwResponseError($array);
|
||||
}
|
||||
$responseHeaders = $response->headers->getAll();
|
||||
$statusCode = $response->status_code;
|
||||
|
||||
if (!$this->verifyServerSignature($body, $responseHeaders, $statusCode)) {
|
||||
throw new FireflyException(sprintf('Could not verify signature for request to "%s"', $uri));
|
||||
}
|
||||
$array['ResponseHeaders'] = $responseHeaders;
|
||||
|
||||
|
||||
return $array;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $uri
|
||||
* @param array $headers
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function sendUnsignedBunqDelete(string $uri, array $headers): array
|
||||
{
|
||||
$fullUri = $this->server . $uri;
|
||||
try {
|
||||
$response = Requests::delete($fullUri, $headers);
|
||||
} catch (Requests_Exception $e) {
|
||||
return ['Error' => [0 => ['error_description' => $e->getMessage(), 'error_description_translated' => $e->getMessage()],]];
|
||||
}
|
||||
$body = $response->body;
|
||||
$array = json_decode($body, true);
|
||||
$responseHeaders = $response->headers->getAll();
|
||||
$statusCode = $response->status_code;
|
||||
$array['ResponseHeaders'] = $responseHeaders;
|
||||
$array['ResponseStatusCode'] = $statusCode;
|
||||
|
||||
if ($this->isErrorResponse($array)) {
|
||||
$this->throwResponseError($array);
|
||||
}
|
||||
|
||||
return $array;
|
||||
}
|
||||
@@ -274,13 +352,17 @@ abstract class BunqRequest
|
||||
} catch (Requests_Exception $e) {
|
||||
return ['Error' => [0 => ['error_description' => $e->getMessage(), 'error_description_translated' => $e->getMessage()],]];
|
||||
}
|
||||
$body = $response->body;
|
||||
$responseHeaders = $response->headers->getAll();
|
||||
$array = json_decode($body, true);
|
||||
$body = $response->body;
|
||||
$array = json_decode($body, true);
|
||||
$responseHeaders = $response->headers->getAll();
|
||||
$statusCode = $response->status_code;
|
||||
$array['ResponseHeaders'] = $responseHeaders;
|
||||
$array['ResponseStatusCode'] = $statusCode;
|
||||
|
||||
if ($this->isErrorResponse($array)) {
|
||||
$this->throwResponseError($array);
|
||||
}
|
||||
$array['ResponseHeaders'] = $responseHeaders;
|
||||
|
||||
|
||||
return $array;
|
||||
}
|
||||
@@ -313,7 +395,7 @@ abstract class BunqRequest
|
||||
$message[] = $error['error_description'];
|
||||
}
|
||||
}
|
||||
throw new FireflyException(join(', ', $message));
|
||||
throw new FireflyException('Bunq ERROR ' . $response['ResponseStatusCode'] . ': ' . join(', ', $message));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
49
app/Services/Bunq/Request/DeleteDeviceSessionRequest.php
Normal file
49
app/Services/Bunq/Request/DeleteDeviceSessionRequest.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
/**
|
||||
* DeleteDeviceSessionRequest.php
|
||||
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
||||
* This software may be modified and distributed under the terms of the
|
||||
* Creative Commons Attribution-ShareAlike 4.0 International License.
|
||||
*
|
||||
* See the LICENSE file for details.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Services\Bunq\Request;
|
||||
|
||||
use FireflyIII\Services\Bunq\Token\InstallationToken;
|
||||
use FireflyIII\Services\Bunq\Token\SessionToken;
|
||||
use Log;
|
||||
|
||||
/**
|
||||
* Class DeleteDeviceSessionRequest
|
||||
*
|
||||
* @package FireflyIII\Services\Bunq\Request
|
||||
*/
|
||||
class DeleteDeviceSessionRequest extends BunqRequest
|
||||
{
|
||||
/** @var SessionToken */
|
||||
private $sessionToken;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function call(): void
|
||||
{
|
||||
Log::debug('Going to send bunq delete session request.');
|
||||
$uri = sprintf('/v1/session/%d', $this->sessionToken->getId());
|
||||
$headers = $this->getDefaultHeaders();
|
||||
$headers['X-Bunq-Client-Authentication'] = $this->sessionToken->getToken();
|
||||
$this->sendSignedBunqDelete($uri, $headers);
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param SessionToken $sessionToken
|
||||
*/
|
||||
public function setSessionToken(SessionToken $sessionToken)
|
||||
{
|
||||
$this->sessionToken = $sessionToken;
|
||||
}
|
||||
}
|
||||
149
app/Services/Bunq/Request/DeviceSessionRequest.php
Normal file
149
app/Services/Bunq/Request/DeviceSessionRequest.php
Normal file
@@ -0,0 +1,149 @@
|
||||
<?php
|
||||
/**
|
||||
* DeviceSessionRequest.php
|
||||
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
||||
* This software may be modified and distributed under the terms of the
|
||||
* Creative Commons Attribution-ShareAlike 4.0 International License.
|
||||
*
|
||||
* See the LICENSE file for details.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Services\Bunq\Request;
|
||||
|
||||
|
||||
use FireflyIII\Services\Bunq\Id\DeviceSessionId;
|
||||
use FireflyIII\Services\Bunq\Object\UserCompany;
|
||||
use FireflyIII\Services\Bunq\Object\UserPerson;
|
||||
use FireflyIII\Services\Bunq\Token\InstallationToken;
|
||||
use FireflyIII\Services\Bunq\Token\SessionToken;
|
||||
use Log;
|
||||
|
||||
/**
|
||||
* Class DeviceSessionRequest
|
||||
*
|
||||
* @package FireflyIII\Services\Bunq\Request
|
||||
*/
|
||||
class DeviceSessionRequest extends BunqRequest
|
||||
{
|
||||
/** @var DeviceSessionId */
|
||||
private $deviceSessionId;
|
||||
/** @var InstallationToken */
|
||||
private $installationToken;
|
||||
/** @var SessionToken */
|
||||
private $sessionToken;
|
||||
/** @var UserCompany */
|
||||
private $userCompany;
|
||||
/** @var UserPerson */
|
||||
private $userPerson;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function call(): void
|
||||
{
|
||||
$uri = '/v1/session-server';
|
||||
$data = ['secret' => $this->secret];
|
||||
$headers = $this->getDefaultHeaders();
|
||||
$headers['X-Bunq-Client-Authentication'] = $this->installationToken->getToken();
|
||||
$response = $this->sendSignedBunqPost($uri, $data, $headers);
|
||||
|
||||
|
||||
$this->deviceSessionId = $this->extractDeviceSessionId($response);
|
||||
$this->sessionToken = $this->extractSessionToken($response);
|
||||
$this->userPerson = $this->extractUserPerson($response);
|
||||
$this->userCompany = $this->extractUserCompany($response);
|
||||
|
||||
Log::debug(sprintf('Session ID: %s', serialize($this->deviceSessionId)));
|
||||
Log::debug(sprintf('Session token: %s', serialize($this->sessionToken)));
|
||||
Log::debug(sprintf('Session user person: %s', serialize($this->userPerson)));
|
||||
Log::debug(sprintf('Session user company: %s', serialize($this->userCompany)));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return DeviceSessionId
|
||||
*/
|
||||
public function getDeviceSessionId(): DeviceSessionId
|
||||
{
|
||||
return $this->deviceSessionId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return SessionToken
|
||||
*/
|
||||
public function getSessionToken(): SessionToken
|
||||
{
|
||||
return $this->sessionToken;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return UserPerson
|
||||
*/
|
||||
public function getUserPerson(): UserPerson
|
||||
{
|
||||
return $this->userPerson;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param InstallationToken $installationToken
|
||||
*/
|
||||
public function setInstallationToken(InstallationToken $installationToken)
|
||||
{
|
||||
$this->installationToken = $installationToken;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $response
|
||||
*
|
||||
* @return DeviceSessionId
|
||||
*/
|
||||
private function extractDeviceSessionId(array $response): DeviceSessionId
|
||||
{
|
||||
$data = $this->getKeyFromResponse('Id', $response);
|
||||
$deviceSessionId = new DeviceSessionId;
|
||||
$deviceSessionId->setId(intval($data['id']));
|
||||
|
||||
return $deviceSessionId;
|
||||
}
|
||||
|
||||
private function extractSessionToken(array $response): SessionToken
|
||||
{
|
||||
$data = $this->getKeyFromResponse('Token', $response);
|
||||
$sessionToken = new SessionToken($data);
|
||||
|
||||
return $sessionToken;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $response
|
||||
*
|
||||
* @return UserCompany
|
||||
*/
|
||||
private function extractUserCompany($response): UserCompany
|
||||
{
|
||||
$data = $this->getKeyFromResponse('UserCompany', $response);
|
||||
$userCompany = new UserCompany($data);
|
||||
|
||||
|
||||
return $userCompany;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $response
|
||||
*
|
||||
* @return UserPerson
|
||||
*/
|
||||
private function extractUserPerson($response): UserPerson
|
||||
{
|
||||
$data = $this->getKeyFromResponse('UserPerson', $response);
|
||||
$userPerson = new UserPerson($data);
|
||||
|
||||
|
||||
return $userPerson;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user