Webhooks
In this guide, we will look at how to register and consume webhooks to integrate your app with Collctiv. With webhooks, your app can know when something happens in Collctiv, such as when a payment is made.
Registering webhooks
To register a new webhook, you need to have a URL in your app that Collctiv can call. You can configure a new webhook from the Collctiv dashboard under API. Give your webhook a name, pick the events you want to listen for, and add your URL.
Now, whenever something of interest happens in your app, a webhook is fired off by Collctiv. In the next section, we'll look at how to consume webhooks.
Consuming webhooks
When your app receives a webhook request from Collctiv, check the type
attribute to see what event caused it. The first part of the event type will tell you the payload type, e.g., a payment, etc.
Example webhook payload
{
"id": "a056V7R7NmNRjl70",
"type": "payment.succeeded",
"payload": {
"id": "WAz8eIbvDR60rouK"
// ...
}
}
In the example above, a payment has suceeded
, and the payload type is a payment
.
Event types
- Name
payment.succeeded
- Description
A new payment has successfully taken payment.
Example payload
{
"id": "7cea88c0-8acd-4a3f-95fb-66d34254f830",
"type":"payment.succeeded",
"payload": {
"id": "999fe8d5-1615-414b-83fc-2740b3c4a899",
"amount": 2100,
"created_at": 692233200,
"pot": "d4748db4-7863-4fa0-9a77-3cd329d5e652",
"billing_details": {
"name": "Steve Connors",
"email": "steve.connors@example.com"
},
"customer": "117b2b86-4b21-4a67-8592-c267a7ba7997",
"status": "paid",
"currency": "gbp",
"note": "Thanks so much for organising",
"metadata": {},
"amount_details": {
"contribution": 2000,
"fee": 100,
"net": 1900
},
"descriptor": "MyCo-Wedding Pot",
"payment_method_details": {
"country": "GB",
"exp_month": 4,
"exp_year": 2024,
"last4": "1234",
"provider": "visa"
}
}
}
Security
To know for sure that a webhook was, in fact, sent by Collctiv instead of a malicious actor, you can verify the request signature. Each webhook request contains a header named x-protocol-signature
, and you can verify this signature by using your secret webhook key. The signature is an HMAC hash of the request payload hashed using your secret key. Here is an example of how to verify the signature in your app:
Verifying a request
const signature = req.headers['x-protocol-signature']
const hash = crypto.createHmac('sha256', secret).update(payload).digest('hex')
if (hash === signature) {
// Request is verified
} else {
// Request could not be verified
}
If your generated signature matches the x-protocol-signature
header, you can be sure that the request was truly coming from Collctiv. It's essential to keep your secret webhook key safe — otherwise, you can no longer be sure that a given webhook was sent by Collctiv. Don't commit your secret webhook key your repository!