|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace LauLamanApps\IzettleApi\Client; |
|
6
|
|
|
|
|
7
|
|
|
use DateTime; |
|
8
|
|
|
use LauLamanApps\IzettleApi\API\Finance\AccountTransaction; |
|
9
|
|
|
use LauLamanApps\IzettleApi\API\Finance\Enum\AccountTypeGroup; |
|
10
|
|
|
use LauLamanApps\IzettleApi\API\Finance\PayoutInfo; |
|
11
|
|
|
use LauLamanApps\IzettleApi\Client\Finance\AccountTransactionParser; |
|
12
|
|
|
use LauLamanApps\IzettleApi\Client\Finance\PayoutInfoParser; |
|
13
|
|
|
use LauLamanApps\IzettleApi\IzettleClientInterface; |
|
14
|
|
|
use Money\Currency; |
|
15
|
|
|
use Money\Money; |
|
16
|
|
|
use Ramsey\Uuid\UuidInterface; |
|
17
|
|
|
|
|
18
|
|
|
final class FinanceClient |
|
19
|
|
|
{ |
|
20
|
|
|
const BASE_URL = 'https://finance.izettle.com/organizations/%s'; |
|
21
|
|
|
|
|
22
|
|
|
const GET_ACCOUNT_TRANSACTIONS = self::BASE_URL . '/accounts/%s/transactions'; |
|
23
|
|
|
const GET_ACCOUNT_BALANCE = self::BASE_URL . '/accounts/%s/balance'; |
|
24
|
|
|
|
|
25
|
|
|
const GET_PAYOUT_INFO = self::BASE_URL . '/payout-info'; |
|
26
|
|
|
/** |
|
27
|
|
|
* @var IzettleClientInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
private $client; |
|
30
|
|
|
/** |
|
31
|
|
|
* @var null|UuidInterface |
|
32
|
|
|
*/ |
|
33
|
|
|
private $organizationUuid; |
|
34
|
|
|
|
|
35
|
|
|
public function __construct( |
|
36
|
|
|
IzettleClientInterface $client, |
|
37
|
|
|
?UuidInterface $organizationUuid = null |
|
38
|
|
|
) { |
|
39
|
|
|
$this->client = $client; |
|
40
|
|
|
$this->organizationUuid = $organizationUuid ? (string) $organizationUuid : 'self'; |
|
|
|
|
|
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @return AccountTransaction[] |
|
45
|
|
|
*/ |
|
46
|
|
|
public function getAccountTransactions( |
|
47
|
|
|
AccountTypeGroup $accountTypeGroup, |
|
48
|
|
|
DateTime $start, |
|
49
|
|
|
DateTime $end, |
|
50
|
|
|
?int $limit = null, |
|
51
|
|
|
?int $offset = null |
|
52
|
|
|
): array { |
|
53
|
|
|
$url = sprintf(self::GET_ACCOUNT_TRANSACTIONS, $this->getOrganizationUuid(), $accountTypeGroup->getValue()); |
|
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
return AccountTransactionParser::createFromResponse( |
|
56
|
|
|
$this->client->get( |
|
57
|
|
|
$url, |
|
58
|
|
|
[ |
|
59
|
|
|
'start' => $start->format('Y-m-d'), |
|
60
|
|
|
'end' => $end->format('Y-m-d'), |
|
61
|
|
|
'limit' => $limit, |
|
62
|
|
|
'offset' => $offset, |
|
63
|
|
|
] |
|
64
|
|
|
) |
|
65
|
|
|
); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function getBalanceInfo(AccountTypeGroup $accountTypeGroup, ?DateTime $at = null): Money |
|
69
|
|
|
{ |
|
70
|
|
|
$url = sprintf(self::GET_ACCOUNT_BALANCE, $this->getOrganizationUuid(), $accountTypeGroup->getValue()); |
|
|
|
|
|
|
71
|
|
|
$response = $this->client->get($url, ['at' => $at ? $at->format('Y-m-d') : null]); |
|
72
|
|
|
$data = json_decode($response->getBody()->getContents(), true)['data']; |
|
73
|
|
|
$currency = new Currency($data['currencyId']); |
|
74
|
|
|
|
|
75
|
|
|
return new Money($data['totalBalance'], $currency); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function getPayoutInfo(?DateTime $at = null): PayoutInfo |
|
79
|
|
|
{ |
|
80
|
|
|
$url = sprintf(self::GET_PAYOUT_INFO, $this->getOrganizationUuid()); |
|
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
return PayoutInfoParser::createFromResponse($this->client->get($url, ['at' => $at ? $at->format('Y-m-d') : null])); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..