### Install Laravel Webhook Server Package Source: https://github.com/spatie/laravel-webhook-server/blob/main/README.md Installs the `spatie/laravel-webhook-server` package into your Laravel application using Composer. This command adds the package to your project's dependencies. ```bash composer require spatie/laravel-webhook-server ``` -------------------------------- ### Configure Proxy for Laravel Webhook Source: https://github.com/spatie/laravel-webhook-server/blob/main/README.md This example demonstrates how to route a specific webhook call through an HTTP proxy. The `useProxy` method accepts the proxy URL, enabling the webhook request to be sent via the specified proxy server. ```php WebhookCall::create() ->useProxy('http://proxy.server:3128') ... ``` -------------------------------- ### Test Webhook Queuing with Laravel Queue (PHP) Source: https://github.com/spatie/laravel-webhook-server/blob/main/README.md This test example shows how to confirm that a webhook job is pushed to the queue using Laravel's `Queue` facade. Faking the queue allows you to assert that `CallWebhookJob` is pushed without processing it. ```PHP use Illuminate\Support\Facades\Queue; use Spatie\WebhookServer\CallWebhookJob; use Tests\TestCase; class TestFile extends TestCase { public function testJobIsQueued() { Queue::fake(); // ... Perform webhook call ... Queue::assertPushed(CallWebhookJob::class); } } ``` -------------------------------- ### Customize HTTP Verb for Laravel Webhook Source: https://github.com/spatie/laravel-webhook-server/blob/main/README.md This example shows how to change the HTTP method used for a webhook request. The `useHttpVerb` method allows you to specify a different verb (e.g., 'GET', 'PUT', 'DELETE') instead of the default 'POST' for a particular webhook call. ```php WebhookCall::create() ->useHttpVerb('get') ... ->dispatch(); ``` -------------------------------- ### Add Meta Information to Webhook Calls (PHP) Source: https://github.com/spatie/laravel-webhook-server/blob/main/README.md This example shows how to attach extra meta information to a webhook call. This data is not transmitted with the webhook but is passed to the package's events, allowing for custom data handling within your application's event listeners. ```PHP WebhookCall::create() ->meta($arrayWithMetaInformation) ... ->dispatch(); ``` -------------------------------- ### Set Maximum Retries for Laravel Webhook Source: https://github.com/spatie/laravel-webhook-server/blob/main/README.md This example shows how to configure the maximum number of retry attempts for a webhook call. Using `maximumTries` allows overriding the global retry setting, ensuring the webhook is dispatched up to the specified number of times before failing permanently. ```php WebhookCall::create() ->maximumTries(5) ... ->dispatch(); ``` -------------------------------- ### Send Raw String Body in Webhook (PHP) Source: https://github.com/spatie/laravel-webhook-server/blob/main/README.md This example shows how to send a raw string body instead of the default JSON payload with a webhook. It also demonstrates disabling the signature for raw body requests due to current API limitations, useful for non-JSON content like XML. ```PHP WebhookCall::create() ->sendRawBody("someXMLContent") ->doNotSign() ... ->dispatch(); ``` -------------------------------- ### Run Package Tests (Bash) Source: https://github.com/spatie/laravel-webhook-server/blob/main/README.md This command is used to execute the automated test suite for the Spatie Laravel Webhook Server package. Running tests ensures the package functions as expected after changes or updates. ```Bash composer test ``` -------------------------------- ### Publish Laravel Webhook Server Configuration File Source: https://github.com/spatie/laravel-webhook-server/blob/main/README.md Publishes the default configuration file for the Laravel Webhook Server package. This command creates `config/webhook-server.php`, allowing you to customize various settings such as queues, HTTP verb, and retry behavior. ```bash php artisan vendor:publish --provider="Spatie\WebhookServer\WebhookServerServiceProvider" ``` -------------------------------- ### Dispatching a Basic Webhook Call in Laravel Source: https://github.com/spatie/laravel-webhook-server/blob/main/README.md This snippet demonstrates the fundamental way to send a webhook using `WebhookCall::create()`. It configures the target URL, payload data, and a secret for signing the request. The package automatically handles JSON encoding of the payload and adds a `Signature` header for verification. It also includes built-in retry logic for failed dispatches. ```php WebhookCall::create() ->url('https://other-app.com/webhooks') ->payload(['key' => 'value']) ->useSecret('sign-using-this-secret') ->dispatch(); ``` -------------------------------- ### Laravel Webhook Server Configuration Options Source: https://github.com/spatie/laravel-webhook-server/blob/main/README.md Defines the configurable options for the Spatie Laravel Webhook Server package. This array allows customization of settings like the queue to use, HTTP method, signature handling, retry attempts, backoff strategy, and SSL verification. ```php return [ /* * The default queue that should be used to send webhook requests. */ 'queue' => 'default', /* * The default http verb to use. */ 'http_verb' => 'post', /* * This class is responsible for calculating the signature that will be added to * the headers of the webhook request. A webhook client can use the signature * to verify the request hasn't been tampered with. */ 'signer' => \Spatie\WebhookServer\Signer\DefaultSigner::class, /* * This is the name of the header where the signature will be added. */ 'signature_header_name' => 'Signature', /* * These are the headers that will be added to all webhook requests. */ 'headers' => [], /* * If a call to a webhook takes longer this amount of seconds * the attempt will be considered failed. */ 'timeout_in_seconds' => 3, /* * The amount of times the webhook should be called before we give up. */ 'tries' => 3, /* * This class determines how many seconds there should be between attempts. */ 'backoff_strategy' => \Spatie\WebhookServer\BackoffStrategy\ExponentialBackoffStrategy::class, /* * This class is used to dispatch webhooks onto the queue. */ 'webhook_job' => \Spatie\WebhookServer\CallWebhookJob::class, /* * By default we will verify that the ssl certificate of the destination * of the webhook is valid. */ 'verify_ssl' => true, /* * When set to true, an exception will be thrown when the last attempt fails */ 'throw_exception_on_failure' => false, /* * When using Laravel Horizon you can specify tags that should be used on the * underlying job that performs the webhook request. */ 'tags' => [] ]; ``` -------------------------------- ### Dispatching Webhooks Synchronously in Laravel Source: https://github.com/spatie/laravel-webhook-server/blob/main/README.md To bypass the queuing mechanism and send a webhook immediately, use the `dispatchSync()` method. This is useful when the webhook dispatch is part of a larger, already queued job and needs to execute without further delay. ```php WebhookCall::create() ... ->dispatchSync(); ``` -------------------------------- ### Spatie Webhook Server Events (APIDOC) Source: https://github.com/spatie/laravel-webhook-server/blob/main/README.md Documentation for events fired by the Spatie Webhook Server package, detailing their purpose and available properties. These events allow for custom logic execution at various stages of the webhook dispatch and response cycle. ```APIDOC Events: - DispatchingWebhookCallEvent Description: Fired right before the webhook call will be dispatched to the queue. Properties: - httpVerb: The HTTP verb used for the request (string) - webhookUrl: The URL where the request was sent (string) - payload: The payload used in the request (array) - headers: The headers sent, including the signature header (array) - meta: Array of values passed with the `meta` call (array) - tags: Array of tags used (array) - uuid: A unique string to identify this call, consistent across attempts (string) - WebhookCallSucceededEvent Description: Fired when the remote app responded with a 2xx response code. Properties: - httpVerb: The HTTP verb used for the request (string) - webhookUrl: The URL where the request was sent (string) - payload: The payload used in the request (array) - headers: The headers sent, including the signature header (array) - meta: Array of values passed with the `meta` call (array) - tags: Array of tags used (array) - uuid: A unique string to identify this call, consistent across attempts (string) - attempt: The attempt number (integer) - response: The response returned by the remote app (GuzzleHttp\Psr7\Response|null) - WebhookCallFailedEvent Description: Fired when the remote app responded with a non-2xx response code, or it did not respond at all. Properties: - httpVerb: The HTTP verb used for the request (string) - webhookUrl: The URL where the request was sent (string) - payload: The payload used in the request (array) - headers: The headers sent, including the signature header (array) - meta: Array of values passed with the `meta` call (array) - tags: Array of tags used (array) - uuid: A unique string to identify this call, consistent across attempts (string) - attempt: The attempt number (integer) - response: The response returned by the remote app (GuzzleHttp\Psr7\Response|null) - FinalWebhookCallFailedEvent Description: Fired when the final attempt to call the webhook failed. Properties: - httpVerb: The HTTP verb used for the request (string) - webhookUrl: The URL where the request was sent (string) - payload: The payload used in the request (array) - headers: The headers sent, including the signature header (array) - meta: Array of values passed with the `meta` call (array) - tags: Array of tags used (array) - uuid: A unique string to identify this call, consistent across attempts (string) - attempt: The attempt number (integer) - response: The response returned by the remote app (GuzzleHttp\Psr7\Response|null) ``` -------------------------------- ### Configure Exception Handling for Failed Webhook Calls (PHP) Source: https://github.com/spatie/laravel-webhook-server/blob/main/README.md This code demonstrates how to configure a webhook call to throw an exception if its final attempt fails. By default, failing jobs are ignored, but this method allows for explicit error handling and reporting. ```PHP WebhookCall::create() ->throwExceptionOnFailure() ... ->dispatch(); ``` -------------------------------- ### Add Tags to Webhook Job (PHP) Source: https://github.com/spatie/laravel-webhook-server/blob/main/README.md This snippet illustrates how to add tags to the underlying Laravel Horizon job responsible for dispatching the webhook. Tags help in organizing and monitoring jobs within Horizon, improving visibility and management. ```PHP WebhookCall::create() ->withTags($tags) ... ->dispatch(); ``` -------------------------------- ### Test Webhook Dispatching with Laravel Bus (PHP) Source: https://github.com/spatie/laravel-webhook-server/blob/main/README.md This test snippet demonstrates how to verify that a webhook job is dispatched using Laravel's `Bus` facade. By faking the bus, you can assert that `CallWebhookJob` is dispatched without actually sending a webhook. ```PHP use Illuminate\Support\Facades\Bus; use Spatie\WebhookServer\CallWebhookJob; use Tests\TestCase; class TestFile extends TestCase { public function testJobIsDispatched() { Bus::fake(); // ... Perform webhook call ... Bus::assertDispatched(CallWebhookJob::class); } } ``` -------------------------------- ### Conditionally Dispatching Webhooks in Laravel Source: https://github.com/spatie/laravel-webhook-server/blob/main/README.md The package provides methods for conditional webhook dispatch, allowing you to send or prevent sending based on a given boolean condition. `dispatchIf()` and `dispatchUnless()` control asynchronous dispatch, while `dispatchSyncIf()` and `dispatchSyncUnless()` apply to synchronous dispatches. ```php WebhookCall::create() ... ->dispatchIf($condition); WebhookCall::create() ... ->dispatchUnless($condition); WebhookCall::create() ... ->dispatchSyncIf($condition); WebhookCall::create() ... ->dispatchSyncUnless($condition); ``` -------------------------------- ### Define Custom Webhook Backoff Strategy Interface Source: https://github.com/spatie/laravel-webhook-server/blob/main/README.md This PHP interface defines the contract for custom backoff strategies in the `laravel-webhook-server` package. Implementations of `BackoffStrategy` must provide the `waitInSecondsAfterAttempt` method, which determines the delay in seconds before the next retry based on the current attempt number. ```php namespace Spatie\WebhookServer\BackoffStrategy; interface BackoffStrategy { public function waitInSecondsAfterAttempt(int $attempt): int; } ``` -------------------------------- ### Using a Custom Webhook Signer in Laravel Source: https://github.com/spatie/laravel-webhook-server/blob/main/README.md After creating a custom signer class that implements `Spatie\WebhookServer\Signer`, you can instruct a specific `WebhookCall` to use it by calling the `signUsing()` method with your custom signer's class name. This allows for fine-grained control over the signing logic per webhook. ```php WebhookCall::create() ->signUsing(YourCustomSigner::class) ... ->dispatch(); ``` -------------------------------- ### Implement Mutual TLS for Laravel Webhook Source: https://github.com/spatie/laravel-webhook-server/blob/main/README.md This snippet shows how to configure mutual TLS authentication for a webhook call. The `mutualTls` method requires paths to the client certificate and private key, along with optional passphrases, to establish a secure, two-way authenticated connection with the webhook recipient. ```php WebhookCall::create() ->mutualTls( certPath: storage_path('path/to/cert.pem'), certPassphrase: 'optional_cert_passphrase', sslKeyPath: storage_path('path/to/key.pem'), sslKeyPassphrase: 'optional_key_passphrase' ) ``` -------------------------------- ### Configure Webhook Timeout in Laravel Source: https://github.com/spatie/laravel-webhook-server/blob/main/README.md This snippet demonstrates how to set a custom timeout duration for a specific webhook call. The `timeoutInSeconds` method overrides the default package configuration, ensuring the webhook waits for a response for the specified number of seconds before considering the call failed. ```php WebhookCall::create() ->timeoutInSeconds(5) ... ->dispatch(); ``` -------------------------------- ### Defining a Custom Webhook Signer Interface in Laravel Source: https://github.com/spatie/laravel-webhook-server/blob/main/README.md For advanced customization of the webhook signing process, you can implement the `Spatie\WebhookServer\Signer` interface. This interface defines the contract for custom signers, requiring methods to specify the signature header name and calculate the signature based on the payload and secret. ```php namespace Spatie\WebhookServer\Signer; interface Signer { public function signatureHeaderName(): string; public function calculateSignature(array $payload, string $secret): string; } ``` -------------------------------- ### Calculating Webhook Request Signature in Laravel Source: https://github.com/spatie/laravel-webhook-server/blob/main/README.md This code illustrates how the `Signature` header is calculated by the Laravel Webhook Server. It uses `hash_hmac` with the `sha256` algorithm, the JSON-encoded payload, and the provided secret to generate a cryptographic signature, ensuring payload integrity. ```php // payload is the array passed to the `payload` method of the webhook // secret is the string given to the `signUsingSecret` method on the webhook. $payloadJson = json_encode($payload); $signature = hash_hmac('sha256', $payloadJson, $secret); ``` -------------------------------- ### Add Custom Headers to Laravel Webhook Source: https://github.com/spatie/laravel-webhook-server/blob/main/README.md This snippet illustrates how to include custom HTTP headers with a webhook request. The `withHeaders` method accepts an associative array of header names and their values, allowing you to send additional information with the webhook payload. ```php WebhookCall::create() ->withHeaders([ 'Another Header' => 'Value of Another Header' ]) ... ->dispatch(); ``` -------------------------------- ### Apply Custom Backoff Strategy to Laravel Webhook Source: https://github.com/spatie/laravel-webhook-server/blob/main/README.md This snippet demonstrates how to assign a custom backoff strategy to a specific webhook call. By passing the class name of a custom `BackoffStrategy` implementation to `useBackoffStrategy`, you can control the delay between retry attempts for that particular webhook. ```php WebhookCall::create() ->useBackoffStrategy(YourBackoffStrategy::class) ... ->dispatch(); ``` -------------------------------- ### Disable SSL Verification for Webhook Calls (PHP) Source: https://github.com/spatie/laravel-webhook-server/blob/main/README.md This snippet demonstrates how to disable SSL certificate verification for a specific webhook call. This is useful in development or for specific endpoints where SSL verification is not required, though generally not recommended for production. ```PHP WebhookCall::create() ->doNotVerifySsl() ... ->dispatch(); ``` -------------------------------- ### Disabling Webhook Request Signing in Laravel Source: https://github.com/spatie/laravel-webhook-server/blob/main/README.md While not recommended for security, you can opt out of signing webhook requests by calling the `doNotSign()` method. This will prevent the `Signature` header from being included in the outgoing webhook request. ```php WebhookCall::create() ->doNotSign() ... ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.