-
Notifications
You must be signed in to change notification settings - Fork 8
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 #7 from tarfin-labs/add-avaliable-credit-package-s…
…ervice Add avaliable credit package service
- Loading branch information
Showing
29 changed files
with
650 additions
and
177 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
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 |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
namespace TarfinLabs\Netgsm\Balance; | ||
|
||
use GuzzleHttp\Exception\GuzzleException; | ||
use TarfinLabs\Netgsm\Exceptions\NetgsmException; | ||
use TarfinLabs\Netgsm\NetgsmApiClient; | ||
use TarfinLabs\Netgsm\NetgsmErrors; | ||
|
||
class NetgsmAvailableCredit extends NetgsmApiClient | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
protected $response; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $successCodes = [ | ||
'00', | ||
]; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $url = 'balance/list/get'; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $errorCodes = [ | ||
'30' => NetgsmErrors::CREDENTIALS_INCORRECT, | ||
'40' => NetgsmErrors::CREDENTIALS_INCORRECT, | ||
'100' => NetgsmErrors::SYSTEM_ERROR, | ||
]; | ||
|
||
/** | ||
* extracts credit from the returned response. | ||
* | ||
* @return string | ||
* @throws NetgsmException | ||
*/ | ||
public function parseResponse(): ?string | ||
{ | ||
$result = explode(' ', $this->response); | ||
|
||
if (empty($result[0])) { | ||
throw new NetgsmException(NetgsmErrors::NETGSM_GENERAL_ERROR); | ||
} | ||
|
||
$code = $result[0]; | ||
|
||
if (! in_array($code, $this->successCodes)) { | ||
$message = $this->errorCodes[$code]; | ||
throw new NetgsmException($message, $code); | ||
} | ||
|
||
return $result[1]; | ||
} | ||
|
||
/** | ||
* returns the credits amount for associated netgsm account. | ||
* | ||
* @throws NetgsmErrors | ||
* @throws GuzzleException | ||
* @throws NetgsmException | ||
*/ | ||
public function getCredit(): ?string | ||
{ | ||
$this->response = $this->callApi('GET', $this->url); | ||
|
||
return $this->parseResponse(); | ||
} | ||
} |
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,73 @@ | ||
<?php | ||
|
||
namespace TarfinLabs\Netgsm\Balance; | ||
|
||
use GuzzleHttp\Exception\GuzzleException; | ||
use TarfinLabs\Netgsm\Exceptions\NetgsmException; | ||
use TarfinLabs\Netgsm\NetgsmApiClient; | ||
use TarfinLabs\Netgsm\NetgsmErrors; | ||
|
||
class NetgsmPackages extends NetgsmApiClient | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
protected $response; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $url = 'balance/list/get'; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $errorCodes = [ | ||
'30' => NetgsmErrors::MESSAGE_TOO_LONG, | ||
'40' => NetgsmErrors::CREDENTIALS_INCORRECT, | ||
'100' => NetgsmErrors::SYSTEM_ERROR, | ||
]; | ||
|
||
/** | ||
* handles the response and return the package list as an array. | ||
* | ||
* @return array | ||
* @throws NetgsmException | ||
*/ | ||
public function parseResponse(): array | ||
{ | ||
$availablePackages = []; | ||
|
||
if (array_key_exists($this->response, $this->errorCodes)) { | ||
$message = $this->errorCodes[$this->response]; | ||
throw new NetgsmException($message, $this->response); | ||
} | ||
|
||
$rows = array_filter(explode('<BR>', $this->response)); | ||
foreach ($rows as $row) { | ||
$columns = array_filter(explode('|', $row)); | ||
$columns = array_map('trim', $columns); | ||
$availablePackages[] = [ | ||
'amount' => (int) $columns[0], | ||
'amountType' => $columns[1] ?? null, | ||
'packageType' => $columns[2] ?? null, | ||
]; | ||
} | ||
|
||
return $availablePackages; | ||
} | ||
|
||
/** | ||
* returns the packages list for associated netgsm account. | ||
* | ||
* @return array | ||
* @throws GuzzleException | ||
* @throws NetgsmException | ||
*/ | ||
public function getPackages(): array | ||
{ | ||
$this->response = $this->callApi('GET', $this->url, ['tip' => 1]); | ||
|
||
return $this->parseResponse(); | ||
} | ||
} |
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
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,9 @@ | ||
<?php | ||
|
||
namespace TarfinLabs\Netgsm\Exceptions; | ||
|
||
use Exception; | ||
|
||
class NetgsmException extends Exception | ||
{ | ||
} |
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
Oops, something went wrong.