forked from woovibr/php-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request woovibr#62 from jonathanpmartins/accounts
Add Accounts Resource to PHP-SDK
- Loading branch information
Showing
3 changed files
with
274 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |