forked from SIMONNABUKO/PHP-MPESA-INTEGRATION
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMpesaProcessor.php
More file actions
86 lines (66 loc) · 2.78 KB
/
MpesaProcessor.php
File metadata and controls
86 lines (66 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
require __DIR__ . '/vendor/autoload.php';
use Carbon\Carbon;
if (isset($_GET['amount'])) {
stkPush($_GET['amount']);
}
function lipaNaMpesaPassword()
{
//timestamp
$timestamp = Carbon::rawParse('now')->format('YmdHms');
//passkey
$passKey ="bfb279f9aa9bdbcf158e97dd71a467cd2e0c893059b10f78e6b72ada1ed2c919";
$businessShortCOde =174379;
//generate password
$mpesaPassword = base64_encode($businessShortCOde.$passKey.$timestamp);
return $mpesaPassword;
}
function newAccessToken()
{
$consumer_key="2sh2YA1fTzQwrZJthIrwLMoiOi3nhhal";
$consumer_secret="CKaCnw224K4Lc56w";
$credentials = base64_encode($consumer_key.":".$consumer_secret);
$url = "https://sandbox.safaricom.co.ke/oauth/v1/generate?grant_type=client_credentials";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Authorization: Basic ".$credentials,"Content-Type:application/json"));
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$curl_response = curl_exec($curl);
$access_token=json_decode($curl_response);
curl_close($curl);
return $access_token->access_token;
}
function stkPush($amount)
{
// $user = $request->user;
// $amount = $request->amount;
// $phone = $request->phone;
// $formatedPhone = substr($phone, 1);//726582228
// $code = "254";
// $phoneNumber = $code.$formatedPhone;//254726582228
$url = 'https://sandbox.safaricom.co.ke/mpesa/stkpush/v1/processrequest';
$curl_post_data = [
'BusinessShortCode' =>174379,
'Password' => lipaNaMpesaPassword(),
'Timestamp' => Carbon::rawParse('now')->format('YmdHms'),
'TransactionType' => 'CustomerPayBillOnline',
'Amount' => $amount,
'PartyA' => "254790765441",
'PartyB' => 174379,
'PhoneNumber' => "254790765441",
'CallBackURL' => 'https://60a8b840129d.ngrok.io/callback',
'AccountReference' => "Simon's Tech School Payment",
'TransactionDesc' => "lipa Na M-PESA"
];
$data_string = json_encode($curl_post_data);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type:application/json','Authorization:Bearer '.newAccessToken()));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
$curl_response = curl_exec($curl);
print_r($curl_response);
}