#RiderFx3 - Stripe CLI
1 messages ยท Page 1 of 1 (latest)
Hi ๐
Can you share the command you used to forward the webhook events?
For example this is what I run:
stripe listen --forward-to localhost:8000/webhook2/ --log-level info
stripe listen --forward-to localhost:4242/cron/stripe_webhooks.php
And you left that running in one terminal window while running the stripe trigger payment_intent.succeeded in another?
And both instances of the CLI were logged in to the same account?
I didn't log the second
The problem is, when I launch stripe listen, I can't type any command after
typing in the terminal is locked
Right
It launches an active process in that window
You need to leave that running and launch a new window to enter any other Stripe CLI commands
These wouldn't show up in your Dashboard list of events but you would see them streamed to the terminal window where you are running stripe listen
Oh wait, sorry nevermind. They should show up in the dashboard
Nothing in terminal too
And what was the output from stripe trigger?
If you did it should prompt you. That looks correct to me
I just ran the same command and my stripe listen showed the expected 3 events
This should be showed in wich prompt ?
The one running stripe listen
But it show that after you type manually the trigger ?
Or after a payment on your account ?
After the command stripe trigger payment_intent.succeeded
If you look at the Webhooks tab in your Dashboard it should indicate if you have an active Local listener (which is created by calling stripe listen)
I try a second time and nothing move
What do you see if you type stripe version?
Yeah I'm pretty stumped here. And you are not seeing any of these events in your dashboard still?
No ๐ฆ
And this is a standard Stripe account?
Except the responses all indicate you are hitting the APIs correctly which I really don't get
Okay this may seem odd but can you try logging out stripe logout and logging back in again stripe login?
Inside the prompt ? Or on the web ?
In the terminal windows
I try
Okay
The term "stripe" is not recognized as the name of a cmdlet, function, script file, or program
executable. Check the spelling of the name, or if a path exists, verify that the path is correct
and try again.
but you just used the Stripe CLI in the terminal windows earlier, didn't you ?
yes but now I try with Powershell
I guess I have to add Stripe to my Path
Ok it works now from everywhere ๐
stripe listen --forward-to localhost:4242/cron/stripe_webhooks.php --log-level debug
Okay so now when you trigger an event do you see anything?
Yeah you can see it's hitting the API
It works now ๐
I guess the problem was the file stripe.exe was not added to my windows path
SO there was authorisation problem ?
Ahhhh......okay. Perhaps.
So now
I see the event objects in my dashboard, but not the response of my php file
Okay well we've got the first part taken care of. The requests are being sent. So now let's look at the PHP file.
For the moment I juste pasted the example
๐ stepping in here.
As Snufkin had to step away
Can you summarize the challenge you are having?
I just want to debug my webhook file to be sure I don't have PHP errors
If I launch it directly it don't detect any event
My localhost server show me only thath
Can you share an event ID you are testing with?
How can I pass event ID to my webhook file ?
With the CLI prompt ?
How are you generating events to test your webhook endpoint?
I simulate manually an order with a testing card ^^
Gotcha. So that should generate an event that is being sent to your webhook. You can find those events in your Dashboard here: https://dashboard.stripe.com/test/events
But webhook file is asynchronous so can't debug
Can you provide one of those that I can take a look at?
For exemple : evt_3L1YV1JCDeVgDheA1AtsZQoa
This was before I setup the local webhook
Okay can you trigger a new one to your local endpoint?
With the CLI or a testing card ?
Either
I just want to know what happens when you try to hit your local endpoint
Actually before you do that... can you add a log at the top of your local endpoint code and log out the req.body?
did a new event : evt_3L1ZihJCDeVgDheA1VmGCDiz
How can i do that in PHP ?
For PHP you should be able to just log the $payload
I think error_log($payload) should work
I'm not a PHP dev but I think that should do it
Can you share your webhook code?
<?php
include( __DIR__.'/autoload.php' );
// This is your Stripe CLI webhook secret for testing your endpoint locally.
$endpoint_secret = 'whsec_bb44d83814b6dc7bd188ef97cdd3208e28f855845bd718f4b9206de06670ae00';
$payload = @file_get_contents('php://input');
$sig_header = $_SERVER['HTTP_STRIPE_SIGNATURE'];
$event = null;
error_log( $payload );
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
http_response_code(400);
exit();
}
// Handle the event
switch ($event->type) {
case 'payment_intent.succeeded':
$paymentIntent = $event->data->object;
// ... handle other event types
default:
echo 'Received unknown event type ' . $event->type;
}
http_response_code(200);
This is just the example for the moment
The endpoint secret ?
Yeah
It's given by stripe dashboard or CLI
Yes you grabbed it from the CLI right? Regardless, that shouldn't stop you from seeing something here in the payload.
Hmm
Thinking
Maybe you can ask a PHP dev ?
Hmm I'm doing the exact same thing as you and it is working fine.
Still thinking
You are getting sent back a 200
It seems to be working
php -S localhost:4242
Why aren't you seeing the event on your server....
Maybe I should activate an option when I launch PHP server ?
Can you change the response code?
No that should be fine
Try sending back a 400 instead of 200
And let's see if that changes in the CLI
Also let's add an error log for $event
So after you construct the $event lets log it out
Instead of echoing received unknown event type... just change that to test123
As far as I can tell everything is working
You are just not seeing anything on your server
Which doesn't make sense
But let's make sure
So echo something other than the default code provided
I did not echo that ^^
You do? switch ($event->type) { case 'payment_intent.succeeded': $paymentIntent = $event->data->object; // ... handle other event types default: echo 'Received unknown event type ' . $event->type; }
Right
OK thanks so everytime I have to read the output in the srtipe dashboard
No
There is still something wrong here... it is like no payload is coming through at all since you are sending a payment_intent.succeeded event but it isn't recognizing it.
And you see nothing in your error log
So like you are hitting your endpoint
Ho right
But you aren't able to ingest the event
Let's try something... you are using the legacy PHP route (with \Stripe\Webhook::constructEvent). Let's use the newer PHP stuff. Can you copy the code you see in our integration builder: https://stripe.com/docs/webhooks/quickstart
If you are curious, we talk about this legacy approach here: https://github.com/stripe/stripe-php/wiki/Migration-to-StripeClient-and-services-in-7.33.0
I can't set API Key before getting the event, because my system is multiAPI Key
I realize I will have the same problem with the endpoint key ...
CRAP
I have a multi seller system, where each seller provied his API Key to the databse
This is the same seller but several location
Can I pass a param after the webhook URL ?
No I don't have a connect platform
Then each Account would have to register their own webhook handler.
And input their own keys
Yes
So they would do that when they set up their integration using your code.
But I don't want to code several webhook files
What do you mean?
So can I pass a seller ID in the URL ?
webhook.php?seller_id=1
Then when Stripe call the webhook I can identify the shop and retrieve the API Key
The events are only going to go to a webhook handler for the account where those events take place.
Yep, but Stripe call the URL I give to him
So if I configure in each account an URL with the good param inside, I will be able to retrive the variable in my webhook script ri ght ?
For example :
That description won't be sent with events to your webhook.
Nop but the param in the URL yes
I'm sorry, you completely lost me. If an event comes through, you know what account it is related to because only the account with that endpoint registered, and their webhook initialized with their key, will receive those events.
in PHP I will be able to receipt $_GET['seller']
Ok I will re-explain.
I will have 1 webhook file for several sellers.
I will pass the Seller ID, in a param in the URL, to retrieve it in my script
I will try now in local
stripe listen --forward-to localhost:4242/cron/stripe_webhooks.php?seller=1
It works ๐
Now I can retrieve my bdd like that
my API in bdd*
Okay so you are talking about wanting a way to identify the webhook endpoint itself?
Then yes, that will work fine.
Sounds good
โ ๏ธ Webhook error while validating signature.
Go ahead and comment out the signature for now
Just comment out $endpoint_secret = 'whsec_...';
Then it won't go into that if block
And it will just handle the event
I have to turn endoint_secret to false
Otherwise it throw an error because don't exist
Should just throw a warning, no?
Doesn't really matter
Sure comment that all out
Let's just see if you receive the payload and event
Also add your error_log($payload) please
What do you see in your local server?
Great so that is working now
Yes
Did not see what?
The error_log
I will check in the folders maybe it write it inside a log file
I will search on my own !
thanks a lot for your help and patience ! โค๏ธ
This is awesome to have this kind of support
Happy to help! I'm really curious what would be causing your logs to not show so I'm checking with a colleague if you want to stick around for a moment
Can you check your PHP version?
php -v
Huh okay I'm on 8.1.5... wonder if that matters
Looking
Are you using Apache?
Can you try running php --info | grep error
Yes Apache with Laragon on Windows
Ok I think this is Apache specific
grep command not found
https://stackoverflow.com/questions/5127838/where-does-php-store-the-error-log-php-5-apache-fastcgi-and-cpanel mentions "PHP stores error logs in /var/log/apache2 if PHP is an apache2 module."
Do you have a folder like that?
I search because on Windows this is not Linux architecture
Ah right you said windows
I tihnk you run php --info | findstr /r /c:"error_log" on windows
To find it
Didn't realize Apache re-routes your logging
I can see the log .... But problem
?
[20-May-2022 20:43:11 Europe/Paris] Received unknown event type
Sure that is because you don't have a case for all the event types you are likely triggering
But this event 20h43 in Stripe is Payment SUCCESS
Ah YES it trigger 3 event at same time
Yeah
I remember
You can set the CLI to only forward certain events if you want
How ?
in listening or triggering ?
listen
If you trigger from the CLI it will only trigger the event you specify
But since you are testing through your own integration it is creating multiple events
Okay thanks a lot for you help !<3
Sure thing! Glad we got it figured out.
Last thing (sorry)
It works because we disabled the endpoint_secret
What when I will enable it ?
Should still work fine. You likely were just using the wrong secret
When you initalize Stripe Listen it should give you a secret that you need to use
But give it a test and let us know if it errors.
That'd do it ๐
Thanks a lot !
I'm headed out, but @ivory orbit is here to help if you have any other questions!
Thanks guys !
I think its Ok for now ๐
I come back if needed