Nothing happens when i handle stripewebook with spatie package

Viewed 958

I use spatie/laravel-stripe-webhook for handle the webhook sending from stripe. I use stripe CLI for test with the following command :

stripe listen --events checkout.session.completed --forward-to localhost:8000/api/webhook/checkout

Everything going well and I have this response in my console :

> Ready! Your webhook signing secret is whsec_mSz72pLPb4B******rfvvYajj641iqZ7 (^C to quit)
2020-06-10 14:12:07   --> \checkout.session.completed\ [\evt_1GsSmjLG****Fak7OpFNC8c2\]
2020-06-10 14:12:07  <--  [200] POST http://localhost:8000/api/webhook/checkout [\evt_1GsSmj*****OFak7OpFNC8c2\]

but... nothing happened... I have no error in my laravel.log, anything...

I following all the readme file in github Spatie and I doing that :

Routes/api.php :

Route::stripeWebhooks('webhook/checkout');

App/Providers/EventServiceProvider.php :

protected $listen = [
        Registered::class => [
            SendEmailVerificationNotification::class,
        ],
        'stripe-webhooks::checkout.session.completed' => [
            App\Listeners\HandleSubscriptions::class
        ]
    ];

I have to disabled :

Parent::boot();

because if enabled this one, I have a throw exceptions :

[object] (ReflectionException(code: -1): Class App\\Providers\\App\\Listeners\\HandleSubscriptions does not exist at /var/www/private/bnb-v2/bnb-back/vendor/laravel/framework/src/Illuminate/Container/Container.php:803)

and I can't handle the stripeWebhook.

App/Listeners/HandleSubscriptions.php :

class HandleSubscriptions implements ShouldQueue
{
    /**
     * Handle the event.
     *
     * @param  object  $event
     * @return void
     */
    public function handle(WebhookCall $webhookCall)
    {
        // you can access the payload of the webhook call with `$webhookCall->payload`
        $payload = $webhookCall->payload;

    //     // Handle the event
        switch ($payload->type) {
            case 'checkout.session.completed':
                $sessions = $payload->data->object; 
                SubscriptionController::handle_checkout_session($sessions);
                break;
            case 'checkout.session.async_payment_succeeded':
                $paymentSuccess = $payload->data->object; // contains a \Stripe\PaymentMethod
                // Then define and call a method to handle the successful attachment of a PaymentMethod.
                // handlePaymentMethodAttached($paymentMethod);
                break;
            case 'customer.created':
                $customer = $payload->data->object;
            break;
                // ... handle other event types
            default:
                // Unexpected event type
                http_response_code(400);
                exit();
        }

        http_response_code(200);
    }
}

If someone have a idea...

2 Answers

You are asking Stripe to contact localhost so you can test your code locally, but because it is not a public URL Stripe can't see it and they can't send you a request. Using "localhost" will only work on your machine.

To test your code with Stripe webhooks locally, you can use expose.sh to expose your local server using a public HTTPS URL which Stripe can use.

Install expose.sh

For Mac or Linux, go to Expose.sh and copy/paste the installation code shown into a terminal.

For Windows go to Expose.sh, download the binary and put it somewhere in your PATH.

Expose your local API server to the web

Start your API server. Then run expose <port> where port is the port your API server is running on, like 80 or 8080.

Expose.sh will generate a random public expose.sh URL. You'll see output like

https://s3rh.expose.sh is forwarding to localhost:80
http://s3rh.expose.sh is forwarding to localhost:80

Then you can get Stripe to use the public HTTPS URL, which will forward to your locally running server.

Once you have the URL, you can then run stripe listen --events checkout.session.completed --forward-to <https expose.sh url> substituting with the URL from the output, which will be similar to https://s3rh.expose.sh.

I've written a full guide here

Disclaimer: I built expose.sh

I was having a similar issue that I finally managed to fix after a few wasted hours. This issue is extra annoying to debug because nothing gets logged in Laravel's logs and specifying the format completely wrong returns a successful status code in the Stripe CLI.

The problem, at least in my case, was that the webhook event in stripe-webhook.php needs to be completely delimited by underscores:

'jobs' => [
    "payment_intent_succeeded" => \App\Jobs\StripeWebhooks\PaymentIntentSucceeded::class,
    // 'source_chargeable' => \App\Jobs\StripeWebhooks\HandleChargeableSource::class,
    // 'charge_failed' => \App\Jobs\StripeWebhooks\HandleFailedCharge::class,
],
Related