A PHP library to aid is the processing of webhooks from GoCardless (see API documentation for more information).
Install the library using Composer. Add the following to your composer.json:
{
"require": {
"ockle/gocardless-webhook": "~1.0"
}
}
and then run:
composer update ockle/gocardless-webhook
Instantaiate a new webhook service:
// $secret is the string entered in the GoCardless admin when setting up a webhook endpoint
$webhook = new \Ockle\GoCardlessWebhook\Service($secret, new \Symfony\Component\EventDispatcher\EventDispatcher);
Then attach event listeners, e.g.:
$webhook->onMandate(function (\Ockle\GoCardlessWebhook\Events\MandateEvent $event) use ($client) {
// Do stuff when a mandate event occurs
// You can check what the action is:
if ($event->actionIs(\Ockle\GoCardlessWebhook\Events\MandateEvent::ACTION_CREATED)) {
// Mandate has been created
}
// However, it is advised that you don't do any important business logic based off this, but
// rather do an API call to get the current status of the mandate and act on that as the accurate
// source of mandate information. This is due to the fact that events may be received in any order.
// There are methods to get information about the event, e.g.:
$mandateId = $event->getMandateId();
});
Next, process the webhook, which will trigger your event listeners as necessary:
$webhook->process($webhookSignatureFromHeaders, $rawPostBody);
Finally, return the correct HTTP status code is your response, which can be got by calling:
$webhook->getResponseStatus();
MIT