#NormanLove - webhooks

1 messages ยท Page 1 of 1 (latest)

frosty warren
#

What is the error message you are receiving?

glass wedge
#

11-Jan-2022 22:41:37 UTC] variable area accessed
[11-Jan-2022 22:41:37 UTC] PHP Fatal error: Uncaught Error: Class 'Stripe\Webhook' not found in D:\wamp\www\income\webhook.php:24
Stack trace:
#0 {main}
thrown in D:\wamp\www\income\webhook.php on line 24
[11-Jan-2022 22:41:37 UTC] variable area accessed
[11-Jan-2022 22:41:37 UTC] PHP Fatal error: Uncaught Error: Class 'Stripe\Webhook' not found in D:\wamp\www\income\webhook.php:24
Stack trace:
#0 {main}
thrown in D:\wamp\www\income\webhook.php on line 24
[11-Jan-2022 22:41:37 UTC] variable area accessed
[11-Jan-2022 22:41:37 UTC] PHP Fatal error: Uncaught Error: Class 'Stripe\Webhook' not found in D:\wamp\www\income\webhook.php:24
Stack trace:
#0 {main}
thrown in D:\wamp\www\income\webhook.php on line 24

#

basically its expecting and cant find \Stripe\Webhook

#

the webhooks code i got from stripe calls that method and no one told me where to get it. I thought stripe would provide it

#

Im doing a local stripe cli thing and I trigger my webhook and it actually connects but does nothing , but as the log shows thats the why....

#

here is the webhook.php code and you can see line 24

#

<?php
// webhook.php
//
// Use this sample code to handle webhook events in your integration.
//
// 1) Paste this code into a new file (webhook.php)
//
// 2) Install dependencies
// composer require stripe/stripe-php
//
// 3) Run the server on http://localhost:4242
// php -S localhost:4242

require 'vendor/autoload.php';

// This is your Stripe CLI webhook secret for testing your endpoint locally.
$endpoint_secret = 'whsec_4t5bz2Fr85h6psqtB3HaTrhwIym5UnwH';
// $endpoint_secret = 'whsec_W2lAZaT9yumHS9dtaBjkKtUHmuYzCcmu';
$payload = @file_get_contents('php://input');
$sig_header = $_SERVER['HTTP_STRIPE_SIGNATURE'];
$event = null;
error_log(print_r('variable area accessed', TRUE));
try {
$event = \Stripe\Webhook::constructEvent(
$payload, $sig_header, $endpoint_secret
);
} catch(\UnexpectedValueException $e) {
// Invalid payload
http_response_code(400);
exit();
} catch(\Stripe\Exception\SignatureVerificationException $e) {
// Invalid signature
error_log(print_r('stripe exception area', TRUE));
http_response_code(400);

exit();
}

// Handle the event
switch ($event->type) {
case 'charge.failed':
$charge = $event->data->object;
error_log(print_r('charge failed', TRUE));
case 'charge.succeeded':
$charge = $event->data->object;
error_log(print_r('charge succeeded', TRUE));
case 'subscription_schedule.canceled':
$subscriptionSchedule = $event->data->object;
error_log(print_r('sub cancelled', TRUE));
case 'subscription_schedule.created':
$subscriptionSchedule = $event->data->object;
//do othere stuff
$myfile = fopen("test.txt", "w") or die("Unable to open file!");
$txt = "Subscription created\n";
fwrite($myfile, $txt);
$txt = "2nd line\n";
fwrite($myfile, $txt);
fclose($myfile);
error_log(print_r('sub created', TRUE));

// ... handle other event types
default:
echo 'Received unknown event type ' . $event->type;
}

http_response_code(200);

#

