'Debugging Stripe Webhook Event
I spend my weekend trying to figure out Stripe Webhooks, but still haven't found a way to debug the response. This is my current code:
http_response_code(200);
// set stripe api key
Stripe::setApiKey(env('STRIPE_SECRET'));
$endpoint_secret = 'whsec_XXX';
$payload = @file_get_contents('php://input');
$sig_header = $_SERVER['HTTP_STRIPE_SIGNATURE'];
$event_json = json_decode($payload);
try {
$event = \Stripe\Webhook::constructEvent(
$payload, $sig_header, $endpoint_secret
);
} catch(\UnexpectedValueException $e) {
// Invalid payload
http_response_code(400);
exit();
} catch(\Stripe\Error\SignatureVerification $e) {
// Invalid signature
http_response_code(400);
exit();
}
$event_id = $event_json->id;
if(isset($event_json->id)) {
try {
// to verify this is a real event, we re-retrieve the event from Stripe
$event = \Stripe\Event::retrieve($event_id);
$invoice = $event->data->object;
// successful payment, both one time and recurring payments
if($event->type == 'charge.succeeded') {
$customer = \Stripe\Customer::retrieve($invoice->customer);
$email = $customer->email;
\Mail::send('emails.new-userlike',
array(
'user' => $customer
), function($message) {
$message->from('[email protected]', 'friendships.me');
$message->to('[email protected]')->subject('Test');
});
}
// failed payment
if($event->type == 'charge.failed') {
// send a failed payment notice email here
}
} catch (Exception $e) {
// something failed, perhaps log a notice or email the site admin
}
}
This results in a error 500 so far... ._.
But that is not the problem, I had it working already. The thing is, I need to check a SEPA subscription for a charge.failed
or charge.succeeded
response and only on a successful charge, create the subscription.
How do I access a subscription-id within this webhook? Or better, how do I debug the response? Because even this does not sent a response:
http_response_code(200);
$payload = @file_get_contents('php://input');
$event_json = json_decode($payload);
print_r("test");
Solution 1:[1]
I would start with the simplest possible webhook handler first
<?php
// Retrieve the request's body and parse it as JSON:
$input = @file_get_contents('php://input');
$event = json_decode($input);
http_response_code(200); // PHP 5.4 or greater
// echo the event id, evt_xxxyyyzzz
echo $event->id;
if($event->type == "charge.succeeded") {
// you want the id of the charge rather than the event, ch_xxxyyzz
echo $event->data->object->id;
}
?>
When you use the "send test webhook" function in your Dashboard, you should then see something like evt_0000000
in the response (and ch_000000
if the event type is charge.succeeded
).
If you're still getting 500
errors that means something is incorrectly configured on your server, and you can get the full error in your web server's error.log
(try looking in /var/log or your server's web dashboard)
Solution 2:[2]
You can try adding something like a man-in-the-middle if you want to easily debug webhooks. For example, https://www.reliablewebhook.com/ allow to receive any webhooks, log them and relay them to your localhost or other web address
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | duck |
Solution 2 | Farmaris |