PHP code example of transistorized-cmd / stripe-toolkit-webhooks
1. Go to this page and download the library: Download transistorized-cmd/stripe-toolkit-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/ */
transistorized-cmd / stripe-toolkit-webhooks example snippets
use TransistorizedCmd\StripeToolkit\Webhooks\Attributes\StripeEvent;
use TransistorizedCmd\StripeToolkit\Webhooks\Contracts\WebhookEventDTO;
use TransistorizedCmd\StripeToolkit\Webhooks\StripeWebhookHandler;
#[StripeEvent('payment_intent.succeeded')]
class FulfillOrder extends StripeWebhookHandler
{
public int $tries = 5;
public array $backoff = [60, 300, 900, 1800, 3600];
public function handle(WebhookEventDTO $event): void
{
/** @var \Stripe\PaymentIntent $intent */
$intent = $event->relatedObject();
Order::where('stripe_pi', $intent->id)
->firstOrFail()
->markPaid($intent->amount, $intent->currency);
}
}
// routes/web.php
use TransistorizedCmd\StripeToolkit\Webhooks\Facades\StripeWebhook;
StripeWebhook::route('stripe/webhook');
->withMiddleware(function (Middleware $middleware): void {
$middleware->validateCsrfTokens(except: [
'stripe/webhook',
'stripe/webhook/*',
]);
})
#[StripeEvent('invoice.payment_failed')]
#[StripeEvent('invoice.payment_action_ebhookEventDTO $event): void
{
/** @var \Stripe\Invoice $invoice */
$invoice = $event->relatedObject();
// …
}
}
// config/stripe-webhooks.php
'handlers' => [
'invoice.payment_failed' => [
\App\Stripe\Handlers\NotifyCustomer::class,
\App\Stripe\Handlers\StartDunning::class,
],
],
// routes/web.php
StripeWebhook::route('stripe/webhook/{configKey}');
// config/stripe-webhooks.php
'webhook_secrets' => [
'default' => env('STRIPE_WEBHOOK_SECRET'),
'platform' => env('STRIPE_WEBHOOK_SECRET_PLATFORM'),
'connect_v2' => env('STRIPE_WEBHOOK_SECRET_CONNECT_V2'),
],
public function handle(WebhookEventDTO $event): void
{
if ($accountId = $event->accountId()) {
$tenant = Tenant::where('stripe_account_id', $accountId)->firstOrFail();
// proceed with tenant context
}
}
use TransistorizedCmd\StripeToolkit\Webhooks\Support\StripeReconciler;
public function reconcile(Order $order, StripeReconciler $reconciler): RedirectResponse
{
/** @var \Stripe\Checkout\Session $session */
$session = $reconciler->fetchObject($order->stripe_checkout_session_id);
if ($session->payment_status === 'paid') {
$order->markPaid(/* … */);
}
return back();
}
$reconciler->reconcile($webhookCall);
use TransistorizedCmd\StripeToolkit\Webhooks\Tests\Support\SignedPayload;
$body = SignedPayload::body($eventArray);
$header = SignedPayload::header($body, 'whsec_test_default');
$this->call('POST', '/stripe/webhook', [], [], [], [
'HTTP_STRIPE_SIGNATURE' => $header,
'CONTENT_TYPE' => 'application/json',
], $body)->assertOk();
use TransistorizedCmd\StripeToolkit\Webhooks\Tests\Fixtures\Fixtures;
$payload = Fixtures::snapshotPaymentIntentSucceeded(accountId: 'acct_…');
$thinPayload = Fixtures::thinV1CustomerCreated();
use TransistorizedCmd\StripeToolkit\Webhooks\Events\WebhookDeadLettered;
use TransistorizedCmd\StripeToolkit\Webhooks\Events\WebhookHandlerFailed;
use TransistorizedCmd\StripeToolkit\Webhooks\Events\WebhookProcessed;
use TransistorizedCmd\StripeToolkit\Webhooks\Events\WebhookReceived;
Event::listen(WebhookDeadLettered::class, function (WebhookDeadLettered $e) {
Slack::send("Stripe webhook DLQ: {$e->webhookCall->stripe_event_id}");
});
Schedule::command('stripe-webhooks:prune')->dailyAt('03:00');
return [
'webhook_secrets' => [
'default' => env('STRIPE_WEBHOOK_SECRET'),
],
'route' => [
'path' => 'stripe/webhook',
'middleware' => ['api'],
],
'queue' => [
'connection' => env('STRIPE_QUEUE_CONNECTION', env('QUEUE_CONNECTION', 'sync')),
'name' => env('STRIPE_QUEUE_NAME', 'stripe-webhooks'),
],
'tables' => [
'webhook_calls' => 'stripe_webhook_calls',
'handler_runs' => 'stripe_webhook_handler_runs',
],
'retention' => [
'processed_days' => 90, // null = retain forever
'failed_days' => 365,
'dead_letter_days' => null, // keep DLQ forever by default
],
'tolerance' => 300, // signature timestamp tolerance (s)
'handlers' => [
// 'invoice.payment_failed' => [\App\Stripe\Handlers\StartDunning::class],
],
'discover_attributes' => true,
'discover_path' => null, // null → app_path('Stripe/Handlers')
'debug' => [
'enabled' => env('STRIPE_WEBHOOKS_DEBUG', null), // null = auto (on outside production)
'path' => env('STRIPE_WEBHOOKS_DEBUG_PATH', 'stripe-webhooks-debug'),
'middleware' => ['web'],
'per_page' => 25,
'auto_refresh_seconds' => 5,
],
];
bash
composer stripe-webhooks:install
php artisan migrate
bash
php artisan queue:work --queue=stripe-webhooks
bash
PHP_CLI_SERVER_WORKERS=4 php artisan serve --no-reload