Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TheClient::getAccountsBalances method added #55

Merged
merged 1 commit into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions src/Model/AccountBalance.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace ThePay\ApiClient\Model;

final class AccountBalance
{
/** @var non-empty-string */
private $iban;
/** @var non-empty-string */
private $accountName;
/** @var array<non-empty-string, numeric-string> */
private $balances;

/**
* @param non-empty-string $iban
* @param non-empty-string $accountName
* @param array<non-empty-string, numeric-string> $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<non-empty-string, numeric-string>
*/
public function getBalances()
{
return $this->balances;
}
}
42 changes: 42 additions & 0 deletions src/Service/ApiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<AccountBalance>
*/
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
*
Expand Down
10 changes: 10 additions & 0 deletions src/Service/ApiServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<AccountBalance>
*/
public function getAccountsBalances(StringValue $accountIban = null, $projectId = null, \DateTime $balanceAt = null);

/**
* @param TransactionFilter $filter
* @param int $page
Expand Down
18 changes: 18 additions & 0 deletions src/TheClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<AccountBalance>
*/
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
Expand Down
56 changes: 56 additions & 0 deletions tests/GetAccountsBalancesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

Triplkrypl marked this conversation as resolved.
Show resolved Hide resolved
namespace ThePay\ApiClient\Tests;

use ThePay\ApiClient\Http\HttpResponse;
use ThePay\ApiClient\Http\HttpServiceInterface;
use ThePay\ApiClient\Model\AccountBalance;
use ThePay\ApiClient\TheClient;

final class GetAccountsBalancesTest extends BaseTestCase
{
/**
* @return void
*/
public function test()
{
/** @var HttpServiceInterface $httpService */
$httpService = \Mockery::mock('ThePay\ApiClient\Http\HttpServiceInterface');
call_user_func(array($httpService, 'shouldReceive'), 'get')->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();
}
}
5 changes: 5 additions & 0 deletions tests/Mocks/Service/ApiMockService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}