Skip to content

Commit

Permalink
v3 update (#7)
Browse files Browse the repository at this point in the history
* Added Updates

* Added Comments

* Updated gitignore

* Updated composer.json

* Updated Sample Config

* Added Headers in config

* Updated Docs
  • Loading branch information
gr8shivam authored Oct 11, 2018
1 parent ad3bc12 commit ad2e7ef
Show file tree
Hide file tree
Showing 10 changed files with 243 additions and 103 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ composer.phar
# Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
# composer.lock

.idea
33 changes: 32 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Use can define multiple gateway configs like this:-
```
// Gateway Configuration
'gateway_name' => [
'method' => 'GET', //Choose Request Method (GET/POST) Default:GET
'url' => 'BaseUrl', //Base URL
'params' => [
'send_to_param_name' => '', //Send to Parameter Name
Expand All @@ -56,10 +57,36 @@ Use can define multiple gateway configs like this:-
//More params can be added
],
],
'headers' => [
'header1' => '',
'header2' => '',
//More headers can be added
],
// 'json' => true, // OPTIONAL: Use if you want the params to be sent in JSON format instead of query params (accepts true/false)
// 'wrapper' => 'wrapper_name', // OPTIONAL: Use only if you want the JSON request to be wrapped (accepts string)
'add_code' => true, //Include Country Code (true/false)
],
```

#### Special Parameters in Gateway Config

##### `json` Parameter
The `json` parameter accepts `true/false`. When `true`, it sends `params` as a JSON payload. It also takes care of `'Content-Type' => 'application/json'` header.

##### `wrapper` Parameter
The `wrapper` is a special parameter which will be required only with some gateways. It wraps the JSON payload in the following structure:
```
"wrapper_name": [
{
"message": "Message",
"to": [
"Receipient1",
"Receipient2"
]
}
]
```

## Usage
### Direct Use
Use the `smsapi()` helper function or `SmsApi` facade to send the messages.
Expand All @@ -73,6 +100,8 @@ Use the `smsapi()` helper function or `SmsApi` facade to send the messages.

- Adding extra parameters `smsapi("TO", "Message", ["param1" => "val"]);` or `smsapi()->sendMessage("TO", "Message", ["param1" => "val"]);`

- Adding extra headers `smsapi("TO", "Message", ["param1" => "val"], ["header1" => "val"]);` or `smsapi()->sendMessage("TO", "Message", ["param1" => "val"], ["header1" => "val"]);`

- Using a different gateway `smsapi()->gateway('GATEWAY_NAME')->sendMessage("TO", "Message");`

- Using a different country code `smsapi()->countryCode('COUNTRY_CODE')->sendMessage("TO", "Message");`
Expand All @@ -84,6 +113,8 @@ Use the `smsapi()` helper function or `SmsApi` facade to send the messages.

- Adding extra parameters `SmsApi::sendMessage("TO", "Message", ["param1" => "val"]);`

- Adding extra headers `SmsApi::sendMessage("TO", "Message", ["param1" => "val"], ["header1" => "val"]);`

- Using a different gateway `SmsApi::gateway('GATEWAY_NAME')->sendMessage("TO", "Message");`

- Using a different country code `SmsApi::countryCode('COUNTRY_CODE')->sendMessage("TO", "Message");`
Expand Down Expand Up @@ -140,7 +171,7 @@ class ExampleNotification extends Notification
}
}
```
You can also use `->params(["param1" => "val"])` to add extra parameters to the request.
You can also use `->params(["param1" => "val"])` to add extra parameters to the request and `->headers(["header1" => "val"])` to add extra headers to the request.

## Support
Feel free to post your issues in the issues section.
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gr8shivam/laravel-sms-api",
"version":"2.0.4",
"version":"3.0.0",
"description": "Laravel package to provide SMS API integration. Any SMS vendor that provides REST API can be used. SMS-API channel for Laravel notifications also included.",
"license": "MIT",
"keywords": [
Expand All @@ -16,7 +16,7 @@
{
"name": "Shivam Agarwal",
"email": "[email protected]",
"homepage": "https://www.shivam.ga",
"homepage": "https://github.com/gr8shivam",
"role": "Developer"
}
],
Expand Down
7 changes: 7 additions & 0 deletions src/Exception/Exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Gr8Shivam\SmsApi\Exception;

class Exception extends \Exception {

}
6 changes: 6 additions & 0 deletions src/Exception/InvalidMethodException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
namespace Gr8Shivam\SmsApi\Exception;

class InvalidMethodException extends Exception {

}
7 changes: 4 additions & 3 deletions src/Notifications/SmsApiChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ public function __construct(SmsApi $client) {
/**
* Send the given notification.
*
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
* @return void
* @throws \Gr8Shivam\SmsApi\Exception\InvalidMethodException
*/
public function send($notifiable, Notification $notification)
{
Expand All @@ -37,6 +38,6 @@ public function send($notifiable, Notification $notification)
$message = new SmsApiMessage($message);
}

$this->client->sendMessage($mobile,$message->content,$message->params);
$this->client->sendMessage($mobile,$message->content,$message->params,$message->headers);
}
}
22 changes: 20 additions & 2 deletions src/Notifications/SmsApiMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ class SmsApiMessage
*/
public $params;

/**
* Add Headers.
*
* @var array
*/
public $headers=[];

/**
* The message type.
*
Expand All @@ -29,10 +36,12 @@ class SmsApiMessage
*
* @param string $content
* @param array $params
* @param array $headers
*/
public function __construct($content = '', $params = null) {
public function __construct($content = '', $params = null, $headers=[]) {
$this->content = $content;
$this->params = $params;
$this->headers = $headers;
}

/**
Expand All @@ -46,7 +55,6 @@ public function content($content) {
return $this;
}


/**
* Set the message params.
*
Expand All @@ -59,6 +67,16 @@ public function params($params)
return $this;
}

/**
* @param $headers
* @return $this
*/
public function headers($headers)
{
$this->headers = $headers;
return $this;
}

/**
* Set the message type.
*
Expand Down
Loading

0 comments on commit ad2e7ef

Please sign in to comment.