PHP code example of binary-cats / laravel-lob-webhooks

1. Go to this page and download the library: Download binary-cats/laravel-lob-webhooks library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

binary-cats / laravel-lob-webhooks example snippets


return [

    /*
     * Lob.com will sign each webhook using a secret. You can find the used secret at the
     * webhook configuration settings: https://dashboard.lob.com/#/webhooks/create.
     */
    'signing_secret' => env('LOB_WEBHOOK_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 Lob.com event type with the `.` replaced by a `_`.
     *
     * You can find a list of Lob.com webhook types here:
     * https://lob.com/docs#all_event_types
     * 
     * The package will automatically convert the keys to lowercase, but you should
     * be congnisant of the fact that array keys are case sensitive
     */
    'jobs' => [
        // 'letter_delivered' => \BinaryCats\LobWebhooks\Jobs\HandleDelivered::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\LobWebhooks\ProcessLobWebhookJob
     */
    'process_webhook_job' => \BinaryCats\LobWebhooks\ProcessLobWebhookJob::class,
];

Route::lobWebhooks('webhooks/lob');

protected $except = [
    'webhooks/lob',
];

namespace App\Jobs\LobWebhooks;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Spatie\WebhookClient\Models\WebhookCall;

class HandleDeliveredSource implements ShouldQueue
{
    use InteractsWithQueue, Queueable, SerializesModels;

    /** @var \Spatie\WebhookClient\Models\WebhookCall */
    public $webhookCall;

    public function __construct(WebhookCall $webhookCall)
    {
        $this->webhookCall = $webhookCall;
    }

    public function handle()
    {
        // do your work here

        // you can access the payload of the webhook call with `$this->webhookCall->payload`
    }
}

// config/lob-webhooks.php

'jobs' => [
    'letter_created' => \App\Jobs\Lob\Webhooks\HandleLetterCreatedJob::class,
],

/**
 * The event listener mappings for the application.
 *
 * @var array
 */
protected $listen = [
    'lob-webhooks::letter.created' => [
        App\Listeners\LetterCreatedListener::class,
    ],
];

namespace App\Listeners;

use Illuminate\Contracts\Queue\ShouldQueue;
use Spatie\WebhookClient\Models\WebhookCall;

class LetterCreatedListener implements ShouldQueue
{
    public function handle(WebhookCall $webhookCall)
    {
        // do your work here

        // you can access the payload of the webhook call with `$webhookCall->payload`
    }
}

use BinaryCats\LobWebhooks\ProcessLobWebhookJob;
use Spatie\WebhookClient\Models\WebhookCall;

dispatch(new ProcessLobWebhookJob(WebhookCall::find($id)));

use BinaryCats\LobWebhooks\ProcessLobWebhookJob;

class MyCustomLobWebhookJob extends ProcessLobWebhookJob
{
    public function handle()
    {
        // do custom stuff beforehand

        parent::handle();

        // do custom stuff afterwards
    }
}

Route::lobWebhooks('webhooks/lob/{configKey}');

Route::post('webhooks/lob/{configKey}', 'BinaryCats\LobWebhooks\LobWebhooksController');

// secret for when Lob.com posts to webhooks/lob/account
'signing_secret_account' => 'whsec_abc',
// secret for when Lob.com posts to webhooks/lob/connect
'signing_secret_connect' => 'whsec_123',
bash
php artisan vendor:publish --provider="BinaryCats\LobWebhooks\LobWebhooksServiceProvider" --tag="config"
bash
php artisan vendor:publish --provider="Spatie\WebhookClient\WebhookClientServiceProvider" --tag="migrations"
bash
php artisan migrate