Skip to content

Commit 1fa2da0

Browse files
committed
Add account top-up functionality
Resolves #53
1 parent 1c5c229 commit 1fa2da0

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

src/Account/Client.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,42 @@ public function getBalance()
7373
$balance = new Balance($body['value'], $body['autoReload']);
7474
return $balance;
7575
}
76+
77+
public function topUp($trx)
78+
{
79+
$body = [
80+
'trx' => $trx
81+
];
82+
83+
$request = new Request(
84+
\Nexmo\Client::BASE_REST . '/account/top-up'
85+
,'POST'
86+
, 'php://temp'
87+
, ['content-type' => 'application/x-www-form-urlencoded']
88+
);
89+
90+
$request->getBody()->write(http_build_query($body));
91+
$response = $this->client->send($request);
92+
93+
if($response->getStatusCode() != '200'){
94+
throw $this->getException($response, $application);
95+
}
96+
}
97+
98+
protected function getException(ResponseInterface $response, $application = null)
99+
{
100+
$body = json_decode($response->getBody()->getContents(), true);
101+
$status = $response->getStatusCode();
102+
103+
if($status >= 400 AND $status < 500) {
104+
$e = new Exception\Request($body['error_title'], $status);
105+
} elseif($status >= 500 AND $status < 600) {
106+
$e = new Exception\Server($body['error_title'], $status);
107+
} else {
108+
$e = new Exception\Exception('Unexpected HTTP Status Code');
109+
}
110+
111+
return $e;
112+
}
113+
76114
}

test/Account/ClientTest.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,20 @@ public function setUp()
3535
$this->nexmoClient = $this->prophesize('Nexmo\Client');
3636
$this->accountClient = new Client();
3737
$this->accountClient->setClient($this->nexmoClient->reveal());
38+
}
39+
40+
public function testTopUp()
41+
{
42+
$this->nexmoClient->send(Argument::that(function (RequestInterface $request) {
43+
$this->assertEquals('/account/top-up', $request->getUri()->getPath());
44+
$this->assertEquals('rest.nexmo.com', $request->getUri()->getHost());
45+
$this->assertEquals('POST', $request->getMethod());
46+
$this->assertRequestFormBodyContains('trx', 'ABC123', $request);
47+
48+
return true;
49+
}))->shouldBeCalledTimes(1)->willReturn($this->getResponse('empty'));
3850

39-
$basic = new \Nexmo\Client\Credentials\Basic('7c9738e6','5e840647a5b710a011A');
40-
$client = new \Nexmo\Client($basic);
41-
//$this->accountClient->setClient($client);
51+
$this->accountClient->topUp('ABC123');
4252
}
4353

4454
public function testGetBalance()

test/Account/responses/empty.json

Whitespace-only changes.

0 commit comments

Comments
 (0)