How To Intergrate Lipa Na M-Pesa Stk Push With Your Website
1 min read

How To Intergrate Lipa Na M-Pesa Stk Push With Your Website


In this tutorial Ill show you how to intergrate m-pesa stk push with your website in php
1.Create stk initiate file and paste in the following code:
<?php

$consumerKey = CONSUMER_KEY; //Fill with your app Consumer Key
$consumerSecret = CONSUMER_SECRET; // Fill with your app Secret

# define the variales
# provide the following details, this part is found on your test credentials on the developer account
$BusinessShortCode = SHORTCODE;
$Passkey = PASSKEY;

/*
This are your info, for
$PartyA should be the ACTUAL clients phone number or your phone number, format 2547
$AccountRefference, it maybe invoice number, account number etc on production systems, but for test just put anything
TransactionDesc can be anything, probably a better description of or the transaction
$Amount this is the total invoiced amount, Any amount here will be
actually deducted from a clients side/your test phone number once the PIN has been entered to authorize the transaction.
for developer/test accounts, this money will be reversed automatically by midnight.
*/
$phone = ”;
$amt = ”;
$transdesc = ”;
$transref = ”;

if(!empty($_POST[‘amt’])){

  $phone = $_POST['phone'];
  $amt = $_POST['amt'];
  $transdesc = $_POST['transdesc'];
  $transref = $_POST['transref'];

}

$PartyA = $phone; // This is your phone number,
$AccountReference = $transref;
$TransactionDesc = $transdesc;
$Amount = $amt;

# Get the timestamp, format YYYYmmddhms -> 20181004151020
//$Timestamp = date(‘YmdGis’);

# Get the timestamp, format YYYYmmddhms -> 20181004151020
$Timestamp = date(‘YmdGis’);
//$dt = new DateTime(‘now’, new DateTimezone(‘Africa/Nairobi’));

//$Timestamp = $dt->format(‘YmdGis’);

# Get the base64 encoded string -> $password. The passkey is the M-PESA Public Key
$Password = base64_encode($BusinessShortCode.$Passkey.$Timestamp);

# header for access token
$headers = [‘Content-Type:application/json; charset=utf8’];

# M-PESA endpoint urls

//$access_token_url = ‘https://sandbox.safaricom.co.ke/oauth/v1/generate?grant_type=client_credentials’;
//$initiate_url = ‘https://sandbox.safaricom.co.ke/mpesa/stkpush/v1/processrequest’;
$access_token_url = ‘https://api.safaricom.co.ke/oauth/v1/generate?grant_type=client_credentials’;
$initiate_url = ‘https://api.safaricom.co.ke/mpesa/stkpush/v1/processrequest’;
if(MODE == 0){
$access_token_url = ‘https://sandbox.safaricom.co.ke/oauth/v1/generate?grant_type=client_credentials’;
$initiate_url = ‘https://sandbox.safaricom.co.ke/mpesa/stkpush/v1/processrequest’;
}
# callback url
$CallBackURL = CALLBACK_URL;

$curl = curl_init($access_token_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;
curl_close($curl);

# header for stk push
$stkheader = [‘Content-Type:application/json’,’Authorization:Bearer ‘.$access_token];

# initiating the transaction
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $initiate_url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $stkheader); //setting custom header

$curl_post_data = array(
//Fill in the request parameters with valid values
‘BusinessShortCode’ => $BusinessShortCode,
‘Password’ => $Password,
‘Timestamp’ => $Timestamp,
‘TransactionType’ => ‘CustomerPayBillOnline’,
‘Amount’ => $Amount,
‘PartyA’ => $PartyA,
‘PartyB’ => $BusinessShortCode,
‘PhoneNumber’ => $PartyA,
‘CallBackURL’ => $CallBackURL,
‘AccountReference’ => $AccountReference,
‘TransactionDesc’ => $TransactionDesc
);

$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);

$jData = json_decode($curl_response,true);
print_r($curl_response);

?>

2.Create a callback file and paste in the below code:

<?php

$callbackResponse = file_get_contents(‘php://input’);

$logFile = “stk-push-callback-response.json”;

$log = fopen($logFile, “a”);

fwrite($log, $callbackResponse);fclose($log);


Leave a Reply

Your email address will not be published. Required fields are marked *