Skip to content

Commit 01809cc

Browse files
authored
Merge pull request #152 from Nexmo/get-prefix-pricing
Add prefix pricing endpoint
2 parents d5aee90 + 7dcfcab commit 01809cc

File tree

8 files changed

+225
-15
lines changed

8 files changed

+225
-15
lines changed

README.md

+29-9
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,25 @@ try {
465465
echo $e->getMessage();
466466
}
467467
```
468+
### Pricing
469+
470+
#### Prefix Pricing
471+
472+
If you know the prefix of a country that you want to call, you can use the `prefix-pricing` endpoint to
473+
find out costs to call that number. Each prefix can return multiple countries (e.g. `1` returns `US`, `CA` and `UM`):
474+
475+
```
476+
$results = $client->account()->getPrefixPricing('1');
477+
foreach ($results as $r) {
478+
echo $r['country_code'].PHP_EOL;
479+
echo $r['country_name'].PHP_EOL;
480+
foreach ($r['networks'] as $n) {
481+
echo $n->getName() .' :: '.$n->getCode().' :: '.$n->getPrefixPrice().PHP_EOL;
482+
}
483+
echo "----------------".PHP_EOL;
484+
}
485+
```
486+
468487

469488
## Troubleshooting
470489

@@ -494,8 +513,9 @@ API Coverage
494513
* [X] Balance
495514
* [X] Pricing
496515
* [ ] Settings
497-
* [ ] Top Up
516+
* [X] Top Up
498517
* [X] Secret Management
518+
* [X] Pricing
499519
* [X] Numbers
500520
* [X] Search
501521
* [X] Buy
@@ -519,16 +539,16 @@ API Coverage
519539
* [X] Message
520540
* [X] Messages
521541
* [X] Rejections
522-
* [ ] US Short Codes
523-
* [ ] Two-Factor Authentication
524-
* [ ] Event Based Alerts
525-
* [ ] Sending Alerts
526-
* [ ] Campaign Subscription Management
542+
* [X] US Short Codes
543+
* [X] Two-Factor Authentication
544+
* [X] Event Based Alerts
545+
* [X] Sending Alerts
546+
* [X] Campaign Subscription Management
527547
* Voice
528548
* [X] Outbound Call
529-
* [ ] Inbound Call
530-
* [ ] Text-To-Speech Call
531-
* [ ] Text-To-Speech Prompt
549+
* [X] Inbound Call
550+
* [X] Text-To-Speech Call
551+
* [X] Text-To-Speech Prompt
532552

533553
Contributing
534554
------------

src/Account/Client.php

+41
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,47 @@ class Client implements ClientAwareInterface
1515
{
1616
use ClientAwareTrait;
1717

18+
public function getPrefixPricing($prefix)
19+
{
20+
$queryString = http_build_query([
21+
'prefix' => $prefix
22+
]);
23+
24+
$request = new Request(
25+
$this->getClient()->getRestUrl() . '/account/get-prefix-pricing/outbound?'.$queryString,
26+
'GET',
27+
'php://temp'
28+
);
29+
30+
$response = $this->client->send($request);
31+
$rawBody = $response->getBody()->getContents();
32+
33+
$body = json_decode($rawBody, true);
34+
35+
$codeCategory = (int) ($response->getStatusCode()/100);
36+
if ($codeCategory != 2) {
37+
if ($codeCategory == 4) {
38+
throw new Exception\Request($body['error-code-label']);
39+
}else if ($codeCategory == 5) {
40+
throw new Exception\Server($body['error-code-label']);
41+
}
42+
}
43+
44+
if ($body['count'] == 0) {
45+
return [];
46+
}
47+
48+
// Multiple countries can match each prefix
49+
$prices = [];
50+
51+
foreach ($body['prices'] as $p) {
52+
$prefixPrice = new PrefixPrice();
53+
$prefixPrice->jsonUnserialize($p);
54+
$prices[] = $prefixPrice;
55+
}
56+
return $prices;
57+
}
58+
1859
public function getSmsPrice($country)
1960
{
2061
$body = $this->makePricingRequest($country, 'sms');

src/Account/PrefixPrice.php

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Nexmo\Account;
4+
5+
class PrefixPrice extends Price {
6+
protected $priceMethod = 'getPrefixPrice';
7+
8+
public function getCurrency()
9+
{
10+
throw new Exception('Currency is unavailable from this endpoint');
11+
}
12+
}
13+

src/Account/Price.php

+36-5
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@ public function getDialingPrefix()
4141

4242
public function getDefaultPrice()
4343
{
44-
return $this['default_price'];
44+
if (isset($this['default_price'])) {
45+
return $this['default_price'];
46+
}
47+
48+
return $this['mt'];
4549
}
4650

4751
public function getCurrency()
@@ -71,15 +75,42 @@ public function jsonUnserialize(array $json)
7175
$data = [];
7276
foreach ($json as $k => $v){
7377
$k = ltrim(strtolower(preg_replace('/[A-Z]([A-Z](?![a-z]))*/', '_$0', $k)), '_');
78+
79+
// PrefixPrice fixes
80+
if ($k == 'country') {
81+
$k = 'country_code';
82+
}
83+
84+
if ($k == 'name') {
85+
$data['country_display_name'] = $v;
86+
$data['country_name'] = $v;
87+
}
88+
89+
if ($k == 'prefix') {
90+
$k = 'dialing_prefix';
91+
}
92+
7493
$data[$k] = $v;
7594
}
7695

7796
// Create objects for all the nested networks too
7897
$networks = [];
79-
foreach ($json['networks'] as $n){
80-
$network = new Network($n['networkCode'], $n['networkName']);
81-
$network->jsonUnserialize($n);
82-
$networks[$network->getCode()] = $network;
98+
if (isset($json['networks'])) {
99+
foreach ($json['networks'] as $n){
100+
if (isset($n['code'])) {
101+
$n['networkCode'] = $n['code'];
102+
unset ($n['code']);
103+
}
104+
105+
if (isset($n['network'])) {
106+
$n['networkName'] = $n['network'];
107+
unset ($n['network']);
108+
}
109+
110+
$network = new Network($n['networkCode'], $n['networkName']);
111+
$network->jsonUnserialize($n);
112+
$networks[$network->getCode()] = $network;
113+
}
83114
}
84115

85116
$data['networks'] = $networks;

src/Network.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ public function getOutboundVoicePrice()
5050
return $this['price'];
5151
}
5252

53+
public function getPrefixPrice() {
54+
return $this['mt_price'];
55+
}
56+
5357
public function getCurrency()
5458
{
5559
return $this['currency'];
@@ -90,4 +94,4 @@ public function offsetUnset($offset)
9094
{
9195
throw new Exception('Network is read only');
9296
}
93-
}
97+
}

test/Account/ClientTest.php

+17
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Nexmo\Network;
1515
use Nexmo\Account\SmsPrice;
1616
use Nexmo\Account\VoicePrice;
17+
use Nexmo\Account\PrefixPrice;
1718
use Nexmo\Account\Client;
1819
use Zend\Diactoros\Response;
1920
use NexmoTest\Psr7AssertionTrait;
@@ -102,6 +103,22 @@ public function testGetVoicePricing()
102103
$this->assertInstanceOf(Network::class, $voicePrice['networks']['311310']);
103104
}
104105

106+
public function testGetPrefixPricing()
107+
{
108+
$this->nexmoClient->send(Argument::that(function (RequestInterface $request) {
109+
$this->assertEquals('/account/get-prefix-pricing/outbound', $request->getUri()->getPath());
110+
$this->assertEquals('rest.nexmo.com', $request->getUri()->getHost());
111+
$this->assertEquals('GET', $request->getMethod());
112+
$this->assertRequestQueryContains('prefix', '263', $request);
113+
114+
return true;
115+
}))->shouldBeCalledTimes(1)->willReturn($this->getResponse('prefix-pricing'));
116+
117+
$prefixPrice = $this->accountClient->getPrefixPricing('263');
118+
$this->assertInstanceOf(PrefixPrice::class, $prefixPrice[0]);
119+
$this->assertInstanceOf(Network::class, $prefixPrice[0]['networks']['64804']);
120+
}
121+
105122
public function testListSecrets()
106123
{
107124
$this->nexmoClient->send(Argument::that(function (RequestInterface $request) {

test/Account/PrefixPriceTest.php

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
/**
3+
* Nexmo Client Library for PHP
4+
*
5+
* @copyright Copyright (c) 2016 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\Network;
12+
use Nexmo\Account\PrefixPrice;
13+
use PHPUnit\Framework\TestCase;
14+
15+
class PrefixPriceTest extends TestCase
16+
{
17+
public function setUp()
18+
{
19+
}
20+
21+
/**
22+
* @dataProvider prefixPriceProvider
23+
*/
24+
public function testFromArray($prefixPrice)
25+
{
26+
$this->assertEquals("ZW", $prefixPrice->getCountryCode());
27+
$this->assertEquals("Zimbabwe", $prefixPrice->getCountryName());
28+
$this->assertEquals("263", $prefixPrice->getDialingPrefix());
29+
}
30+
31+
/**
32+
* @dataProvider prefixPriceProvider
33+
*/
34+
public function testGetters($prefixPrice)
35+
{
36+
$this->assertEquals("ZW", $prefixPrice->getCountryCode());
37+
$this->assertEquals("Zimbabwe", $prefixPrice->getCountryName());
38+
$this->assertEquals("Zimbabwe", $prefixPrice->getCountryDisplayName());
39+
$this->assertEquals("263", $prefixPrice->getDialingPrefix());
40+
}
41+
42+
/**
43+
* @dataProvider prefixPriceProvider
44+
*/
45+
public function testArrayAccess($prefixPrice)
46+
{
47+
$this->assertEquals("ZW", $prefixPrice['country_code']);
48+
$this->assertEquals("Zimbabwe", $prefixPrice['country_name']);
49+
$this->assertEquals("Zimbabwe", $prefixPrice['country_display_name']);
50+
$this->assertEquals("263", $prefixPrice['dialing_prefix']);
51+
}
52+
53+
/**
54+
* @dataProvider prefixPriceProvider
55+
*/
56+
public function testUsesCustomPriceForKnownNetwork($prefixPrice)
57+
{
58+
$this->assertEquals("0.123", $prefixPrice->getPriceForNetwork('21039'));
59+
}
60+
61+
public function prefixPriceProvider()
62+
{
63+
$r = [];
64+
65+
$prefixPrice = new PrefixPrice();
66+
$prefixPrice->jsonUnserialize([
67+
'country' => 'ZW',
68+
'name' => 'Zimbabwe',
69+
'prefix' => 263,
70+
'networks' => [
71+
[
72+
'code' => '21039',
73+
'network' => 'Demo Network',
74+
'mtPrice' => '0.123'
75+
]
76+
]
77+
]);
78+
$r['jsonUnserialize'] = [$prefixPrice];
79+
80+
return $r;
81+
}
82+
}
83+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"count":1,"prices":[{"country":"ZW","name":"Zimbabwe","prefix":"263","mt":"0.02570000","networks":[{"code":"ZW-TOLL-FREE","network":"Zimbabwe Toll Free","mtPrice":"0.02570000"},{"code":"ZW-UNKNOWN","network":"Zimbabwe Unknown","mtPrice":"0.02570000"},{"code":"ZW-VOIP","network":"Zimbabwe VoIP","mtPrice":"0.02570000"},{"code":"64801","network":"NetOne","mtPrice":"0.02570000"},{"code":"64803","network":"Telecel","mtPrice":"0.02570000"},{"code":"64804","network":"Econet","mtPrice":"0.02570000"}],"countryDisplayName":"Zimbabwe"}]}

0 commit comments

Comments
 (0)