PHP code example of techrays-labs / laravel-webhooker
1. Go to this page and download the library: Download techrays-labs/laravel-webhooker 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/ */
techrays-labs / laravel-webhooker example snippets
use TechraysLabs\Webhooker\Facades\Webhook;
$endpoint = Webhook::registerEndpoint([
'name' => 'Payment Service',
'url' => 'https://payments.example.com/webhook',
'direction' => 'outbound',
'secret' => 'your-webhook-secret',
'is_active' => true,
'timeout_seconds' => 30,
]);
Webhook::dispatch($endpoint->id, 'order.created', [
'order_id' => 12345,
'total' => 99.99,
'currency' => 'USD',
]);
Webhook::dispatch($endpoint->id, 'order.created', $payload, [
'idempotency_key' => 'order-123-created',
]);
Webhook::broadcast('order.shipped', [
'order_id' => 12345,
'tracking_number' => 'ABC123',
]);
Webhook::dispatchToTag('payments', 'order.created', $payload);
$endpoint = Webhook::registerEndpoint([
'name' => 'Stripe Webhooks',
'url' => 'https://yourapp.com/api/webhooks/inbound',
'direction' => 'inbound',
'secret' => 'whsec_your_stripe_secret',
'is_active' => true,
'timeout_seconds' => 30,
]);
use TechraysLabs\Webhooker\Contracts\InboundProcessor;
$this->app->bind(InboundProcessor::class, MyStripeProcessor::class);
use TechraysLabs\Webhooker\Contracts\InboundProcessor;
use TechraysLabs\Webhooker\Models\WebhookEvent;
class MyStripeProcessor implements InboundProcessor
{
public function process(WebhookEvent $event): bool
{
match ($event->event_name) {
'payment_intent.succeeded' => $this->handlePayment($event->payload),
'customer.subscription.deleted' => $this->handleCancellation($event->payload),
default => null,
};
return true;
}
}
Gate::define('viewWebhookDashboard', function ($user) {
return $user->isAdmin();
});
// config/webhooks.php
'dashboard' => [
'enabled' => false,
],
// config/webhooks.php
'circuit_breaker' => [
'enabled' => true,
'failure_threshold' => 10, // Consecutive failures to trip
'cooldown_seconds' => 300, // Wait before half-open test
'success_threshold' => 2, // Successes in half-open to close
],
Webhook::disable($endpointId, 'Scheduled maintenance');
Webhook::enable($endpointId);
Webhook::isEnabled($endpointId);
$endpoint->attachTag('payments');
$endpoint->detachTag('payments');
$endpoint->hasTag('payments');
// Dispatch to all endpoints with a tag
Webhook::dispatchToTag('payments', 'order.created', $payload);
$newSecret = Webhook::rotateSecret($endpointId);
$schedule->command('webhook:prune')->daily();
$schedule->command('webhook:secret:cleanup')->hourly();
// config/webhooks.php
'retry' => [
'max_attempts' => 5,
'base_delay_seconds' => 10,
'multiplier' => 2,
],
$endpoint->max_retries = 10;
$endpoint->retry_strategy = MyCustomRetryStrategy::class;
$endpoint->save();
use TechraysLabs\Webhooker\Contracts\RetryStrategy;
$this->app->bind(RetryStrategy::class, MyCustomRetryStrategy::class);
// config/webhooks.php
'rate_limiting' => [
'enabled' => false,
'default_per_minute' => 60,
],
// config/webhooks.php
'payload_validation' => [
'enabled' => false,
'schemas' => [
'order.created' => [
'order_id' => '
// config/webhooks.php
'inbound' => [
'ip_allowlist' => [
'enabled' => false,
'global' => ['192.168.1.0/24'],
'trust_proxy' => false,
],
],
// config/webhooks.php
'storage' => [
'driver' => env('WEBHOOK_STORAGE_DRIVER', 'eloquent'),
'drivers' => [
'eloquent' => [
'connection' => null,
'read_connection' => null,
],
],
],
use TechraysLabs\Webhooker\Storage\WebhookStorageManager;
app(WebhookStorageManager::class)->extend('dynamodb', function ($app) {
return new DynamoDbWebhookRepository(config('webhooks.storage.drivers.dynamodb'));
});
// config/webhooks.php
'dead_letter' => [
'enabled' => false,
'auto_move' => true, // Auto-move after retries exhausted
'retention_days' => 90, // DLQ retention before pruning
],
use TechraysLabs\Webhooker\Facades\Webhook;
// Dispatch to specific endpoints
$batch = Webhook::dispatchBatch([1, 2, 3], 'order.created', [
'order_id' => 12345,
'total' => 99.99,
]);
// Broadcast to all active outbound endpoints as a batch
$batch = Webhook::broadcastBatch('order.shipped', [
'order_id' => 12345,
'tracking_number' => 'ABC123',
]);
// Check batch progress
$batch = Webhook::batchStatus($batch->id);
echo $batch->status; // pending, processing, completed, partial_failure, failed
echo $batch->success_count;
echo $batch->failure_count;
// config/webhooks.php
'batching' => [
'enabled' => true,
'max_batch_size' => 1000,
'allow_partial_failure' => true,
],
// config/webhooks.php
'health_history' => [
'enabled' => false,
'snapshot_interval' => 60, // Minutes between snapshots
'retention_days' => 90,
],
$schedule->command('webhook:health:snapshot')->hourly();
use TechraysLabs\Webhooker\Contracts\WebhookMetrics;
$metrics = app(WebhookMetrics::class);
$history = $metrics->endpointHealthHistory($endpointId, days: 30);
foreach ($history as $point) {
echo "{$point->date}: {$point->successRate}% ({$point->status})";
}
// config/webhooks.php
'storage' => [
'driver' => 'eloquent',
'drivers' => [
'eloquent' => [
'connection' => 'mysql', // Primary for writes
'read_connection' => 'mysql-replica', // Replica for reads
],
],
],
// config/webhooks.php
'partitioning' => [
'enabled' => false,
'strategy' => 'monthly',
'tables' => ['webhook_events', 'webhook_attempts'],
'future_partitions' => 3,
],
// config/webhooks.php
'scaling' => [
'enabled' => false,
'lock_driver' => 'cache',
'lock_ttl' => 300, // Lock timeout in seconds
'unique_jobs' => true,
],
use TechraysLabs\Webhooker\Contracts\WebhookLock;
$this->app->bind(WebhookLock::class, MyRedisLockProvider::class);
use TechraysLabs\Webhooker\Facades\Webhook;
Webhook::fake();
// ... trigger your application code ...
Webhook::assertDispatched('order.created');
Webhook::assertDispatched('order.created', function ($event) {
return $event->payload['order_id'] === 123;
});
Webhook::assertNothingDispatched();
Webhook::assertDispatchedTimes('order.created', 3);
use TechraysLabs\Webhooker\Testing\InteractsWithWebhooks;
class MyTest extends TestCase
{
use InteractsWithWebhooks;
public function test_something(): void
{
$fake = $this->fakeWebhooks();
// ... trigger your app code ...
$fake->assertDispatched('order.created');
}
}
// config/webhooks.php
'debug' => [
'enabled' => env('WEBHOOK_DEBUG', false),
'log_full_payload' => false,
'log_full_headers' => false,
'log_full_response_body' => false,
],
// config/webhooks.php
return [
'retry' => [
'max_attempts' => 5,
'base_delay_seconds' => 10,
'multiplier' => 2,
],
'timeout' => 30,
'signing_algorithm' => 'sha256',
'signature_header' => 'X-Webhook-Signature',
'queue' => [
'connection' => null,
'name' => 'webhooks',
],
'retention_days' => 30,
'store_response_body' => true,
'log_request_headers' => false,
'dashboard' => [
'enabled' => true,
'prefix' => 'webhooks',
'middleware' => ['web', 'auth'],
'gate' => 'viewWebhookDashboard',
'max_bulk_size' => 100,
],
'metrics' => [
'cache_ttl' => 60,
'healthy_threshold' => 95,
'degraded_threshold' => 70,
],
'circuit_breaker' => [
'enabled' => true,
'failure_threshold' => 10,
'cooldown_seconds' => 300,
'success_threshold' => 2,
],
'logging' => [
'channel' => env('WEBHOOK_LOG_CHANNEL', null),
'log_payload' => false,
'log_headers' => false,
'log_level' => 'info',
],
'debug' => [
'enabled' => env('WEBHOOK_DEBUG', false),
'log_full_payload' => false,
'log_full_headers' => false,
'log_full_response_body' => false,
],
'rate_limiting' => [
'enabled' => false,
'default_per_minute' => 60,
],
'payload_validation' => [
'enabled' => false,
'schemas' => [],
],
'inbound' => [
'ip_allowlist' => [
'enabled' => false,
'global' => [],
'trust_proxy' => false,
],
],
'secret_rotation' => [
'grace_period_hours' => 24,
],
'storage' => [
'driver' => env('WEBHOOK_STORAGE_DRIVER', 'eloquent'),
'drivers' => [
'eloquent' => [
'connection' => null,
'read_connection' => null,
],
],
],
'dead_letter' => [
'enabled' => false,
'auto_move' => true,
'retention_days' => 90,
],
'batching' => [
'enabled' => true,
'max_batch_size' => 1000,
'allow_partial_failure' => true,
],
'health_history' => [
'enabled' => false,
'snapshot_interval' => 60,
'retention_days' => 90,
],
'partitioning' => [
'enabled' => false,
'strategy' => 'monthly',
'tables' => ['webhook_events', 'webhook_attempts'],
'future_partitions' => 3,
],
'scaling' => [
'enabled' => false,
'lock_driver' => 'cache',
'lock_ttl' => 300,
'unique_jobs' => true,
],
];
bash
php artisan vendor:publish --tag=webhooker-config
bash
php artisan migrate
bash
php artisan webhook:prune
bash
php artisan webhook:prune --days=7
bash
php artisan vendor:publish --tag=webhooker-stubs
bash
php artisan migrate
bash
php artisan vendor:publish --tag=webhooker-config --force