Debugging Stripe Webhook Event
up vote
0
down vote
favorite
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 = StripeWebhook::constructEvent(
$payload, $sig_header, $endpoint_secret
);
} catch(UnexpectedValueException $e) {
// Invalid payload
http_response_code(400);
exit();
} catch(StripeErrorSignatureVerification $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 = StripeEvent::retrieve($event_id);
$invoice = $event->data->object;
// successful payment, both one time and recurring payments
if($event->type == 'charge.succeeded') {
$customer = StripeCustomer::retrieve($invoice->customer);
$email = $customer->email;
Mail::send('emails.new-userlike',
array(
'user' => $customer
), function($message) {
$message->from('info@friendships.me', 'friendships.me');
$message->to('info@friendships.me')->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");
php stripe-payments webhooks
add a comment |
up vote
0
down vote
favorite
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 = StripeWebhook::constructEvent(
$payload, $sig_header, $endpoint_secret
);
} catch(UnexpectedValueException $e) {
// Invalid payload
http_response_code(400);
exit();
} catch(StripeErrorSignatureVerification $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 = StripeEvent::retrieve($event_id);
$invoice = $event->data->object;
// successful payment, both one time and recurring payments
if($event->type == 'charge.succeeded') {
$customer = StripeCustomer::retrieve($invoice->customer);
$email = $customer->email;
Mail::send('emails.new-userlike',
array(
'user' => $customer
), function($message) {
$message->from('info@friendships.me', 'friendships.me');
$message->to('info@friendships.me')->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");
php stripe-payments webhooks
First, try to find out why it caused 500. You can send some dummy webhook event JSON to dev server from your own machine just for debug purpose. Then figure out the problem.
– Zico
Nov 12 at 10:09
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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 = StripeWebhook::constructEvent(
$payload, $sig_header, $endpoint_secret
);
} catch(UnexpectedValueException $e) {
// Invalid payload
http_response_code(400);
exit();
} catch(StripeErrorSignatureVerification $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 = StripeEvent::retrieve($event_id);
$invoice = $event->data->object;
// successful payment, both one time and recurring payments
if($event->type == 'charge.succeeded') {
$customer = StripeCustomer::retrieve($invoice->customer);
$email = $customer->email;
Mail::send('emails.new-userlike',
array(
'user' => $customer
), function($message) {
$message->from('info@friendships.me', 'friendships.me');
$message->to('info@friendships.me')->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");
php stripe-payments webhooks
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 = StripeWebhook::constructEvent(
$payload, $sig_header, $endpoint_secret
);
} catch(UnexpectedValueException $e) {
// Invalid payload
http_response_code(400);
exit();
} catch(StripeErrorSignatureVerification $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 = StripeEvent::retrieve($event_id);
$invoice = $event->data->object;
// successful payment, both one time and recurring payments
if($event->type == 'charge.succeeded') {
$customer = StripeCustomer::retrieve($invoice->customer);
$email = $customer->email;
Mail::send('emails.new-userlike',
array(
'user' => $customer
), function($message) {
$message->from('info@friendships.me', 'friendships.me');
$message->to('info@friendships.me')->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");
php stripe-payments webhooks
php stripe-payments webhooks
asked Nov 11 at 15:04
Andreas
132110
132110
First, try to find out why it caused 500. You can send some dummy webhook event JSON to dev server from your own machine just for debug purpose. Then figure out the problem.
– Zico
Nov 12 at 10:09
add a comment |
First, try to find out why it caused 500. You can send some dummy webhook event JSON to dev server from your own machine just for debug purpose. Then figure out the problem.
– Zico
Nov 12 at 10:09
First, try to find out why it caused 500. You can send some dummy webhook event JSON to dev server from your own machine just for debug purpose. Then figure out the problem.
– Zico
Nov 12 at 10:09
First, try to find out why it caused 500. You can send some dummy webhook event JSON to dev server from your own machine just for debug purpose. Then figure out the problem.
– Zico
Nov 12 at 10:09
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
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)
The basic handler worked, but I did not receive any response. I got it working now, though. Before I open another issue: am I not able to send mails with Mail::send? I use this across the whole system, but it throws an error 500 when trying within the webhook. A simple PHP Mail() works fine.
– Andreas
Nov 11 at 18:34
Stripe's SDK doesn't include a Mail class AFAIK. Are you using a framework like Laravel or a SDK from another company? laravel.com/docs/5.2/mail#sending-mail
– duck
Nov 11 at 19:47
Everything works fine now, thanks! :) I had a bunch of errors but did not think of the error.log :/
– Andreas
Nov 12 at 18:23
Woohoo! Glad to hear all is well :)
– duck
Nov 12 at 21:35
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
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)
The basic handler worked, but I did not receive any response. I got it working now, though. Before I open another issue: am I not able to send mails with Mail::send? I use this across the whole system, but it throws an error 500 when trying within the webhook. A simple PHP Mail() works fine.
– Andreas
Nov 11 at 18:34
Stripe's SDK doesn't include a Mail class AFAIK. Are you using a framework like Laravel or a SDK from another company? laravel.com/docs/5.2/mail#sending-mail
– duck
Nov 11 at 19:47
Everything works fine now, thanks! :) I had a bunch of errors but did not think of the error.log :/
– Andreas
Nov 12 at 18:23
Woohoo! Glad to hear all is well :)
– duck
Nov 12 at 21:35
add a comment |
up vote
1
down vote
accepted
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)
The basic handler worked, but I did not receive any response. I got it working now, though. Before I open another issue: am I not able to send mails with Mail::send? I use this across the whole system, but it throws an error 500 when trying within the webhook. A simple PHP Mail() works fine.
– Andreas
Nov 11 at 18:34
Stripe's SDK doesn't include a Mail class AFAIK. Are you using a framework like Laravel or a SDK from another company? laravel.com/docs/5.2/mail#sending-mail
– duck
Nov 11 at 19:47
Everything works fine now, thanks! :) I had a bunch of errors but did not think of the error.log :/
– Andreas
Nov 12 at 18:23
Woohoo! Glad to hear all is well :)
– duck
Nov 12 at 21:35
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
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)
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)
answered Nov 11 at 17:54
duck
1,694513
1,694513
The basic handler worked, but I did not receive any response. I got it working now, though. Before I open another issue: am I not able to send mails with Mail::send? I use this across the whole system, but it throws an error 500 when trying within the webhook. A simple PHP Mail() works fine.
– Andreas
Nov 11 at 18:34
Stripe's SDK doesn't include a Mail class AFAIK. Are you using a framework like Laravel or a SDK from another company? laravel.com/docs/5.2/mail#sending-mail
– duck
Nov 11 at 19:47
Everything works fine now, thanks! :) I had a bunch of errors but did not think of the error.log :/
– Andreas
Nov 12 at 18:23
Woohoo! Glad to hear all is well :)
– duck
Nov 12 at 21:35
add a comment |
The basic handler worked, but I did not receive any response. I got it working now, though. Before I open another issue: am I not able to send mails with Mail::send? I use this across the whole system, but it throws an error 500 when trying within the webhook. A simple PHP Mail() works fine.
– Andreas
Nov 11 at 18:34
Stripe's SDK doesn't include a Mail class AFAIK. Are you using a framework like Laravel or a SDK from another company? laravel.com/docs/5.2/mail#sending-mail
– duck
Nov 11 at 19:47
Everything works fine now, thanks! :) I had a bunch of errors but did not think of the error.log :/
– Andreas
Nov 12 at 18:23
Woohoo! Glad to hear all is well :)
– duck
Nov 12 at 21:35
The basic handler worked, but I did not receive any response. I got it working now, though. Before I open another issue: am I not able to send mails with Mail::send? I use this across the whole system, but it throws an error 500 when trying within the webhook. A simple PHP Mail() works fine.
– Andreas
Nov 11 at 18:34
The basic handler worked, but I did not receive any response. I got it working now, though. Before I open another issue: am I not able to send mails with Mail::send? I use this across the whole system, but it throws an error 500 when trying within the webhook. A simple PHP Mail() works fine.
– Andreas
Nov 11 at 18:34
Stripe's SDK doesn't include a Mail class AFAIK. Are you using a framework like Laravel or a SDK from another company? laravel.com/docs/5.2/mail#sending-mail
– duck
Nov 11 at 19:47
Stripe's SDK doesn't include a Mail class AFAIK. Are you using a framework like Laravel or a SDK from another company? laravel.com/docs/5.2/mail#sending-mail
– duck
Nov 11 at 19:47
Everything works fine now, thanks! :) I had a bunch of errors but did not think of the error.log :/
– Andreas
Nov 12 at 18:23
Everything works fine now, thanks! :) I had a bunch of errors but did not think of the error.log :/
– Andreas
Nov 12 at 18:23
Woohoo! Glad to hear all is well :)
– duck
Nov 12 at 21:35
Woohoo! Glad to hear all is well :)
– duck
Nov 12 at 21:35
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250022%2fdebugging-stripe-webhook-event%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
First, try to find out why it caused 500. You can send some dummy webhook event JSON to dev server from your own machine just for debug purpose. Then figure out the problem.
– Zico
Nov 12 at 10:09