-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented OTP SMS API functionality and added examples in the README.md
- Loading branch information
Showing
8 changed files
with
255 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
namespace MobiWeb\Rest; | ||
|
||
use MobiWeb\Rest\Authentication as Auth; | ||
use MobiWeb\Rest\Client as APIClient; | ||
use MobiWeb\Http\Client as HttpClient; | ||
use MobiWeb\Rest\Error as APIError; | ||
|
||
class OTP { | ||
|
||
const GENERATE_ENDPOINT = "/otp/v3/generate"; | ||
const VALIDATE_ENDPOINT = "/otp/v3/validate/"; | ||
const OTP_METHOD = "POST"; | ||
|
||
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){ | ||
|
||
if (!$auth) { | ||
throw new \Exception("Cannot generate OTP without authentication"); | ||
} | ||
|
||
$access_token = $auth->getAccessToken(); | ||
if(!$access_token){ | ||
throw new \Exception("Cannot retrieve Access Token"); | ||
return false; | ||
} | ||
|
||
$http = new HttpClient(); | ||
$headers = array(); | ||
$headers["Authorization"] = "Bearer " . $access_token; | ||
$body = new \stdClass(); | ||
$body->mobile = $mobile; | ||
$body->sender = $sender; | ||
$body->message = $message; | ||
$body->validity = $validity; | ||
|
||
$executedRequest=$http->request(APIClient::API_ENDPOINT . OTP::GENERATE_ENDPOINT, OTP::OTP_METHOD, $headers, $body); | ||
|
||
if($executedRequest->response->body->status_code != HttpClient::HTTP_CREATED){ | ||
$apiError = new APIError($executedRequest->response->body->status_code, $executedRequest->response->body->status_message, $executedRequest->response->body->errors); | ||
throw new \Exception($apiError->print()); | ||
return false; | ||
} | ||
|
||
return array($executedRequest->response->body->payload); | ||
} | ||
|
||
public static function validate(Auth $auth = null, string $id, string $mobile, string $pin): bool{ | ||
|
||
if (!$auth) { | ||
throw new \Exception("Cannot validate OTP without authentication"); | ||
} | ||
|
||
$access_token = $auth->getAccessToken(); | ||
if(!$access_token){ | ||
throw new \Exception("Cannot retrieve Access Token"); | ||
return false; | ||
} | ||
|
||
$http = new HttpClient(); | ||
$headers = array(); | ||
$headers["Authorization"] = "Bearer " . $access_token; | ||
$body = new \stdClass(); | ||
$body->mobile = $mobile; | ||
$body->pin = $pin; | ||
|
||
$executedRequest=$http->request(APIClient::API_ENDPOINT . OTP::VALIDATE_ENDPOINT . $id, OTP::OTP_METHOD, $headers, $body); | ||
|
||
if($executedRequest->response->body->status_code != HttpClient::HTTP_OK){ | ||
$apiError = new APIError($executedRequest->response->body->status_code, $executedRequest->response->body->status_message, $executedRequest->response->body->errors); | ||
throw new \Exception($apiError->print()); | ||
return false; | ||
} | ||
|
||
return true; | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
require __DIR__ . '/../../../autoload.php'; // Loads MobiWeb package | ||
|
||
use MobiWeb\Rest\Client as APIClient; | ||
|
||
//Your account username and password | ||
$username = ""; | ||
$password = ""; | ||
|
||
$client = new APIClient($username, $password); | ||
|
||
//Generate OTP and send it via SMS to a mobile number | ||
$otp = $client->generate( | ||
"44xxxxxxxxxx", //The mobile number in international E.164 format. | ||
"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. | ||
"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. | ||
600, //The validity period of the pin in seconds. The default value is 600 seconds (10 minutes). | ||
); | ||
|
||
//Print the generate OTP result. Remember to store the mobile number and the OTP id for later use. | ||
print_r($otp); | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
require __DIR__ . '/../../../autoload.php'; // Loads MobiWeb package | ||
|
||
use MobiWeb\Rest\Client as APIClient; | ||
|
||
//Your account username and password | ||
$username = ""; | ||
$password = ""; | ||
|
||
$client = new APIClient($username, $password); | ||
|
||
//Get account OTP pricing and print it | ||
print_r($client->getPricing(APIClient::OTP)); | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
require __DIR__ . '/../../../autoload.php'; // Loads MobiWeb package | ||
|
||
use MobiWeb\Rest\Client as APIClient; | ||
|
||
//Your account username and password | ||
$username = ""; | ||
$password = ""; | ||
|
||
$client = new APIClient($username, $password); | ||
|
||
//Validate a previously generated OTP with the OTP ID. OTP is provided by the mobile number subscriber. | ||
$otp = $client->validate( | ||
"564xxx", //The OTP ID returned by the generated OTP. | ||
"44xxxxxxxxxx", //The mobile number of the subscriber in international E.164 format. | ||
"265xxx", //The OTP provided by the mobile number subscriber. | ||
); | ||
|
||
//Print the OTP validation attempt result. If result is TRUE, OTP is validated. | ||
echo $otp; | ||
|
||
?> |