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... ._.



enter image description here



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");


enter image description here










share|improve this question





















  • 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














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... ._.



enter image description here



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");


enter image description here










share|improve this question





















  • 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












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... ._.



enter image description here



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");


enter image description here










share|improve this question













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... ._.



enter image description here



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");


enter image description here







php stripe-payments webhooks






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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
















  • 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












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)






share|improve this answer




















  • 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











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















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

























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)






share|improve this answer




















  • 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















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)






share|improve this answer




















  • 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













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)






share|improve this answer












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)







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 17:54









duck

1,694613




1,694613











  • 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











  • 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


















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto