#NormanLove - webhooks
1 messages ยท Page 1 of 1 (latest)
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(
Okay. I'm not super familiar with PHP but let me take a look
Are you including the stripe library?
Can you take a look here: https://github.com/stripe/stripe-php#composer
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 ๐
Yeah you cannot just copy/paste the sample code and expect it to work if you haven't included the stripe-php library
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
That sounds like something is wrong in your project configuration such that the webhook code cannot find the stripe-php library
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';
ok it worked!
Hooray ๐
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
Happy to help ๐
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?
Yes you have to return a 200 response
The server that is reached by that URL is apparently not returning the correct response though.
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);
But is that code reachable on the server?
Or is this working on your local machine?
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
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
not sure what you mean by BOTH, mine and which other one?
The local listener and the one that is reached at https://simpleincometracker.com
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
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
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.
which server?
You would have no problems if your server at that URL did what your local machine is configured to do
what do you mean by That URL, localhost:80/income/webhook.php?
The one in the screen shot with all the errors
cause thats the webhook point I have set in dashboard, the only one
In the image you sent, the URL specified was simpleincomtracker.com
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
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
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
the only way I can test is to have that local webpoint ๐
A hosted endpoint is different from a local listener
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
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?
kinda, im almost there
Okay, great. Which part is unclear
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
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
so I should remove the production one
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
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
Alright ๐
I forgot I had the production one there also. one moment while I trigger again
Okay, let me know how it goes
everything looks green on local end, but dashboard webhooks section still showing zero successful webhooks on graph
Yeah, local listener data doesn't show up in the dashboard
Those metrics are more for monitoring the health of your endpoint in production.
well thats good to know ๐คฃ๐คฃ
thats been my concern for the last 30 minutes of this conversation ๐
Alright, well glad we got that sorted out.
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
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.
ok cool. please take the rest of the day off. thank you very much, you are doing gods work. ๐๐
Will do! ๐คฃ
just saw there is a microphone here, does this mean we could have talked with audio!!!
Nope, it's disabled because we are hopping between so many threads.
ok cool.
Also we like to maintain written records if we need to hand off to another Stripe employee
Helps use get better at our jobs
makes sense ciao!