Skip to content

Commit 265c6fd

Browse files
committed
Version 3.0.1
Implemented OTP SMS API functionality and added examples in the README.md
1 parent 074cfc5 commit 265c6fd

File tree

8 files changed

+255
-0
lines changed

8 files changed

+255
-0
lines changed

README.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,90 @@ print_r($client->getPricing(MobiWeb\Rest\Client::HLR));
367367
?>
368368
```
369369

370+
### OTP SMS API
371+
### Generate and send OTP
372+
373+
```php
374+
<?
375+
376+
//Your account username and password
377+
$username = "";
378+
$password = "";
379+
380+
$client = new MobiWeb\Rest\Client($username, $password);
381+
382+
//Generate OTP and send it via SMS to a mobile number
383+
$otp = $client->generate(
384+
"44xxxxxxxxxx", //The mobile number in international E.164 format.
385+
"SECUREPIN", //The sender that will be displayed in the OTP SMS. Can be composed of 2-11 alphanumeric characters (A-z,0-9, ,-,.) or 14 numeric characters (0-9). Special characters are not allowed.
386+
"Please do not share your password pin. Your password pin is: [PIN]", //The text message of OTP SMS. Remember to put placeholder [PIN] in the message. If all characters in the message belong to the 3GPP GSM 7-bit GSM 03.38 ASCII character table, you can send up to 160 characters. If one or more characters in the message belong to the 16-bit Unicode / UCS-2 character table, because of the increased memory requirement for each character, you can send up to 70 characters.
387+
600, //The validity period of the pin in seconds. The default value is 600 seconds (10 minutes).
388+
);
389+
390+
//Print the generate OTP result. Remember to store the mobile number and the OTP id for later use.
391+
print_r($otp);
392+
393+
?>
394+
```
395+
396+
### Validate OTP
397+
398+
```php
399+
<?
400+
401+
//Your account username and password
402+
$username = "";
403+
$password = "";
404+
405+
$client = new MobiWeb\Rest\Client($username, $password);
406+
407+
//Validate a previously generated OTP with the OTP ID. OTP is provided by the mobile number subscriber.
408+
$otp = $client->validate(
409+
"564xxx", //The OTP ID returned by the generated OTP.
410+
"44xxxxxxxxxx", //The mobile number of the subscriber in international E.164 format.
411+
"265xxx", //The OTP provided by the mobile number subscriber.
412+
);
413+
414+
//Print the OTP validation attempt result. If result is TRUE, OTP is validated.
415+
echo $otp;
416+
417+
?>
418+
```
419+
420+
### Get account balance
421+
422+
```php
423+
<?php
424+
425+
//Your account username and password
426+
$username = "";
427+
$password = "";
428+
429+
$client = new MobiWeb\Rest\Client($username, $password);
430+
431+
//Get account balance and print it
432+
echo $client->getBalance();
433+
434+
?>
435+
```
436+
437+
### Get account pricing
438+
439+
```php
440+
<?php
441+
442+
//Your account username and password
443+
$username = "";
444+
$password = "";
445+
446+
$client = new MobiWeb\Rest\Client($username, $password);
447+
448+
//Get account OTP pricing and print it
449+
print_r($client->getPricing(MobiWeb\Rest\Client::OTP));
450+
451+
?>
452+
```
453+
370454
## Getting help
371455

372456
If you need help installing or using the library, please [contact us][MobiWebSupportCenter].

src/MobiWeb/Http/Client.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class Client {
77
const GET = "GET";
88
const POST = "POST";
99
const HTTP_OK = "200";
10+
const HTTP_CREATED = "201";
1011
const HTTP_BAD = "400";
1112
const HTTP_UNAUTH = "401";
1213
const HTTP_NOTFOUND = "404";

src/MobiWeb/Rest/Client.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use MobiWeb\Rest\Authentication as Auth;
66
use MobiWeb\Rest\Message;
7+
use MobiWeb\Rest\HLR;
8+
use MobiWeb\Rest\OTP;
79
use MobiWeb\Rest\Utility as Util;
810

911
class Client {
@@ -12,6 +14,7 @@ class Client {
1214
const API_ENDPOINT = "https://sms.solutions4mobiles.com/apis";
1315
const HLR = "hlr";
1416
const SMS = "sms";
17+
const OTP = "otp";
1518

1619

1720
public function __construct(string $username = null, string $password = null){
@@ -37,6 +40,26 @@ public function broadcast(array $args): array{
3740

3841
}
3942

43+
public function generate(string $mobile, string $sender, string $message, int $validity){
44+
45+
if (!$mobile) {
46+
throw new \Exception("Mobile number is required to generate an OTP");
47+
}
48+
49+
return OTP::generate($this->auth, $mobile, $sender, $message, $validity);
50+
51+
}
52+
53+
public function validate(string $id, string $mobile, string $pin): bool{
54+
55+
if (!$mobile || !$id || !$pin) {
56+
throw new \Exception("Mobile number, OTP pin and OTP ID is required to validate an OTP");
57+
}
58+
59+
return OTP::validate($this->auth, $id, $mobile, $pin);
60+
61+
}
62+
4063
public function lookup(string $mobile): array{
4164

4265
if (!$mobile) {

src/MobiWeb/Rest/OTP.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
namespace MobiWeb\Rest;
4+
5+
use MobiWeb\Rest\Authentication as Auth;
6+
use MobiWeb\Rest\Client as APIClient;
7+
use MobiWeb\Http\Client as HttpClient;
8+
use MobiWeb\Rest\Error as APIError;
9+
10+
class OTP {
11+
12+
const GENERATE_ENDPOINT = "/otp/v3/generate";
13+
const VALIDATE_ENDPOINT = "/otp/v3/validate/";
14+
const OTP_METHOD = "POST";
15+
16+
public static function generate(Auth $auth = null, string $mobile, string $sender = "SECUREPIN", string $message = "Please do not share your password pin. Your password pin is: [PIN]", int $validity = 600){
17+
18+
if (!$auth) {
19+
throw new \Exception("Cannot generate OTP without authentication");
20+
}
21+
22+
$access_token = $auth->getAccessToken();
23+
if(!$access_token){
24+
throw new \Exception("Cannot retrieve Access Token");
25+
return false;
26+
}
27+
28+
$http = new HttpClient();
29+
$headers = array();
30+
$headers["Authorization"] = "Bearer " . $access_token;
31+
$body = new \stdClass();
32+
$body->mobile = $mobile;
33+
$body->sender = $sender;
34+
$body->message = $message;
35+
$body->validity = $validity;
36+
37+
$executedRequest=$http->request(APIClient::API_ENDPOINT . OTP::GENERATE_ENDPOINT, OTP::OTP_METHOD, $headers, $body);
38+
39+
if($executedRequest->response->body->status_code != HttpClient::HTTP_CREATED){
40+
$apiError = new APIError($executedRequest->response->body->status_code, $executedRequest->response->body->status_message, $executedRequest->response->body->errors);
41+
throw new \Exception($apiError->print());
42+
return false;
43+
}
44+
45+
return array($executedRequest->response->body->payload);
46+
}
47+
48+
public static function validate(Auth $auth = null, string $id, string $mobile, string $pin): bool{
49+
50+
if (!$auth) {
51+
throw new \Exception("Cannot validate OTP without authentication");
52+
}
53+
54+
$access_token = $auth->getAccessToken();
55+
if(!$access_token){
56+
throw new \Exception("Cannot retrieve Access Token");
57+
return false;
58+
}
59+
60+
$http = new HttpClient();
61+
$headers = array();
62+
$headers["Authorization"] = "Bearer " . $access_token;
63+
$body = new \stdClass();
64+
$body->mobile = $mobile;
65+
$body->pin = $pin;
66+
67+
$executedRequest=$http->request(APIClient::API_ENDPOINT . OTP::VALIDATE_ENDPOINT . $id, OTP::OTP_METHOD, $headers, $body);
68+
69+
if($executedRequest->response->body->status_code != HttpClient::HTTP_OK){
70+
$apiError = new APIError($executedRequest->response->body->status_code, $executedRequest->response->body->status_message, $executedRequest->response->body->errors);
71+
throw new \Exception($apiError->print());
72+
return false;
73+
}
74+
75+
return true;
76+
77+
}
78+
79+
}

src/MobiWeb/Rest/Utility.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class Utility {
1313
const BALANCE_METHOD = "GET";
1414
const PRICING_SMS_ENDPOINT = "/sms/mt/v2/pricing";
1515
const PRICING_HLR_ENDPOINT = "/hlr/v2/pricing";
16+
const PRICING_OTP_ENDPOINT = "/otp/v3/pricing";
1617
const PRICING_METHOD = "GET";
1718

1819

@@ -66,6 +67,9 @@ public static function getPricing(Auth $auth = null, string $service): array{
6667
case APIClient::HLR:
6768
$pricing_endpoint = Utility::PRICING_HLR_ENDPOINT;
6869
break;
70+
case APIClient::OTP:
71+
$pricing_endpoint = Utility::PRICING_OTP_ENDPOINT;
72+
break;
6973
}
7074

7175
$executedRequest=$http->request(APIClient::API_ENDPOINT . $pricing_endpoint, Utility::PRICING_METHOD, $headers);
@@ -84,6 +88,7 @@ public static function getPricing(Auth $auth = null, string $service): array{
8488

8589
switch($service){
8690
case APIClient::SMS:
91+
case APIClient::OTP:
8792
foreach ($pricing as $key => $value)$arr_pricing[$value->id] = array("countryname" => $value->operatorname, "operator" => $value->operatorname, "mcc" => $value->mcc, "mnc" => $value->mnc, "price" => $value->price, "currency" => $currency);
8893
break;
8994
case APIClient::HLR:

tests/generate_otp.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
require __DIR__ . '/../../../autoload.php'; // Loads MobiWeb package
4+
5+
use MobiWeb\Rest\Client as APIClient;
6+
7+
//Your account username and password
8+
$username = "";
9+
$password = "";
10+
11+
$client = new APIClient($username, $password);
12+
13+
//Generate OTP and send it via SMS to a mobile number
14+
$otp = $client->generate(
15+
"44xxxxxxxxxx", //The mobile number in international E.164 format.
16+
"SECUREPIN", //The sender that will be displayed in the OTP SMS. Can be composed of 2-11 alphanumeric characters (A-z,0-9, ,-,.) or 14 numeric characters (0-9). Special characters are not allowed.
17+
"Please do not share your password pin. Your password pin is: [PIN]", //The text message of OTP SMS. Remember to put placeholder [PIN] in the message. If all characters in the message belong to the 3GPP GSM 7-bit GSM 03.38 ASCII character table, you can send up to 160 characters. If one or more characters in the message belong to the 16-bit Unicode / UCS-2 character table, because of the increased memory requirement for each character, you can send up to 70 characters.
18+
600, //The validity period of the pin in seconds. The default value is 600 seconds (10 minutes).
19+
);
20+
21+
//Print the generate OTP result. Remember to store the mobile number and the OTP id for later use.
22+
print_r($otp);
23+
24+
?>

tests/get_otp_pricing.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
require __DIR__ . '/../../../autoload.php'; // Loads MobiWeb package
4+
5+
use MobiWeb\Rest\Client as APIClient;
6+
7+
//Your account username and password
8+
$username = "";
9+
$password = "";
10+
11+
$client = new APIClient($username, $password);
12+
13+
//Get account OTP pricing and print it
14+
print_r($client->getPricing(APIClient::OTP));
15+
16+
?>

tests/validate_otp.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
require __DIR__ . '/../../../autoload.php'; // Loads MobiWeb package
4+
5+
use MobiWeb\Rest\Client as APIClient;
6+
7+
//Your account username and password
8+
$username = "";
9+
$password = "";
10+
11+
$client = new APIClient($username, $password);
12+
13+
//Validate a previously generated OTP with the OTP ID. OTP is provided by the mobile number subscriber.
14+
$otp = $client->validate(
15+
"564xxx", //The OTP ID returned by the generated OTP.
16+
"44xxxxxxxxxx", //The mobile number of the subscriber in international E.164 format.
17+
"265xxx", //The OTP provided by the mobile number subscriber.
18+
);
19+
20+
//Print the OTP validation attempt result. If result is TRUE, OTP is validated.
21+
echo $otp;
22+
23+
?>

0 commit comments

Comments
 (0)