Usage | laravel-stripe-webhooks-rust | Spatie       

 SPATIE  

laravel-stripe-webhooks-rust
============================

spatie.be/open-source

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

 Version   v1      

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

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

 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 a JSON event with an `id`, a `type` and a `data.object` payload. See [Stripe's event types](https://docs.stripe.com/api/events/types) for the complete list.

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

Map event types to your own queueable jobs. A job receives the stored webhook call, including the event payload. Keep payment and subscription logic in these jobs, not in the HTTP endpoint.

```
use spatie_laravel_stripe_webhooks::{Config, ProcessStripeWebhookJob};
use spatie_laravel_stripe_webhooks::webhook_client::{QueuedJob, WebhookCall};

#[derive(laravel::queue::Queueable)]
pub struct HandleInvoice { pub webhook_call: WebhookCall }

#[laravel::queue::job]
impl HandleInvoice {
    async fn handle(self) -> laravel::Result<()> {
        let invoice = &self.webhook_call.payload["data"]["object"];
        // Apply your application's invoice logic here.
        let _ = invoice;
        Ok(())
    }
}

let mut config = Config::new("whsec_example");
config.jobs = ProcessStripeWebhookJob::default().job(
    "invoice.paid",
    QueuedJob::new(|webhook_call| HandleInvoice { webhook_call })
        .on_connection("database")
        .on_queue("payments"),
);
```

Set this `Config` in the application's `with_config` callback. Dotted event names and underscore keys such as `invoice_paid` both work. Configure the selected queue connection in your application and run its worker; the package does not create queue-driver tables.

A default job can handle event types without an explicit mapping. Without a matching or default job, retain the call and dispatch its event without running an application job.

Use `.default_job(QueuedJob::new(...))` on `ProcessStripeWebhookJob` to configure the fallback. To replace processing entirely, construct a webhook-client `WebhookProcessor` with your own `ProcessWebhookJob` implementation.

Make your jobs idempotent. A queue worker may retry a job after a timeout or failure even when HTTP delivery duplicates are filtered.

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

Listen for Stripe events when several parts of your application need to react to the same webhook. The event carries the stored webhook call and its original Stripe event type. An event is dispatched even if no job is configured for that type.

```
use laravel::{Application, event::Event};
use spatie_laravel_stripe_webhooks::StripeWebhookEvent;

async fn invoice_paid(event: Event<StripeWebhookEvent>) {
    if event.event_type == "invoice.paid" {
        let invoice = &event.webhook_call.payload["data"]["object"];
        // React to the paid invoice.
        let _ = invoice;
    }
}

let app = Application::configure()
    .with_events(|events, _| { events.listen(invoice_paid); })
    .create();
```

`StripeWebhookEvent::name()` returns the familiar name, such as `stripe-webhooks::invoice.paid`. Laravel listeners subscribe to the typed `StripeWebhookEvent`; filter by `event_type` inside the listener. Events run before the mapped job is dispatched. A failing listener stops that dispatch and records a processing error on the call.

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

Verification uses the original request body and `Stripe-Signature` header. Invalid signatures must not reach storage or your handlers. Valid calls are stored before processing so failures can be investigated and retried.

Stripe can deliver the same event more than once. Duplicate filtering uses the event ID in the signed JSON body, not a query parameter or route attribute. Other webhook providers' IDs must not suppress Stripe calls.

The default profile checks `webhook_calls` under the name `stripe`. This prevents sequential duplicates, not concurrent races between the lookup and insert. Use a custom model/profile with an atomic claim or database constraint when stronger delivery deduplication is required. Events without a non-empty string ID cannot be deduplicated by this profile.

Do not treat delivery deduplication as a transaction covering your business logic. Use database constraints or transactions for effects such as crediting an account. Pruning calls also removes their duplicate-detection history.

Successful and profile-ignored requests receive HTTP 200 with `{"message":"ok"}`. Invalid signatures return HTTP 400 without storage. Dispatch or storage errors return a failure response; processing errors are saved on the stored call. A missing, empty or non-string event type is a processing error, not an event or job dispatch.

 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)
