Language

Processing webhooks: Example

Let’s assume that you have a webhook subscription and Kopo Kopo has just delivered the following payload to your specified endpoint:

Sample Payload
{
  "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",
      "hashed_sender_phone": "8f7bd03d28bb39ffbe7e074ad6a85352b4de2c8a8af1db7db7e5a520e37e015d",
      "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"
  }
}

For example if you have Rails code on your server to handle a request from Kopo Kopo’s servers:

Ruby
require 'k2-connect-ruby'

webhook_test = K2Client.new(API_KEY)
webhook_test.parse_request(request)
K2ProcessWebhook.process(webhook_test.hash_body, webhook_test.api_secret_key, webhook_test.k2_signature)

Let’s recap. From the event we can retrieve the transaction resource and parse it. We can then check that it is not a duplicate in our system before we pass it off for further processing.

That’s it! You’ve learned the basics of Kopo Kopo webhooks.