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 NetgsmAvailableCredit extends NetgsmApiClient |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $response; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
protected $successCodes = [ |
21
|
|
|
'00', |
22
|
|
|
]; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $url = 'balance/list/get'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected $errorCodes = [ |
33
|
|
|
'30' => NetgsmErrors::CREDENTIALS_INCORRECT, |
34
|
|
|
'40' => NetgsmErrors::NO_RECORD, |
35
|
|
|
'100' => NetgsmErrors::SYSTEM_ERROR, |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* extracts credit from the returned response. |
40
|
|
|
* |
41
|
|
|
* @return string |
42
|
|
|
* @throws NetgsmException |
43
|
|
|
*/ |
44
|
|
|
public function parseResponse(): ?string |
45
|
|
|
{ |
46
|
|
|
$result = explode(' ', $this->response); |
47
|
|
|
|
48
|
|
|
if (empty($result[0])) { |
49
|
|
|
throw new NetgsmException(NetgsmErrors::NETGSM_GENERAL_ERROR); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$code = $result[0]; |
53
|
|
|
|
54
|
|
|
if (! in_array($code, $this->successCodes)) { |
55
|
|
|
$message = $this->errorCodes[$code] ?? NetgsmErrors::SYSTEM_ERROR; |
56
|
|
|
throw new NetgsmException($message, $code); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $result[1]; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* returns the credits amount for associated netgsm account. |
64
|
|
|
* |
65
|
|
|
* @throws NetgsmErrors |
66
|
|
|
* @throws GuzzleException |
67
|
|
|
* @throws NetgsmException |
68
|
|
|
*/ |
69
|
|
|
public function getCredit(): ?string |
70
|
|
|
{ |
71
|
|
|
$this->response = $this->callApi('GET', $this->url); |
72
|
|
|
|
73
|
|
|
return $this->parseResponse(); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|