Skip to content

Commit

Permalink
Merge pull request #21 from tookantech/WIP_FarapayamakImpl
Browse files Browse the repository at this point in the history
Wip farapayamak impl
  • Loading branch information
miladnouri authored Oct 31, 2023
2 parents b1a6316 + b6903e7 commit 1fa1d93
Show file tree
Hide file tree
Showing 8 changed files with 256 additions and 2 deletions.
7 changes: 7 additions & 0 deletions config/chapaar.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,12 @@
'api_key' => '',
'line_number' => '',
],
// Configuration for the 'ghasedak' driver
'farapayamak' => [
'title' => 'chapaar::driver.farapayamak',
'url' => 'https://rest.payamak-panel.com/api/SendSMS/%s',
'username' => '',
'password' => '',
],
],
];
2 changes: 2 additions & 0 deletions resources/lang/en/driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
'kavenegar' => 'kavenegar',
'smsir' => 'smsir',
'ghasedak' => 'ghasedak',
'farapayamak' => 'farapayamak',

];
2 changes: 2 additions & 0 deletions resources/lang/fa/driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
'kavenegar' => 'کاوه‌نگار',
'smsir' => 'اس‌ام‌اس آی‌آر',
'ghasedak' => 'قاصدک',
'farapayamak' => 'فراپیامک',

];
138 changes: 138 additions & 0 deletions src/Drivers/Farapayamak/FarapayamakConnector.php
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);
}
}
}
99 changes: 99 additions & 0 deletions src/Drivers/Farapayamak/FarapayamakMessage.php
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;
}
}
2 changes: 1 addition & 1 deletion src/Drivers/Ghasedak/GhasedakMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function setTokens(array $tokens): self
{
$token_array = [];
foreach ($tokens as $key => $token) {
$key_name = is_numeric($key) ? sprintf('param%s', $key + 1) : $key;
$key_name = is_numeric($key) ? sprintf('param%s', $key + 1) : $key;
$token_array[$key_name] = $token;
}

Expand Down
6 changes: 6 additions & 0 deletions src/Enums/Drivers.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace TookanTech\Chapaar\Enums;

use TookanTech\Chapaar\Drivers\Farapayamak\FarapayamakConnector;
use TookanTech\Chapaar\Drivers\Farapayamak\FarapayamakMessage;
use TookanTech\Chapaar\Drivers\Ghasedak\GhasedakConnector;
use TookanTech\Chapaar\Drivers\Ghasedak\GhasedakMessage;
use TookanTech\Chapaar\Drivers\Kavenegar\KavenegarConnector;
Expand All @@ -14,13 +16,16 @@ enum Drivers: string
case KAVENEGAR = 'kavenegar';
case SMSIR = 'smsir';
case GHASEDAK = 'ghasedak';
case FARAPAYAMAK = 'farapayamak';

public function connector(): string
{
return match ($this) {
self::KAVENEGAR => KavenegarConnector::class,
self::SMSIR => SmsIrConnector::class,
self::GHASEDAK => GhasedakConnector::class,
self::FARAPAYAMAK => FarapayamakConnector::class,

};
}

Expand All @@ -30,6 +35,7 @@ public function message(): string
self::KAVENEGAR => KavenegarMessage::class,
self::SMSIR => SmsIrMessage::class,
self::GHASEDAK => GhasedakMessage::class,
self::FARAPAYAMAK => FarapayamakMessage::class
};
}
}
2 changes: 1 addition & 1 deletion src/Traits/HasResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function generateAccountResponse($credit, $expire_date): object
];
}

public function generateReportResponse($message_id, $receptor, $content, $sent_date, $line_number, $cost): object
public function generateReportResponse($message_id, $receptor, $content, $sent_date, $line_number, $cost = null): object
{

return (object) [
Expand Down

0 comments on commit 1fa1d93

Please sign in to comment.