Reputation: 333
I'm trying to get my application to accept and read Stripe webhooks but I'm having a somewhat unpredictable 404 problem. I have a PHP file that handles the webhook requests from Stripe. It definitely exists. I can navigate to it, and I generally get a successful 200 response in the webhook details when the only code in the file is:
$input = @file_get_contents("php://input");
$event_json = json_decode($input);
http_response_code(200);
However, when I add more code (and the code that I add is too complicated to post here) I start getting 404s. I've verified that the extra code isn't generating any Fatal errors or even Warnings/Notices in the error log.
Also, with the same code, when I send multiple test webhooks from https://dashboard.stripe.com/account/webhooks about half of them will say "Test webhook sent successfully" and about half will say "Test webhook error: 404."
Does anyone know what could cause these 404s? Memory problems? Unlogged errors (don't see how this could happen, but who knows)? DNS errors? Stripe is out to make me crazy?
Thanks.
Upvotes: 1
Views: 6935
Reputation: 333
Turns out it was a DNS error. I have a distributed hosting environment and 2 of the 3 IP addresses were returning 404s. However I was locked in locally to the 1 working IP address. Changed my A records to the one working IP (as the site in question is a development site, not a live site and doesn't need multiple fallbacks) and that solved the problem. Stupid mistake.
Upvotes: 0
Reputation: 335
Here is the code from my working webhook. Mine always produces 200 when hit by Stripe. Hope it is helpful:
include_once('path/to/Stripe/Stripe.php');
// Set your secret key: remember to change this to your live secret key in production
if($_SERVER['HTTP_HOST'] == 'example.com')
{
Stripe::setApiKey("sk_live_my_key");
}
else
{
Stripe::setApiKey("sk_test_my_key");
}
// Retrieve the request's body and parse it as JSON
$body = @file_get_contents('php://input');
$event_json = json_decode($body, true);
if(!empty($event_json))
{
//do stuff...
echo '<pre>'; print_r($event_json); die();
}
die('success');
It's worth noting that I do have a die at the end.
Upvotes: 1
Reputation: 837
If you can save your account settings with the webhook there, you can run a test charge, go to https://dashboard.stripe.com/events, open an event, and go into the webhook details at the bottom. That'll let you see the response your server sent back to Stripe (if any).
If that doesn't have anything useful in there, I'd try to trim down your full code until you have as small an example as possible that still 404s. That may make the problem easier to find, and if not, you can post that code.
Upvotes: 1