diff --git a/src/Model/AccountBalance.php b/src/Model/AccountBalance.php new file mode 100644 index 0000000..2dea223 --- /dev/null +++ b/src/Model/AccountBalance.php @@ -0,0 +1,52 @@ + */ + private $balances; + + /** + * @param non-empty-string $iban + * @param non-empty-string $accountName + * @param array $balances key is ISO 4217 currency code, value is balance in smallest unit of currency + */ + public function __construct( + $iban, + $accountName, + array $balances + ) { + $this->iban = $iban; + $this->accountName = $accountName; + $this->balances = $balances; + } + + /** + * @return non-empty-string + */ + public function getIban() + { + return $this->iban; + } + + /** + * @return non-empty-string + */ + public function getAccountName() + { + return $this->accountName; + } + + /** + * @return array + */ + public function getBalances() + { + return $this->balances; + } +} diff --git a/src/Service/ApiService.php b/src/Service/ApiService.php index d1119d7..4c081ca 100644 --- a/src/Service/ApiService.php +++ b/src/Service/ApiService.php @@ -7,6 +7,7 @@ use ThePay\ApiClient\Filter\TransactionFilter; use ThePay\ApiClient\Http\HttpResponse; use ThePay\ApiClient\Http\HttpServiceInterface; +use ThePay\ApiClient\Model\AccountBalance; use ThePay\ApiClient\Model\ApiResponse; use ThePay\ApiClient\Model\Collection\PaymentCollection; use ThePay\ApiClient\Model\Collection\PaymentMethodCollection; @@ -108,6 +109,47 @@ public function getActivePaymentMethods(LanguageCode $languageCode = null) return new PaymentMethodCollection($response->getBody()); } + /** + * @see https://dataapi21.docs.apiary.io/#reference/data-retrieval/transactions/get-balance-history + * + * @param int|null $projectId + * + * @return array + */ + public function getAccountsBalances(StringValue $accountIban = null, $projectId = null, \DateTime $balanceAt = null) + { + $arguments = array(); + if ($accountIban !== null) { + $arguments['account_iban'] = $accountIban->getValue(); + } + if ($projectId !== null) { + $arguments['project_id'] = $projectId; + } + if ($balanceAt) { + $arguments['balance_at'] = $balanceAt->format(\DateTime::ATOM); + } + + $url = $this->url(array('balances'), $arguments, false); + $response = $this->httpService->get($url); + + if ($response->getCode() !== 200) { + throw $this->buildException($url, $response); + } + + $responseArray = Json::decode($response->getBody(), true); + + return array_map( + static function (array $accountBalance) { + return new AccountBalance( + $accountBalance['iban'], + $accountBalance['name'], + $accountBalance['balance'] + ); + }, + $responseArray + ); + } + /** * @see https://dataapi21.docs.apiary.io/#reference/0/merchant-level-resources/get-account-transaction-history * diff --git a/src/Service/ApiServiceInterface.php b/src/Service/ApiServiceInterface.php index 6fee368..2ea52e1 100644 --- a/src/Service/ApiServiceInterface.php +++ b/src/Service/ApiServiceInterface.php @@ -6,6 +6,7 @@ use ThePay\ApiClient\Filter\PaymentsFilter; use ThePay\ApiClient\Filter\TransactionFilter; use ThePay\ApiClient\Http\HttpServiceInterface; +use ThePay\ApiClient\Model\AccountBalance; use ThePay\ApiClient\Model\ApiResponse; use ThePay\ApiClient\Model\Collection\PaymentCollection; use ThePay\ApiClient\Model\Collection\PaymentMethodCollection; @@ -105,6 +106,15 @@ public function realizePaymentBySavedAuthorization(Identifier $parentPaymentUid, */ public function getPayments(PaymentsFilter $filter, $page = 1, $limit = null); + /** + * @see https://dataapi21.docs.apiary.io/#reference/data-retrieval/transactions/get-balance-history + * + * @param int|null $projectId + * + * @return array + */ + public function getAccountsBalances(StringValue $accountIban = null, $projectId = null, \DateTime $balanceAt = null); + /** * @param TransactionFilter $filter * @param int $page diff --git a/src/TheClient.php b/src/TheClient.php index 6e49160..eb696ae 100644 --- a/src/TheClient.php +++ b/src/TheClient.php @@ -10,6 +10,7 @@ use ThePay\ApiClient\Filter\TransactionFilter; use ThePay\ApiClient\Http\HttpCurlService; use ThePay\ApiClient\Http\HttpServiceInterface; +use ThePay\ApiClient\Model\AccountBalance; use ThePay\ApiClient\Model\ApiResponse; use ThePay\ApiClient\Model\Collection\PaymentCollection; use ThePay\ApiClient\Model\Collection\PaymentMethodCollection; @@ -82,6 +83,23 @@ public function getProjects() return $this->api->getProjects(); } + /** + * @see https://dataapi21.docs.apiary.io/#reference/data-retrieval/transactions/get-balance-history + * + * @param string|null $accountIban + * @param int|null $projectId + * + * @return array + */ + public function getAccountsBalances($accountIban = null, $projectId = null, \DateTime $balanceAt = null) + { + return $this->api->getAccountsBalances( + $accountIban !== null ? new StringValue($accountIban) : null, + $projectId, + $balanceAt + ); + } + /** * @param TransactionFilter $filter * @param int $page diff --git a/tests/GetAccountsBalancesTest.php b/tests/GetAccountsBalancesTest.php new file mode 100644 index 0000000..594101d --- /dev/null +++ b/tests/GetAccountsBalancesTest.php @@ -0,0 +1,56 @@ +once() + ->with($this->config->getApiUrl() . 'balances?account_iban=TP7811112150822790787055&project_id=1&balance_at=2023-03-14T15%3A08%3A44%2B00%3A00&merchant_id=' . self::MERCHANT_ID) + ->andReturn( + new HttpResponse( + null, + 200, + '', + null, + '[ + { + "iban": "TP7811112150822790787055", + "name": "Test", + "balance": { + "CZK": "45899", + "EUR": "500" + } + } + ]' + ) + ); + + $client = new TheClient($this->config, null, $httpService); + + $balances = $client->getAccountsBalances('TP7811112150822790787055', 1, new \DateTime('2023-03-14 15:08:44+00:00')); + self::assertEquals( + array( + new AccountBalance( + 'TP7811112150822790787055', + 'Test', + array('CZK' => '45899', 'EUR' => '500') + ), + ), + $balances + ); + + \Mockery::close(); + } +} diff --git a/tests/Mocks/Service/ApiMockService.php b/tests/Mocks/Service/ApiMockService.php index 09febb5..f2d477e 100644 --- a/tests/Mocks/Service/ApiMockService.php +++ b/tests/Mocks/Service/ApiMockService.php @@ -627,4 +627,9 @@ public function generatePaymentConfirmationPdf(Identifier $uid, LanguageCode $la { return 'test content'; } + + public function getAccountsBalances(StringValue $accountIban = null, $projectId = null, \DateTime $balanceAt = null) + { + return array(); + } }