0

I've been working on a web-based system using PHP. The system is linking to the payment gateway using API and webhooks for payment notification. I've worked on everything else but webhooks are foreign to me. I've subscribed to webhooks and the payment gateway is supposed to send in JSON via the url submitted when creating webhooks. Below is the sample JSON.

{
  "topic": "buygoods_transaction_received",
  "id": "2133dbfb-24b9-40fc-ae57-2d7559785760",
  "created_at": "2020-10-22T10:43:20+03:00",
  "event": {
    "type": "Buygoods Transaction",
    "resource": {
      "id": "458712f-gr76y-24b9-40fc-ae57-2d35785760",
      "amount": "100.0",
      "status": "Received",
      "system": "Lipa Na M-PESA",
      "currency": "KES",
      "reference": "OJM6Q1W84K",
      "till_number": "000000",
      "sender_phone_number": "+254999999999",
      "origination_time": "2020-10-22T10:43:19+03:00",
      "sender_last_name": "Doe",
      "sender_first_name": "Jane",
      "sender_middle_name": null
    }
  },
  "_links": {
    "self": "https://sandbox.kopokopo.com/webhook_events/2133dbfb-24b9-40fc-ae57-2d7559785760",
    "resource": "https://sandbox.kopokopo.com/financial_transaction/458712f-gr76y-24b9-40fc-ae57-2d35785760"
  }
}

I need to capture some fields like OJM6Q1W84K,100.0 etc and store them into my db. How do I capture the JSON? The rest will be easy for me once I figure out that one. Thanks guys.

ADyson
  • 57,178
  • 14
  • 51
  • 63
  • `$data = json_decoded($returned_json_response, true);` and now you have array, play with it. – Alive to die - Anant May 31 '22 at 13:22
  • A "webhook" is just a fancy word for some code which makes a HTTP request (usually in response to some other event). So when the hook is triggered, you'll get a HTTP request to your server (specifically to the URL you told the hook to use). Now all you have to do is have a PHP script at that URL which reads the incoming JSON. Assuming the webhook sends it in a POST request, then please read [Receive JSON POST with PHP](https://stackoverflow.com/questions/18866571/receive-json-post-with-php) for how to access it. – ADyson May 31 '22 at 14:20
  • Or if you're struggling with how to access specific elements from the JSON once you've got it into a variable (it wasn't clear which part of the task is actually the problem, since you only gave brief details, showed no code and only gave a vague problem description), then please read [How to extract and access data from JSON with PHP?](https://stackoverflow.com/questions/29308898/how-to-extract-and-access-data-from-json-with-php) instead. – ADyson May 31 '22 at 14:22
  • create a file on your server in public directory so with postman write the url, select post request and in the body of the post put this json. On the file u created on server put print_r(file_get_contents('php://input')); So postman show you the param the server get – Weber May 31 '22 at 16:15

0 Answers0