Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

lat4732's avatar
Level 12

Defining custom actions on webhook recieved - Laravel Cashier (Stripe)

Hello everyone!

How can I add custom logic to handled webhooks from Stripe using Laravel Cashier? Cashier is handling only necessary events with already based logic (updating database etc..) but how can I add a custom logic to it and where are stored the files?

0 likes
10 replies
lat4732's avatar
Level 12

@Tippin Okay, but for example I register the following event:

if ($event->payload['type'] === 'charge.failed') {
	...........
}

How do I know which customer this applies to? How can I see what actually this webhook contains? Does it contain customer_id and stuff like that so I can take actions for a specific user?

Tippin's avatar

@crypt.001111101

Both events contain the full payload of the Stripe webhook.

So everything stripe posted to your webhook, and shows in the stripe dash, is available. So yes, you will have all the data including the stripe_id which is really the stripe customer. aka cus_SomeRandomCharacters

lat4732's avatar
Level 12

@Tippin Okay, but where can I see the structure of the array/object that the webhook sends? This documentation isn't understandable enough... The only thing I need is to get that customer_id from the webhook recieved so I can build the logic what to do with the user after receiving the webhook.

Tippin's avatar

@crypt.001111101 Because it is not on laravel to document all the possible data a 3rd party may send them. Cashier is built on top of the stripe php client:

https://github.com/stripe/stripe-php

You either need to read through the stripe API docs:

https://stripe.com/docs/api

Or, use the other tools available for you, such as the stripe CLI to hit your webhook locally, and you can use something like telescope to inspect the payloads.

https://stripe.com/docs/stripe-cli

Or, within your stripe dashboard under developers, you can see every request and payload stripe sends to your webhook. Options.

lat4732's avatar
Level 12

@Tippin I've read everything you suggested and nothing helps me out that's why I'm posting this thread. So can you help me out how to retrieve the customer_id or not? The things you have mentioned so far are not helpful regards to my questions.

Tippin's avatar
Tippin
Best Answer
Level 13

@crypt.001111101 I am not farming post. I am trying to let you know there are multiple ways to find out what the payload stripe sends you is, including just setting up the event listener and logging the data, such as:

class StripeEventListener
{
    public function handle(WebhookReceived $event)
    {
        dump($event->payload);
        info($event->payload);
    }
}

I find it rather nice to go into my stripe dashboard under webhooks and see the data in their event explorer that were sent.

What you are asking for is part of the payload...the customer which is the ID cashier stores on your users table for stripe_id. example payload:

{
  "id": "evt_id",
  "object": "event",
  "api_version": "2020-08-27",
  "created": 1639892129,
  "data": {
      ...
      "customer": "cus_Ab1Cd2E4hY", // You want this
      ...
  },
  "livemode": false,
  "type": "customer.subscription.updated"
}

You can take that customer and use cashier to find via Billable

use Laravel\Cashier\Cashier;

$user = Cashier::findBillable($stripeId); //cus_Ab1Cd2E4hY

Also I find it helpful to check out the cashier controller for how they handle the included events:

https://github.com/laravel/cashier-stripe/blob/13.x/src/Http/Controllers/WebhookController.php#L68

1 like
lat4732's avatar
Level 12

@Tippin So if I understood correct I'm retrieving customer's id with:

$event->payload->data->customer

right?

Tippin's avatar

@crypt.001111101

$event->payload['data']['object']['customer'];

Pretty sure you will be dealing with an array as the payload. My example above of a real payload was cut short so I removed the parent object key.

lat4732's avatar
Level 12

@Tippin That's what I call a helpful answer! Thank you very much for the help and I apologize if I was rude. Have a nice day!

1 like

Please or to participate in this conversation.