1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TarfinLabs\Netgsm\Balance; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
6
|
|
|
use TarfinLabs\Netgsm\Exceptions\NetgsmException; |
7
|
|
|
use TarfinLabs\Netgsm\NetgsmApiClient; |
8
|
|
|
use TarfinLabs\Netgsm\NetgsmErrors; |
9
|
|
|
|
10
|
|
|
class NetgsmPackages extends NetgsmApiClient |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $response; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $url = 'balance/list/get'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
protected $errorCodes = [ |
26
|
|
|
'30' => NetgsmErrors::CREDENTIALS_INCORRECT, |
27
|
|
|
'40' => NetgsmErrors::NO_RECORD, |
28
|
|
|
'100' => NetgsmErrors::SYSTEM_ERROR, |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* handles the response and return the package list as an array. |
33
|
|
|
* |
34
|
|
|
* @return array |
35
|
|
|
* @throws NetgsmException |
36
|
|
|
*/ |
37
|
|
|
public function parseResponse(): array |
38
|
|
|
{ |
39
|
|
|
$availablePackages = []; |
40
|
|
|
|
41
|
|
|
if (array_key_exists($this->response, $this->errorCodes)) { |
42
|
|
|
$message = $this->errorCodes[$this->response] ?? NetgsmErrors::SYSTEM_ERROR; |
43
|
|
|
throw new NetgsmException($message, $this->response); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$rows = array_filter(explode('<BR>', $this->response)); |
47
|
|
|
foreach ($rows as $row) { |
48
|
|
|
$columns = array_filter(explode('|', $row)); |
49
|
|
|
$columns = array_map('trim', $columns); |
50
|
|
|
$availablePackages[] = [ |
51
|
|
|
'amount' => (int) $columns[0], |
52
|
|
|
'amountType' => $columns[1] ?? null, |
53
|
|
|
'packageType' => $columns[2] ?? null, |
54
|
|
|
]; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $availablePackages; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* returns the packages list for associated netgsm account. |
62
|
|
|
* |
63
|
|
|
* @return array |
64
|
|
|
* @throws GuzzleException |
65
|
|
|
* @throws NetgsmException |
66
|
|
|
*/ |
67
|
|
|
public function getPackages(): array |
68
|
|
|
{ |
69
|
|
|
$this->response = $this->callApi('GET', $this->url, ['tip' => 1]); |
70
|
|
|
|
71
|
|
|
return $this->parseResponse(); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|