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

RobTheDev's avatar

Stripe extend Laravel subscription creation webhook handling

Hello,

I am looking for a way to extend what the Cashier package for Stripe does when handling a subscription creation in the src/Http/Controllers/WebhookController.php.

Essentially what happens is something like this:

$subscription = $user->subscriptions()->create([
                    'name' => $data['metadata']['name'] ?? $this->newSubscriptionName($payload),
                    'stripe_id' => $data['id'],
                    'stripe_status' => $data['status'],
                    'stripe_price' => $isSinglePrice ? $firstItem['price']['id'] : null,
                    'quantity' => $isSinglePrice && isset($firstItem['quantity']) ? $firstItem['quantity'] : null,
                    'trial_ends_at' => $trialEndsAt,
                    'ends_at' => null,
                ]);

                foreach ($data['items']['data'] as $item) {
                    $subscription->items()->create([
                        'stripe_id' => $item['id'],
                        'stripe_product' => $item['price']['product'],
                        'stripe_price' => $item['price']['id'],
                        'quantity' => $item['quantity'] ?? null,
                    ]);
                }

Now I would like to extend that to add more fields from Stripe subscription object. I would like to have current period start and end dates as I need to report usage.

My plan is as follows:

  1. Drag in that extra period data.
  2. Collect usage locally.
  3. Last day of the billing period fire API calls to update the usage on stripe. Maybe a day before just to be on the safe side. Not important right now.

For that I would like to have those dates. Now I can see how I could possibly just write my own controller and extend Webhookone and then override the route and all that. However I can't help but wonder if there is a less messy or annoying way of doing that?

The only other thing I can think of is if I would ditch Cashier (which seems to be super tempting right now) and create my own integration.

What do you think?

Rob

0 likes
3 replies
illuminatixs's avatar

Hi Rob,

If I understood your question correctly: You can create a custom class that extends the WebhookController.php, if you define the protected function which I assume is: handleCustomerSubscriptionCreated you can simply do:

protected function handleCustomerSubscriptionCreated(array $payload)
{
	$response = parent::handleCustomerSubscriptionCreated($payload);
	
	// Your custom logic here

	return $response;
}

Then overwrite the route that is currently pointed to the parent controller and point it to your child controller instead.

RobTheDev's avatar

@illuminatixs Hey,

Thanks for your answer. This is what I did straight away. I was just wondering if that was the way to go but by the looks of it that is it.

illuminatixs's avatar

@RobTheDev I think this would be the cleanest and most readable way to achieve your desired outcome, yes.

1 like

Please or to participate in this conversation.