1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Starnerz\LaravelDaraja\Requests; |
4
|
|
|
|
5
|
|
|
use Starnerz\LaravelDaraja\MpesaApiClient; |
6
|
|
|
|
7
|
|
|
class Transaction extends MpesaApiClient |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* The transaction status query end point on Safricom API. |
11
|
|
|
* |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
protected $queryEndPoint = 'mpesa/transactionstatus/v1/query'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The initiator's name for the short code. |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $initiatorName; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The encrypted initiator's security credential for the short code. |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $securityCredential; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The sender of the transaction. |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $partyA; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Safaricom API identifier types for organization short code or MSISDN. |
39
|
|
|
* |
40
|
|
|
* @var int |
41
|
|
|
*/ |
42
|
|
|
protected $identifierType; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* The URL where Safaricom Transaction Status API will send result of the |
46
|
|
|
* transaction. |
47
|
|
|
* |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
protected $resultURL; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* The URL where Safaricom Transaction Status API will send notification of |
54
|
|
|
* the transaction timing out while in the Safaricom servers queue. |
55
|
|
|
* |
56
|
|
|
* @var string |
57
|
|
|
*/ |
58
|
|
|
protected $queueTimeoutURL; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Transaction constructor. |
62
|
|
|
*/ |
63
|
|
|
public function __construct() |
64
|
|
|
{ |
65
|
|
|
parent::__construct(); |
66
|
|
|
$this->initiatorName = config('laravel-daraja.initiator.name'); |
67
|
|
|
$this->partyA = config('laravel-daraja.initiator.short_code'); |
68
|
|
|
$this->securityCredential = $this->securityCredential(config('laravel-daraja.initiator.credential')); |
69
|
|
|
|
70
|
|
|
$this->resultURL = $this->setUrl(config('laravel-daraja.result_url.transaction_status')); |
71
|
|
|
$this->queueTimeoutURL = $this->setUrl(config('laravel-daraja.queue_timeout_url.transaction_status')); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Set different initiator from the one set in the laravel-daraja configurations. |
76
|
|
|
* |
77
|
|
|
* @param string $name |
78
|
|
|
* @param string $securityCredential |
79
|
|
|
*/ |
80
|
|
|
public function setInitiator($name, $securityCredential) |
81
|
|
|
{ |
82
|
|
|
$this->initiatorName = $name; |
83
|
|
|
$this->securityCredential = $this->securityCredential($securityCredential); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Set the business short code to use if you want to use a different one |
88
|
|
|
* from the one set in the configs. |
89
|
|
|
* |
90
|
|
|
* @param string $code |
91
|
|
|
*/ |
92
|
|
|
public function setShortCode($code) |
93
|
|
|
{ |
94
|
|
|
$this->partyA = $code; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Check the transaction status from a business short code to a pay bill number. |
99
|
|
|
* |
100
|
|
|
* @param string $transactionID |
101
|
|
|
* @param string $remarks |
102
|
|
|
* @param string $occasion |
103
|
|
|
* @return mixed |
104
|
|
|
*/ |
105
|
|
|
public function toPayBillStatus($transactionID, $remarks, $occasion = '') |
106
|
|
|
{ |
107
|
|
|
$this->identifierType = $this->identifier['paybill']; |
108
|
|
|
|
109
|
|
|
return $this->status($transactionID, $remarks, $occasion); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Check the transaction status from a business short code to a till number. |
114
|
|
|
* |
115
|
|
|
* @param string $transactionID |
116
|
|
|
* @param string $remarks |
117
|
|
|
* @param string $occasion |
118
|
|
|
* @return mixed |
119
|
|
|
*/ |
120
|
|
|
public function toTillStatus($transactionID, $remarks, $occasion = '') |
121
|
|
|
{ |
122
|
|
|
$this->identifierType = $this->identifier['till']; |
123
|
|
|
|
124
|
|
|
return $this->status($transactionID, $remarks, $occasion); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Check the transaction status from a business short code to a msisdn number. |
129
|
|
|
* |
130
|
|
|
* @param string $transactionID |
131
|
|
|
* @param string $remarks |
132
|
|
|
* @param string $occasion |
133
|
|
|
* @return mixed |
134
|
|
|
*/ |
135
|
|
|
public function toMsisdnStatus($transactionID, $remarks, $occasion = '') |
136
|
|
|
{ |
137
|
|
|
$this->identifierType = $this->identifier['msisdn']; |
138
|
|
|
|
139
|
|
|
return $this->status($transactionID, $remarks, $occasion); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Check the transaction status from a msisdn number to a short code. |
144
|
|
|
* |
145
|
|
|
* @param string $msisdn |
146
|
|
|
* @param string $transactionID |
147
|
|
|
* @param string $remarks |
148
|
|
|
* @param string $occasion |
149
|
|
|
* @return mixed |
150
|
|
|
*/ |
151
|
|
|
public function fromMsisdnStatus($msisdn, $transactionID, $remarks, $occasion = '') |
152
|
|
|
{ |
153
|
|
|
$this->partyA = $msisdn; |
154
|
|
|
$this->identifierType = $this->identifier['msisdn']; |
155
|
|
|
|
156
|
|
|
return $this->status($transactionID, $remarks, $occasion); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Send the transaction status query to the Safaricom Transaction |
161
|
|
|
* Status API. |
162
|
|
|
* |
163
|
|
|
* @param string $transactionId |
164
|
|
|
* @param string $remarks |
165
|
|
|
* @param string $occasion |
166
|
|
|
* @return mixed |
167
|
|
|
*/ |
168
|
|
|
protected function status($transactionId, $remarks, $occasion = '') |
169
|
|
|
{ |
170
|
|
|
$parameters = [ |
171
|
|
|
'Initiator' => $this->initiatorName, |
172
|
|
|
'SecurityCredential' => $this->securityCredential, |
173
|
|
|
'CommandID' => 'TransactionStatusQuery', |
174
|
|
|
'TransactionID' => $transactionId, |
175
|
|
|
'PartyA' => $this->partyA, |
176
|
|
|
'IdentifierType' => $this->identifierType, |
177
|
|
|
'ResultURL' => $this->resultURL, |
178
|
|
|
'QueueTimeOutURL' => $this->queueTimeoutURL, |
179
|
|
|
'Remarks' => $remarks, |
180
|
|
|
'Occasion' => $occasion, |
181
|
|
|
]; |
182
|
|
|
|
183
|
|
|
return $this->call($this->queryEndPoint, ['json' => $parameters]); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|