1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Starnerz\LaravelDaraja\Requests; |
4
|
|
|
|
5
|
|
|
use Starnerz\LaravelDaraja\MpesaApiClient; |
6
|
|
|
|
7
|
|
|
class Balance extends MpesaApiClient |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Safaricom Balance API endpoint. |
11
|
|
|
* |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
protected $queryEndPoint = 'mpesa/accountbalance/v1/query'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The initiator name for the short enquiring for balance. |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $initiatorName; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The encrypted security credential for the short code enquiring |
25
|
|
|
* for the balance. |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $securityCredential; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The URL where Safaricom Balance API will send result of the transaction. |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $resultURL; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The URL where Safaricom Balance API will send notification of the transaction |
40
|
|
|
* timing out while in the Safaricom servers queue. |
41
|
|
|
* |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
protected $queueTimeoutURL; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Balance constructor. |
48
|
|
|
*/ |
49
|
|
|
public function __construct() |
50
|
|
|
{ |
51
|
|
|
parent::__construct(); |
52
|
|
|
$this->initiatorName = config('laravel-daraja.initiator.name'); |
53
|
|
|
$this->securityCredential = $this->securityCredential(config('laravel-daraja.intiator.credential')); |
54
|
|
|
|
55
|
|
|
$this->resultURL = $this->setUrl(config('laravel-daraja.result_url.balance')); |
56
|
|
|
$this->queueTimeoutURL = $this->setUrl(config('laravel-daraja.queue_timeout_url.balance')); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Set initiator other than the one in the laravel-daraja config file. |
61
|
|
|
* |
62
|
|
|
* @param string $initiatorName |
63
|
|
|
* @param string $securityCredential |
64
|
|
|
*/ |
65
|
|
|
public function setInitiator($initiatorName, $securityCredential) |
66
|
|
|
{ |
67
|
|
|
$this->initiatorName = $initiatorName; |
68
|
|
|
$this->securityCredential = $this->securityCredential($securityCredential); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Set the url that will handle the timeout response from the |
73
|
|
|
* MPESA Balance API. |
74
|
|
|
* |
75
|
|
|
* @param string $url |
76
|
|
|
*/ |
77
|
|
|
public function setQueueTimeoutURL($url) |
78
|
|
|
{ |
79
|
|
|
$this->queueTimeoutURL = $url; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Set the url that will handle the result of the transaction |
84
|
|
|
* from the MPESA Balance API. |
85
|
|
|
* |
86
|
|
|
* @param string $url |
87
|
|
|
*/ |
88
|
|
|
public function setResultURL($url) |
89
|
|
|
{ |
90
|
|
|
$this->resultURL = $url; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Send the balance query to the Safaricom Account Balance API. |
95
|
|
|
* |
96
|
|
|
* @param string $remarks |
97
|
|
|
* @param null|string $shortCode |
98
|
|
|
* @return mixed |
99
|
|
|
*/ |
100
|
|
|
public function query($remarks, $shortCode = null) |
101
|
|
|
{ |
102
|
|
|
$parameters = [ |
103
|
|
|
'Initiator' => $this->initiatorName, |
104
|
|
|
'SecurityCredential' => $this->securityCredential, |
105
|
|
|
'CommandID' => 'AccountBalance', |
106
|
|
|
'PartyA' => is_null($shortCode) ? config('laravel-daraja.initiator.short_code') : $shortCode, |
107
|
|
|
'IdentifierType' => '4', |
108
|
|
|
'Remarks' => str_limit($remarks, 100, ''), |
|
|
|
|
109
|
|
|
'QueueTimeOutURL' => $this->queueTimeoutURL, |
110
|
|
|
'ResultURL' => $this->resultURL, |
111
|
|
|
]; |
112
|
|
|
|
113
|
|
|
return $this->call($this->queryEndPoint, ['json' => $parameters]); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|