Notifications

Receive notifications about any changes related to your payments.

Overview

Unzer supports notifications for payment events that you can configure as webhooks.

Webhooks are individually defined HTTP callbacks from one website to another. They can go either way and are usually triggered by events. To keep your app or website up to date with any changes happening to your payments, you can register a webhook triggered by relevant events. To send notifications, we’re using the standard HTTP/1.1 protocol.

For the full list of webhook events supported by the Unzer API go to Supported webhook events.

To get the updated state of a resource:

  • Don’t use the event name as an indicator of the state of a resource. To get the latest state of the resource, you should always fetch the resource using the Unzer API.
  • Don’t use the event’s retrieveUrl to fetch the event’s resource without making sure the domain is legit.
  • Always fetch the event by creating an API request for the affected resource with correct Unzer API domain.

Step 1: Prepare your API

Other than meeting the basic requirements your API should also fulfill the following:

  • Accept text/plain content-type requests
  • Respond with the standard HTTP 200 OK success code, to acknowledge a received notification and prevent sending it again.

Notification examples

You’ll get notifications based on your public key, as a JSON payload. For example:

{
   "event":"types",
   "publicKey":"s-pub-xxxxxxxxxx",
   "retrieveUrl":"https://api.unzer.com/v1/types/card/s-crd-88xu7qjboupc"
}
{
  "event":"payment.pending",
  "publicKey":"s-pub-xxxxxxxxxx",
  "retrieveUrl":"https://api.unzer.com/v1/payments/s-pay-774",
  "paymentId":"s-pay-774"
}

Error handling

If our system doesn’t receive the standard HTTP 200 OK success code within 20 seconds, it tries to resend the notification in the following intervals:

Retry attempt Interval
1 1-2 minutes after the initial webhook event
2 5-6 minutes after the 1st retry attempt
3 10-11 minutes after the 2nd retry attempt
4 30-31 minutes after the 3rd retry attempt
5 60-61 minutes after the 4th retry attempt
6-16 (10 times) Every 60 minutes

If your API doesn’t respond after the last retry attempt, the notification gets marked as undeliverable, and you can’t retrieve it anymore.

Step 2: Register event notification

Register a URL to which you want to get notifications, and specify the events that you want to get notified about.

Selected event types

To enable notifications for selected event use event key. For example to trigger notifications for all Charge related events:

$unzer   = new UnzerSDK\Unzer('s-priv-xxxxxxxxxx');
$webhook = $unzer->createWebhook(
  'https://your.notification.handler.url',
  UnzerSDK\Constants\WebhookEvents::AUTHORIZE
);

The method returns a Webhook object that stores the PAPI response.

{
  "id": "s-whk-1",
  "event": "charge",
  "url": "https://www.yourApiUrl.com/notifications"
}

To enable notifications for multiple events with one request call registerMultipleWebhooks:

The method returns a Webhooks object that stores the eventList from the PAPI response.

$unzer    = new UnzerSDK\Unzer('s-priv-xxxxxxxxxx');
$webhooks = $unzer->registerMultipleWebhooks(
        'https://your.notification.handler.url',
        [
            UnzerSDK\Constants\WebhookEvents::AUTHORIZE,
            UnzerSDK\Constants\WebhookEvents::CHARGE,
            UnzerSDK\Constants\WebhookEvents::SHIPMENT
        ]
);
```json { "events": [ { "id": "s-whk-3", "url": "https://www.yourApiUrl.com/notifications", "event": "authorize" }, { "id": "s-whk-4", "url": "https://www.yourApiUrl.com/notifications", "event": "charge" }, { "id": "s-whk-5", "url": "https://www.yourApiUrl.com/notifications", "event": "shipment" } ] } ```

All events

To get notified about every single event, set event to all, for example:

$unzer   = new UnzerSDK\Unzer('s-priv-xxxxxxxxxx');
$webhook = $unzer->createWebhook(
  'https://your.notification.handler.url',
  UnzerSDK\Constants\WebhookEvents::ALL
);

Step 3: Manage webhooks

Using Unzer API webhooks endpoint you can retrieve, update and delete your webhooks.

Retrieve webhooks

To retrieve an existing webhook, call the Unzer::fetchWebhook method with the webhook’s ID in the URL:

$unzer   = new UnzerSDK\Unzer('s-priv-xxxxxxxxxx');
$webhook = $unzer->fetchWebhook('s-whk-1');

To retrieve all of your existing webhooks, call the Unzer::fetchWebhooks method with no webhook ID:

unzer   = new UnzerSDK\Unzer('s-priv-xxxxxxxxxx');
$webhooks = $unzer->fetchWebhooks();

Update webhooks

To update the URL for an existing webhook, call the Unzer::updateWebhookmethod, with the edited webhook object.

$unzer   = new UnzerSDK\Unzer('s-priv-xxxxxxxxxx');
$webhook = $unzer->fetchWebhook('s-whk-1');
$webhook->setUrl("https://newNotificationUrl.com");
$unzer->updateWebhook($webhook);

Delete webhooks

To delete an existing webhook, call the Unzer::deleteWebhook method, with the webhook’s ID as parameter:

$unzer   = new UnzerSDK\Unzer('s-priv-xxxxxxxxxx');
$webhook = $unzer->deleteWebhook('s-whk-1');

To delete all of your existing webhooks, call the Unzer::deleteAllWebhooks method with no parameter:

$unzer   = new UnzerSDK\Unzer('s-priv-xxxxxxxxxx');
$webhook = $unzer->deleteAllWebhooks();

For details on Unzer API webhook calls please check API Reference.

Handle incoming events

Make sure to keep Security in mind when handling Webhooks.

Whenever an event is triggered, the corresponding URL is called. The POST request contains the json-information about the event which you can use to fetch the corresponding resource which in turn can be used to update the state of the order/payment in your shop.

Just add the following code to the handler of the URL you registered, fetch the JSON from the event payload and inject it to the Unzer::fetchResourceFromEvent method.

The method will fetch the corresponding resource object and return it.

$unzer       = new UnzerSDK\Unzer('s-priv-xxxxxxxxxx');
$jsonRequest = file_get_contents('php://input');
$resource    = $unzer->fetchResourceFromEvent($jsonRequest);

After that you can determine the type of the resource and handle it accordingly. Please refer to the latest integration examples for a working example implementation of webhooks and the event handling.

See also