|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* RequestSecretCommand. |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace Bmatovu\MtnMomo\Console; |
|
7
|
|
|
|
|
8
|
|
|
use Bmatovu\MtnMomo\Traits\CommandUtilTrait; |
|
9
|
|
|
use GuzzleHttp\ClientInterface; |
|
10
|
|
|
use GuzzleHttp\Exception\ClientException; |
|
11
|
|
|
use GuzzleHttp\Exception\ConnectException; |
|
12
|
|
|
use GuzzleHttp\Exception\ServerException; |
|
13
|
|
|
use Illuminate\Console\Command; |
|
14
|
|
|
use Ramsey\Uuid\Uuid; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Request client app secret. |
|
18
|
|
|
*/ |
|
19
|
|
|
class RequestSecretCommand extends Command |
|
20
|
|
|
{ |
|
21
|
|
|
use CommandUtilTrait; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Guzzle HTTP client instance. |
|
25
|
|
|
* |
|
26
|
|
|
* @var \GuzzleHttp\ClientInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $client; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Product subscribed too. |
|
32
|
|
|
* |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $product; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* The name and signature of the console command. |
|
39
|
|
|
* |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $signature = 'mtn-momo:request-secret |
|
43
|
|
|
{--id= : Client APP ID.} |
|
44
|
|
|
{--product= : Product subscribed to.} |
|
45
|
|
|
{--no-write : Don\'t credentials to .env file.} |
|
46
|
|
|
{--f|force : Force the operation to run when in production.}'; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* The console command description. |
|
50
|
|
|
* |
|
51
|
|
|
* @var string |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $description = "Request client APP secret; 'apikey'."; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Create a new command instance. |
|
57
|
|
|
* |
|
58
|
|
|
* @param \GuzzleHttp\ClientInterface $client |
|
59
|
|
|
*/ |
|
60
|
25 |
|
public function __construct(ClientInterface $client) |
|
61
|
|
|
{ |
|
62
|
25 |
|
parent::__construct(); |
|
63
|
|
|
|
|
64
|
25 |
|
$this->client = $client; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Execute the console command. |
|
69
|
|
|
* |
|
70
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
|
71
|
|
|
* |
|
72
|
|
|
* @return void |
|
73
|
|
|
*/ |
|
74
|
1 |
|
public function handle() |
|
75
|
|
|
{ |
|
76
|
1 |
|
if (! $this->runInProduction()) { |
|
77
|
|
|
return; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
1 |
|
$this->printLabels('Request -> Client APP secret'); |
|
81
|
|
|
|
|
82
|
1 |
|
$this->product = $this->option('product'); |
|
83
|
|
|
|
|
84
|
1 |
|
if (! $this->product) { |
|
85
|
1 |
|
$this->product = $this->laravel['config']->get('mtn-momo.product'); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
1 |
|
$clientId = $this->getClientId(); |
|
89
|
|
|
|
|
90
|
1 |
|
$clientSecret = $this->requestClientSecret($clientId); |
|
91
|
|
|
|
|
92
|
1 |
|
if (! $clientSecret) { |
|
93
|
|
|
return; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
1 |
|
$this->updateSetting("MOMO_{$this->product}_SECRET", "mtn-momo.products.{$this->product}.secret", $clientSecret); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Determine client ID. |
|
101
|
|
|
* |
|
102
|
|
|
* @return string |
|
103
|
|
|
*/ |
|
104
|
1 |
|
protected function getClientId() |
|
105
|
|
|
{ |
|
106
|
1 |
|
$this->info('Client APP ID - [X-Reference-Id, api_user_id]'); |
|
107
|
|
|
|
|
108
|
|
|
// Client ID from command options. |
|
109
|
1 |
|
$id = $this->option('id'); |
|
110
|
|
|
|
|
111
|
|
|
// Client ID from .env |
|
112
|
1 |
|
if (! $id) { |
|
113
|
|
|
$id = $this->laravel['config']->get("mtn-momo.products.{$this->product}.id"); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
// Confirm Client ID |
|
117
|
1 |
|
$id = $this->ask('Use client app ID?', $id); |
|
118
|
|
|
|
|
119
|
|
|
// Validate Client ID |
|
120
|
1 |
|
while (! Uuid::isValid($id)) { |
|
121
|
|
|
$this->info(' Invalid UUID (Format: 4). #IETF RFC4122'); |
|
122
|
|
|
$id = $this->ask('MOMO_CLIENT_ID'); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
1 |
|
return $id; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Request for client APP secret. |
|
130
|
|
|
* |
|
131
|
|
|
* @link https://momodeveloper.mtn.com/docs/services/sandbox-provisioning-api/operations/post-v1_0-apiuser-apikey Documentation |
|
132
|
|
|
* |
|
133
|
|
|
* @param string $clientId |
|
134
|
|
|
* |
|
135
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
|
136
|
|
|
* |
|
137
|
|
|
* @return string|null Client Secret |
|
138
|
|
|
*/ |
|
139
|
1 |
|
protected function requestClientSecret($clientId) |
|
140
|
|
|
{ |
|
141
|
|
|
try { |
|
142
|
1 |
|
$requestSecretUri = $this->laravel['config']->get('mtn-momo.api.request_secret_uri'); |
|
143
|
|
|
|
|
144
|
1 |
|
$requestSecretUri = str_replace('{clientId}', $clientId, $requestSecretUri); |
|
145
|
|
|
|
|
146
|
1 |
|
$response = $this->client->request('POST', $requestSecretUri, []); |
|
147
|
|
|
|
|
148
|
1 |
|
$this->line("\r\nStatus: <fg=green>".$response->getStatusCode().' '.$response->getReasonPhrase().'</>'); |
|
149
|
|
|
|
|
150
|
1 |
|
$this->line("\r\nBody: <fg=green>".$response->getBody()."</>\r\n"); |
|
151
|
|
|
|
|
152
|
1 |
|
$apiResponse = json_decode($response->getBody(), true); |
|
153
|
|
|
|
|
154
|
1 |
|
return $apiResponse['apiKey']; |
|
155
|
|
|
} catch (ConnectException $ex) { |
|
156
|
|
|
$this->line("\r\n<fg=red>".$ex->getMessage().'</>'); |
|
157
|
|
|
} catch (ClientException $ex) { |
|
158
|
|
|
$response = $ex->getResponse(); |
|
159
|
|
|
$this->line("\r\nStatus: <fg=yellow>".$response->getStatusCode().' '.$response->getReasonPhrase().'</>'); |
|
160
|
|
|
$this->line("\r\nBody: <fg=yellow>".$response->getBody()."</>\r\n"); |
|
161
|
|
|
} catch (ServerException $ex) { |
|
162
|
|
|
$response = $ex->getResponse(); |
|
163
|
|
|
$this->line("\r\nStatus: <fg=red>".$response->getStatusCode().' '.$response->getReasonPhrase().'</>'); |
|
164
|
|
|
$this->line("\r\nBody: <fg=red>".$response->getBody()."</>\r\n"); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
return null; |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|