Skip to content

Commit 29f6856

Browse files
authored
Merge pull request #160 from Nexmo/update-account-config
Add ability to check and update account settings
2 parents 8232a57 + 80cc14f commit 29f6856

File tree

6 files changed

+266
-0
lines changed

6 files changed

+266
-0
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,34 @@ foreach ($results as $r) {
505505
```
506506

507507

508+
### Check your Balance
509+
510+
Check how much credit remains on your account:
511+
512+
```php
513+
$response = $client->account()->getBalance();
514+
echo round($response->data['balance'], 2) . " EUR\n";
515+
```
516+
517+
### View and Change Account Configuration
518+
519+
Inspect the current settings on the account:
520+
521+
```php
522+
$response = $client->account()->getConfig();
523+
print_r($response->data);
524+
```
525+
526+
Update the default callback URLs for incoming SMS messages and delivery receipts:
527+
528+
```php
529+
$response = $client->account()->updateConfig([
530+
"sms_callback_url" => "http://example.com/webhooks/incoming-sms",
531+
"dr_callback_url" => "http://example.com/webhooks/delivery-receipt"
532+
]);
533+
print_r($response->data);
534+
```
535+
508536
## Troubleshooting
509537

510538
Some users have issues making requests due to the following error:

src/Account/Client.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,78 @@ public function topUp($trx)
137137
}
138138
}
139139

140+
public function getConfig()
141+
{
142+
143+
$request = new Request(
144+
$this->getClient()->getRestUrl() . '/account/settings',
145+
'POST',
146+
'php://temp'
147+
);
148+
149+
$response = $this->client->send($request);
150+
$rawBody = $response->getBody()->getContents();
151+
152+
if ($rawBody === '') {
153+
throw new Exception\Server('Response was empty');
154+
}
155+
156+
$body = json_decode($rawBody, true);
157+
158+
$config = new Config(
159+
$body['mo-callback-url'],
160+
$body['dr-callback-url'],
161+
$body['max-outbound-request'],
162+
$body['max-inbound-request'],
163+
$body['max-calls-per-second']
164+
);
165+
return $config;
166+
}
167+
168+
public function updateConfig($options)
169+
{
170+
// supported options are SMS Callback and DR Callback
171+
$params = [];
172+
if(isset($options['sms_callback_url'])) {
173+
$params['moCallBackUrl'] = $options['sms_callback_url'];
174+
}
175+
176+
if(isset($options['dr_callback_url'])) {
177+
$params['drCallBackUrl'] = $options['dr_callback_url'];
178+
}
179+
180+
$request = new Request(
181+
$this->getClient()->getRestUrl() . '/account/settings',
182+
'POST',
183+
'php://temp',
184+
['content-type' => 'application/x-www-form-urlencoded']
185+
);
186+
187+
$request->getBody()->write(http_build_query($params));
188+
$response = $this->client->send($request);
189+
190+
if($response->getStatusCode() != '200'){
191+
throw $this->getException($response);
192+
}
193+
194+
$rawBody = $response->getBody()->getContents();
195+
196+
if ($rawBody === '') {
197+
throw new Exception\Server('Response was empty');
198+
}
199+
200+
$body = json_decode($rawBody, true);
201+
202+
$config = new Config(
203+
$body['mo-callback-url'],
204+
$body['dr-callback-url'],
205+
$body['max-outbound-request'],
206+
$body['max-inbound-request'],
207+
$body['max-calls-per-second']
208+
);
209+
return $config;
210+
}
211+
140212
public function listSecrets($apiKey)
141213
{
142214
$body = $this->get( $this->getClient()->getApiUrl() . '/accounts/'.$apiKey.'/secrets');

src/Account/Config.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
namespace Nexmo\Account;
4+
5+
use ArrayAccess;
6+
use Nexmo\Client\Exception\Exception;
7+
use Nexmo\Entity\JsonSerializableInterface;
8+
use Nexmo\Entity\JsonUnserializableInterface;
9+
10+
class Config implements JsonSerializableInterface, JsonUnserializableInterface, ArrayAccess {
11+
public function __construct($sms_callback_url = null, $dr_callback_url = null, $max_outbound_request = null, $max_inbound_request = null, $max_calls_per_second = null)
12+
{
13+
if(!is_null($sms_callback_url)) {
14+
$this->data['sms_callback_url'] = $sms_callback_url;
15+
}
16+
if(!is_null($dr_callback_url)) {
17+
$this->data['dr_callback_url'] = $dr_callback_url;
18+
}
19+
if(!is_null($max_outbound_request)) {
20+
$this->data['max_outbound_request'] = $max_outbound_request;
21+
}
22+
if(!is_null($max_inbound_request)) {
23+
$this->data['max_inbound_request'] = $max_inbound_request;
24+
}
25+
if(!is_null($max_calls_per_second)) {
26+
$this->data['max_calls_per_second'] = $max_calls_per_second;
27+
}
28+
}
29+
30+
public function getSmsCallbackUrl()
31+
{
32+
return $this['sms_callback_url'];
33+
}
34+
35+
public function getDrCallbackUrl()
36+
{
37+
return $this['dr_callback_url'];
38+
}
39+
40+
public function getMaxOutboundRequest()
41+
{
42+
return $this['max_outbound_request'];
43+
}
44+
45+
public function getMaxInboundRequest()
46+
{
47+
return $this['max_inbound_request'];
48+
}
49+
50+
public function getMaxCallsPerSecond()
51+
{
52+
return $this['max_calls_per_second'];
53+
}
54+
55+
public function jsonUnserialize(array $json)
56+
{
57+
$this->data = [
58+
'sms_callback_url' => $json['sms_callback_url'],
59+
'dr_callback_url' => $json['dr_callback_url'],
60+
'max_outbound_request' => $json['max_outbound_request'],
61+
'max_inbound_request' => $json['max_inbound_request'],
62+
'max_calls_per_second' => $json['max_calls_per_second'],
63+
];
64+
}
65+
66+
function jsonSerialize()
67+
{
68+
return $this->data;
69+
}
70+
71+
public function offsetExists($offset)
72+
{
73+
return isset($this->data[$offset]);
74+
}
75+
76+
public function offsetGet($offset)
77+
{
78+
return $this->data[$offset];
79+
}
80+
81+
public function offsetSet($offset, $value)
82+
{
83+
throw new Exception('Balance is read only');
84+
}
85+
86+
public function offsetUnset($offset)
87+
{
88+
throw new Exception('Balance is read only');
89+
}
90+
}
91+

test/Account/ClientTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace NexmoTest\Account;
1010

1111
use Nexmo\Account\Balance;
12+
use Nexmo\Account\Config;
1213
use Nexmo\Account\Secret;
1314
use Nexmo\Account\SecretCollection;
1415
use Nexmo\Network;
@@ -71,6 +72,35 @@ public function testGetBalance()
7172
$this->assertInstanceOf(Balance::class, $balance);
7273
}
7374

75+
public function testGetConfig()
76+
{
77+
$this->nexmoClient->send(Argument::that(function (RequestInterface $request) {
78+
$this->assertEquals('/account/settings', $request->getUri()->getPath());
79+
$this->assertEquals('rest.nexmo.com', $request->getUri()->getHost());
80+
$this->assertEquals('POST', $request->getMethod());
81+
82+
return true;
83+
}))->shouldBeCalledTimes(1)->willReturn($this->getResponse('get-config'));
84+
85+
$config = $this->accountClient->getConfig();
86+
$this->assertInstanceOf(Config::class, $config);
87+
}
88+
89+
public function testUpdateConfig()
90+
{
91+
$this->nexmoClient->send(Argument::that(function (RequestInterface $request) {
92+
$this->assertEquals('/account/settings', $request->getUri()->getPath());
93+
$this->assertEquals('rest.nexmo.com', $request->getUri()->getHost());
94+
$this->assertEquals('POST', $request->getMethod());
95+
$this->assertRequestFormBodyContains('moCallBackUrl', 'https://example.com/other', $request);
96+
97+
return true;
98+
}))->shouldBeCalledTimes(1)->willReturn($this->getResponse('get-config'));
99+
100+
$config = $this->accountClient->updateConfig(["sms_callback_url" => "https://example.com/other"]);
101+
$this->assertInstanceOf(Config::class, $config);
102+
}
103+
74104
public function testGetSmsPricing()
75105
{
76106
$this->nexmoClient->send(Argument::that(function (RequestInterface $request) {

test/Account/ConfigTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/**
3+
* Nexmo Client Library for PHP
4+
*
5+
* @copyright Copyright (c) 2019 Nexmo, Inc. (http://nexmo.com)
6+
* @license https://github.com/Nexmo/nexmo-php/blob/master/LICENSE.txt MIT License
7+
*/
8+
9+
namespace NexmoTest\Account;
10+
11+
use Nexmo\Account\Config;
12+
use PHPUnit\Framework\TestCase;
13+
14+
class ConfigTest extends TestCase
15+
{
16+
public function setUp()
17+
{
18+
$this->config = new Config(
19+
"https://example.com/webhooks/inbound-sms",
20+
"https://example.com/webhooks/delivery-receipt",
21+
30, // different values so we can check if we reversed one anywhere
22+
31,
23+
32
24+
);
25+
}
26+
27+
public function testObjectAccess()
28+
{
29+
$this->assertEquals("https://example.com/webhooks/inbound-sms", $this->config->getSmsCallbackUrl());
30+
$this->assertEquals("https://example.com/webhooks/delivery-receipt", $this->config->getDrCallbackUrl());
31+
$this->assertEquals(30, $this->config->getMaxOutboundRequest());
32+
$this->assertEquals(31, $this->config->getMaxInboundRequest());
33+
$this->assertEquals(32, $this->config->getMaxCallsPerSecond());
34+
}
35+
36+
public function testArrayAccess()
37+
{
38+
$this->assertEquals("https://example.com/webhooks/inbound-sms", $this->config['sms_callback_url']);
39+
$this->assertEquals("https://example.com/webhooks/delivery-receipt", $this->config['dr_callback_url']);
40+
$this->assertEquals(30, $this->config['max_outbound_request']);
41+
$this->assertEquals(31, $this->config['max_inbound_request']);
42+
$this->assertEquals(32, $this->config['max_calls_per_second']);
43+
}
44+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"mo-callback-url":"https://example.com/webhooks/inbound-sms","dr-callback-url":"https://example.com/webhooks/delivery-receipt","max-outbound-request":30,"max-inbound-request":30,"max-calls-per-second":30}

0 commit comments

Comments
 (0)