-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstkpush.php
More file actions
executable file
·59 lines (48 loc) · 2.39 KB
/
Copy pathstkpush.php
File metadata and controls
executable file
·59 lines (48 loc) · 2.39 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
<?php
//Lipa na M-Pesa Online Payment API is used to initiate
//a M-Pesa transaction on behalf of a customer using STK Push.
//This is the same technique mySafaricom App uses whenever the
//app is used to make payments.
require "access_token.php";//access token
$url = 'https://sandbox.safaricom.co.ke/mpesa/stkpush/v1/processrequest';//sktpush url
$BusinessShortCode='174379';//shortcode
$Passkey='bfb279f9aa9bdbcf158e97dd71a467cd2e0c893059b10f78e6b72ada1ed2c9';//passkey
$Timestamp=date("YmdGis");//timestamp
$Password=base64_encode($BusinessShortCode.$Passkey.$Timestamp);//password encoded Base64
//log password and timestamp
$logFile = "logs/password.txt";
$log = fopen($logFile, "a");
fwrite($log, "Password=".$Password);
fwrite($log, " Timestamp=".$Timestamp);
fclose($log);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type:application/json', 'Authorization:Bearer '.$access_token)); //setting custom header
$curl_post_data = array(
//Fill in the request parameters with valid values
'BusinessShortCode' => $BusinessShortCode,//The organization shortcode used to receive the transaction.
'Password' => $Password,//This is generated by base64 encoding BusinessShortcode, Passkey and Timestamp.
'Timestamp' => $Timestamp,//The timestamp of the transaction in the format yyyymmddhhiiss.
'TransactionType' => 'CustomerPayBillOnline',//The transaction type to be used for this request.
'Amount' => '1000',//The amount to be transacted.
'PartyA' => '254700352822',//The MSISDN sending the funds.
'PartyB' => '174379',//The organization shortcode receiving the funds
'PhoneNumber' => '254700352822',//The MSISDN sending the funds.
'CallBackURL' => 'http://j/callback.php',//The url to where logs from M-Pesa will be sent to.
'AccountReference' => 'STK001',//Used with M-Pesa PayBills.
'TransactionDesc' => 'stk testing'//A description of the transaction.
);
$data_string = json_encode($curl_post_data);
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);
/******* log the response**********/
$logFile = "logs/stkpush.txt";
// write the M-PESA Response to file
$log = fopen($logFile, "a");
fwrite($log, $curl_response);
fclose($log);
//display result
echo $curl_response;