Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
[![Latest Version on Packagist](https://img.shields.io/packagist/v/robust-tools/resala.svg?style=flat-square)](https://packagist.org/packages/robust-tools/resala)
[![Total Downloads](https://img.shields.io/packagist/dt/robust-tools/resala.svg?style=flat-square)](https://packagist.org/packages/robust-tools/resala)

**Resala** is a PHP & Laravel Package, (Designed to add support to your laravel or just native php app for sending SMS using local operators in the MENA region Like `Vodafone`, `Infobip`, `Connekio`, `VectoryLink`, `Gateway SA`, `BrandEncode`).
**Resala** is a PHP & Laravel Package, (Designed to add support to your laravel or just native php app for sending SMS using local operators in the MENA region Like `Vodafone`, `Infobip`, `Connekio`, `VectoryLink`, `Community Ads`, `Gateway SA`, `BrandEncode`).
**Resala** not just tied to use inside Laravel you can hook it up in any php code

## Supported Providers
- Vodafone SMS Gateway
- Connekio SMS Gateway
- Infobip SMS Gateway
- Vectory Link SMS Gateway
- Community Ads SMS Gateway
- Gateway SA SMS Gateway
- BrandEncode SMS Gateway

Expand Down Expand Up @@ -85,6 +86,14 @@ return [
'lang' => env('VECTORY_LINK_LANG', 'E')
],

'community_ads' => [
'end_point' => env('COMMUNITY_ADS_END_POINT', 'https://app.community-ads.com/SendSMSAPI/api/SMSSender/SendSMS'),
'username' => env('COMMUNITY_ADS_USERNAME'),
'password' => env('COMMUNITY_ADS_PASSWORD'),
'sender_name' => env('COMMUNITY_ADS_SENDER_NAME', 'Community Ads'),
'lang' => env('COMMUNITY_ADS_LANG', 'E')
],

'gateway_sa' => [
'end_point' => env('GATEWAYSA_END_POINT', 'http://REST.GATEWAY.SA/api/SendSMS'),
'api_id' => env('GATEWAYSA_API_ID'),
Expand Down Expand Up @@ -117,6 +126,7 @@ return [
'connekio' => ConnekioDriver::class,
'infobip' => InfobipDriver::class,
'vectory_link' => VectoryLinkDriver::class,
'community_ads' => CommunityAdsDriver::class,
'gateway_sa' => GatewaySA::class,
'brandencode' => BrandEncodeDriver::class
],
Expand Down Expand Up @@ -144,6 +154,11 @@ This adds `vectory_link` environment variables to your .env file.
php artisan resala:make vectory_link
```

This adds `community_ads` environment variables to your .env file.
```bash
php artisan resala:make community_ads
```

This adds `brandencode` environment variables to your .env file.
```bash
php artisan resala:make brandencode
Expand Down
10 changes: 10 additions & 0 deletions config/resala.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use RobustTools\Resala\Drivers\InfobipDriver;
use RobustTools\Resala\Drivers\VodafoneDriver;
use RobustTools\Resala\Drivers\VectoryLinkDriver;
use RobustTools\Resala\Drivers\CommunityAdsDriver;
use RobustTools\Resala\Drivers\BrandEncodeDriver;

return [
Expand Down Expand Up @@ -58,6 +59,14 @@
'lang' => env('VECTORY_LINK_LANG', 'E')
],

'community_ads' => [
'end_point' => env('COMMUNITY_ADS_END_POINT', 'https://app.community-ads.com/SendSMSAPI/api/SMSSender/SendSMS'),
'username' => env('COMMUNITY_ADS_USERNAME'),
'password' => env('COMMUNITY_ADS_PASSWORD'),
'sender_name' => env('COMMUNITY_ADS_SENDER_NAME', 'Community Ads'),
'lang' => env('COMMUNITY_ADS_LANG', 'E')
],

'gateway_sa' => [
'end_point' => env('GATEWAYSA_END_POINT', 'http://REST.GATEWAY.SA/api/SendSMS'),
'api_id' => env('GATEWAYSA_API_ID'),
Expand Down Expand Up @@ -90,6 +99,7 @@
'connekio' => ConnekioDriver::class,
'infobip' => InfobipDriver::class,
'vectory_link' => VectoryLinkDriver::class,
'community_ads' => CommunityAdsDriver::class,
'gateway_sa' => GatewaySA::class,
'brandencode' => BrandEncodeDriver::class
],
Expand Down
4 changes: 4 additions & 0 deletions src/Console/PublishProviderEnvVariablesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ private function getStubContent(): string
return File::get(__DIR__ . "/../../stubs/vectory_link.env.stub");
}

if ($this->argument('driver') == "community_ads") {
return File::get(__DIR__ . "/../../stubs/community_ads.env.stub");
}

if ($this->argument('driver') == "brandencode") {
return File::get(__DIR__ . "/../../stubs/brandencode.env.stub");
}
Expand Down
89 changes: 89 additions & 0 deletions src/Drivers/CommunityAdsDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php
namespace RobustTools\Resala\Drivers;

use RobustTools\Resala\Abstracts\Driver;
use RobustTools\Resala\Contracts\{SMSDriverInterface, SMSDriverResponseInterface};
use RobustTools\Resala\Response\CommunityAdsResponse;
use RobustTools\Resala\Support\HTTP;

final class CommunityAdsDriver extends Driver implements SMSDriverInterface
{
/**
* @var string|array
*/
private $recipients;

private string $message;

private string $username;

private string $password;

private string $senderName;

private string $endPoint;

private string $lang;

public function __construct(array $config)
{
$this->username = $config["username"];
$this->password = $config["password"];
$this->senderName = $config["sender_name"];
$this->endPoint = $config["end_point"];
$this->lang = $config["lang"];
}

/**
* @param string|array $recipients
* @return string|array
*/
public function to($recipients)
{
return $this->recipients = $this->toMultiple($recipients)
? implode(', ', $recipients)
: $recipients;
}

public function message(string $message): string
{
return $this->message = $message;
}

public function send(): SMSDriverResponseInterface
{
return new CommunityAdsResponse(HTTP::post($this->endPoint, $this->headers(), json_encode($this->payload())));
}

/**
* @return array<string, mixed>
*/
protected function payload(): array
{
return [
'SMSText' => $this->message,
'SMSReceiver' => $this->recipients,
'SMSSender' => $this->senderName,
'SMSLang' => $this->lang,
'UserName' => $this->username,
'Password' => $this->password,
'SMSID' => $this->generateGuid(),
];
}

protected function headers(): array
{
return [
'Content-Type' => 'application/json',
];
}

private function generateGuid(): string
{
$data = random_bytes(16);
$data[6] = chr(ord($data[6]) & 0x0f | 0x40);
$data[8] = chr(ord($data[8]) & 0x3f | 0x80);

return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
}
}
87 changes: 87 additions & 0 deletions src/Response/CommunityAdsResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
namespace RobustTools\Resala\Response;

use Psr\Http\Message\ResponseInterface;
use RobustTools\Resala\Contracts\SMSDriverResponseInterface;

final class CommunityAdsResponse implements SMSDriverResponseInterface
{
private const STATUS_SUCCESS = 0;
private const STATUS_INVALID_CREDENTIALS = -1;
private const STATUS_INVALID_ACCOUNT_IP = -2;
private const STATUS_INVALID_ANI_BLACK_LIST = -3;
private const STATUS_OUT_OF_CREDIT = -5;
private const STATUS_DATABASE_DOWN = -6;
private const STATUS_INACTIVE_ACCOUNT = -7;
private const STATUS_NO_DLR_RECEIVED = -8;
private const STATUS_ACCOUNT_EXPIRED = -11;
private const STATUS_SMS_EMPTY = -12;
private const STATUS_INVALID_SENDER_WITH_CONNECTION = -13;
private const STATUS_SMS_SENDING_FAILED = -14;
private const STATUS_USER_CAN_NOT_SEND_WITH_DLR = -16;
private const STATUS_OTHER_ERROR = -100;
private const STATUS_INVALID_ANI = -18;
private const STATUS_SMS_ID_EXISTS = -19;
private const STATUS_INVALID_ACCOUNT = -21;
private const STATUS_SMS_NOT_VALIDATE = -22;
private const STATUS_INVALID_ACCOUNT_OPERATOR_CONNECTION = -23;
private const STATUS_INVALID_USER_SMS_ID = -26;
private const STATUS_EMPTY_USERNAME_OR_PASSWORD = -29;
private const STATUS_INVALID_SENDER = -30;
private const STATUS_INVALID_START_TIME = -31;
private const STATUS_INVALID_SMS_TEMPLATE_OR_ITEMS = -32;

private int $status;

public function __construct(ResponseInterface $response)
{
$contents = trim($response->getBody()->getContents());
$this->status = $this->parseStatus($contents);
}

public function success(): bool
{
return $this->status === self::STATUS_SUCCESS;
}

public function body(): string
{
$messages = [
self::STATUS_SUCCESS => 'Success',
self::STATUS_INVALID_CREDENTIALS => 'Invalid Credentials',
self::STATUS_INVALID_ACCOUNT_IP => 'Invalid Account IP',
self::STATUS_INVALID_ANI_BLACK_LIST => 'Invalid ANI Black List',
self::STATUS_OUT_OF_CREDIT => 'Out Of Credit',
self::STATUS_DATABASE_DOWN => 'Database Down',
self::STATUS_INACTIVE_ACCOUNT => 'Inactive Account',
self::STATUS_NO_DLR_RECEIVED => 'No DLR received',
self::STATUS_ACCOUNT_EXPIRED => 'Account Is Expired',
self::STATUS_SMS_EMPTY => 'SMS Is Empty',
self::STATUS_INVALID_SENDER_WITH_CONNECTION => 'Invalid Sender With Connection',
self::STATUS_SMS_SENDING_FAILED => 'SMS Sending Failed Try Again',
self::STATUS_USER_CAN_NOT_SEND_WITH_DLR => 'User Can Not Send With DLR',
self::STATUS_OTHER_ERROR => 'Other Error',
self::STATUS_INVALID_ANI => 'Invalid ANI',
self::STATUS_SMS_ID_EXISTS => 'SMS Id Is Exist',
self::STATUS_INVALID_ACCOUNT => 'Invalid Account',
self::STATUS_SMS_NOT_VALIDATE => 'SMS Not Validate',
self::STATUS_INVALID_ACCOUNT_OPERATOR_CONNECTION => 'Invalid Account Operator Connection',
self::STATUS_INVALID_USER_SMS_ID => 'Invalid User SMS Id',
self::STATUS_EMPTY_USERNAME_OR_PASSWORD => 'Empty User Name Or Password',
self::STATUS_INVALID_SENDER => 'Invalid Sender',
self::STATUS_INVALID_START_TIME => 'InvalidStartTime',
self::STATUS_INVALID_SMS_TEMPLATE_OR_ITEMS => 'InvalidSMSTemplateOrItems',
];

return $messages[$this->status] ?? 'Something wrong happened';
}

private function parseStatus(string $contents): int
{
if ($contents === '' || !is_numeric($contents)) {
return self::STATUS_OTHER_ERROR;
}

return (int) $contents;
}
}
5 changes: 5 additions & 0 deletions stubs/community_ads.env.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
COMMUNITY_ADS_USERNAME=
COMMUNITY_ADS_PASSWORD=
COMMUNITY_ADS_SENDER_NAME=
COMMUNITY_ADS_END_POINT=https://app.community-ads.com/SendSMSAPI/api/SMSSender/SendSMS
COMMUNITY_ADS_LANG=E
Loading