PHPackages                             binary-cats/laravel-surveymonkey-webhooks - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. binary-cats/laravel-surveymonkey-webhooks

ActiveLibrary[API Development](/categories/api)

binary-cats/laravel-surveymonkey-webhooks
=========================================

Handle surveymonkey.com webhooks in a Laravel application

2.0.0(1mo ago)3392MITPHPPHP ^8.2CI passing

Since Jan 30Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/binary-cats/laravel-surveymonkey-webhooks)[ Packagist](https://packagist.org/packages/binary-cats/laravel-surveymonkey-webhooks)[ Docs](https://github.com/binary-cats/laravel-surveymonkey-webhooks)[ RSS](/packages/binary-cats-laravel-surveymonkey-webhooks/feed)WikiDiscussions main Synced today

READMEChangelog (5)Dependencies (7)Versions (10)Used By (0)

Handle Survey Monkey Webhooks in a Laravel application
======================================================

[](#handle-survey-monkey-webhooks-in-a-laravel-application)

[![https://github.com/binary-cats/laravel-surveymonkey-webhooks/actions](https://github.com/binary-cats/laravel-surveymonkey-webhooks/workflows/run-tests/badge.svg)](https://github.com/binary-cats/laravel-surveymonkey-webhooks/workflows/run-tests/badge.svg)[![https://github.styleci.io/repos/](https://camo.githubusercontent.com/c103df15d318ebecd9d7dda7fd6b3039f1ae174e5c0e6eb08d016a2314e00430/68747470733a2f2f6769746875622e7374796c6563692e696f2f7265706f732f3233373134303834372f736869656c64)](https://camo.githubusercontent.com/c103df15d318ebecd9d7dda7fd6b3039f1ae174e5c0e6eb08d016a2314e00430/68747470733a2f2f6769746875622e7374796c6563692e696f2f7265706f732f3233373134303834372f736869656c64)

[SurveyMonkey.com](https://surveymonkey.com) is an online survey development cloud-based software as a service company. SurveyMonkey also can notify your application of collector and response events using webhooks. This package can help you handle those webhooks. Out of the box it will verify SurveyMonkey signature of all incoming requests. All valid calls will be logged to the database. You can easily define jobs or events that should be dispatched when specific events hit your app.

 [![](https://repository-images.githubusercontent.com/237140847/c77ea380-42e9-11ea-86b6-c4037de3a3bd)](https://repository-images.githubusercontent.com/237140847/c77ea380-42e9-11ea-86b6-c4037de3a3bd)

This package will not handle what should be done after the webhook request has been validated and the right job or event is called. You should still code up any work yourself.

Before using this package we highly recommend reading [the entire documentation on webhooks over at surveymonkey.com](https://developer.surveymonkey.com/api/v3/#webhooks).

This package is an almost line-to-line adapted copy of absolutely amazing [spatie/laravel-stripe-webhooks](https://github.com/spatie/laravel-stripe-webhooks). Give them your love!

Lastly, this package assumes you are working with **Survey Monkey API version 3**

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require binary-cats/laravel-surveymonkey-webhooks
```

The service provider will automatically register itself.

You must publish the config file with:

```
php artisan vendor:publish --provider="BinaryCats\SurveyMonkeyWebhooks\SurveyMonkeyWebhooksServiceProvider" --tag="config"
```

This is the contents of the config file that will be published at `config/surveymonkey-webhooks.php`:

```
return [

    /*
     * Survey Monkey is an online survey development cloud-based software as a service company.
     * You can find the used secret after creating Survey Monkey App: https://developer.surveymonkey.com/
     */
    'signing_secret' => env('SURVEYMONKEY_API_SECRET'),

    /*
     * You can define the job that should be run when a certain webhook hits your application
     * here. The key is the name of the Survey Monkey event type with the `.` replaced by a `_`.
     *
     * You can find a list of Survey Monkey webhook types here:
     * https://developer.surveymonkey.com/api/v3/#webhook-callbacks
     */
    'jobs' => [
        // Example:
        // 'response_completed' => \BinaryCats\SurveyMonkeyWebhooks\Jobs\HandleResponseCompleted::class,
    ],

    /*
     * The classname of the model to be used. The class should equal or extend
     * Spatie\WebhookClient\Models\WebhookCall
     */
    'model' => \Spatie\WebhookClient\Models\WebhookCall::class,

    /*
     * The classname of the model to be used. The class should equal or extend
     * BinaryCats\SurveyMonkeyWebhooks\ProcessSurveyMonkeyWebhookJob
     */
    'process_webhook_job' => \BinaryCats\SurveyMonkeyWebhooks\ProcessSurveyMonkeyWebhookJob::class,
];
```

In the `signing_secret` key of the config file you should add a valid webhook secret. The secrets are assigned per application and you can get them on your [developer dashboard](https://developer.surveymonkey.com/apps/).

**You can skip migrating is you have already installed `Spatie\WebhookClient`**

Next, you must publish the migration with:

```
php artisan vendor:publish --provider="Spatie\WebhookClient\WebhookClientServiceProvider" --tag="migrations"
```

After migration has been published you can create the `webhook_calls` table by running the migrations:

```
php artisan migrate
```

### Routing

[](#routing)

Finally, take care of the routing: At [Survey Monkey app dashboard](https://developer.surveymonkey.com/apps/) you must configure at what url Survey Monkey webhooks should hit your app. In the routes file of your app you must pass that route to `Route::surveyMonkeyWebhooks()`:

I like to group functionality by domain, so would suggest `webwooks/survey-monkey`

```
Route::surveyMonkeyWebhooks('webwooks/survey-monkey');
```

Behind the scenes this will register a `POST` route to a controller provided by this package. Because Survey Monkey has no way of getting a csrf-token, you must add that route to the `except` array of the `VerifyCsrfToken` middleware:

```
protected $except = [
    'webwooks/survey-monkey',
];
```

Usage
-----

[](#usage)

Survey Monkey will send out webhooks for several event types. You can find the [full list of events types](https://developer.surveymonkey.com/api/v3/#webhooks) in SurveyMonkey.com documentation.

SurveyMonkey.com will sign all requests hitting the webhook url of your app. This package will automatically verify if the signature is valid. If it is not, the request was probably not sent by SurveyMonkey.com.

Unless something goes terribly wrong, this package will always respond with a `200` to webhook requests. Sending a `200` will prevent SurveyMonkey.com from resending the same event over and over again. All webhook requests with a valid signature will be logged in the `webhook_calls` table. The table has a `payload` column where the entire payload of the incoming webhook is saved.

If the signature is not valid, the request will not be logged in the `webhook_calls` table but a `BinaryCats\SurveyMonkeyWebhooks\Exceptions\WebhookFailed` exception will be thrown. If something goes wrong during the webhook request the thrown exception will be saved in the `exception` column. In that case the controller will send a `500` instead of `200`.

There are two ways this package enables you to handle webhook requests: you can opt to queue a job or listen to the events the package will fire.

### Handling webhook requests using jobs

[](#handling-webhook-requests-using-jobs)

If you want to do something when a specific event type comes in you can define a job that does the work. Here's an example of such a job:

```
