remymumoh

JsonMpesa

May 7th, 2019
215
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. $host="localhost";
  4. $username="root";
  5. $password="";
  6. $dbname="lnm";
  7. //create a Connection
  8. $conn=mysqli_connect($host,$username,$password,$dbname);
  9.  
  10. //check the connection
  11.  
  12. if(!$conn)
  13. {
  14.     die("connection does not established successfully :".mysqli_connect_error());
  15. }
  16. //read the json file using php method   file_get_contents('filename.json')
  17. $jsondata=file_get_contents('mpesa.json');
  18.  
  19. //convert json into php array
  20. $data=json_decode($jsondata,true);
  21.  
  22. $Body = $result->Body;
  23. $stkCallback = $Body->stkCallback;
  24. $CallbackMetadata = $stkCallback->CallbackMetadata;
  25. $Item = $CallbackMetadata->Item;
  26.  
  27. $MerchantRequestID = $data['Body']['stkCallback']['MerchantRequestID'];
  28. $CheckoutRequestID = $data['Body']['stkCallback']['CheckoutRequestID'];
  29. $ResultCode = $data['Body']['stkCallback']['ResultCode'];
  30. $ResultDesc = $data['Body']['stkCallback']['ResultDesc'];
  31.  
  32. if (is_array($Item) || is_object($Item)){
  33.     foreach ($Item as $key => $value) {
  34.         $Name = $value->Name;
  35.         if (property_exists($value, "Value")) {
  36.             $Value = $value->Value;
  37.  
  38.  
  39.             if (strcasecmp($Name, "Amount") == 0) {
  40.                 $data->amount = $Value;
  41.             }
  42.  
  43.             if (strcasecmp($Name, "MpesaReceiptNumber") == 0) {
  44.                 $data->mpesa_receipt_number = $Value;
  45.             }
  46.  
  47.             if (strcasecmp($Name, "Balance") == 0) {
  48.                 $data->balance = $Value;
  49.             }
  50.  
  51.             if (strcasecmp($Name, "TransactionDate") == 0) {
  52.  
  53.                 $data->transaction_date =$Value;
  54.             }
  55.  
  56.             if (strcasecmp($Name, "PhoneNumber") == 0) {
  57.                 $data->phone_number = $Value;
  58.             }
  59.         }
  60.     }
  61.  
  62.  
  63. }
  64.  
  65.  
  66.  
  67. //$sql = "insert into transactions(MerchantRequestID,CheckoutRequestID,ResultCode,ResultDesc)
  68. //values('$MerchantRequestID','$CheckoutRequestID','$ResultCode','$ResultDesc')";
  69.  
  70. $sql="insert into transactions (merchant_request_id,checkout_request_id,result_code,result_desc)
  71. values('$MerchantRequestID','$CheckoutRequestID','$ResultCode','$ResultDesc')";
  72.  
  73. //run the above query
  74. $result=mysqli_query($conn,$sql);
  75.  
  76. if(!$result)
  77. {
  78.     die("ERROR :". mysqli_error($conn));
  79. }
  80. else
  81. {
  82.     echo "Records has been successfully inserted";
  83. }
  84.  
  85.  
  86. //close the connection
  87. 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.

×