1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Djunehor\Vtu\Concrete; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Djunehor\Vtu\Contracts\VtuServiceInterface; |
8
|
|
|
use GuzzleHttp\Exception\ClientException; |
9
|
|
|
use GuzzleHttp\Psr7\Request; |
10
|
|
|
use Illuminate\Support\Str; |
11
|
|
|
|
12
|
|
|
class VTPass extends Vtu |
13
|
|
|
{ |
14
|
|
|
private $sandboxURL='https://sandbox.vtpass.com/api/'; |
15
|
|
|
private $liveAPI='https://vtpass.com/api/'; |
16
|
|
|
private $baseUrl; |
17
|
|
|
|
18
|
1 |
|
public function __construct($username = null, $password = null, $live = true) |
19
|
|
|
{ |
20
|
1 |
|
$this->username = $username ?? config('laravel-vtu.vtpass.username'); |
21
|
1 |
|
$this->password = $password ?? config('laravel-vtu.vtpass.password'); |
22
|
1 |
|
$this->baseUrl = $live ? $this->liveAPI : $this->sandboxURL; |
23
|
|
|
|
24
|
1 |
|
$this->client = $this->getInstance(); |
25
|
|
|
|
26
|
1 |
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* acceptable network: mtn, airtel, etisalat, glo |
30
|
|
|
* @param $amount |
31
|
|
|
* @param $mobileNumber |
32
|
|
|
* @param $mobileNetwork |
33
|
|
|
* @param null $callBackUrl |
34
|
|
|
* @return bool |
35
|
|
|
*/ |
36
|
1 |
|
public function buyAirtime($amount, $mobileNumber, $mobileNetwork, $callBackUrl = null): bool |
37
|
|
|
{ |
38
|
|
|
$headers = [ |
39
|
1 |
|
"Authorization" => "Basic ".$this->username.$this->password |
40
|
|
|
]; |
41
|
1 |
|
$this->request = new Request('POST', $this->baseUrl."pay", $headers); |
42
|
|
|
|
43
|
|
|
try { |
44
|
1 |
|
$response = $this->client->send($this->request, [ |
45
|
|
|
'form_params' => [ |
46
|
1 |
|
'request_id' => $requestId = Str::random(32), |
47
|
1 |
|
'serviceID' => $mobileNetwork, |
48
|
1 |
|
'amount' => $amount, |
49
|
1 |
|
'phone' => $mobileNumber, |
50
|
|
|
], |
51
|
|
|
]); |
52
|
|
|
|
53
|
|
|
$response = json_decode($response->getBody()->getContents(), true); |
54
|
|
|
$this->response = $response['response_description']; |
55
|
|
|
$this->orderId = $requestId; |
|
|
|
|
56
|
|
|
|
57
|
|
|
return $response['code'] == '000' ? true : false; |
58
|
1 |
|
} catch (ClientException $e) { |
59
|
1 |
|
$this->httpError = $e; |
60
|
|
|
|
61
|
1 |
|
return false; |
62
|
|
|
} catch (\Exception $e) { |
63
|
|
|
$this->httpError = $e; |
64
|
|
|
|
65
|
|
|
return false; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @inheritDoc |
71
|
|
|
*/ |
72
|
|
|
public function buyData($mobileNumber, $mobileNetwork, $dataPlan, $callBackUrl): bool |
73
|
|
|
{ |
74
|
|
|
// TODO: Implement buyData() method. |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
|
78
|
|
View Code Duplication |
public function queryOrder($orderId): bool |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
$headers = [ |
81
|
|
|
"Authorization" => "Basic ".$this->username.$this->password |
82
|
|
|
]; |
83
|
|
|
$this->request = new Request('POST', $this->baseUrl."pay", $headers); |
84
|
|
|
|
85
|
|
|
try { |
86
|
|
|
$response = $this->client->send($this->request, [ |
87
|
|
|
'form_params' => [ |
88
|
|
|
'request_id' => $requestId = Str::random(32), |
89
|
|
|
], |
90
|
|
|
]); |
91
|
|
|
|
92
|
|
|
$response = json_decode($response->getBody()->getContents(), true); |
93
|
|
|
$this->response = $response['content']; |
94
|
|
|
|
95
|
|
|
return $response['code'] == '001' ? true : false; |
96
|
|
|
} catch (ClientException $e) { |
97
|
|
|
$this->httpError = $e; |
98
|
|
|
|
99
|
|
|
return false; |
100
|
|
|
} catch (\Exception $e) { |
101
|
|
|
$this->httpError = $e; |
102
|
|
|
|
103
|
|
|
return false; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
View Code Duplication |
public function variationCodes($serviceId): bool |
|
|
|
|
108
|
|
|
{ |
109
|
|
|
$headers = [ |
110
|
|
|
"Authorization" => "Basic ".$this->username.$this->password |
111
|
|
|
]; |
112
|
|
|
$this->request = new Request('GET', $this->baseUrl."service-variations", $headers); |
113
|
|
|
|
114
|
|
|
try { |
115
|
|
|
$response = $this->client->send($this->request, [ |
116
|
|
|
'query_params' => [ |
117
|
|
|
'serviceID' => $serviceId, |
118
|
|
|
], |
119
|
|
|
]); |
120
|
|
|
|
121
|
|
|
$response = json_decode($response->getBody()->getContents(), true); |
122
|
|
|
$this->response = $response['content']; |
123
|
|
|
|
124
|
|
|
return $response['content'] ? true : false; |
125
|
|
|
} catch (ClientException $e) { |
126
|
|
|
$this->httpError = $e; |
127
|
|
|
|
128
|
|
|
return false; |
129
|
|
|
} catch (\Exception $e) { |
130
|
|
|
$this->httpError = $e; |
131
|
|
|
|
132
|
|
|
return false; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
View Code Duplication |
public function verifySmartCard($smartCardNumber, $serviceId): bool |
|
|
|
|
137
|
|
|
{ |
138
|
|
|
$headers = [ |
139
|
|
|
"Authorization" => "Basic ".$this->username.$this->password |
140
|
|
|
]; |
141
|
|
|
$this->request = new Request('GET', $this->baseUrl."merchant-verify", $headers); |
142
|
|
|
|
143
|
|
|
try { |
144
|
|
|
$response = $this->client->send($this->request, [ |
145
|
|
|
'query_params' => [ |
146
|
|
|
'billersCode' => $smartCardNumber, |
147
|
|
|
'serviceID' => $serviceId, |
148
|
|
|
], |
149
|
|
|
]); |
150
|
|
|
|
151
|
|
|
$response = json_decode($response->getBody()->getContents(), true); |
152
|
|
|
$this->response = $response['content']; |
153
|
|
|
|
154
|
|
|
return $response['code'] == '000' ? true : false; |
155
|
|
|
} catch (ClientException $e) { |
156
|
|
|
$this->httpError = $e; |
157
|
|
|
|
158
|
|
|
return false; |
159
|
|
|
} catch (\Exception $e) { |
160
|
|
|
$this->httpError = $e; |
161
|
|
|
|
162
|
|
|
return false; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public function payUtility($smartCardNumber, $cableTv, $package, $callbackUrl = null, $phone = null): bool |
167
|
|
|
{ |
168
|
|
|
$headers = [ |
169
|
|
|
"Authorization" => "Basic ".$this->username.$this->password |
170
|
|
|
]; |
171
|
|
|
$this->request = new Request('GET', $this->baseUrl."pay", $headers); |
172
|
|
|
|
173
|
|
|
try { |
174
|
|
|
$response = $this->client->send($this->request, [ |
175
|
|
|
'query_params' => [ |
176
|
|
|
'billersCode' => $smartCardNumber, |
177
|
|
|
'serviceID' => $cableTv, |
178
|
|
|
'variation_code' => $package, |
179
|
|
|
'phone' => $phone, |
180
|
|
|
], |
181
|
|
|
]); |
182
|
|
|
|
183
|
|
|
$response = json_decode($response->getBody()->getContents(), true); |
184
|
|
|
$this->response = $response['content']; |
185
|
|
|
|
186
|
|
|
return $response['code'] == '000' ? true : false; |
187
|
|
|
} catch (ClientException $e) { |
188
|
|
|
$this->httpError = $e; |
189
|
|
|
|
190
|
|
|
return false; |
191
|
|
|
} catch (\Exception $e) { |
192
|
|
|
$this->httpError = $e; |
193
|
|
|
|
194
|
|
|
return false; |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
} |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: