Skip to content
This repository was archived by the owner on Oct 15, 2024. It is now read-only.

Commit 37b085a

Browse files
authored
Merge pull request #242 from leoneparise/balances
Obtém saldos de uma conta moip
2 parents 1c297d1 + f7a2bba commit 37b085a

File tree

6 files changed

+149
-10
lines changed

6 files changed

+149
-10
lines changed

src/Moip.php

+11
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Moip\Contracts\Authentication;
66
use Moip\Resource\Account;
7+
use Moip\Resource\Balances;
78
use Moip\Resource\BankAccount;
89
use Moip\Resource\Customer;
910
use Moip\Resource\Entry;
@@ -257,6 +258,16 @@ public function bankaccount()
257258
return new BankAccount($this);
258259
}
259260

261+
/**
262+
* Create a new Balances instance.
263+
*
264+
* @return Balances
265+
*/
266+
public function balances()
267+
{
268+
return new Balances($this);
269+
}
270+
260271
/**
261272
* Get the endpoint.
262273
*

src/Resource/Balances.php

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
namespace Moip\Resource;
4+
5+
use stdClass;
6+
7+
/**
8+
* Class Balances.
9+
*/
10+
class Balances extends MoipResource
11+
{
12+
/**
13+
* Path balances API.
14+
*
15+
* @const string
16+
*/
17+
const PATH = 'balances';
18+
19+
/**
20+
* Initialize a new instance.
21+
*/
22+
public function initialize()
23+
{
24+
$this->data = new stdClass();
25+
$this->data->unavailable = [];
26+
$this->data->future = [];
27+
$this->data->current = [];
28+
}
29+
30+
/**
31+
* Populate this instance.
32+
*
33+
* @param stdClass $response response object
34+
*
35+
* @return mixed|Balances
36+
*/
37+
protected function populate(stdClass $response)
38+
{
39+
$balances = clone $this;
40+
$balances->data->unavailable = $this->getIfSet('unavailable', $response) ?: [];
41+
$balances->data->future = $this->getIfSet('future', $response) ?: [];
42+
$balances->data->current = $this->getIfSet('current', $response) ?: [];
43+
44+
return $balances;
45+
}
46+
47+
/**
48+
* Get all balances.
49+
*
50+
* @return stdClass
51+
*/
52+
public function get()
53+
{
54+
$path = sprintf('/%s/%s', MoipResource::VERSION, self::PATH);
55+
return $this->getByPath($path, ['Accept' => static::ACCEPT_VERSION]);
56+
}
57+
58+
/**
59+
* Get unavailable balances. Returns an array of objects with the amount and currency.
60+
*
61+
* @return array
62+
*/
63+
public function getUnavailable()
64+
{
65+
return $this->data->unavailable;
66+
}
67+
68+
/**
69+
* Get future balances. Returns an array of objects with the amount and currency.
70+
*
71+
* @return array
72+
*/
73+
public function getFuture()
74+
{
75+
return $this->data->future;
76+
}
77+
78+
/**
79+
* Get current balances. Returns an array of objects with the amount and currency.
80+
*
81+
* @return array
82+
*/
83+
public function getCurrent()
84+
{
85+
return $this->data->current;
86+
}
87+
}

src/Resource/MoipResource.php

+20-10
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ abstract class MoipResource implements JsonSerializable
2424
*/
2525
const VERSION = 'v2';
2626

27+
/**
28+
* Api version content type.
29+
*
30+
* @cont string
31+
*/
32+
const ACCEPT_VERSION = 'application/json;version=2.1';
33+
2734
/**
2835
* @var \Moip\Moip
2936
*/
@@ -210,25 +217,27 @@ public function generateListPath(Pagination $pagination = null, Filters $filters
210217
* Execute a http request. If payload == null no body will be sent. Empty body ('{}') is supported by sending a
211218
* empty stdClass.
212219
*
213-
* @param string $path
214-
* @param string $method
215-
* @param mixed|null $payload
220+
* @param string $path request path
221+
* @param string $method http method
222+
* @param mixed|null $payload request body
223+
* @param array $headers request headers
216224
*
217225
* @throws Exceptions\ValidationException if the API returns a 4xx http status code. Usually means invalid data was sent.
218226
* @throws Exceptions\UnautorizedException if the API returns a 401 http status code. Check API token and key.
219227
* @throws Exceptions\UnexpectedException if the API returns a 500 http status code or something unexpected happens (ie.: Network error).
220228
*
221229
* @return stdClass
222230
*/
223-
protected function httpRequest($path, $method, $payload = null)
231+
protected function httpRequest($path, $method, $payload = null, $headers = [])
224232
{
225233
$http_sess = $this->moip->getSession();
226-
$headers = [];
227234
$body = null;
228235
if ($payload !== null) {
229236
$body = json_encode($payload, JSON_UNESCAPED_SLASHES);
230-
if ($body) {// if it's json serializable
231-
$headers['Content-Type'] = 'application/json';
237+
if ($body) { // if it's json serializable
238+
if (!isset($headers['Content-Type'])) {
239+
$headers['Content-Type'] = 'application/json';
240+
}
232241
} else {
233242
$body = null;
234243
}
@@ -258,13 +267,14 @@ protected function httpRequest($path, $method, $payload = null)
258267
/**
259268
* Find by path.
260269
*
261-
* @param string $path
270+
* @param string $path resource path
271+
* @param array $headers request headers
262272
*
263273
* @return stdClass
264274
*/
265-
public function getByPath($path)
275+
public function getByPath($path, $headers = [])
266276
{
267-
$response = $this->httpRequest($path, Requests::GET);
277+
$response = $this->httpRequest($path, Requests::GET, null, $headers);
268278

269279
if (is_array($response)) {
270280
$response = (object) $response;

tests/Resource/BalancesTest.php

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Moip\Tests\Resource;
4+
5+
use Moip\Tests\TestCase;
6+
7+
class BalancesTest extends TestCase
8+
{
9+
public function testShouldGetBalances()
10+
{
11+
$this->mockHttpSession($this->body_balances);
12+
13+
$balances = $this->moip->balances()->get();
14+
$current = $balances->getCurrent();
15+
$future = $balances->getFuture();
16+
$unavailable = $balances->getUnavailable();
17+
18+
$this->assertNotEmpty($current);
19+
$this->assertNotEmpty($future);
20+
$this->assertNotEmpty($unavailable);
21+
$this->assertEquals(44592168, $current[0]->amount);
22+
}
23+
}

tests/TestCase.php

+7
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,11 @@ abstract class TestCase extends BaseTestCase
141141
*/
142142
protected $body_bank_account_update;
143143

144+
/**
145+
* @var string response from moip API.
146+
*/
147+
protected $body_balances;
148+
144149
/**
145150
* @var string holds the last generated customer ownId. In mock mode it'll be always the default, but it changes on sandbox mode.
146151
*/
@@ -243,6 +248,8 @@ public function __construct()
243248
$this->body_bank_account_list = $this->readJsonFile('jsons/bank_account/list');
244249

245250
$this->body_bank_account_update = $this->readJsonFile('jsons/bank_account/update');
251+
252+
$this->body_balances = $this->readJsonFile('jsons/balances/get');
246253
}
247254

248255
/**

tests/jsons/balances/get.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"unavailable": [{"amount": 0, "currency": "BRL"}], "future": [{"amount": 0, "currency": "BRL"}], "current": [{"amount": 44592168, "currency": "BRL"}]}

0 commit comments

Comments
 (0)