ockle/gocardless-webhook

Process webhooks from GoCardless

v1.0.3 2017-01-27 22:32 UTC

This package is not auto-updated.

Last update: 2024-12-17 05:47:59 UTC


README

Build Status Coverage Status

A PHP library to aid is the processing of webhooks from GoCardless (see API documentation for more information).

Installation

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

Usage

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();

License

MIT