PHP code example of zain-ul-abdain / laravel-webhook-ledger

1. Go to this page and download the library: Download zain-ul-abdain/laravel-webhook-ledger 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/ */

    

zain-ul-abdain / laravel-webhook-ledger example snippets


// This does not work.
if (WebhookEvent::where('event_id', $id)->exists()) {
    return response('duplicate', 200);
}

WebhookEvent::create(['event_id' => $id, ...]);
$this->handle($payload);

$table->unique(['provider', 'event_id']);

try {
    $event = WebhookEvent::create([...]);   // the claim
} catch (UniqueConstraintViolationException) {
    return $this->handleExisting(...);      // someone else got there first
}

$handler($payload, $event);                 // runs at most once

use Zain\WebhookLedger\Facades\WebhookLedger;

Route::post('/webhooks/stripe', function (Request $request) {
    $result = WebhookLedger::process('stripe', $request, function (array $payload, $event) {
        match ($payload['type']) {
            'checkout.session.completed' => $orders->markPaid($payload['data']['object']),
            'charge.refunded'            => $orders->refund($payload['data']['object']),
            default                      => null,
        };
    });

    return response()->noContent();
});

'providers' => [
    'stripe' => [
        'verifier'   => StripeSignatureVerifier::class,
        'secret'     => env('STRIPE_WEBHOOK_SECRET'),
        'tolerance'  => 300,
        'paths'      => [
            'id'          => 'id',
            'type'        => 'type',
            'external_id' => ['data.object.payment_intent', 'data.object.id'],
        ],
    ],
],

'stale_claim_after' => 900,

Schedule::command('webhook-ledger:sweep')->hourly();

use Zain\WebhookLedger\Exceptions\DeferWebhook;

WebhookLedger::process('stripe', $request, function (array $payload, $event) {
    $order = Order::where('reference', $payload['data']['object']['id'])->first();

    if (! $order) {
        throw DeferWebhook::because('Order row not committed yet');
    }

    $order->markPaid();
});

WebhookLedger::process('stripe', $request, function (array $payload, $event) {
    $order = Order::findByPaymentIntent($payload['data']['object']['payment_intent']);

    $event->attachTo($order);
    $order->markPaid();
});

$history = WebhookEvent::where('subject_type', Order::class)
    ->where('subject_id', $order->id)
    ->latest()
    ->get();

'stripe' => [
    // ...
    'handler' => App\Webhooks\StripeHandler::class,
],

$table->string('provider', 64);
$table->string('event_id', 191);
$table->string('event_type', 191)->nullable();
$table->string('external_id', 191)->nullable();
$table->string('status', 16);          // processing | processed | failed
$table->unsignedSmallInteger('attempts');
$table->json('payload');
$table->text('last_error')->nullable();
$table->timestamp('claimed_at')->nullable();
$table->timestamp('processed_at')->nullable();

$table->unique(['provider', 'event_id']);   // the guarantee
$table->index(['provider', 'external_id']); // "what have we received about this order?"
$table->index(['status', 'claimed_at']);    // sweep and replay
bash
php artisan vendor:publish --tag=webhook-ledger-config
bash
php artisan webhook-ledger:replay --pretend
php artisan webhook-ledger:replay --provider=stripe --limit=50
php artisan webhook-ledger:replay --id=1041 --id=1042