remymumoh

Mpesa

May 21st, 2019
313
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. header("Content-Type:application/json");
  3.  
  4.  
  5. $host="localhost";
  6. $username="root";
  7. $password="ray13a26b";
  8. $dbname="lnm";
  9.  
  10. //create a Connection
  11. $conn=mysqli_connect($host,$username,$password,$dbname);
  12.  
  13. //check the connection
  14.  
  15. if(!$conn)
  16. {
  17.     die("connection does not established successfully :".mysqli_connect_error());
  18. }
  19.  
  20. $dataPOST = trim(file_get_contents('php://input'));
  21. $obj = json_decode($dataPOST,true);
  22. if(empty($obj)){return ['message'=>'empty body'];}
  23. $mydata=$obj['Body']['stkCallback']['CallbackMetadata']['Item'];
  24. $merchant_id=$obj['Body']['stkCallback']['MerchantRequestID'];
  25. $request_id=$obj['Body']['stkCallback']['CheckoutRequestID'];
  26. $ResultCode=$obj['Body']['stkCallback']['ResultCode'];
  27. if ($ResultCode != 0) {
  28.     // $this->send_sms($request_id);
  29.     return ['message'=>'something went wrong'];
  30. }else{
  31.     foreach ($mydata as $key => $value) {
  32.         if($value['Name']=="Amount"){
  33.             $amount=$value['Value'];
  34.         }else if($value['Name']=="MpesaReceiptNumber"){
  35.             $MpesaReceiptNumber=$value['Value'];
  36.         }else if($value['Name']=="TransactionDate"){
  37.             $TransactionDate=$value['Value'];
  38.         }else if($value['Name']=="PhoneNumber"){
  39.             $PhoneNumber=$value['Value'];
  40.         }}}
  41.  
  42.  
  43. $sql="insert into transactions
  44. (merchant_request_id,checkout_request_id,result_code,amount,mpesa_receipt_number,transaction_date,phone_number )
  45. values
  46. ('$merchant_id','$request_id','$ResultCode','$amount','$MpesaReceiptNumber','$TransactionDate','$PhoneNumber')";
  47.  
  48. //run the above query
  49. $result=mysqli_query($conn,$sql);
  50.  
  51. if(!$result)
  52. {
  53.     die("ERROR :". mysqli_error($conn));
  54. }
  55. else
  56. {
  57.     echo "Records successfully inserted";
  58. }
  59.  
  60.  
  61. //close the connection
  62. mysqli_close($conn);
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×