Index
Index
php
<?php
date_default_timezone_set('Africa/Nairobi');
# provide the following details, this part is found on your test credentials on the developer account
$BusinessShortCode = '';
$Passkey = '';
/*
$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
$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.
*/
$TransactionDesc = 'test';
$Timestamp = date('YmdHis');
# Get the base64 encoded string -> $password. The passkey is the M-PESA Public Key
$Password = base64_encode($BusinessShortCode.$Passkey.$Timestamp);
$access_token_url = '';
$initiate_url = '';
# callback url
$CallBackURL = '';
$curl = curl_init($access_token_url);
$result = curl_exec($curl);
$result = json_decode($result);
$access_token = $result->access_token;
curl_close($curl);
#start of register
$url = '';
$confirmationUrl = ''; // path to your confirmation url. can be IP address that is publicly accessible or a
url
$validationUrl = ''; // path to your validation url. can be IP address that is publicly accessible or a url
$curl = curl_init();
$curl_post_data = array(
);
$data_string = json_encode($curl_post_data);
$curl_response = curl_exec($curl);
print_r($curl_response);
echo $curl_response;
#end of register
$curl = curl_init();
$curl_post_data = array(
);
$data_string = json_encode($curl_post_data);
$curl_response = curl_exec($curl);
print_r($curl_response);
echo $curl_response;
header("Content-Type: application/json");
$mpesaResponse = file_get_contents('php://input');
/* If we have any validation, we will do it here then change the $response if we reject the transaction */
// Your Validation
/* Ofcourse we will be checking for amount, account number(incase of paybill), invoice number and
inventory.
$logFile = "validationResponse.txt";
// will be used when we want to save the response to database for our reference
fwrite($log, $mpesaResponse);
fclose($log);
echo $response;
if ($response['ResultCode']==0){
exit();
else{
header("");
echo
$status;
{"Body":
"stkCallback": #key
{
"MerchantRequestID":"20431-10944643-1",
"CheckoutRequestID":"ws_CO_24062022140929823728785868",
"ResultCode":1032,
$resData= $response->Body->stkCallback->ResultCode;
?>
transaction_status_query.php
<?php
/* access token */
$access_token_url = 'https://sandbox.safaricom.co.ke/oauth/v1/generate?
grant_type=client_credentials';
$curl = curl_init($access_token_url);
$result = curl_exec($curl);
$result = json_decode($result);
$access_token = $result->access_token;
curl_close($curl);
/* making the request */
$tstatus_url = 'https://sandbox.safaricom.co.ke/mpesa/transactionstatus/v1/query';
$curl = curl_init();
curl_setopt($curl, CURLOPT_HTTPHEADER,
array('Content-Type:application/json','Authorization:Bearer '.$access_token)); //setting custom header
$curl_post_data = array(
);
$data_string = json_encode($curl_post_data);
$curl_response = curl_exec($curl);
print_r($curl_response);
echo $curl_response;
?>
confirmation_url.php
<?php
require 'config.php';
$headers = ['Content-Type:application/json'];
$response = '{
"ResultCode":0,
}';
//DATA
$mpesaResponse=file_get_contents('php://input');
$logFile="M_PESAResponse.txt";
#"M_PESAConfirmationResponse.txt"
$jssonMpesaResponse=json_decode($mpesaResponse, true);
$transaction=array(
`:InvoiceNumber`=> $jssonMpesaResponse['InvoiceNumber'],
`:OrgAccountBalance` => $jssonMpesaResponse['OrgAccountBalance'],
`:LastName`=> $jssonMpesaResponse['LastName']
);
//Write to file
fwrite($log, $mpesaResponse);
fclose($log);
echo $response;
insert_response($transaction);
?>
register_url.php
<?php
$url = 'https://sandbox.safaricom.co.ke/mpesa/c2b/v1/registerurl';
$access_token = ''; // check the mpesa_accesstoken.php file for this. No need to writing a new file
here, just combine the code as in the tutorial.
$shortCode = ''; // provide the short code obtained from your test credentials
$confirmationUrl = ''; // path to your confirmation url. can be IP address that is publicly accessible or a
url
$validationUrl = ''; // path to your validation url. can be IP address that is publicly accessible or a url
$curl = curl_init();
$curl_post_data = array(
);
$data_string = json_encode($curl_post_data);
$curl_response = curl_exec($curl);
print_r($curl_response);
echo $curl_response;
?>
ResultURL.php
<?php
$callbackResponse = file_get_contents('php://input');
$logFile = "transaction_status.json";
fwrite($log, $callbackResponse);
fclose($log);
validation_url.php
<?php
header("Content-Type: application/json");
$mpesaResponse = file_get_contents('php://input');
/* If we have any validation, we will do it here then change the $response if we reject the transaction
*/
// Your Validation
/* Ofcourse we will be checking for amount, account number(incase of paybill), invoice number and
inventory.
$logFile = "validationResponse.txt";
// will be used when we want to save the response to database for our reference
fwrite($log, $mpesaResponse);
fclose($log);
echo $response;
?>
access_token.php
<?php
//---------------------step1--------------
//-------------------Access token---------
//initiae curl
$curl = curl_init($url);
$result = curl_exec($curl);
$result = json_decode($result);
$access_token = $result->access_token;
echo $access_token;
curl_close($curl);
//---------------------step2--------------
//-------------------Register-------------
?>