Customizing the package | laravel-webhook-client-go | Spatie       

 SPATIE  

laravel-webhook-client-go
=========================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-webhook-client-go](https://spatie.be/docs/laravel-webhook-client-go/v1)  Customizing the package

 Version   v1      

 Other versions for crawler [v1](https://spatie.be/docs/laravel-webhook-client-go/v1) 

  Customizing the package    
- [ Introduction ](https://spatie.be/docs/laravel-webhook-client-go/v1/introduction)
- [ Installation and setup ](https://spatie.be/docs/laravel-webhook-client-go/v1/installation-and-setup)
- [ Usage ](https://spatie.be/docs/laravel-webhook-client-go/v1/usage)
- [ Customizing the package ](https://spatie.be/docs/laravel-webhook-client-go/v1/customizing-the-package)
- [ Storing attachments ](https://spatie.be/docs/laravel-webhook-client-go/v1/attachments)
- [ Testing ](https://spatie.be/docs/laravel-webhook-client-go/v1/testing)
- [ Releasing ](https://spatie.be/docs/laravel-webhook-client-go/v1/releasing)
- [ Changelog ](https://spatie.be/docs/laravel-webhook-client-go/v1/changelog)
- [ Support us ](https://spatie.be/docs/laravel-webhook-client-go/v1/support-us)

 Customizing the package
=======================

###  On this page 

1. [ Creating your own signature validator ](#content-creating-your-own-signature-validator)
2. [ Creating your own webhook profile ](#content-creating-your-own-webhook-profile)
3. [ Creating your own webhook response ](#content-creating-your-own-webhook-response)
4. [ Using your own WebhookCall model ](#content-using-your-own-webhookcall-model)
5. [ Creating your own ProcessWebhookJob ](#content-creating-your-own-processwebhookjob)

Creating your own signature validator
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

Assign a function or a method to `WebhookConfig.SignatureValidator`:

```
cfg.SignatureValidator = func(r *web.Request, cfg webhookclient.WebhookConfig) (bool, error) {
    return webhookclient.DefaultSignatureValidator(r, cfg)
}
```

Use the sender's constant-time signature verification. Return false for invalid credentials and an error for operational failures. The original JSON or URL-encoded body is available in `r.Body`. Restore it if you read it yourself. `r.JSONInto` also buffers and preserves it. A multipart sender needs its own documented authentication scheme because Laravel has already consumed that body.

`InvalidWebhookSignatureEvent` contains the request and config. Register native Laravel listeners with `laravel.Listen`. Keep this event in-process because it includes a live request and policies. Do not log the config's signing secret or queue the live request.

Creating your own webhook profile
-----------------------------------------------------------------------------------------------------------------------------------------------------------

A webhook profile decides whether a valid request should be stored and processed. Assign a function or method with this signature:

```
cfg.WebhookProfile = func(r *web.Request) (bool, error) {
    payload, err := r.JSON[struct {
        Type string `json:"type"`
    }]()
    return payload.Type == "invoice.paid", err
}
```

Authenticate in the validator, not in the profile. The profile runs only after signature validation. Errors stop storage and dispatch.

Creating your own webhook response
--------------------------------------------------------------------------------------------------------------------------------------------------------------

Assign `WebhookResponse` to choose status, headers and body using Laravel's response type:

```
cfg.WebhookResponse = func(r *web.Request, cfg webhookclient.WebhookConfig) (*web.Response, error) {
    return web.Text("accepted").WithStatus(202), nil
}
```

The response applies to both processed and profile-ignored requests. It does not replace error responses.

Using your own WebhookCall model
--------------------------------------------------------------------------------------------------------------------------------------------------------

PHP model subclasses become a storage contract parameterized by your actual call type:

```
type WebhookCallModel[T any] interface {
    StoreWebhook(context.Context, *web.Request, webhookclient.WebhookConfig, []webhookclient.Attachment) (T, error)
    SaveException(context.Context, *T, error) error
}
```

`StoreWebhook` owns extraction and persistence and returns a saved call. `SaveException` must clear the saved exception when its error argument is nil, or persist the supplied error otherwise. Its pointer receives updates before the job factory sees the call. A failure while saving a dispatch error is joined with the original dispatch error so `errors.Is` can inspect both.

Pass your storage model to `NewProcessor`. Its call type flows unchanged into `NewQueuedJob` and `Processor.Dispatch`. Your model can add columns, use a different table or implement another storage backend. It does not have to embed `WebhookCall`.

Set `Config.WithoutMigration = true` to skip the default table migration and register your custom migrations. Your application uses its own model for lookup, worker reloads and replay. The package's prune command only targets `webhook_calls`. Supply your own pruning command for a custom table. With `DeleteAfterDays = nil`, the default prune command does no database work.

`HeadersToStore` is exported so a custom model can reuse the default header-selection policy without copying it. `AttachmentsFromRequest` and `AttachmentFromUpload` are available for custom upload behavior.

Creating your own ProcessWebhookJob
-----------------------------------------------------------------------------------------------------------------------------------------------------------------

Any serializable Laravel job with `Handle(context.Context) error` can process a webhook. The typed factory declares the actual concrete job type to register:

```
job := webhookclient.NewQueuedJob(func(call MyCall) MyProcessWebhook {
    return MyProcessWebhook{Call: call}
})
job.Connection = "database"
job.Queue = "payments"
processor := webhookclient.NewProcessor(cfg, MyStorage{}, job)
```

Job fields hold JSON data or native `queue:"model"` fields. Never put requests, closures or connections in the serialized job. Native model fields store the ID and reload your custom model on the worker. Use an explicit ID and your storage's own lookup for non-ORM models.

Use a concrete job return type, not the `NativeJob` interface, so Laravel can register and decode the selected type. Your job may then dispatch other application jobs. Retries, timeouts, middleware and failures remain native Laravel behavior. Register any additional job types explicitly.

When another package keeps a map of typed `QueuedJob` values, it can call `job.Register(app)` for each configured job in its registrar, then `job.Dispatch(ctx, call)` at execution time. This registers the same concrete type selected by the factory without exposing its internals. Processor endpoints use this registration method too.

Configure processors and policy instances before serving requests and treat the settings as immutable. Policies and custom models must be safe for concurrent requests. Package registration copies endpoint settings and reapplies environment defaults for each application. Application-specific dependencies belong in context-accessible services or callbacks created per application, not mutable globals.

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
