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