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..7ae6cfb --- /dev/null +++ b/src/Drivers/Farapayamak/FarapayamakConnector.php @@ -0,0 +1,138 @@ +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); + } + } +} diff --git a/src/Drivers/Farapayamak/FarapayamakMessage.php b/src/Drivers/Farapayamak/FarapayamakMessage.php new file mode 100644 index 0000000..fb5162f --- /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/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 1f45325..4afbebc 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\Farapayamak\FarapayamakMessage; use TookanTech\Chapaar\Drivers\Ghasedak\GhasedakConnector; use TookanTech\Chapaar\Drivers\Ghasedak\GhasedakMessage; use TookanTech\Chapaar\Drivers\Kavenegar\KavenegarConnector; @@ -14,6 +16,7 @@ enum Drivers: string case KAVENEGAR = 'kavenegar'; case SMSIR = 'smsir'; case GHASEDAK = 'ghasedak'; + case FARAPAYAMAK = 'farapayamak'; public function connector(): string { @@ -21,6 +24,8 @@ public function connector(): string self::KAVENEGAR => KavenegarConnector::class, self::SMSIR => SmsIrConnector::class, self::GHASEDAK => GhasedakConnector::class, + self::FARAPAYAMAK => FarapayamakConnector::class, + }; } @@ -30,6 +35,7 @@ public function message(): string self::KAVENEGAR => KavenegarMessage::class, self::SMSIR => SmsIrMessage::class, self::GHASEDAK => GhasedakMessage::class, + self::FARAPAYAMAK => FarapayamakMessage::class }; } } 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) [