generated from spatie/package-skeleton-laravel
-
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.
Merge pull request #21 from tookantech/WIP_FarapayamakImpl
Wip farapayamak impl
- Loading branch information
Showing
8 changed files
with
256 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,6 @@ | |
'kavenegar' => 'kavenegar', | ||
'smsir' => 'smsir', | ||
'ghasedak' => 'ghasedak', | ||
'farapayamak' => 'farapayamak', | ||
|
||
]; |
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 |
---|---|---|
|
@@ -4,4 +4,6 @@ | |
'kavenegar' => 'کاوهنگار', | ||
'smsir' => 'اساماس آیآر', | ||
'ghasedak' => 'قاصدک', | ||
'farapayamak' => 'فراپیامک', | ||
|
||
]; |
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,138 @@ | ||
<?php | ||
|
||
namespace TookanTech\Chapaar\Drivers\Farapayamak; | ||
|
||
use GuzzleHttp\Client; | ||
use GuzzleHttp\Exception\GuzzleException; | ||
use TookanTech\Chapaar\Contracts\DriverConnector; | ||
use TookanTech\Chapaar\Exceptions\ApiException; | ||
use TookanTech\Chapaar\Exceptions\HttpException; | ||
use TookanTech\Chapaar\Traits\HasResponse; | ||
|
||
class FarapayamakConnector implements DriverConnector | ||
{ | ||
use HasResponse; | ||
|
||
protected Client $client; | ||
|
||
protected string $content = ''; | ||
|
||
public function __construct() | ||
{ | ||
self::$setting = (object) config('chapaar.drivers.farapayamak'); | ||
$this->client = new Client([ | ||
'headers' => [ | ||
'Content-Type' => 'application/x-www-form-urlencoded', | ||
'charset' => 'utf-8', | ||
], | ||
'http_errors' => false, | ||
]); | ||
|
||
} | ||
|
||
/** | ||
* @param FarapayamakMessage $message | ||
* | ||
* @throws GuzzleException | ||
*/ | ||
public function send($message): object | ||
{ | ||
$url = self::endpoint('SendSMS'); | ||
$params = [ | ||
'from' => $message->getFrom(), | ||
'to' => $message->getTo(), | ||
'text' => $message->getContent(), | ||
'isFlash' => $message->isFlash(), | ||
]; | ||
$response = $this->performApi($url, $params); | ||
|
||
return $this->generateResponse($response->RetStatus, $response->Value, (array) $response->StrRetStatus); | ||
} | ||
|
||
/** | ||
* @param FarapayamakMessage $message | ||
* | ||
* @throws GuzzleException | ||
*/ | ||
public function verify($message): object | ||
{ | ||
$url = self::endpoint('BaseServiceNumber'); | ||
$params = [ | ||
'text' => $message->getTokens(), | ||
'to' => $message->getTo(), | ||
'bodyId' => $message->getTemplate(), | ||
]; | ||
|
||
$response = $this->performApi($url, $params); | ||
|
||
return $this->generateResponse($response->result->RetStatus, $response->result->StrRetStatus, (array) $response->Value); | ||
|
||
} | ||
|
||
/** | ||
* @throws GuzzleException | ||
*/ | ||
public function account(): object | ||
{ | ||
$url = self::endpoint('GetCredit2'); | ||
$response = $this->performApi($url); | ||
|
||
return $this->generateAccountResponse($response->Value, 0); | ||
} | ||
|
||
/** | ||
* @throws GuzzleException | ||
*/ | ||
public function outbox($page_size = 100, $page_number = 1): object | ||
{ | ||
$url = self::endpoint('GetMessages'); | ||
$params = [ | ||
'location' => 2, // sent messages | ||
'index' => 0, | ||
'count' => 100, | ||
]; | ||
$response = $this->performApi($url, $params); | ||
|
||
return collect($response->Data)->map(function ($item) { | ||
return $this->generateReportResponse($item->MsgID, $item->Receiver, $item->Body, $item->SendDate, $item->Sender); | ||
}); | ||
} | ||
|
||
/** | ||
* @throws GuzzleException | ||
*/ | ||
public function performApi(string $url, array $params = []): object | ||
{ | ||
|
||
$params = [...$params, ...[ | ||
'username' => $this->setting->username, | ||
'password' => $this->setting->password, | ||
]]; | ||
$response = $this->client->post($url, [ | ||
'form_params' => $params, | ||
]); | ||
|
||
return $this->processApiResponse($response); | ||
} | ||
|
||
protected function processApiResponse($response): object | ||
{ | ||
$status_code = $response->getStatusCode(); | ||
$json_response = json_decode($response->getBody()->getContents()); | ||
$this->validateResponseStatus($status_code, $json_response); | ||
|
||
return $json_response; | ||
|
||
} | ||
|
||
protected function validateResponseStatus($status_code, $json_response): void | ||
{ | ||
if ($json_response === null) { | ||
throw new HttpException('Response is not valid JSON', $status_code); | ||
} | ||
|
||
if ($json_response->result->RetStatus !== 1) { | ||
throw new ApiException($json_response->result->message, $json_response->result->code); | ||
} | ||
} | ||
} |
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,99 @@ | ||
<?php | ||
|
||
namespace TookanTech\Chapaar\Drivers\Farapayamak; | ||
|
||
use TookanTech\Chapaar\Contracts\DriverMessage; | ||
|
||
class FarapayamakMessage implements DriverMessage | ||
{ | ||
protected string $content = ''; | ||
|
||
protected string $from = ''; | ||
|
||
protected string $to = ''; | ||
|
||
protected int $template = 0; | ||
|
||
protected array $tokens = []; | ||
|
||
protected bool $flash = false; | ||
|
||
/** | ||
* @var int Set 1 to send text message and 2 to send voice message. | ||
*/ | ||
protected int $type = 1; | ||
|
||
public function getContent(): string | ||
{ | ||
return $this->content; | ||
} | ||
|
||
public function setContent(string $content): self | ||
{ | ||
$this->content = $content; | ||
|
||
return $this; | ||
} | ||
|
||
public function getFrom(): string | ||
{ | ||
return $this->from; | ||
} | ||
|
||
public function setFrom($from): self | ||
{ | ||
$this->from = $from; | ||
|
||
return $this; | ||
} | ||
|
||
public function getTo(): string | ||
{ | ||
return $this->to; | ||
} | ||
|
||
public function setTo(array|string $to): static | ||
{ | ||
if (is_array($to)) { | ||
$to = $this->getTemplate() ? implode(',', $to) : reset($to); | ||
} | ||
|
||
$this->to = $to; | ||
|
||
return $this; | ||
} | ||
|
||
public function getTemplate(): int | ||
{ | ||
return $this->template; | ||
} | ||
|
||
public function setTemplate($template): self | ||
{ | ||
$this->template = $template; | ||
|
||
return $this; | ||
} | ||
|
||
public function getTokens(): string | ||
{ | ||
return implode(';', $this->tokens); | ||
} | ||
|
||
public function setTokens(array $tokens): self | ||
{ | ||
$this->tokens = $tokens; | ||
|
||
return $this; | ||
} | ||
|
||
public function isFlash(): bool | ||
{ | ||
return $this->flash; | ||
} | ||
|
||
public function setFlash(bool $flash): void | ||
{ | ||
$this->flash = $flash; | ||
} | ||
} |
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