this is line 24 $event = \Stripe\Webhook::constructEvent(

frosty warren
#

Okay. I'm not super familiar with PHP but let me take a look

#

Are you including the stripe library?

glass wedge
#

basically the webhook.php file contains the webhooks sample code from stripe docs. but the code seems to expect the stripe-php library, which I use to have a folder , or method called webhooks

#

I believe thats whats happening, but not sure, hence why im here ๐Ÿ™‚

frosty warren
#

Yeah you cannot just copy/paste the sample code and expect it to work if you haven't included the stripe-php library

glass wedge
#

I actually did use composer to install that very same stripe library, so it should have all I need. and I have the autoload line in my code. so it should have everything and access to stripe library. I see the folder composer brought in also locally.

#

I have been using the stripe library so far and know it works, cause Im using it to send payments and that is working fine and already done coded. im just now working on the webhooks part and seeing this error with the code from stripe

frosty warren
#

That sounds like something is wrong in your project configuration such that the webhook code cannot find the stripe-php library

glass wedge
#

ok im trying some things. im adding code I have on payments page to this page to see if those includes work. on the payments page I have
require_once 'stripe-php/init.php';

frosty warren
#

Alright ๐Ÿคž

#

Let me know how it goes

glass wedge
#

ok it worked!

frosty warren
#

Hooray ๐ŸŽ‰

glass wedge
#

seems like autoload dis not auto loading via composer like is expected, for some reason, BUT when I manually load the stripe library with // Include Stripe PHP library
require_once 'stripe-php/init.php'; the error goes away and the webhook did its job and my local code only runs for the trigger its looking for, so im a happy camper. Once again you guys and or gals have made my day thank you

frosty warren
#

Happy to help ๐Ÿ‘

glass wedge
#

my only question is that on the dashboard, teh webhooks I setup is not reporting any being successful. says all failed. nothing under succeded. thinking If it connected and no errors, it should be a success. do I have to manually send a 200 response code for that to happen?

frosty warren
#

Yes you have to return a 200 response

glass wedge
#

this is after I encluded 200 response code

frosty warren
#

The server that is reached by that URL is apparently not returning the correct response though.

glass wedge
#

the stripe docs is not clear, if it mentions it at all, that we have to do that

#

and how

#

I mean, if there is a charge my webhook would fire and I can get info and take action, so technically all is fine on my end, just wondering why dashboard is saying 45 failed webhooks and zero successfull, and all is working

#

here is my code that runs successfully and at the end is the 200 response code

#

$myfile = fopen("test.txt", "w") or die("Unable to open file!");
$txt = "Subscription created\n";
fwrite($myfile, $txt);
$txt = "2nd line\n";
fwrite($myfile, $txt);
fclose($myfile);
error_log(print_r('sub created', TRUE));
http_response_code(200);

frosty warren
#

But is that code reachable on the server?

#

Or is this working on your local machine?

glass wedge
#

good question. you thinking its just a side affect of me using the stripe cli to do local testing? If so I would expect the stripe cli page to mention that fact

frosty warren
#

It's not a side effect

#

It's that the events are sent to both webhooks

#

and the code to respond to it does not exist on the server so those webhook requests are failing

glass wedge
frosty warren
#

The local listener and the one that is reached at https://simpleincometracker.com

glass wedge
#

ok I get that, but isnt the whole idea behind me using stripe client that I get to test same as production, here on local machine?

#

if im sending triggers from local stripe cli, why is it expecting from my production server. I have the dashbord webhook set to localhost etc

frosty warren
#

Only if production is synced to your local machine

#

It doesn't matter where the events come from. If they are on your account, using your secret_key then those events get sent to any and all webhook endpoints listening to those event types

#

Every event you trigger from the CLI will hit the URL you specified

#

Unless that server is configured to respond, it will cause an error

glass wedge
#

so I was right, in a way then, a "side effect" of using stripe cli is that you can NOT see a successfull webhook trigger.

frosty warren
#

No

#

Only if you have not configured your server to respond to them

glass wedge
#

which server?

frosty warren
#

You would have no problems if your server at that URL did what your local machine is configured to do

glass wedge
#

what do you mean by That URL, localhost:80/income/webhook.php?

frosty warren
#

The one in the screen shot with all the errors

glass wedge
#

cause thats the webhook point I have set in dashboard, the only one

frosty warren
glass wedge
#

although you see simpleincometracker.com, there is no webhook set there , yet. Im testing and so my webhook is set to the localhost being triggered by stripe cli

frosty warren
#

That's the problem

#

Because you have that entry in the Stripe dashboard, it means POST request are being sent to that URL

#

end of story

#

You can disable that endpoint while you are testing to avoid the failed requests

glass wedge
#

ok so you are basically saying that the webhook would succeed on my production server and can never do so on local testing...right?

#

cause i can live with that

glass wedge
frosty warren
glass wedge
#

ok so you are basically saying that the webhook would succeed on my production server and can never do so on local testing...right?
cause i can live with that

frosty warren
#

No that is not what I am saying. Let me explain.

#

You can configure a local listener using the Stripe CLI and stripe listen

#

This is separate from the Hosted Endpoints you see on the Webhooks tab of the Developer page on the Stripe Dashboard

#

However, both endpoints will be sent webhook POST requests when you A) trigger events with the Stripe CLI or B) trigger them through actions like creating a paymentIntent.

