1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Product. |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Bmatovu\MtnMomo\Products; |
7
|
|
|
|
8
|
|
|
use Bmatovu\MtnMomo\Repositories\TokenRepository; |
9
|
|
|
use Bmatovu\OAuthNegotiator\GrantTypes\ClientCredentials; |
10
|
|
|
use Bmatovu\OAuthNegotiator\OAuth2Middleware; |
11
|
|
|
use GuzzleHttp\Client; |
12
|
|
|
use GuzzleHttp\ClientInterface; |
13
|
|
|
use GuzzleHttp\HandlerStack; |
14
|
|
|
use GuzzleHttp\Middleware; |
15
|
|
|
use Illuminate\Container\Container; |
16
|
|
|
use Illuminate\Contracts\Config\Repository; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Generic product/service. |
20
|
|
|
*/ |
21
|
|
|
abstract class Product |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Product. |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
const PRODUCT = null; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Configuration. |
32
|
|
|
* |
33
|
|
|
* @var \Illuminate\Contracts\Config\Repository |
34
|
|
|
*/ |
35
|
|
|
protected $config; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* HTTP client. |
39
|
|
|
* |
40
|
|
|
* @var \GuzzleHttp\ClientInterface |
41
|
|
|
*/ |
42
|
|
|
protected $client; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Base URI. |
46
|
|
|
* |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
protected $baseUri; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Token URI. |
53
|
|
|
* |
54
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
protected $tokenUri; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Subscription key. |
60
|
|
|
* |
61
|
|
|
* @var string |
62
|
|
|
*/ |
63
|
|
|
protected $subscriptionKey; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Client ID. |
67
|
|
|
* |
68
|
|
|
* @var string |
69
|
|
|
*/ |
70
|
|
|
protected $clientId; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Client secret. |
74
|
|
|
* |
75
|
|
|
* @var string |
76
|
|
|
*/ |
77
|
|
|
protected $clientSecret; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Client callback URI. |
81
|
|
|
* |
82
|
|
|
* @var string |
83
|
|
|
*/ |
84
|
|
|
protected $clientCallbackUri; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Currency. |
88
|
|
|
* |
89
|
|
|
* @var string |
90
|
|
|
*/ |
91
|
|
|
protected $currency; |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Environment. |
95
|
|
|
* |
96
|
|
|
* @var string |
97
|
|
|
*/ |
98
|
|
|
protected $environment; |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Party ID type. |
102
|
|
|
* |
103
|
|
|
* @var string |
104
|
|
|
*/ |
105
|
|
|
protected $partyIdType; |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Log file. |
109
|
|
|
* |
110
|
|
|
* @var string |
111
|
|
|
*/ |
112
|
|
|
protected $logFile; |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return \GuzzleHttp\ClientInterface |
116
|
|
|
*/ |
117
|
|
|
public function getClient() |
118
|
|
|
{ |
119
|
|
|
return $this->client; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param \GuzzleHttp\ClientInterface $client |
124
|
|
|
*/ |
125
|
|
|
public function setClient($client) |
126
|
|
|
{ |
127
|
|
|
$this->client = $client; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
|
|
public function getBaseUri() |
134
|
|
|
{ |
135
|
|
|
return $this->baseUri; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param string $baseUri |
140
|
|
|
*/ |
141
|
|
|
public function setBaseUri($baseUri) |
142
|
|
|
{ |
143
|
|
|
$this->baseUri = $baseUri; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @return string |
148
|
|
|
*/ |
149
|
|
|
public function getTokenUri() |
150
|
|
|
{ |
151
|
|
|
return $this->tokenUri; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param string $tokenUri |
156
|
|
|
*/ |
157
|
|
|
public function setTokenUri($tokenUri) |
158
|
|
|
{ |
159
|
|
|
$this->tokenUri = $tokenUri; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @return string |
164
|
|
|
*/ |
165
|
|
|
public function getSubscriptionKey() |
166
|
|
|
{ |
167
|
|
|
return $this->subscriptionKey; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param string $subscriptionKey |
172
|
|
|
*/ |
173
|
|
|
public function setSubscriptionKey($subscriptionKey) |
174
|
|
|
{ |
175
|
|
|
$this->subscriptionKey = $subscriptionKey; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @return string |
180
|
|
|
*/ |
181
|
|
|
public function getClientId() |
182
|
|
|
{ |
183
|
|
|
return $this->clientId; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @param string $clientId |
188
|
|
|
*/ |
189
|
|
|
public function setClientId($clientId) |
190
|
|
|
{ |
191
|
|
|
$this->clientId = $clientId; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @return string |
196
|
|
|
*/ |
197
|
|
|
public function getClientSecret() |
198
|
|
|
{ |
199
|
|
|
return $this->clientSecret; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @param string $clientSecret |
204
|
|
|
*/ |
205
|
|
|
public function setClientSecret($clientSecret) |
206
|
|
|
{ |
207
|
|
|
$this->clientSecret = $clientSecret; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @return string |
212
|
|
|
*/ |
213
|
|
|
public function getClientCallbackUri() |
214
|
|
|
{ |
215
|
|
|
return $this->clientCallbackUri; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @param string $clientCallbackUri |
220
|
|
|
*/ |
221
|
|
|
public function setClientCallbackUri($clientCallbackUri) |
222
|
|
|
{ |
223
|
|
|
$this->clientCallbackUri = $clientCallbackUri; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @return string |
228
|
|
|
*/ |
229
|
|
|
public function getCurrency() |
230
|
|
|
{ |
231
|
|
|
return $this->currency; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @param string $currency |
236
|
|
|
*/ |
237
|
|
|
public function setCurrency($currency) |
238
|
|
|
{ |
239
|
|
|
$this->currency = $currency; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @return string |
244
|
|
|
*/ |
245
|
|
|
public function getEnvironment() |
246
|
|
|
{ |
247
|
|
|
return $this->environment; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @param string $environment |
252
|
|
|
*/ |
253
|
|
|
public function setEnvironment($environment) |
254
|
|
|
{ |
255
|
|
|
$this->environment = $environment; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* @return string |
260
|
|
|
*/ |
261
|
|
|
public function getPartyIdType() |
262
|
|
|
{ |
263
|
|
|
return $this->partyIdType; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* @param string $partyIdType |
268
|
|
|
*/ |
269
|
|
|
public function setPartyIdType($partyIdType) |
270
|
|
|
{ |
271
|
|
|
$this->partyIdType = $partyIdType; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* @return string |
276
|
|
|
*/ |
277
|
|
|
public function getLogFile() |
278
|
|
|
{ |
279
|
|
|
return $this->logFile; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* @param string $logFile |
284
|
|
|
*/ |
285
|
|
|
public function setLogFile($logFile) |
286
|
|
|
{ |
287
|
|
|
$this->logFile = $logFile; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Constructor. |
292
|
|
|
* |
293
|
|
|
* @param array $headers |
294
|
|
|
* @param array $middleware |
295
|
|
|
* @param \GuzzleHttp\ClientInterface $client |
296
|
|
|
* |
297
|
|
|
* @throws \Exception |
298
|
|
|
*/ |
299
|
25 |
|
public function __construct($headers = [], $middleware = [], ClientInterface $client = null) |
300
|
|
|
{ |
301
|
25 |
|
$this->config = Container::getInstance()->make(Repository::class); |
302
|
|
|
|
303
|
|
|
// Set defaults. |
304
|
25 |
|
$this->setConfigurations(); |
305
|
|
|
|
306
|
25 |
|
if ($client) { |
307
|
22 |
|
$this->client = $client; |
308
|
|
|
|
309
|
22 |
|
return; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
// Guzzle http request headers. |
313
|
3 |
|
$headers = array_merge([ |
314
|
3 |
|
'Accept' => 'application/json', |
315
|
3 |
|
'Content-Type' => 'application/json', |
316
|
3 |
|
'Ocp-Apim-Subscription-Key' => $this->subscriptionKey, |
317
|
3 |
|
], $headers); |
318
|
|
|
|
319
|
|
|
// Guzzle http client middleware. |
320
|
|
|
|
321
|
3 |
|
$handlerStack = HandlerStack::create(); |
322
|
|
|
|
323
|
3 |
|
foreach ($middleware as $mw) { |
324
|
|
|
$handlerStack->push($mw); |
325
|
|
|
} |
326
|
|
|
|
327
|
3 |
|
$handlerStack->push($this->getAuthBroker($headers)); |
328
|
|
|
|
329
|
3 |
|
$handlerStack = append_log_middleware($handlerStack); |
330
|
|
|
|
331
|
3 |
|
$options = array_merge([ |
332
|
3 |
|
'handler' => $handlerStack, |
333
|
3 |
|
'base_uri' => $this->baseUri, |
334
|
3 |
|
'headers' => $headers, |
335
|
3 |
|
], $this->config->get('mtn-momo.guzzle.options')); |
336
|
|
|
|
337
|
|
|
// Set http client. |
338
|
3 |
|
$this->client = new Client($options); |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* Request access token. |
343
|
|
|
* |
344
|
|
|
* @return array |
345
|
|
|
*/ |
346
|
|
|
abstract public function getToken(); |
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* Setup default configurations. |
350
|
|
|
* |
351
|
|
|
* @uses \Illuminate\Contracts\Config\Repository |
352
|
|
|
* |
353
|
|
|
* @return void |
354
|
|
|
*/ |
355
|
25 |
|
private function setConfigurations() |
356
|
|
|
{ |
357
|
25 |
|
$this->baseUri = $this->config->get('mtn-momo.api.base_uri'); |
358
|
25 |
|
$this->currency = $this->config->get('mtn-momo.currency'); |
359
|
25 |
|
$this->environment = $this->config->get('mtn-momo.environment'); |
360
|
25 |
|
$this->logFile = $this->config->get('mtn-momo.log'); |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
/** |
364
|
|
|
* Get authentication broker. |
365
|
|
|
* |
366
|
|
|
* @link https://momodeveloper.mtn.com/api-documentation/api-description/#oauth-2-0 Documentation |
367
|
|
|
* |
368
|
|
|
* @param array $headers HTTP request headers |
369
|
|
|
* |
370
|
|
|
* @throws \Exception |
371
|
|
|
* |
372
|
|
|
* @return \Bmatovu\OAuthNegotiator\OAuth2Middleware |
373
|
|
|
*/ |
374
|
3 |
|
protected function getAuthBroker($headers) |
375
|
|
|
{ |
376
|
3 |
|
$handlerStack = HandlerStack::create(); |
377
|
|
|
|
378
|
3 |
|
$handlerStack = append_log_middleware($handlerStack); |
379
|
|
|
|
380
|
3 |
|
$options = array_merge([ |
381
|
3 |
|
'base_uri' => $this->baseUri, |
382
|
3 |
|
'handler' => $handlerStack, |
383
|
3 |
|
'headers' => $headers, |
384
|
3 |
|
'json' => [ |
385
|
3 |
|
'body', |
386
|
3 |
|
], |
387
|
3 |
|
], $this->config->get('mtn-momo.guzzle.options')); |
388
|
|
|
|
389
|
|
|
// Authorization client - this is used to request OAuth access tokens |
390
|
3 |
|
$client = new Client($options); |
391
|
|
|
|
392
|
3 |
|
$config = [ |
393
|
3 |
|
'client_id' => $this->clientId, |
394
|
3 |
|
'client_secret' => $this->clientSecret, |
395
|
3 |
|
'token_uri' => $this->tokenUri, |
396
|
3 |
|
]; |
397
|
|
|
|
398
|
|
|
// This grant type is used to get a new Access Token and, |
399
|
|
|
// Refresh Token when no valid Access Token or Refresh Token is available |
400
|
3 |
|
$clientCredGrant = new ClientCredentials($client, $config); |
401
|
|
|
|
402
|
|
|
// Create token repository |
403
|
3 |
|
$tokenRepo = new TokenRepository(static::PRODUCT); |
404
|
|
|
|
405
|
|
|
// Tell the middleware to use both the client and refresh token grants |
406
|
3 |
|
return new OAuth2Middleware($clientCredGrant, null, $tokenRepo); |
407
|
|
|
} |
408
|
|
|
} |
409
|
|
|
|