Skip to content

Commit

Permalink
Merge pull request woovibr#62 from jonathanpmartins/accounts
Browse files Browse the repository at this point in the history
Add Accounts Resource to PHP-SDK
  • Loading branch information
sibelius authored Dec 20, 2024
2 parents 82d1ae3 + 66e191f commit 3964283
Show file tree
Hide file tree
Showing 3 changed files with 274 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace OpenPix\PhpSdk;

use OpenPix\PhpSdk\RequestTransport;
use OpenPix\PhpSdk\Resources\Accounts;
use OpenPix\PhpSdk\Resources\Charges;
use OpenPix\PhpSdk\Resources\Customers;
use OpenPix\PhpSdk\Resources\Partners;
Expand Down Expand Up @@ -122,4 +123,12 @@ public function partners(): Partners
{
return new Partners($this->requestTransport);
}

/**
* Returns operations for the `Accounts` resource.
*/
public function accounts(): Accounts
{
return new Accounts($this->requestTransport);
}
}
128 changes: 128 additions & 0 deletions src/Resources/Accounts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php

namespace OpenPix\PhpSdk\Resources;

use OpenPix\PhpSdk\Request;
use OpenPix\PhpSdk\RequestTransport;

/**
* Operations on accounts.
*
* @link https://developers.openpix.com.br/api#tag/account
*/
class Accounts
{
/**
* Used to send HTTP requests to accounts API.
*
* @var RequestTransport
*/
private $requestTransport;

/**
* Create a new Accounts instance.
*
* @param RequestTransport $requestTransport Used to send HTTP requests to accounts API.
*/
public function __construct(RequestTransport $requestTransport)
{
$this->requestTransport = $requestTransport;
}

/**
* Get a list of Accounts.
*
* ## Usage
* ```php
* $result = $client->accounts()->list();
*
* foreach ($result['accounts'] as $account) {
* $account["accountId"]; // string
* $account["isDefault"]; // boolean
* $account["balance"]; // array
* $account["balance"]["total"]; // int
* $account["balance"]["blocked"]; // int
* $account["balance"]["available"]; // int
* }
* ```
*
* @link https://developers.openpix.com.br/api#tag/account/paths/~1api~1v1~1account~1/get
*
* @return array<string, mixed> Result from API.
*/
public function list(): array
{
$request = (new Request())
->method("GET")
->path("/api/v1/account");

return $this->requestTransport->transport($request);
}

/**
* Get an Account via accountId param.
*
* ```php
* $result = $client->accounts()->getOne("accountId");
*
* $result["account"]["accountId"]; // string
* $result["account"]["isDefault"]; // boolean
* $result["account"]["balance"]; // array
* $result["account"]["balance"]["total"]; // int
* $result["account"]["balance"]["blocked"]; // int
* $result["account"]["balance"]["available"]; // int
* ```
*
* @link https://developers.openpix.com.br/api#tag/account/paths/~1api~1v1~1account~1%7BaccountId%7D/get
*
* @param string $accountId Account ID.
*
* @return array<string, mixed> Result from API.
*/
public function getOne(string $accountId): array
{
$request = (new Request())
->method("GET")
->path("/api/v1/account/" . $accountId);

return $this->requestTransport->transport($request);
}

/**
* Withdraw from an Account via accountId param
*
* ```php
* $result = $client->accounts()->withdraw('accountId', [
* "value" => 1000, // R$ 10,00
* ]);
*
* $result["withdraw"]["account"]; // array
* $result["withdraw"]["account"]["accountId"]; // string
* $result["withdraw"]["account"]["isDefault"]; // boolean
* $result["withdraw"]["account"]["balance"]; // array
* $result["withdraw"]["account"]["balance"]["total"]; // int
* $result["withdraw"]["account"]["balance"]["blocked"]; // int
* $result["withdraw"]["account"]["balance"]["available"]; // int
*
* $result["withdraw"]["transaction"]; // array
* $result["withdraw"]["transaction"]["endToEndId"]; // string
* $result["withdraw"]["transaction"]["transaction"]; // int
* ```
*
* @link https://developers.openpix.com.br/api#tag/account/paths/~1api~1v1~1account~1%7BaccountId%7D~1withdraw/post
*
* @param string $accountId Account ID.
* @param array<string, mixed> $data amount data.
*
* @return array<string, mixed> Result from API.
*/
public function withdraw(string $accountId, array $data): array
{
$request = (new Request())
->method("POST")
->path("/api/v1/account/".$accountId."/withdraw")
->body($data);

return $this->requestTransport->transport($request);
}
}
137 changes: 137 additions & 0 deletions tests/Resources/AccountsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?php

namespace Resources;

use OpenPix\PhpSdk\Request;
use OpenPix\PhpSdk\RequestTransport;
use OpenPix\PhpSdk\Resources\Accounts;
use PHPUnit\Framework\TestCase;

final class AccountsTest extends TestCase
{
public function testList(): void
{
$accounts = [
[
"account" => [
"accountId" => "356a192b7913b04c54574d18c28d46e6395428ab",
"isDefault" => true,
"balance" => [
"total" => 129430,
"blocked" => 0,
"available" => 129430,
],
],
],
[
"account" => [
"accountId" => "77de68daecd823babbb58edb1c8e14d7106e83bb",
"isDefault" => true,
"balance" => [
"total" => 3129430,
"blocked" => 0,
"available" => 3129430,
],
],
]
];

$requestTransportMock = $this->createMock(RequestTransport::class);
$requestTransportMock->expects($this->once())
->method("transport")
->willReturnCallback(function (Request $request) use ($accounts) {
$this->assertSame("GET", $request->getMethod());
$this->assertSame("/api/v1/account", $request->getPath());
$this->assertSame($request->getBody(), null);
$this->assertSame($request->getQueryParams(), []);

return $accounts;
});

$partners = new Accounts($requestTransportMock);

$result = $partners->list();

$this->assertSame($result, $accounts);
}

public function testGetOne(): void
{
$accountId = "356a192b7913b04c54574d18c28d46e6395428ab";
$account = [
"account" => [
"accountId" => "356a192b7913b04c54574d18c28d46e6395428ab",
"isDefault" => true,
"balance" => [
"total" => 129430,
"blocked" => 0,
"available" => 129430,
],
],
];

$requestTransportMock = $this->createMock(RequestTransport::class);
$requestTransportMock->expects($this->once())
->method("transport")
->willReturnCallback(function (Request $request) use ($accountId, $account) {
$this->assertSame("GET", $request->getMethod());
$this->assertSame("/api/v1/account/" . $accountId, $request->getPath());
$this->assertSame($request->getBody(), null);
$this->assertSame($request->getQueryParams(), []);

return $account;
});

$accounts = new Accounts($requestTransportMock);

$result = $accounts->getOne($accountId);

$this->assertSame($result, $account);
}

public function testWithdraw(): void
{
$accountId = "356a192b7913b04c54574d18c28d46e6395428ab";
$value = 1000; // R$ 10,00

$payload = [
'value' => $value,
];

$withdraw = [
"withdraw" => [
"account" => [
"accountId" => $accountId,
"isDefault" => true,
"balance" => [
"total" => 122430,
"blocked" => 0,
"available" => 122430,
],
],
"transaction" => [
"endToEndId" => "da4b9237bacccdf19c0760cab7aec4a8359010b0",
"transaction" => $value,
],
],
];

$requestTransportMock = $this->createMock(RequestTransport::class);
$requestTransportMock->expects($this->once())
->method("transport")
->willReturnCallback(function (Request $request) use ($accountId, $payload, $withdraw) {
$this->assertSame("POST", $request->getMethod());
$this->assertSame("/api/v1/account/" . $accountId . "/withdraw", $request->getPath());
$this->assertSame($request->getBody(), $payload);
$this->assertSame($request->getQueryParams(), []);

return $withdraw;
});

$partners = new Accounts($requestTransportMock);

$result = $partners->withdraw($accountId, $payload);

$this->assertSame($result, $withdraw);
}
}

0 comments on commit 3964283

Please sign in to comment.