Usage | laravel-stripe-webhooks-go | Spatie       

 SPATIE  

laravel-stripe-webhooks-go
==========================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-stripe-webhooks-go](https://spatie.be/docs/laravel-stripe-webhooks-go/v1)  Usage

 Version   v1      

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

  Usage    
- [ Introduction ](https://spatie.be/docs/laravel-stripe-webhooks-go/v1/introduction)
- [ Installation and setup ](https://spatie.be/docs/laravel-stripe-webhooks-go/v1/installation-and-setup)
- [ Usage ](https://spatie.be/docs/laravel-stripe-webhooks-go/v1/usage)
- [ Advanced usage ](https://spatie.be/docs/laravel-stripe-webhooks-go/v1/advanced-usage)
- [ Testing ](https://spatie.be/docs/laravel-stripe-webhooks-go/v1/testing)
- [ Releasing ](https://spatie.be/docs/laravel-stripe-webhooks-go/v1/releasing)
- [ Changelog ](https://spatie.be/docs/laravel-stripe-webhooks-go/v1/changelog)
- [ Support us ](https://spatie.be/docs/laravel-stripe-webhooks-go/v1/support-us)

 Usage
=====

###  On this page 

1. [ Handling webhook requests using jobs ](#content-handling-webhook-requests-using-jobs)
2. [ Handling webhook requests using events ](#content-handling-webhook-requests-using-events)
3. [ Validation and duplicates ](#content-validation-and-duplicates)

Stripe sends webhooks for several event types. You can find the [full list of event types](https://docs.stripe.com/api/events/types) in the Stripe documentation.

Stripe signs requests hitting the webhook URL. Invalid signatures do not reach storage, profiles, events or application jobs. Successful and profile-ignored requests receive HTTP 200 with `{"message":"ok"}` by default.

Storage, validation and synchronous dispatch failures return a failure response. The Webhook Client error renderer currently uses HTTP 500 for invalid signatures. Processing failures are saved on the stored call. With an asynchronous connection, a successful HTTP response means the first processing job was queued, not that business work has completed.

Handling webhook requests using jobs
--------------------------------------------------------------------------------------------------------------------------------------------------------------------

Define a job that does the work when a specific event type comes in. The job receives the stored webhook call, including its complete payload. Queue work that could delay the HTTP response.

Here is an application job:

```
type HandleInvoice struct {
    Call webhookclient.WebhookCall `json:"call"`
}

func (j HandleInvoice) Handle(ctx context.Context) error {
    invoice := j.Call.Payload.V["data"]
    return applyInvoice(ctx, invoice)
}
```

`applyInvoice` is your application logic. Import Webhook Client as `webhookclient`. Register the job mapping when installing the Stripe package:

```
stripewebhooks.New(func(cfg *stripewebhooks.Config) {
    job := webhookclient.NewQueuedJob(func(call webhookclient.WebhookCall) HandleInvoice {
        return HandleInvoice{Call: call}
    })
    cfg.Jobs["invoice.paid"] = &job
    cfg.Connection = "database"
    cfg.Queue = "payments"
})
```

The package registers the concrete job type as well as dispatching it. You do not need to list mapped jobs separately in `laravel.Options.Jobs`. Set up the selected queue connection, its tables and worker in your application.

Event-specific jobs take precedence over the default job. Dotted Stripe names and PHP-style underscore keys both work. Defining both forms for the same normalized name is an error rather than an ordering-dependent choice.

```
fallback := webhookclient.NewQueuedJob(func(call webhookclient.WebhookCall) HandleOtherEvent {
    return HandleOtherEvent{Call: call}
})
cfg.DefaultJob = &fallback
```

Without a matching job or a default job, the package still stores the event and dispatches its event. A nil mapping, such as `cfg.Jobs["customer.updated"] = nil`, emits the event but suppresses the fallback job for that type, matching an empty PHP job mapping. To suppress storage and processing entirely, use a custom profile. Make jobs idempotent because workers may retry them after a failure or timeout.

Handling webhook requests using events
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Instead of queueing jobs, you can listen to the events the package fires. PHP calls these events `stripe-webhooks::<name-of-the-event>`. Go listeners will receive a typed event with the original type and the application-selected stored model. Events run before a mapped job is dispatched.

```
func InvoicePaid(ctx context.Context, e stripewebhooks.Event[webhookclient.WebhookCall]) error {
    if e.Type != "invoice.paid" {
        return nil
    }
    return applyInvoice(ctx, e.Call.Payload.V["data"])
}

// In laravel.Options:
Listeners: []laravel.Listener{laravel.Listen(InvoicePaid)},
```

`e.Name()` returns the familiar name, such as `stripe-webhooks::invoice.paid`. Listeners subscribe to the typed event, not its string name. A failing listener stops the mapped job from being dispatched and saves a processing exception on the call. Use native queued listeners for slow work.

Validation and duplicates
-----------------------------------------------------------------------------------------------------------------------------------

Signature verification uses the original body and every `v1` signature in the `Stripe-Signature` header. The implemented validator accepts signatures exactly five minutes old or in the future and rejects timestamps beyond that window. Like Stripe's PHP library and the Rust port, it checks both directions.

The default duplicate policy looks for a stored Stripe event with the same event ID. Another webhook provider's event ID does not suppress a Stripe event. An ID in the signed body takes precedence over a query parameter. If the body has no ID, a query ID is used, matching PHP's input behavior. Request attributes are not used. Changing an event's type does not make a duplicate ID new. Empty or non-string IDs cannot be deduplicated by this profile.

Like PHP, lookup and insert are separate operations. They do not guarantee exactly-once processing under concurrency. Use an atomic storage policy and idempotent business jobs when your application needs stronger guarantees. Pruning stored calls also removes their duplicate history.

A missing, empty or non-string event type is a processing error. No Stripe event or mapped job is dispatched for it. The package's first-stage job stores that error on the call. Later failures in application jobs are handled by Laravel's failed-job system.

 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)
