Skip to content

Commit

Permalink
[feat] implement farapayamak driver
Browse files Browse the repository at this point in the history
  • Loading branch information
aryala7 committed Oct 31, 2023
1 parent 0b8c22b commit 1855bd6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 21 deletions.
42 changes: 22 additions & 20 deletions src/Drivers/Farapayamak/FarapayamakConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,56 +23,50 @@ 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
*/
public function send($message): object

Check failure on line 39 in src/Drivers/Farapayamak/FarapayamakConnector.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter $message of method TookanTech\Chapaar\Drivers\Farapayamak\FarapayamakConnector::send() has invalid type TookanTech\Chapaar\Drivers\Farapayamak\FarapayamakMessage.
{
$url = self::endpoint('SendSMS');
$params = [
'username' => $this->setting->username,
'password' => $this->setting->password,
'from' => $message->getFrom(),

Check failure on line 43 in src/Drivers/Farapayamak/FarapayamakConnector.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to method getFrom() on an unknown class TookanTech\Chapaar\Drivers\Farapayamak\FarapayamakMessage.
'to' => $message->getTo(),

Check failure on line 44 in src/Drivers/Farapayamak/FarapayamakConnector.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to method getTo() on an unknown class TookanTech\Chapaar\Drivers\Farapayamak\FarapayamakMessage.
'text' => $message->getContent(),

Check failure on line 45 in src/Drivers/Farapayamak/FarapayamakConnector.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to method getContent() on an unknown class TookanTech\Chapaar\Drivers\Farapayamak\FarapayamakMessage.
'isFlash' => $message->isFlash(),

Check failure on line 46 in src/Drivers/Farapayamak/FarapayamakConnector.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to method isFlash() on an unknown class TookanTech\Chapaar\Drivers\Farapayamak\FarapayamakMessage.
];
$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
*/
public function verify($message): object

Check failure on line 58 in src/Drivers/Farapayamak/FarapayamakConnector.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter $message of method TookanTech\Chapaar\Drivers\Farapayamak\FarapayamakConnector::verify() has invalid type TookanTech\Chapaar\Drivers\Farapayamak\FarapayamakMessage.
{
$url = self::endpoint('BaseServiceNumber');
$params = [
'username' => $this->setting->username,
'password' => $this->setting->password,
'text' => $message->getTokens(),

Check failure on line 62 in src/Drivers/Farapayamak/FarapayamakConnector.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to method getTokens() on an unknown class TookanTech\Chapaar\Drivers\Farapayamak\FarapayamakMessage.
'to' => $message->getTo(),

Check failure on line 63 in src/Drivers/Farapayamak/FarapayamakConnector.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to method getTo() on an unknown class TookanTech\Chapaar\Drivers\Farapayamak\FarapayamakMessage.
'bodyId' => $message->getTemplate(),

Check failure on line 64 in src/Drivers/Farapayamak/FarapayamakConnector.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to method getTemplate() on an unknown class TookanTech\Chapaar\Drivers\Farapayamak\FarapayamakMessage.
];

$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);

}

Expand All @@ -81,24 +75,27 @@ 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);
}

/**
* @throws GuzzleException
*/
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);
});
}

Expand All @@ -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,
]);
Expand All @@ -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);
}
}
Expand Down
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 1855bd6

Please sign in to comment.