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

Mandrizzy's avatar

Laravel stripe and Webhooks

I'm completely stumped and google searching isn't helping at all. I have recently started with stripe webhooks and I have followed the Laravel documentation but still having troubles. First I added the stripe url to exempt it from csrf protection


use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__.'/../routes/web.php',
        commands: __DIR__.'/../routes/console.php',
        health: '/up',
    )
    ->withMiddleware(function (Middleware $middleware) {

        $middleware->validateCsrfTokens(except:['stripe_event_webhook','stripe/*']);
    })
    ->withExceptions(function (Exceptions $exceptions) {
        //
    })->create();

Then I created an event listener to listen to stripe event with the payload

In stripe cli it hits the endpoint payment_intent.created gives 200 OK, payment_intent.succeeded 200OK, charge.succeeded 200 OK. checkout.session.completed 500 server error I don't know what else to do tried so many times even on a live production server it hits the correct endpoint but ntn even in my routes file I tried


Route::post('/stripe_event_webhook', [StripeEventHandlerController::class, 'event']);

Then I tried creating a controller


<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use Laravel\Cashier\Events\WebhookReceived;

class StripeEventHandlerController extends Controller
{
    public function event  (Request $request)
    {
            WebhookReceived::dispatch($request);
	}
}

But it still doesn't work as you can see in the event listener I want to update some records in the database but the record stays the same which tell me the event isn't being dispatched or the listener not working can someone help really hit a hard roadblock?

0 likes
4 replies
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

To troubleshoot and resolve the issue with your Stripe webhook integration in Laravel, let's go through a few steps to ensure everything is set up correctly:

  1. CSRF Protection: Ensure that your CSRF middleware is correctly configured to exempt the Stripe webhook route. It looks like you are using a custom configuration setup. Make sure that the route /stripe_event_webhook is correctly exempted from CSRF protection.

  2. Route Configuration: Ensure that your route is correctly defined in your web.php or api.php routes file. Since webhooks are typically API calls, it's better to place them in api.php:

    use App\Http\Controllers\StripeEventHandlerController;
    
    Route::post('/stripe_event_webhook', [StripeEventHandlerController::class, 'event']);
    
  3. Event Listener: Make sure your event listener is correctly registered. In Laravel, you typically register event listeners in the EventServiceProvider. Check your EventServiceProvider.php:

    protected $listen = [
        \Laravel\Cashier\Events\WebhookReceived::class => [
            \App\Listeners\StripeEventListener::class,
        ],
    ];
    
  4. Debugging the Listener: Add some logging to your listener to ensure it's being triggered and to help identify where it might be failing:

    use Illuminate\Support\Facades\Log;
    
    public function handle(WebhookReceived $event): void
    {
        Log::info('Webhook received', ['payload' => $event->payload]);
    
        if ($event->payload['type'] === "checkout.session.completed") {
            Log::info('Checkout session completed event received');
    
            $website = auth()->user()->websites->where('payment_completed', false)->last();
            if ($website) {
                $website->payment_completed = true;
                $website->save();
                Log::info('Website payment status updated');
            } else {
                Log::warning('No website found for user');
            }
        }
    }
    
  5. Authentication Issue: The use of auth()->user() in a webhook context might be problematic because webhooks are not tied to a user session. You should retrieve the user based on the data in the webhook payload instead of using auth().

  6. Error Handling: Check your logs for any errors that might be occurring when the checkout.session.completed event is received. The 500 error indicates something is going wrong in your code execution.

  7. Testing with Stripe CLI: Use the Stripe CLI to send test events to your webhook endpoint and monitor the logs to see if the events are being received and processed correctly.

By following these steps, you should be able to identify and resolve the issue with your Stripe webhook integration. If you continue to experience issues, consider checking the Laravel and Stripe documentation for any updates or additional troubleshooting tips.

1 like
Snapey's avatar

why not just handle the event in the controller? Thats what I always do.

1 like
Mandrizzy's avatar

Thanks for the reply guys I finally got it to work by using the AI Lary advice and checking the logs for the errors

1 like

Please or to participate in this conversation.