From 7b14c11146f76a53f9508009d7ca021f7effa618 Mon Sep 17 00:00:00 2001 From: arya Date: Sat, 21 Oct 2023 12:33:15 +0330 Subject: [PATCH 1/5] [wip] farapayamak implementation --- config/chapaar.php | 7 + resources/lang/en/driver.php | 2 + resources/lang/fa/driver.php | 2 + .../Farapayamak/FarapayamakConnector.php | 137 ++++++++++++++++++ .../Farapayamak/FarapayamakMessage.php | 99 +++++++++++++ src/Enums/Drivers.php | 7 + 6 files changed, 254 insertions(+) create mode 100644 src/Drivers/Farapayamak/FarapayamakConnector.php create mode 100644 src/Drivers/Farapayamak/FarapayamakMessage.php diff --git a/config/chapaar.php b/config/chapaar.php index 55717be..7d20774 100644 --- a/config/chapaar.php +++ b/config/chapaar.php @@ -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' => '', + ], ], ]; diff --git a/resources/lang/en/driver.php b/resources/lang/en/driver.php index 853396d..b60c169 100644 --- a/resources/lang/en/driver.php +++ b/resources/lang/en/driver.php @@ -4,4 +4,6 @@ 'kavenegar' => 'kavenegar', 'smsir' => 'smsir', 'ghasedak' => 'ghasedak', + 'farapayamak' => 'farapayamak', + ]; diff --git a/resources/lang/fa/driver.php b/resources/lang/fa/driver.php index 186c52f..e5bfcfd 100644 --- a/resources/lang/fa/driver.php +++ b/resources/lang/fa/driver.php @@ -4,4 +4,6 @@ 'kavenegar' => 'کاوه‌نگار', 'smsir' => 'اس‌ام‌اس آی‌آر', 'ghasedak' => 'قاصدک', + 'farapayamak' => 'فراپیامک', + ]; diff --git a/src/Drivers/Farapayamak/FarapayamakConnector.php b/src/Drivers/Farapayamak/FarapayamakConnector.php new file mode 100644 index 0000000..41f1961 --- /dev/null +++ b/src/Drivers/Farapayamak/FarapayamakConnector.php @@ -0,0 +1,137 @@ +client = new Client([ + 'headers' => [ + 'Accept' => 'application/json', + 'Content-Type' => 'application/json', + 'charset' => 'utf-8', + ], + 'verify' => false, + 'http_errors' => false, + ]); + + } + + /** + * @param GhasedakMessage $message + * + * @throws GuzzleException + */ + public function send($message): object + { + $url = self::endpoint('SendSMS'); + $params = [ + 'username' => $this->setting->username, + 'password' => $this->setting->password, + 'from' => $message->getFrom(), + 'to' => $message->getTo(), + 'text' => $message->getContent(), + 'isFlash' => $message->isFlash(), + ]; + $response = $this->performApi($url, $params); + + return $this->generateResponse($response->result->code, $response->result->message, (array) $response->items); + } + + /** + * @param GhasedakMessage $message + * + * @throws GuzzleException + */ + public function verify($message): object + { + $url = self::endpoint('BaseServiceNumber'); + $params = [ + 'username' => $this->setting->username, + 'password' => $this->setting->password, + 'text' => $message->getTokens(), + 'to' => $message->getTo(), + 'bodyId' => $message->getTemplate() + ]; + + $response = $this->performApi($url, $params); + + return $this->generateResponse($response->result->code, $response->result->message, (array) $response->items); + + } + + /** + * @throws GuzzleException + */ + public function account(): object + { + $url = self::endpoint('account', 'info'); + $response = $this->performApi($url); + + $items = $response->items; + + return $this->generateAccountResponse($items->balance, $items->expire); + } + + /** + * @throws GuzzleException + */ + public function outbox($page_size = 100, $page_number = 1): object + { + $url = self::endpoint('sms', 'status'); + $response = $this->performApi($url); + + return collect($response->items)->map(function ($item) { + return $this->generateReportResponse($item->messageid, $item->receptor, $item->message, $item->senddate, $item->sender, $item->price); + }); + } + + /** + * @throws GuzzleException + */ + public function performApi(string $url, array $params = []): object + { + $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->code !== Response::HTTP_OK) { + throw new ApiException($json_response->result->message, $json_response->result->code); + } + } +} diff --git a/src/Drivers/Farapayamak/FarapayamakMessage.php b/src/Drivers/Farapayamak/FarapayamakMessage.php new file mode 100644 index 0000000..790a9e1 --- /dev/null +++ b/src/Drivers/Farapayamak/FarapayamakMessage.php @@ -0,0 +1,99 @@ +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; + } +} diff --git a/src/Enums/Drivers.php b/src/Enums/Drivers.php index 1f45325..2740c49 100644 --- a/src/Enums/Drivers.php +++ b/src/Enums/Drivers.php @@ -2,6 +2,8 @@ namespace TookanTech\Chapaar\Enums; +use TookanTech\Chapaar\Drivers\Farapayamak\FarapayamakConnector; +use TookanTech\Chapaar\Drivers\Ghasedak\FarapayamakMessage; use TookanTech\Chapaar\Drivers\Ghasedak\GhasedakConnector; use TookanTech\Chapaar\Drivers\Ghasedak\GhasedakMessage; use TookanTech\Chapaar\Drivers\Kavenegar\KavenegarConnector; @@ -14,6 +16,8 @@ enum Drivers: string case KAVENEGAR = 'kavenegar'; case SMSIR = 'smsir'; case GHASEDAK = 'ghasedak'; + case FARAPAYAMAK = 'farapayamak'; + public function connector(): string { @@ -21,6 +25,8 @@ public function connector(): string self::KAVENEGAR => KavenegarConnector::class, self::SMSIR => SmsIrConnector::class, self::GHASEDAK => GhasedakConnector::class, + self::FARAPAYAMAK => FarapayamakConnector::class, + }; } @@ -30,6 +36,7 @@ public function message(): string self::KAVENEGAR => KavenegarMessage::class, self::SMSIR => SmsIrMessage::class, self::GHASEDAK => GhasedakMessage::class, + self::FARAPAYAMAK => FarapayamakMessage::class }; } } From 0b8c22b4e0c5bc7fc5383830b57cc6ddbc92bdb4 Mon Sep 17 00:00:00 2001 From: aryala7 Date: Sat, 21 Oct 2023 09:03:59 +0000 Subject: [PATCH 2/5] Fix styling --- src/Drivers/Farapayamak/FarapayamakConnector.php | 2 +- src/Drivers/Farapayamak/FarapayamakMessage.php | 4 ++-- src/Drivers/Ghasedak/GhasedakMessage.php | 2 +- src/Enums/Drivers.php | 1 - 4 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/Drivers/Farapayamak/FarapayamakConnector.php b/src/Drivers/Farapayamak/FarapayamakConnector.php index 41f1961..94d51f4 100644 --- a/src/Drivers/Farapayamak/FarapayamakConnector.php +++ b/src/Drivers/Farapayamak/FarapayamakConnector.php @@ -67,7 +67,7 @@ public function verify($message): object 'password' => $this->setting->password, 'text' => $message->getTokens(), 'to' => $message->getTo(), - 'bodyId' => $message->getTemplate() + 'bodyId' => $message->getTemplate(), ]; $response = $this->performApi($url, $params); diff --git a/src/Drivers/Farapayamak/FarapayamakMessage.php b/src/Drivers/Farapayamak/FarapayamakMessage.php index 790a9e1..5a0813b 100644 --- a/src/Drivers/Farapayamak/FarapayamakMessage.php +++ b/src/Drivers/Farapayamak/FarapayamakMessage.php @@ -16,7 +16,6 @@ class FarapayamakMessage implements DriverMessage protected array $tokens = []; - protected bool $flash = false; /** @@ -78,12 +77,13 @@ public function setTemplate($template): self public function getTokens(): string { - return implode(';',$this->tokens); + return implode(';', $this->tokens); } public function setTokens(array $tokens): self { $this->tokens = $tokens; + return $this; } diff --git a/src/Drivers/Ghasedak/GhasedakMessage.php b/src/Drivers/Ghasedak/GhasedakMessage.php index 99a388d..e9b56e3 100644 --- a/src/Drivers/Ghasedak/GhasedakMessage.php +++ b/src/Drivers/Ghasedak/GhasedakMessage.php @@ -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; } diff --git a/src/Enums/Drivers.php b/src/Enums/Drivers.php index 2740c49..92a0783 100644 --- a/src/Enums/Drivers.php +++ b/src/Enums/Drivers.php @@ -18,7 +18,6 @@ enum Drivers: string case GHASEDAK = 'ghasedak'; case FARAPAYAMAK = 'farapayamak'; - public function connector(): string { return match ($this) { From 1855bd63b250571c8b409a92ed3e837a44b1bd87 Mon Sep 17 00:00:00 2001 From: arya Date: Tue, 31 Oct 2023 10:52:52 +0330 Subject: [PATCH 3/5] [feat] implement farapayamak driver --- .../Farapayamak/FarapayamakConnector.php | 42 ++++++++++--------- src/Traits/HasResponse.php | 2 +- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/src/Drivers/Farapayamak/FarapayamakConnector.php b/src/Drivers/Farapayamak/FarapayamakConnector.php index 94d51f4..48c5aeb 100644 --- a/src/Drivers/Farapayamak/FarapayamakConnector.php +++ b/src/Drivers/Farapayamak/FarapayamakConnector.php @@ -23,18 +23,16 @@ public function __construct() self::$setting = (object) config('chapaar.drivers.farapayamak'); $this->client = new Client([ 'headers' => [ - 'Accept' => 'application/json', - 'Content-Type' => 'application/json', + 'Content-Type' => 'application/x-www-form-urlencoded', 'charset' => 'utf-8', ], - 'verify' => false, 'http_errors' => false, ]); } /** - * @param GhasedakMessage $message + * @param FarapayamakMessage $message * * @throws GuzzleException */ @@ -42,8 +40,6 @@ public function send($message): object { $url = self::endpoint('SendSMS'); $params = [ - 'username' => $this->setting->username, - 'password' => $this->setting->password, 'from' => $message->getFrom(), 'to' => $message->getTo(), 'text' => $message->getContent(), @@ -51,11 +47,11 @@ public function send($message): object ]; $response = $this->performApi($url, $params); - return $this->generateResponse($response->result->code, $response->result->message, (array) $response->items); + return $this->generateResponse($response->RetStatus, $response->Value, (array) $response->StrRetStatus); } /** - * @param GhasedakMessage $message + * @param FarapayamakMessage $message * * @throws GuzzleException */ @@ -63,8 +59,6 @@ public function verify($message): object { $url = self::endpoint('BaseServiceNumber'); $params = [ - 'username' => $this->setting->username, - 'password' => $this->setting->password, 'text' => $message->getTokens(), 'to' => $message->getTo(), 'bodyId' => $message->getTemplate(), @@ -72,7 +66,7 @@ public function verify($message): object $response = $this->performApi($url, $params); - return $this->generateResponse($response->result->code, $response->result->message, (array) $response->items); + return $this->generateResponse($response->result->RetStatus, $response->result->StrRetStatus, (array) $response->Value); } @@ -81,12 +75,10 @@ public function verify($message): object */ public function account(): object { - $url = self::endpoint('account', 'info'); + $url = self::endpoint('GetCredit2'); $response = $this->performApi($url); - $items = $response->items; - - return $this->generateAccountResponse($items->balance, $items->expire); + return $this->generateAccountResponse($response->Value, 0); } /** @@ -94,11 +86,16 @@ public function account(): object */ public function outbox($page_size = 100, $page_number = 1): object { - $url = self::endpoint('sms', 'status'); - $response = $this->performApi($url); + $url = self::endpoint('GetMessages'); + $params = [ + 'location' => 2, // sent messages + 'index' => 0, + 'count' => 100 + ]; + $response = $this->performApi($url,$params); - return collect($response->items)->map(function ($item) { - return $this->generateReportResponse($item->messageid, $item->receptor, $item->message, $item->senddate, $item->sender, $item->price); + return collect($response->Data)->map(function ($item) { + return $this->generateReportResponse($item->MsgID, $item->Receiver, $item->Body, $item->SendDate, $item->Sender); }); } @@ -107,6 +104,11 @@ public function outbox($page_size = 100, $page_number = 1): object */ 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, ]); @@ -130,7 +132,7 @@ protected function validateResponseStatus($status_code, $json_response): void throw new HttpException('Response is not valid JSON', $status_code); } - if ($json_response->result->code !== Response::HTTP_OK) { + if ($json_response->result->RetStatus !== 1) { throw new ApiException($json_response->result->message, $json_response->result->code); } } diff --git a/src/Traits/HasResponse.php b/src/Traits/HasResponse.php index 9e81f50..bef5283 100644 --- a/src/Traits/HasResponse.php +++ b/src/Traits/HasResponse.php @@ -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) [ From ee274857c4afeba0419dcbcc432dc137c9c9a95f Mon Sep 17 00:00:00 2001 From: aryala7 Date: Tue, 31 Oct 2023 07:24:46 +0000 Subject: [PATCH 4/5] Fix styling --- src/Drivers/Farapayamak/FarapayamakConnector.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Drivers/Farapayamak/FarapayamakConnector.php b/src/Drivers/Farapayamak/FarapayamakConnector.php index 48c5aeb..7ae6cfb 100644 --- a/src/Drivers/Farapayamak/FarapayamakConnector.php +++ b/src/Drivers/Farapayamak/FarapayamakConnector.php @@ -4,7 +4,6 @@ use GuzzleHttp\Client; use GuzzleHttp\Exception\GuzzleException; -use Illuminate\Http\Response; use TookanTech\Chapaar\Contracts\DriverConnector; use TookanTech\Chapaar\Exceptions\ApiException; use TookanTech\Chapaar\Exceptions\HttpException; @@ -90,9 +89,9 @@ public function outbox($page_size = 100, $page_number = 1): object $params = [ 'location' => 2, // sent messages 'index' => 0, - 'count' => 100 + 'count' => 100, ]; - $response = $this->performApi($url,$params); + $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); @@ -105,7 +104,7 @@ public function outbox($page_size = 100, $page_number = 1): object public function performApi(string $url, array $params = []): object { - $params = [...$params,...[ + $params = [...$params, ...[ 'username' => $this->setting->username, 'password' => $this->setting->password, ]]; From b6903e7aa03cc54c42d935493f83f8ed0e4ddcfb Mon Sep 17 00:00:00 2001 From: arya Date: Tue, 31 Oct 2023 10:58:24 +0330 Subject: [PATCH 5/5] [fix] fix namespace --- src/Drivers/Farapayamak/FarapayamakMessage.php | 2 +- src/Enums/Drivers.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Drivers/Farapayamak/FarapayamakMessage.php b/src/Drivers/Farapayamak/FarapayamakMessage.php index 5a0813b..fb5162f 100644 --- a/src/Drivers/Farapayamak/FarapayamakMessage.php +++ b/src/Drivers/Farapayamak/FarapayamakMessage.php @@ -1,6 +1,6 @@