Léo Coco
Léo Coco

Reputation: 4282

Laravel - Stripe - Ultrahook - Webhook are not caught

I'm using Laravel 5.4 and Laravel Cashier. I would like to be able to catch Stripe webhooks in my localhost:8888

To do so I installed ultrahook and I started it like this

Ultrahook

http://stripe.leococo.ultrahook.com -> http://localhost:8888/stripe/webhook

Laravel routes

Route::post('stripe/webhook', '\Laravel\Cashier\Http\Controllers\WebhookController@handleWebhook');

Stripe Webhook configuration

http://stripe.leococo.ultrahook.com

Problem

When I send a webhook from Stripe I get Test webhook sent successfully

In the terminal ultrahook I get this

[2017-05-31 19:26:04] POST http://localhost:8888/stripe/webhook - 200

But it seems the handleWebhook function is not triggered. It does not stop on a break point neither die('test')

I tried php artisan route:clear php artisan config:clear. I don't know wether it is normal or not, but I do not see anything in the network section in the Chrome Inspector

Upvotes: 1

Views: 1098

Answers (1)

Yo1
Yo1

Reputation: 181

Add the following line in your .env

CASHIER_ENV=testing

Laravel/Cashier checks if your call to the webhook has a valid event id. To verify this, eventExistsOnStripe calls back stripe servers with this event id to check its existence.

Here is the main webhook entry point from Laravel/Cashier 7.0:

public function handleWebhook(Request $request)
{
    $payload = json_decode($request->getContent(), true);

    if (! $this->isInTestingEnvironment() && ! $this->eventExistsOnStripe($payload['id'])) {
        return;
    }

    $method = 'handle'.studly_case(str_replace('.', '_', $payload['type']));

    if (method_exists($this, $method)) {
        return $this->{$method}($payload);
    } else {
        return $this->missingMethod();
    }
}

isInTestingEnvironment just check whether we are in a testing environnment or not : Thank you Cpt Obvious :)

protected function isInTestingEnvironment()
{
    return getenv('CASHIER_ENV') === 'testing';
}

Upvotes: 1

Related Questions