#

From what you have said previously it seems that you do not have any function configured to respond on the server at simpleincometracker.com.

#

This is causing the errors.

#

I currently have a local listener running on my machine and I have a hosted endpoint, which is disabled. I can confirm that you can disable your hosted endpoint while still receiving events on your local listener.

#

does that make sense?

glass wedge
#

kinda, im almost there

frosty warren
#

Okay, great. Which part is unclear

glass wedge
#

I literally have NO end point on production, its all here locally

#

and the local endpoint IS responding 200 response code, why doesnt dashboard see and recognize this

#

how can we test locally if its relying on production input, THAT is what I cans understand

#

cant understand

frosty warren
#

What does the Webhooks tab look like on your Stripe dashboard

#

For reference, this is what mine currently looks like

#

Here I see 1 entry in Hosted Endpoints and 1 entry in Local Listeners

glass wedge
#

so I should remove the production one

frosty warren
#

You don't have to remove it. You can disable it so it doesn't receive the POST requests

#

That way you can just turn it back on when you're ready

glass wedge
#

ok this makes total sens now, just not immiedietely apparent to a novice.

#

I diabled it and running local trigger again, lets see what happens

frosty warren
#

Alright ๐Ÿ‘

glass wedge
#

I forgot I had the production one there also. one moment while I trigger again

frosty warren
#

Okay, let me know how it goes

glass wedge
#

everything looks green on local end, but dashboard webhooks section still showing zero successful webhooks on graph

frosty warren
#

Yeah, local listener data doesn't show up in the dashboard

#

Those metrics are more for monitoring the health of your endpoint in production.

glass wedge
#

well thats good to know ๐Ÿคฃ๐Ÿคฃ

#

thats been my concern for the last 30 minutes of this conversation ๐Ÿ™‚

frosty warren
#

Alright, well glad we got that sorted out.

glass wedge
#

this line here in webhooks code ...$subscriptionSchedule = $event->data->object;

#

Im assuming its the data response being sent by stripe to the enpoint right? so is$subscriptionSchedule an array? trying to figure how to access the info locally in my code

frosty warren
#

In that case I find it really useful to check out the Events tab on the screen you shared. You can select an individual event and see the event type as well as the data payload sent to your local machine.

glass wedge
#

ok cool. please take the rest of the day off. thank you very much, you are doing gods work. ๐Ÿ™๐Ÿ™

frosty warren
#

Will do! ๐Ÿคฃ

glass wedge
#

just saw there is a microphone here, does this mean we could have talked with audio!!!

frosty warren
#

Nope, it's disabled because we are hopping between so many threads.

glass wedge
#

ok cool.

frosty warren
#

Also we like to maintain written records if we need to hand off to another Stripe employee

#

Helps use get better at our jobs

glass wedge
#

makes sense ciao!