|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Djunehor\Eyowo; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use GuzzleHttp\Client; |
|
7
|
|
|
use GuzzleHttp\Exception\ClientException; |
|
8
|
|
|
use Psr\Http\Message\MessageInterface; |
|
9
|
|
|
|
|
10
|
|
|
class Api |
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
private $baseUrl = "https://api.console.eyowo.com/"; |
|
14
|
|
|
private $appKey; |
|
15
|
|
|
private $appSecret; |
|
16
|
|
|
public $client; |
|
17
|
|
|
protected $output; |
|
18
|
|
|
protected $factors = ['sms']; |
|
19
|
|
|
protected $providers = ['etisalat', 'glo', 'airtel', 'mtn']; |
|
20
|
|
|
protected $errorCodes = [ |
|
21
|
|
|
400 => 'Bad Request -- Your request is invalid.', |
|
22
|
|
|
401 => 'Unauthorized -- Your API key is wrong.', |
|
23
|
|
|
403 => 'Forbidden -- The kitten requested is hidden for administrators only.', |
|
24
|
|
|
404 => 'Not Found -- The specified kitten could not be found.', |
|
25
|
|
|
405 => 'Method Not Allowed -- You tried to access a kitten with an invalid method.', |
|
26
|
|
|
406 => "Not Acceptable -- You requested a format that isn't json.", |
|
27
|
|
|
410 => 'Gone -- The kitten requested has been removed from our servers.', |
|
28
|
|
|
418 => "I'm a teapot.", |
|
29
|
|
|
429 => "Too Many Requests -- You're requesting too many kittens! Slow down!", |
|
30
|
|
|
500 => "Internal Server Error -- We had a problem with our server. Try again later.", |
|
31
|
|
|
503 => "Service Unavailable -- We're temporarily offline for maintenance. Please try again later." |
|
32
|
|
|
]; |
|
33
|
|
|
|
|
34
|
|
|
protected $errorMessage; |
|
35
|
|
|
protected $statusCode; |
|
36
|
|
|
|
|
37
|
14 |
|
public function __construct($appKey = null) |
|
38
|
|
|
{ |
|
39
|
14 |
|
$this->appKey = isset($appKey) ? $appKey : getenv('EYOWO_APP_KEY'); |
|
40
|
|
|
|
|
41
|
14 |
|
$this->client = new Client([ |
|
42
|
14 |
|
'base_uri' => $this->baseUrl, |
|
43
|
|
|
'headers' => [ |
|
44
|
14 |
|
'X-App-Key' => $this->appKey, |
|
45
|
14 |
|
'Content-Type' => 'application/json', |
|
46
|
|
|
] |
|
47
|
|
|
]);; |
|
48
|
14 |
|
} |
|
49
|
|
|
|
|
50
|
2 |
|
public function resolveResponse(MessageInterface $response) |
|
51
|
|
|
{ |
|
52
|
2 |
|
$this->statusCode = $response->getStatusCode(); |
|
|
|
|
|
|
53
|
2 |
|
$this->errorMessage = array_key_exists($this->statusCode, $this->errorCodes) ? $this->errorCodes[$this->statusCode] : ''; |
|
54
|
|
|
|
|
55
|
2 |
|
return $this->output = json_decode($response->getBody()->getContents(), true); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
9 |
|
public function handleException(ClientException $exception) |
|
59
|
|
|
{ |
|
60
|
9 |
|
$this->statusCode = $exception->getCode(); |
|
61
|
9 |
|
$this->errorMessage = $exception->getMessage(); |
|
62
|
|
|
|
|
63
|
9 |
|
$output = json_decode($exception->getResponse()->getBody(), true); |
|
64
|
9 |
|
$this->output['success'] = false; |
|
65
|
9 |
|
$this->output['message'] = array_key_exists('message', $output) ? $output['message'] : $this->errorMessage; |
|
66
|
|
|
|
|
67
|
9 |
|
return $this->output; |
|
68
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function getStatusCode() |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->statusCode; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
1 |
|
public function getError() |
|
77
|
|
|
{ |
|
78
|
1 |
|
return $this->errorMessage; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
1 |
View Code Duplication |
public function validate($phone) |
|
|
|
|
|
|
82
|
|
|
{ |
|
83
|
|
|
try { |
|
84
|
1 |
|
$response = $this->client->post('/v1/users/auth/validate', [ |
|
85
|
|
|
'form_params' => [ |
|
86
|
1 |
|
'mobile' => $phone |
|
87
|
|
|
] |
|
88
|
|
|
]); |
|
89
|
1 |
|
} catch (ClientException $e) { |
|
90
|
1 |
|
return $this->handleException($e); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return $this->resolveResponse($response); |
|
94
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
2 |
|
public function initiateAuthorization($phone, $factor = 'sms') |
|
98
|
|
|
{ |
|
99
|
2 |
|
if (!in_array($factor, $this->factors)) { |
|
100
|
1 |
|
throw new \Exception("Factor [$factor] not available. Available factors are: " . join(", ", $this->factors)); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
try { |
|
104
|
1 |
|
$response = $this->client->post('/v1/users/auth', [ |
|
105
|
|
|
'form_params' => [ |
|
106
|
1 |
|
'mobile' => $phone, |
|
107
|
1 |
|
'factor' => $factor |
|
108
|
|
|
] |
|
109
|
|
|
]); |
|
110
|
1 |
|
} catch (ClientException $e) { |
|
111
|
1 |
|
return $this->handleException($e); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
return $this->resolveResponse($response); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
1 |
View Code Duplication |
public function generateToken($phone, $passcode, $factor = 'sms') |
|
|
|
|
|
|
118
|
|
|
{ |
|
119
|
1 |
|
if (!in_array($factor, $this->factors)) { |
|
120
|
|
|
throw new \Exception("Factor [$factor] not available. Available factors are: " . join(", ", $this->factors)); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
try { |
|
124
|
1 |
|
$response = $this->client->post('/v1/users/auth', [ |
|
125
|
|
|
'form_params' => [ |
|
126
|
1 |
|
'mobile' => $phone, |
|
127
|
1 |
|
'factor' => $factor, |
|
128
|
1 |
|
'passcode' => $passcode |
|
129
|
|
|
] |
|
130
|
|
|
]); |
|
131
|
1 |
|
} catch (ClientException $e) { |
|
132
|
1 |
|
return $this->handleException($e); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
return $this->resolveResponse($response); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
1 |
View Code Duplication |
public function refreshToken($refreshToken) |
|
|
|
|
|
|
139
|
|
|
{ |
|
140
|
|
|
|
|
141
|
|
|
try { |
|
142
|
1 |
|
$response = $this->client->post('/v1//users/accessToken', [ |
|
143
|
|
|
'form_params' => [ |
|
144
|
1 |
|
'refreshToken' => $refreshToken, |
|
145
|
|
|
] |
|
146
|
|
|
]); |
|
147
|
1 |
|
} catch (ClientException $e) { |
|
148
|
1 |
|
return $this->handleException($e); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
return $this->resolveResponse($response); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
1 |
|
public function transferToPhone($walletToken, $amount, $mobile) |
|
155
|
|
|
{ |
|
156
|
|
|
|
|
157
|
|
|
try { |
|
158
|
1 |
|
$response = $this->client->post('/v1/transfers/phone', [ |
|
159
|
1 |
|
'headers' => ['X-App-Wallet-Access-Token' => $walletToken], |
|
160
|
|
|
'form_params' => [ |
|
161
|
1 |
|
'amount' => $amount, |
|
162
|
1 |
|
'mobile' => $mobile |
|
163
|
|
|
] |
|
164
|
|
|
]); |
|
165
|
1 |
|
} catch (ClientException $e) { |
|
166
|
1 |
|
return $this->handleException($e); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
return $this->resolveResponse($response); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
1 |
|
public function transferToBank($walletToken, $amount, $accountName, $accountNumber, $bankCode) |
|
173
|
|
|
{ |
|
174
|
|
|
|
|
175
|
|
|
try { |
|
176
|
1 |
|
$response = $this->client->post('/v1/transfers/bank', [ |
|
177
|
1 |
|
'headers' => ['X-App-Wallet-Access-Token' => $walletToken], |
|
178
|
|
|
'form_params' => [ |
|
179
|
1 |
|
'amount' => $amount, |
|
180
|
1 |
|
'accountName' => $accountName, |
|
181
|
1 |
|
'accountNumber' => $accountNumber, |
|
182
|
1 |
|
'bankCode' => $bankCode |
|
183
|
|
|
] |
|
184
|
|
|
]); |
|
185
|
1 |
|
} catch (ClientException $e) { |
|
186
|
1 |
|
return $this->handleException($e); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
return $this->resolveResponse($response); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
2 |
View Code Duplication |
public function vtu($walletToken, $mobile, $amount, $provider) |
|
|
|
|
|
|
193
|
|
|
{ |
|
194
|
|
|
|
|
195
|
2 |
|
if (!in_array(strtolower($provider), $this->providers)) { |
|
196
|
1 |
|
throw new \Exception("Provider [$provider] not supported. Supported providers are: " . join(",", $this->providers)); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
try { |
|
200
|
1 |
|
$response = $this->client->post('/v1/users/payments/bills/vtu', [ |
|
201
|
1 |
|
'headers' => ['X-App-Wallet-Access-Token' => $walletToken], |
|
202
|
|
|
'form_params' => [ |
|
203
|
1 |
|
'amount' => $amount, |
|
204
|
1 |
|
'mobile' => $mobile, |
|
205
|
1 |
|
'provider' => $provider |
|
206
|
|
|
] |
|
207
|
|
|
]); |
|
208
|
1 |
|
} catch (ClientException $e) { |
|
209
|
1 |
|
return $this->handleException($e); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
return $this->resolveResponse($response); |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
2 |
View Code Duplication |
public function balance($walletToken) |
|
|
|
|
|
|
216
|
|
|
{ |
|
217
|
|
|
|
|
218
|
|
|
try { |
|
219
|
2 |
|
$response = $this->client->get('/v1/users/accessToken', [ |
|
220
|
2 |
|
'headers' => ['X-App-Wallet-Access-Token' => $walletToken] |
|
221
|
|
|
]); |
|
222
|
|
|
|
|
223
|
|
|
return $this->resolveResponse($response); |
|
224
|
2 |
|
} catch (ClientException $e) { |
|
225
|
2 |
|
return $this->handleException($e); |
|
226
|
|
|
} |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
2 |
|
public function banks() |
|
230
|
|
|
{ |
|
231
|
|
|
try { |
|
232
|
2 |
|
$response = $this->client->get('/v1/queries/banks'); |
|
233
|
|
|
} catch (ClientException $e) { |
|
234
|
|
|
return $this->handleException($e); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
2 |
|
return $this->resolveResponse($response); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
1 |
|
public function getBanks() |
|
241
|
|
|
{ |
|
242
|
1 |
|
return isset($this->output['data']['banks']) ? $this->output['data']['banks'] : []; |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
1 |
|
public function getWalletBalance() |
|
246
|
|
|
{ |
|
247
|
1 |
|
return isset($this->output['balance']) ? $this->output['balance'] : 0; |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
1 |
|
public function getTokenData() |
|
251
|
|
|
{ |
|
252
|
1 |
|
return isset($this->output['data']) ? $this->output['data'] : []; |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
1 |
|
public function getAccessToken() |
|
256
|
|
|
{ |
|
257
|
1 |
|
$tokenData = $this->getTokenData(); |
|
258
|
1 |
|
return isset($tokenData['accessToken']) ? $tokenData['accessToken'] : null; |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
1 |
|
public function getRefreshToken() |
|
262
|
|
|
{ |
|
263
|
1 |
|
$tokenData = $this->getTokenData(); |
|
264
|
1 |
|
return isset($tokenData['refreshToken']) ? $tokenData['refreshToken'] : null; |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
public function getUser() |
|
268
|
|
|
{ |
|
269
|
|
|
return isset($this->output['data'], $this->output['data']['user']) ? $this->output['data']['user'] : []; |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
public function mobileIsValid() |
|
273
|
|
|
{ |
|
274
|
|
|
return isset($this->output['success']); |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
} |
|
278
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: