Created
January 14, 2020 09:22
-
-
Save ngangavic/346f9f9a0566c828d02390bb75553399 to your computer and use it in GitHub Desktop.
Lipa Na Mpesa Online php script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//Authenticate your app-----Access Token | |
$consumerKey=''; | |
$consumerSecret=''; | |
$headers=['Content-Type:application/json; charset=utf8']; | |
$url = 'https://sandbox.safaricom.co.ke/oauth/v1/generate?grant_type=client_credentials'; | |
$curl = curl_init($url); | |
curl_setopt($curl,CURLOPT_HTTPHEADER,$headers); | |
curl_setopt($curl,CURLOPT_RETURNTRANSFER,true); | |
curl_setopt($curl,CURLOPT_HEADER,false); | |
curl_setopt($curl,CURLOPT_USERPWD,$consumerKey.':'.$consumerSecret); | |
$result = curl_exec($curl); | |
$status = curl_getinfo($curl,CURLINFO_HTTP_CODE); | |
$result = json_decode($result); | |
$access_token = $result->access_token; | |
echo 'The access token is: '.$access_token; | |
//curl_close($curl); | |
//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. | |
$url = 'https://sandbox.safaricom.co.ke/mpesa/stkpush/v1/processrequest';//sktpush url | |
$BusinessShortCode='';//shortcode | |
$Passkey='';//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' => '254708374149',//The MSISDN sending the funds. | |
'PartyB' => '174379',//The organization shortcode receiving the funds | |
'PhoneNumber' => '254708374149',//The MSISDN sending the funds. | |
'CallBackURL' => 'http://yourCallbackURL',//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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment