Search code examples
phpstripe-paymentslaravel-5webhooksngrok

Stripe Cashier Webhooks


I'm looking for clarification of the use of the Webhooks Controller in Stripe Cashier as described in the Laravel Docs as I can't confirm that my application is receiving the webhook events:

http://laravel.com/docs/5.0/billing#handling-failed-payments

The docs advise to point a route to the webhook controller like this:

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

The URI in the route must be modified to be the URI in my Stripe settings. In the testing environment, I'm using ngrok to expose my local server.

What I am looking for clarification on is what the URI should be for both testing and production. For testing, should I just use the ngrok forwarding url (eg. http://3a4bfceb.ngrok.com), or do I need to have a script in a public directory to handle the webhook event from Stripe.

I'm not sure if the controller is able to handle receiving the data with the handlePayload function or if I need to add an additional php script (eg. webhook.php) with something as described in the Stripe docs such as:

// Set your secret key: remember to change this to your live secret key in production
// See your keys here https://dashboard.stripe.com/account
Stripe::setApiKey("sk_test_xxxxxxxxxxxxxxxxxxxxxxx");

// Retrieve the request's body and parse it as JSON
$input = @file_get_contents("php://input");
$event_json = json_decode($input);

// Do something with $event_json

http_response_code(200); // PHP 5.4 or greater

If anyone can help with the testing and production URI and whether or not additional handling script is required beyond what Cashier's WebhookController.php offers, I would appreciate it.


Solution

  • With respect to the URI the local/test and production URIs are something like (assuming ngrok is used):

    Local/Test: http://3a4bfceb.ngrok.com/laravel/public/stripewebhooks

    Production: http://website.com/stripewebhooks

    The route in both cases would be:

    Route::post('stripewebhooks','Laravel\Cashier\WebhookController@handleWebhook');
    

    WebhookController.php (which is part of the Cashier package) handles all of the incoming events, so there is no need to create a file like stripewebhooks.php containing file_get_contents a 200 response code as described in the Stripe docs for an implementation without Cashier.