Skip to content

Instantly share code, notes, and snippets.

@DavidInnocent
Created June 2, 2020 16:54
Show Gist options
  • Save DavidInnocent/7fae6b4b19e6deb9a2d9efa56e171fc5 to your computer and use it in GitHub Desktop.
Save DavidInnocent/7fae6b4b19e6deb9a2d9efa56e171fc5 to your computer and use it in GitHub Desktop.
<?php
header("Content-Type: application/json");
$response = '{
"ResultCode":0,
"ResultDesc":"Confirmation recieved successfuly"
}';
$mpesaResponse = file_get_contents('php://input');
$logFile = "M_PESAConfirmationResponse.txt";
$jsonMpesaResponse = json_decode($mpesaResponse, true);
$transaction = array (
':TransactionType' =>$jsonMpesaResponse['TransactionType'],
':TransID' =>$jsonMpesaResponse['TransID'],
':TransTime' =>$jsonMpesaResponse['TransTime'],
':TransAmount' =>$jsonMpesaResponse['TransAmount'],
':BusinessShortCode'=>$jsonMpesaResponse['BusinessShortCode'],
':BillRefNumber' =>$jsonMpesaResponse['BillRefNumber'],
':InvoiceNumber' =>$jsonMpesaResponse['InvoiceNumber'],
':MSISDN' =>$jsonMpesaResponse['MSISDN'],
':FirstName' =>$jsonMpesaResponse['FirstName'],
':MiddleName' =>$jsonMpesaResponse['MiddleName'],
':LastName' =>$jsonMpesaResponse['LastName']
);
$log = fopen($logfile,"a");
fwrite($log, $mpesaResponse);
fclose($log);
echo $response;
insert_response($transcation);
function insert_response($jsonMpesaResponse){
try {
$con = new PDO("mysql:dbhost=localhost;dbname=sample_pms",'sample_qwerty','mypassword');
echo "connection successful";
}
catch(PDOException $e){
die("error connecting".$e->getMessage());
}
try{
$insert = $con->prepare ("INSERT INTO 'mpesa'('TransactionType','TransID','TransTime','TransAmount','BusinessShortCode','BillRefNumber','InvoiceNumber','MSISDN','FirstName','MiddleName','LastName') VALUES (:TransactionType,:TransID,:TransTime,:TransAmount,:BusinessShortCode,:BillRefNumber,:InvoiceNumber,:MSISDN,:FirstName,:MiddleName,:LastName))");
$insert->execute((array)($jsonMpesaResponse));
$Transaction = fopen('Transaction.txt','a');
fwrite($Transaction,json_encode($jsonMpesaResponse));
fclose($Transaction);
}
catch(PDOException $e){
$errLog = fopen('error.txt','a');
fwrite($errLog,$e->getMessage());
fclose($errLog);
$LogFailedTransaction = fopen('failedtransaction.txt','a');
fwrite($LogFailedTransaction,$e->getMessage());
fclose($LogFailedTransaction);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment