Language

Step 2: Create a Kopo Kopo webhook subscription

Each application can have multiple subscriptions associated to it. While one subscription is sufficient, you can create as many as you want for redundancy.

To make the following request, we need to use the access_token we just previously obtained. Security considerations:

Your webhook endpoint should only be accessible over TLS (HTTPS) and your server should have a valid certificate. You can use the API Key you got when creating an oauth application to validate the authenticity of the webhook request from Kopo Kopo.

POST https://sandbox.kopokopo.com/api/v1/webhook_subscriptions
Accept: application/json
Content-Type: application/json
Authorization: Bearer 0Sn0W6kzNicvoWhDbQcVSKLRUpGjIdlPSEYyrHqrDDoRnQwE7Q
{
  "event_type": "buygooods_transaction_received",
  "url": "https://myapplication.com/webhooks",
  "scope": "till",
  "scope_reference": "555555"
}
request_body = {
  event_type: "buygoods_transaction_received",
  url: "https://myawesomeapplication.com/destination",
  scope: 'till',
  scope_reference: '555555'
}

# Using K2Connect - https://github.com/kopo-kopo/k2-connect-ruby (Recommended)
subscription = K2Subscribe.new('your_access_token')
subscription.webhook_subscribe(request_body) 
subscription.location_url # => "https://sandbox.kopokopo.com/api/v1/webhook_subscriptions/5af4c10a-f6de-4ac8-840d-42cb65454216"
//  Using Kopo Kopo Connect - https://github.com/kopokopo/k2-connect-node (Recommended)
const Webhooks = K2.Webhooks

var requestBody = {
  eventType: 'buygoods_transaction_received',
  url: 'https://myawesomeapplication.com/destination',
  scope: 'till',
  scopeReference: '555555', // Your till number
  accessToken: 'my_access_token'
}

Webhooks
  .subscribe(subscribeOptions) 
  .then(response => { console.log(response) }) 
  .catch(error => { console.log(error) })
  // => 'https://sandbox.kopokopo.com/api/v1/webhook_subscriptions/5af4c10a-f6de-4ac8-840d-42cb65454216'
import k2connect
payload = {
            "access_token": ACCESS_TOKEN,
            "event_type": 'buygoods_transaction_received',
            "webhook_endpoint": 'https://webhook.site/52fd1913-778e-4ee1-bdc4-74517abb758d',
            "scope": 'till',
            "scope_reference": '112233'
        }

k2connect.initialize(CLIENT_ID, CLIENT_SECRET, BASE_URL)
webhook_service = k2connect.Webhooks
subscription = webhook_service.create_subscription(payload)
<?
//  Using Kopo Kopo Connect - https://github.com/kopokopo/k2-connect-php (Recommended)
$webhooks = $K2->Webhooks();

$response = $webhooks->subscribe([
    'eventType' => 'buygoods_transaction_received',
    'url' => 'https://myawesomeapplication.com/destination',
    'scope' => 'till',
    'scopeReference' => '555555', // Your till number
    'accessToken' => 'my_access_token'
]);

if($response['status'] == 'success')
{
    echo "The resource location is:" . json_encode($response['location']);
}
  // => 'https://sandbox.kopokopo.com/webhook_subscriptions/5af4c10a-f6de-4ac8-840d-42cb65454216'

Retrieve your newly created subscription by its resource location:

GET https://sandbox.kopokopo.com/api/v1/webhook_subscriptions/5af4c10a-f6de-4ac8-840d-42cb65454216
Accept: application/json
Authorization: Bearer pBA9fVDBEyYZCEsLf/wKehyh1RTpzjUj5KzIRfDi0wKTii7DqY

...

{
  "data": {
    "id": "f24f203e-a909-4341-a483-1e96830b4301",
    "type": "webhook_subscription",
    "attributes": {
      "event_type": "customer_created",
      "webhook_uri": "https://webhook.site/675d4ef4-0629-481f-83cd-d101f55e4bc8",
      "status": "Active",
      "scope": "till",
      "scope_reference": "123456"
    }
  }
}
subscription_url = 'https://sandbox.kopokopo.com/api/v1/webhook_subscriptions/5af4c10a-f6de-4ac8-840d-42cb65454216'

# Using K2Connect - https://github.com/kopo-kopo/k2-connect-ruby (Recommended)
subscription = K2Subscribe.new(your_access_token)
subscription.query_resource_url(subscription_url)
//  Using Kopo Kopo Connect - https://github.com/kopokopo/k2-connect-node (Recommended)
const Webhooks = K2.Webhooks

var webhookSubscriptionUrl = 'https://sandbox.kopokopo.com/webhook_subscriptions/5af4c10a-f6de-4ac8-840d-42cb65454216'

Webhooks
  .getStatus({accessToken: 'myRand0mAcc3ssT0k3n', location: webhookSubscriptionUrl})
  .then(response => { console.log(response) }) 
  .catch(error => { console.log(error) })
subscription_url = 'https://sandbox.kopokopo.com/api/v1/webhook_subscriptions/5af4c10a-f6de-4ac8-840d-42cb65454216'

#Provide example using K2Connect https://github.com/kopo-kopo/k2-connect-python (Recommended)
k2connect.initialize(CLIENT_ID, CLIENT_SECRET, BASE_URL)
webhook_service = k2connect.Webhooks
webhook_service.webhook_status(ACCESS_TOKEN, subscription_url) 
<?
//  Using Kopo Kopo Connect - https://github.com/kopokopo/k2-connect-php (Recommended)
$webhooks = $K2->Webhooks();

$options = [
  'location' => 'https://sandbox.kopokopo.com/api/v1/webhook_subscriptions/d76265cd-0951-e511-80da-0aa34a9b2388',
  'accessToken' => 'myRand0mAcc3ssT0k3n',
];
$response = $webhooks->getStatus($options);

echo $response;