1
|
|
|
<?php |
2
|
|
|
namespace Karson\MpesaPhpSdk; |
3
|
|
|
|
4
|
|
|
|
5
|
|
|
class Mpesa { |
6
|
|
|
|
7
|
|
|
private $api_url = 'https://api.sandbox.vm.co.mz'; |
8
|
|
|
private $api_port; |
9
|
|
|
public $public_key; |
10
|
|
|
public $api_key; |
11
|
|
|
|
12
|
|
|
public function __construct($config = []) |
13
|
|
|
{ |
14
|
|
|
if (is_array($config)) { |
15
|
|
|
$this->setPublicKey($config['public_key']); |
16
|
|
|
$this->setApiKey($config['api_key']); |
17
|
|
|
$this->setEnv($config['env']); |
18
|
|
|
} |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function setPublicKey($public_key){ |
22
|
|
|
$this->public_key = trim($public_key); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function setApiKey($api_key){ |
26
|
|
|
$this->api_key = $api_key; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function setEnv($env){ |
30
|
|
|
if ($env == 'live') { |
31
|
|
|
$this->api_url = 'https://api.vm.co.mz'; |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
/* Standard customer-to-business transaction |
37
|
|
|
* |
38
|
|
|
* @param string $transactionReference This is the reference of the transaction for the customer or business making the * transaction. This can be a smartcard number for a TV subscription or a reference number of a utility bill. |
39
|
|
|
* @param string $customerMSISDN MSISDN of the customer for the transaction |
40
|
|
|
* @param string $amount The amount for the transaction. |
41
|
|
|
* @param string $thirdPartReferece This is the unique reference of the third party system. When there are queries about transactions, this will usually be used to track a transaction. |
42
|
|
|
* @param string $serviceCode Shortcode of the business where funds will be credited to. |
43
|
|
|
* @return \stdClass |
44
|
|
|
*/ |
45
|
|
|
function c2b($transactionReference, $customerMSISDN, $amount, $thirdPartReferece, $serviceCode){ |
|
|
|
|
46
|
|
|
|
47
|
|
|
$fields = [ |
48
|
|
|
"input_TransactionReference"=> $transactionReference, |
49
|
|
|
"input_CustomerMSISDN"=> $customerMSISDN, |
50
|
|
|
"input_Amount"=> $amount, |
51
|
|
|
"input_ThirdPartyReference"=> $thirdPartReferece, |
52
|
|
|
"input_ServiceProviderCode" => $serviceCode |
53
|
|
|
]; |
54
|
|
|
|
55
|
|
|
return $this->makeRequest(':18352/ipg/v1x/c2bPayment/singleStage/', 'POST', $fields); |
56
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param $transactionID |
61
|
|
|
* @param $securityCredential |
62
|
|
|
* @param $initiatorIdentifier |
63
|
|
|
* @param $thirdPartyReference |
64
|
|
|
* @param $serviceProviderCode |
65
|
|
|
* @param $reversalAmount |
66
|
|
|
* @return \stdClass |
67
|
|
|
*/ |
68
|
|
|
public function transactionReversal($transactionID, $securityCredential, $initiatorIdentifier, $thirdPartyReference, $serviceProviderCode, $reversalAmount) |
69
|
|
|
{ |
70
|
|
|
$fields = [ |
71
|
|
|
"input_TransactionID" => $transactionID, |
72
|
|
|
"input_SecurityCredential" => $securityCredential, |
73
|
|
|
"input_InitiatorIdentifier" => $initiatorIdentifier, |
74
|
|
|
"input_ThirdPartyReference" => $thirdPartyReference, |
75
|
|
|
"input_ServiceProviderCode" => $serviceProviderCode, |
76
|
|
|
"input_ReversalAmount" => $reversalAmount |
77
|
|
|
]; |
78
|
|
|
return $this->makeRequest(':18354/ipg/v1x/reversal/', 'POST', $fields); |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param $thirdPartyReference |
85
|
|
|
* @param $queryReference |
86
|
|
|
* @param $serviceProviderCode |
87
|
|
|
* @return \stdClass |
88
|
|
|
*/ |
89
|
|
|
public function status($thirdPartyReference, $queryReference, $serviceProviderCode) |
90
|
|
|
{ |
91
|
|
|
|
92
|
|
|
$fields = [ |
93
|
|
|
'input_ThirdPartyReference' => $thirdPartyReference, |
94
|
|
|
'input_QueryReference' => $queryReference, |
95
|
|
|
'input_ServiceProviderCode' => $serviceProviderCode |
96
|
|
|
]; |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
|
100
|
|
|
return $this->makeRequest(':18353/ipg/v1x/queryTransactionStatus/', 'GET', $fields); |
101
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Generates a base64 encoded token |
106
|
|
|
*/ |
107
|
|
|
public function getToken() |
108
|
|
|
{ |
109
|
|
|
|
110
|
|
|
if (!empty($this->public_key) && !empty($this->api_key)) |
111
|
|
|
{ |
112
|
|
|
$key = "-----BEGIN PUBLIC KEY-----\n"; |
113
|
|
|
$key .= wordwrap($this->public_key, 60, "\n", true); |
114
|
|
|
$key .= "\n-----END PUBLIC KEY-----"; |
115
|
|
|
$pk = openssl_get_publickey($key); |
116
|
|
|
openssl_public_encrypt($this->api_key, $token, $pk, OPENSSL_PKCS1_PADDING); |
117
|
|
|
|
118
|
|
|
return base64_encode($token); |
119
|
|
|
} |
120
|
|
|
return 'error'; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param string $url |
125
|
|
|
* @param string $method |
126
|
|
|
* @param array $fields |
127
|
|
|
* @return \stdClass |
128
|
|
|
*/ |
129
|
|
|
private function makeRequest(string $url,string $method, array $fields = []) |
130
|
|
|
{ |
131
|
|
|
$url = $this->api_url.$url; |
132
|
|
|
|
133
|
|
|
$ch = curl_init(); |
134
|
|
|
|
135
|
|
|
if($method == 'POST') |
136
|
|
|
{ |
137
|
|
|
$fieldsString = json_encode($fields); |
138
|
|
|
curl_setopt($ch, CURLOPT_POST, 1); |
139
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $fieldsString); |
140
|
|
|
} |
141
|
|
|
else |
142
|
|
|
{ |
143
|
|
|
$fieldsString = http_build_query($fields); |
144
|
|
|
$url .= '?'.$fieldsString; |
145
|
|
|
} |
146
|
|
|
$header = $this->getHeader(); |
147
|
|
|
|
148
|
|
|
|
149
|
|
|
curl_setopt($ch, CURLOPT_POST, count($fields)); |
150
|
|
|
curl_setopt($ch, CURLOPT_PORT, $this->api_port); |
151
|
|
|
curl_setopt($ch, CURLOPT_URL, $url); |
152
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
153
|
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 90); |
154
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); |
155
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
156
|
|
|
|
157
|
|
|
$result = curl_exec($ch); |
158
|
|
|
|
159
|
|
|
$return = new \stdClass(); |
160
|
|
|
$return->response = json_decode($result); |
161
|
|
|
|
162
|
|
|
if ($return->response == false) |
163
|
|
|
$return->response = $result; |
164
|
|
|
|
165
|
|
|
$return->status = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
166
|
|
|
curl_close($ch); |
167
|
|
|
return $return; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @return array |
172
|
|
|
*/ |
173
|
|
|
private function getHeader() |
174
|
|
|
{ |
175
|
|
|
$header = [ |
176
|
|
|
'Content-Type: application/json', |
177
|
|
|
'Authorization: Bearer ' . $this->getToken(), |
178
|
|
|
'origin: developer.mpesa.vm.co.mz', |
179
|
|
|
'Connection: keep-alive' |
180
|
|
|
]; |
181
|
|
|
return $header; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
|
185
|
|
|
} |
186
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.