Passed
Push — master ( 015cf1...83dbe9 )
by Brian
14:23
created

Remittance::getAccountHolderBasicInfo()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2.0145

Importance

Changes 0
Metric Value
cc 2
eloc 11
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 19
ccs 11
cts 13
cp 0.8462
crap 2.0145
rs 9.9
1
<?php
2
3
/**
4
 * Remittance.
5
 */
6
7
namespace Bmatovu\MtnMomo\Products;
8
9
use Bmatovu\MtnMomo\Exceptions\RemittanceRequestException;
10
use GuzzleHttp\ClientInterface;
11
use GuzzleHttp\Exception\RequestException;
12
use Illuminate\Container\Container;
13
use Illuminate\Contracts\Config\Repository;
14
use Ramsey\Uuid\Uuid;
15
16
/**
17
 * Remittance service/product.
18
 */
19
class Remittance extends Product
20
{
21
    /**
22
     * Product.
23
     *
24
     * @var string
25
     */
26
    const PRODUCT = 'remittance';
27
28
    /**
29
     * Transact URI.
30
     *
31
     * @var string
32
     */
33
    protected $transactionUri;
34
35
    /**
36
     * Transaction status URI.
37
     *
38
     * @var string
39
     */
40
    protected $transactionStatusUri;
41
42
    /**
43
     * Account status URI.
44
     *
45
     * @var string
46
     */
47
    protected $accountStatusUri;
48
49
    /**
50
     * Account balance URI.
51
     *
52
     * @var string
53
     */
54
    protected $accountBalanceUri;
55
56
    /**
57
     * Account holder basic info URI.
58
     *
59
     * @var string
60
     */
61
    protected $accountHolderInfoUri;
62
63
    /**
64
     * @return string
65
     */
66
    public function getTransactionUri()
67
    {
68
        return $this->transactionUri;
69
    }
70
71
    /**
72
     * @param string $transactionUri
73
     */
74
    public function setTransactionUri($transactionUri)
75
    {
76
        $this->transactionUri = $transactionUri;
77
    }
78
79
    /**
80
     * @return string
81
     */
82
    public function getTransactionStatusUri()
83
    {
84
        return $this->transactionStatusUri;
85
    }
86
87
    /**
88
     * @param string $transactionStatusUri
89
     */
90
    public function setTransactionStatusUri($transactionStatusUri)
91
    {
92
        $this->transactionStatusUri = $transactionStatusUri;
93
    }
94
95
    /**
96
     * @return string
97
     */
98
    public function getAccountStatusUri()
99
    {
100
        return $this->accountStatusUri;
101
    }
102
103
    /**
104
     * @param string $accountStatusUri
105
     */
106
    public function setAccountStatusUri($accountStatusUri)
107
    {
108
        $this->accountStatusUri = $accountStatusUri;
109
    }
110
111
    /**
112
     * @return string
113
     */
114
    public function getAccountBalanceUri()
115
    {
116
        return $this->accountBalanceUri;
117
    }
118
119
    /**
120
     * @param string $accountBalanceUri
121
     */
122
    public function setAccountBalanceUri($accountBalanceUri)
123
    {
124
        $this->accountBalanceUri = $accountBalanceUri;
125
    }
126
127
    /**
128
     * @return string
129
     */
130
    public function getAccountHolderInfoUri()
131
    {
132
        return $this->accountHolderInfoUri;
133
    }
134
135
    /**
136
     * @param string $accountHolderInfoUri
137
     */
138
    public function setAccountHolderInfoUri($accountHolderInfoUri)
139
    {
140
        $this->accountHolderInfoUri = $accountHolderInfoUri;
141
    }
142
143
    /**
144
     * Constructor.
145
     *
146
     * @param array $headers
147
     * @param array $middleware
148
     * @param \GuzzleHttp\ClientInterface $client
149
     *
150
     * @uses \Illuminate\Contracts\Config\Repository
151
     *
152
     * @throws \Exception
153
     */
154 8
    public function __construct($headers = [], $middleware = [], ClientInterface $client = null)
155
    {
156 8
        $config = Container::getInstance()->make(Repository::class);
157
158 8
        $this->subscriptionKey = $config->get('mtn-momo.products.remittance.key');
159 8
        $this->clientId = $config->get('mtn-momo.products.remittance.id');
160 8
        $this->clientSecret = $config->get('mtn-momo.products.remittance.secret');
161 8
        $this->clientCallbackUri = $config->get('mtn-momo.products.remittance.callback_uri');
162
163 8
        $this->tokenUri = $config->get('mtn-momo.products.remittance.token_uri');
164 8
        $this->transactionUri = $config->get('mtn-momo.products.remittance.transaction_uri');
165 8
        $this->transactionStatusUri = $config->get('mtn-momo.products.remittance.transaction_status_uri');
166 8
        $this->accountStatusUri = $config->get('mtn-momo.products.remittance.account_status_uri');
167 8
        $this->accountBalanceUri = $config->get('mtn-momo.products.remittance.account_balance_uri');
168 8
        $this->accountHolderInfoUri = $config->get('mtn-momo.products.remittance.account_holder_info_uri');
169 8
        $this->partyIdType = $config->get('mtn-momo.products.remittance.party_id_type');
170
171 8
        parent::__construct($headers, $middleware, $client);
172
    }
173
174
    /**
175
     * Request remittance access token.
176
     *
177
     * @see https://momodeveloper.mtn.com/docs/services/remittance/operations/token-POST Documentation
178
     *
179
     * @throws \Bmatovu\MtnMomo\Exceptions\RemittanceRequestException
180
     * @throws \GuzzleHttp\Exception\GuzzleException
181
     *
182
     * @return array
183
     */
184 1
    public function getToken()
185
    {
186
        try {
187 1
            $response = $this->client->request('POST', $this->tokenUri, [
188 1
                'headers' => [
189 1
                    'Authorization' => 'Basic '.base64_encode($this->clientId.':'.$this->clientSecret),
190 1
                ],
191 1
                'json' => [
192 1
                    'grant_type' => 'client_credentials',
193 1
                ],
194 1
            ]);
195
196 1
            return json_decode($response->getBody(), true);
197
        } catch (RequestException $ex) {
198
            throw new RemittanceRequestException('Unable to get token.', 0, $ex);
199
        }
200
    }
201
202
    /**
203
     * Transfer from your own account to another person's account.
204
     *
205
     * @see https://momodeveloper.mtn.com/docs/services/remittance/operations/transfer-POST Documentation
206
     *
207
     * @param string $transactionId  Your internal transaction reference ID.
208
     * @param string $partyId        Account holder. Usually phone number if type is MSISDN.
209
     * @param int    $amount          How much to credit the payer.
210
     * @param string $payerMessage   Payer transaction message.
211
     * @param string $payeeNote      Payee transaction message.
212
     *
213
     * @throws \Bmatovu\MtnMomo\Exceptions\RemittanceRequestException
214
     * @throws \GuzzleHttp\Exception\GuzzleException
215
     * @throws \Exception
216
     *
217
     * @return string Auto generated transaction reference. Format: UUID
218
     */
219 2
    public function transfer($transactionId, $partyId, $amount, $payerMessage = '', $payeeNote = '')
220
    {
221 2
        $momoTransactionId = Uuid::uuid4()->toString();
222
223 2
        $headers = [
224 2
            'X-Reference-Id' => $momoTransactionId,
225 2
            'X-Target-Environment' => $this->environment,
226 2
        ];
227
228 2
        if ($this->environment != 'sandbox' && $this->clientCallbackUri) {
229
            $headers['X-Callback-Url'] = $this->clientCallbackUri;
230
        }
231
232
        try {
233 2
            $this->client->request('POST', $this->transactionUri, [
234 2
                'headers' => $headers,
235 2
                'json' => [
236 2
                    'amount' => $amount,
237 2
                    'currency' => $this->currency,
238 2
                    'externalId' => $transactionId,
239 2
                    'payee' => [
240 2
                        'partyIdType' => $this->partyIdType,
241 2
                        'partyId' => $partyId,
242 2
                    ],
243 2
                    'payerMessage' => alphanumeric($payerMessage),
244 2
                    'payeeNote' => alphanumeric($payeeNote),
245 2
                ],
246 2
            ]);
247
248 2
            return $momoTransactionId;
249
        } catch (RequestException $ex) {
250
            throw new RemittanceRequestException('Transfer unsuccessful.', 0, $ex);
251
        }
252
    }
253
254
    /**
255
     * Get transaction status.
256
     *
257
     * @see https://momodeveloper.mtn.com/docs/services/remittance/operations/transfer-referenceId-GET Documentation
258
     *
259
     * @param  string $momoTransactionId That was returned by transact (transfer)
260
     *
261
     * @throws \Bmatovu\MtnMomo\Exceptions\RemittanceRequestException
262
     * @throws \GuzzleHttp\Exception\GuzzleException
263
     *
264
     * @return array
265
     */
266 1
    public function getTransactionStatus($momoTransactionId)
267
    {
268 1
        $transactionStatusUri = str_replace('{momoTransactionId}', $momoTransactionId, $this->transactionStatusUri);
269
270
        try {
271 1
            $response = $this->client->request('GET', $transactionStatusUri, [
272 1
                'headers' => [
273 1
                    'X-Target-Environment' => $this->environment,
274 1
                ],
275 1
            ]);
276
277 1
            return json_decode($response->getBody(), true);
278
        } catch (RequestException $ex) {
279
            throw new RemittanceRequestException('Unable to get transaction status.', 0, $ex);
280
        }
281
    }
282
283
    /**
284
     * Get account balance.
285
     *
286
     * @see https://momodeveloper.mtn.com/docs/services/remittance/operations/get-v1_0-account-balance Documentation
287
     *
288
     * @throws \Bmatovu\MtnMomo\Exceptions\RemittanceRequestException
289
     * @throws \GuzzleHttp\Exception\GuzzleException
290
     *
291
     * @return array Account balance.
292
     */
293 1
    public function getAccountBalance()
294
    {
295
        try {
296 1
            $response = $this->client->request('GET', $this->accountBalanceUri, [
297 1
                'headers' => [
298 1
                    'X-Target-Environment' => $this->environment,
299 1
                ],
300 1
            ]);
301
302 1
            return json_decode($response->getBody(), true);
303
        } catch (RequestException $ex) {
304
            throw new RemittanceRequestException('Unable to get account balance.', 0, $ex);
305
        }
306
    }
307
308
    /**
309
     * Determine if an account holder is registered and active.
310
     *
311
     * @see https://momodeveloper.mtn.com/docs/services/remittance/operations/get-v1_0-accountholder-accountholderidtype-accountholderid-active Documentation
312
     *
313
     * @param  string $partyId Party number - MSISDN, email, or code - UUID.
314
     * @param  string $partyIdType Specifies the type of the account ID. Allowed values [msisdn, email, party_code].
315
     *
316
     * @throws \Bmatovu\MtnMomo\Exceptions\RemittanceRequestException
317
     * @throws \GuzzleHttp\Exception\GuzzleException
318
     *
319
     * @return bool True if account holder is registered and active, false if the account holder is not active or not found
320
     */
321 1
    public function isActive($partyId, $partyIdType = null)
322
    {
323 1
        if (is_null($partyIdType)) {
324 1
            $partyIdType = $this->partyIdType;
325
        }
326
327 1
        $patterns = $replacements = [];
328
329 1
        $patterns[] = '/(\{\bpartyIdType\b\})/';
330 1
        $replacements[] = strtolower($partyIdType);
331
332 1
        $patterns[] = '/(\{\bpartyId\b\})/';
333 1
        $replacements[] = urlencode($partyId);
334
335 1
        $accountStatusUri = preg_replace($patterns, $replacements, $this->accountStatusUri);
336
337
        try {
338 1
            $response = $this->client->request('GET', $accountStatusUri, [
339 1
                'headers' => [
340 1
                    'X-Target-Environment' => $this->environment,
341 1
                ],
342 1
            ]);
343
344 1
            $body = json_decode($response->getBody(), true);
345
346 1
            return (bool) $body['result'];
347
        } catch (RequestException $ex) {
348
            throw new RemittanceRequestException('Unable to get account status.', 0, $ex);
349
        }
350
    }
351
352
    /**
353
     * Get basic info of an account holder.
354
     *
355
     * @see https://momodeveloper.mtn.com/docs/services/remittance/operations/basicuserInfo-GET Documentation
356
     *
357
     * @param  string $partyId Party number - MSISDN.
358
     *
359
     * @throws \Bmatovu\MtnMomo\Exceptions\RemittanceRequestException
360
     * @throws \GuzzleHttp\Exception\GuzzleException
361
     *
362
     * @return array account basic info
363
     */
364 1
    public function getAccountHolderBasicInfo($partyId)
365
    {
366 1
        $patterns = $replacements = [];
367
368 1
        $patterns[] = '/(\{\bpartyId\b\})/';
369 1
        $replacements[] = urlencode($partyId);
370
371 1
        $accountHolderInfoUri = preg_replace($patterns, $replacements, $this->accountHolderInfoUri);
372
373
        try {
374 1
            $response = $this->client->request('GET', $accountHolderInfoUri, [
375 1
                'headers' => [
376 1
                    'X-Target-Environment' => $this->environment,
377 1
                ],
378 1
            ]);
379
380 1
            return json_decode($response->getBody(), true);
381
        } catch (RequestException $ex) {
382
            throw new RemittanceRequestException('Unable to get user account information.', 0, $ex);
383
        }
384
    }
385
}
386