Skip to content

Commit

Permalink
Merge pull request #5 from AQuadic/master
Browse files Browse the repository at this point in the history
Update api scheme to latest smsmisr. and update to Laravel/PHP 8
  • Loading branch information
AbdullahGhanem authored Mar 4, 2022
2 parents 5d09a2d + d5a2e8e commit 9e1a5d4
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 41 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
"email": "[email protected]"
}],
"require": {
"php": ">=7.0",
"php": "^8.0",
"illuminate/support": ">=5.3",
"symfony/psr-http-message-bridge": "^1.2",
"guzzlehttp/guzzle": "^6.0|^7.0"
"symfony/psr-http-message-bridge": "^2.0",
"guzzlehttp/guzzle": "^7.0"
},
"autoload": {
"psr-4": {
Expand Down
22 changes: 21 additions & 1 deletion config/smsmisr.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/*
* SMSMISR_USERNAME
*/
'username' => env('SMSMISR_USERNAME'),
'username' => env('SMSMISR_USERNAME'),

/*
* SMSMISR_PASSWORD
Expand All @@ -20,4 +20,24 @@
* SMSMISR_FROM
*/
'sender' => env('SMSMISR_SENDER'),

/*
* SMSMISR_M_SIGNATURE
*/
'm_signature' => env('SMSMISR_M_SIGNATURE'),

/*
* SMSMISR_TOKEN
*/
'token' => env('SMSMISR_TOKEN'),

/*
* SMSMISR_SMS_ID
*/
'sms_id' => env('SMSMISR_SMS_ID', 4945703),

/*
* SMSMISR_SMS_VERIFY_ID
*/
'sms_verify_id' => env('SMSMISR_SMS_ID', 72973),
];
99 changes: 62 additions & 37 deletions src/Smsmisr.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,24 @@
namespace Ghanem\LaravelSmsmisr;

use GuzzleHttp\Client;
use Illuminate\Contracts\Foundation\Application;
use GuzzleHttp\Exception\GuzzleException;
use Psr\Http\Message\ResponseInterface;

class Smsmisr
{
/**
* Codes FROM SMSMISR.
*/
public const SMSMISR_SUCCESS_CODE = 1901;
public const SMSMISR_VERIFY_SUCCESS_CODE = 4901;
public const SMSMISR_BALANCE_SUCCESS_CODE = 6000;

/**
* GuzzleHttp\Client.
* @var Client
*/
public $client;

public function __construct()
{
$this->client = new Client([
Expand All @@ -18,16 +29,19 @@ public function __construct()
}

/**
* Send Normal SMS using SMSMISR API.
*
* @param string $message
* @param string $to
* @param string|null $from
* @param string|null $sender
* @param int $language
* @return \Psr\Http\Message\ResponseInterface
* @return array|ResponseInterface
* @throws GuzzleException
*/
public function send(string $message, string $to, $sender = null, $language = 1)
public function send(string $message, string $to, ?string $sender = null, int $language = 1)
{
$sender = $sender ?? config('smsmisr.sender');

$response = $this->client->request('POST', 'webapi', [
'query' => [
'username' => config('smsmisr.username'),
Expand All @@ -39,41 +53,38 @@ public function send(string $message, string $to, $sender = null, $language = 1)
'DelayUntil' => null,
]
]);
$array = json_decode($response->getBody(), true) ;
return $array;
return json_decode($response->getBody(), true);
}

/**
* @param string $message
* Send Verify SMS using SMSMISR API.
*
* @param string $code
* @param string $to
* @param string|null $from
* @param int $language
* @return \Psr\Http\Message\ResponseInterface
* @return array|ResponseInterface
* @throws GuzzleException
*/
public function sendVerify(string $message, string $to, $sender = null, $language = 1)
public function sendVerify(string $code, string $to)
{
$sender = $sender ?? config('smsmisr.sender');

$response = $this->client->request('POST', 'verify', [
$response = $this->client->request('POST', 'vSMS', [
'query' => [
'username' => config('smsmisr.username'),
'Username' => config('smsmisr.username'),
'password' => config('smsmisr.password'),
'sender' => $sender,
'language' => $language,
'message' => $message,
'Msignature' => config('smsmisr.m_signature'),
'Token' => config('smsmisr.token'),
'mobile' => $to,
'DelayUntil' => null,
'Code' => $code,
]
]);
$array = json_decode($response->getBody(), true) ;
return $array;
return json_decode($response->getBody(), true);
}

/**
* @param string $message
* @param string $to
* @param string|null $from
* @return \Psr\Http\Message\ResponseInterface
* Check Normal SMS Balance using SMSMISR API.
*
* @return array|ResponseInterface
* @throws GuzzleException
*/
public function balance()
{
Expand All @@ -82,18 +93,17 @@ public function balance()
'username' => config('smsmisr.username'),
'password' => config('smsmisr.password'),
'request' => 'status',
'SMSID' => 4945703,
'SMSID' => config('smsmisr.sms_id'),
]
]);
$array = json_decode($response->getBody(), true) ;
return $array;
return json_decode($response->getBody(), true);
}

/**
* @param string $message
* @param string $to
* @param string|null $from
* @return \Psr\Http\Message\ResponseInterface
* Check Verify SMS Balance using SMSMISR API.
*
* @return array|ResponseInterface
* @throws GuzzleException
*/
public function balanceVerify()
{
Expand All @@ -102,11 +112,26 @@ public function balanceVerify()
'username' => config('smsmisr.username'),
'password' => config('smsmisr.password'),
'request' => 'status',
'SMSID' => 72973,
'SMSID' => config('smsmisr.sms_verify_id'),
]
]);
$array = json_decode($response->getBody(), true) ;
return json_decode($response->getBody(), true);
}

/**
* Helper Function to determine if the request was successful or not
*
* @param array|ResponseInterface $responseCode
* @return bool
*/
public function isSuccessful($responseCode): bool
{
if ($responseCode == null || !is_array($responseCode) || !array_key_exists('code', $responseCode)) return false;

if ($responseCode['code'] == self::SMSMISR_SUCCESS_CODE) return true;
if ($responseCode['code'] == self::SMSMISR_VERIFY_SUCCESS_CODE) return true;
if ($responseCode['code'] == self::SMSMISR_BALANCE_SUCCESS_CODE) return true;

return $array;
return false;
}
}

0 comments on commit 9e1a5d4

Please sign in to comment.