#RiderFx3 - Stripe CLI

1 messages ยท Page 1 of 1 (latest)

quick wharf
#

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
cedar herald
#

stripe listen --forward-to localhost:4242/cron/stripe_webhooks.php

quick wharf
#

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?

cedar herald
#

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

quick wharf
#

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

cedar herald
#

Yep that's what I did

#

But nothing got fired after a payment

quick wharf
#

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

cedar herald
quick wharf
#

Oh wait, sorry nevermind. They should show up in the dashboard

cedar herald
#

Nothing in terminal too

quick wharf
#

And what was the output from stripe trigger?

cedar herald
#

Do I have to login before typing the Stripe trigger ?

quick wharf
#

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

cedar herald
#

This should be showed in wich prompt ?

quick wharf
#

The one running stripe listen

cedar herald
#

But it show that after you type manually the trigger ?
Or after a payment on your account ?

quick wharf
#

After the command stripe trigger payment_intent.succeeded

cedar herald
quick wharf
#

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)

cedar herald
#

I try a second time and nothing move

quick wharf
cedar herald
#

Yep it' ok for that

quick wharf
#

What do you see if you type stripe version?

cedar herald
quick wharf
#

Yeah I'm pretty stumped here. And you are not seeing any of these events in your dashboard still?

cedar herald
#

No ๐Ÿ˜ฆ

quick wharf
#

And this is a standard Stripe account?

cedar herald
#

Yes

#

Maybe I have to try to close and relaunch the prompts ?

quick wharf
#

Except the responses all indicate you are hitting the APIs correctly which I really don't get

cedar herald
#

I try to run in the console of PHP Storm, and same problem

quick wharf
#

Okay this may seem odd but can you try logging out stripe logout and logging back in again stripe login?

cedar herald
#

Inside the prompt ? Or on the web ?

quick wharf
#

In the terminal windows

cedar herald
#

I try

quick wharf
#

Okay

cedar herald
#

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.

quick wharf
#

but you just used the Stripe CLI in the terminal windows earlier, didn't you ?

cedar herald
#

yes but now I try with Powershell

#

I guess I have to add Stripe to my Path

#

Ok it works now from everywhere ๐Ÿ˜‰

quick wharf
#

Okay can you rerun stripe listen with the --log-level debug?

cedar herald
#

stripe listen --forward-to localhost:4242/cron/stripe_webhooks.php --log-level debug

quick wharf
#

Okay so now when you trigger an event do you see anything?

cedar herald
#

Looks different now

quick wharf
#

Yeah you can see it's hitting the API

cedar herald
#

It works now ๐Ÿ˜‰

#

I guess the problem was the file stripe.exe was not added to my windows path

#

SO there was authorisation problem ?

quick wharf
#

Ahhhh......okay. Perhaps.

cedar herald
#

So now

#

I see the event objects in my dashboard, but not the response of my php file

quick wharf
#

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.

cedar herald
#

For the moment I juste pasted the example

dreamy forum
#

๐Ÿ‘‹ stepping in here.

#

As Snufkin had to step away

#

Can you summarize the challenge you are having?

cedar herald
#

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

dreamy forum
#

Can you share an event ID you are testing with?

cedar herald
#

How can I pass event ID to my webhook file ?
With the CLI prompt ?

dreamy forum
#

How are you generating events to test your webhook endpoint?

cedar herald
#

I simulate manually an order with a testing card ^^

dreamy forum
cedar herald
#

But webhook file is asynchronous so can't debug

dreamy forum
#

Can you provide one of those that I can take a look at?

cedar herald
#

For exemple : evt_3L1YV1JCDeVgDheA1AtsZQoa

#

This was before I setup the local webhook

dreamy forum
#

Okay can you trigger a new one to your local endpoint?

cedar herald
#

With the CLI or a testing card ?

dreamy forum
#

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?

cedar herald
#

did a new event : evt_3L1ZihJCDeVgDheA1VmGCDiz

dreamy forum
#

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

cedar herald
#

Done

#

I retry a new payment now ?

dreamy forum
#

Yep

#

And look in your local server

#

And see what logs there

cedar herald
#

Show nothing ๐Ÿ˜ฆ

dreamy forum
#

Can you share your webhook code?

cedar herald
#
<?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

dreamy forum
#

That secret is from the CLI, yes?

#

Just checking

cedar herald
#

The endpoint secret ?

dreamy forum
#

Yeah

cedar herald
#

It's given by stripe dashboard or CLI

dreamy forum
#

Yes you grabbed it from the CLI right? Regardless, that shouldn't stop you from seeing something here in the payload.

#

Hmm

#

Thinking

cedar herald
#

Maybe you can ask a PHP dev ?

dreamy forum
#

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

cedar herald
#

php -S localhost:4242

dreamy forum
#

Why aren't you seeing the event on your server....

cedar herald
#

Maybe I should activate an option when I launch PHP server ?

dreamy forum
#

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

cedar herald
#

Trigger a new event and still the same

#

And I added error_log in the try

dreamy forum
#

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

dreamy forum
#

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; }

cedar herald
#

Oh sorry yes

#

yes sorry lol it was in the example

dreamy forum
#

Right

cedar herald
#

I can see it in the dashboard

dreamy forum
#

Okay so the code is working fine

#

Well sorry

#

Not working fine lol

cedar herald
#

OK thanks so everytime I have to read the output in the srtipe dashboard

dreamy forum
#

No

cedar herald
#

Ha

#

?

#

What is not working ?

dreamy forum
#

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

cedar herald
#

Ho right

dreamy forum
#

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

cedar herald
#

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

dreamy forum
#

How were you initializing Stripe before?

#

Also what do you mean by "multiAPI key"?

cedar herald
#

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 ?

dreamy forum
#

Are you a Connect platform?

#

Sounds like no

cedar herald
#

No I don't have a connect platform

dreamy forum
#

Then each Account would have to register their own webhook handler.

#

And input their own keys

cedar herald
#

Yes

dreamy forum
#

So they would do that when they set up their integration using your code.

cedar herald
#

But I don't want to code several webhook files

dreamy forum
#

What do you mean?

cedar herald
#

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

dreamy forum
#

The events are only going to go to a webhook handler for the account where those events take place.

cedar herald
#

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 :

dreamy forum
#

That description won't be sent with events to your webhook.

cedar herald
#

Nop but the param in the URL yes

dreamy forum
#

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.

cedar herald
#

in PHP I will be able to receipt $_GET['seller']

cedar herald
#

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*

dreamy forum
#

Okay so you are talking about wanting a way to identify the webhook endpoint itself?

#

Then yes, that will work fine.

cedar herald
#

Yes, no I try the new endpoint method

#

now*

dreamy forum
#

Sounds good

cedar herald
#

โš ๏ธ Webhook error while validating signature.

dreamy forum
#

Go ahead and comment out the signature for now

cedar herald
#

This ?

dreamy forum
#

Just comment out $endpoint_secret = 'whsec_...';

#

Then it won't go into that if block

#

And it will just handle the event

cedar herald
#

I have to turn endoint_secret to false

#

Otherwise it throw an error because don't exist

dreamy forum
#

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

cedar herald
dreamy forum
#

What do you see in your local server?

cedar herald
#

Still nothing in local server ๐Ÿ˜ฆ

dreamy forum
#

Great so that is working now

cedar herald
dreamy forum
#

^ I meant you are receiving the event

#

Hmmm

cedar herald
#

Yes

dreamy forum
#

add error_log($paymentIntent)

#

Right above your echo

cedar herald
#

Did not see even on Stripe ^^

dreamy forum
#

Did not see what?

cedar herald
#

The error_log

dreamy forum
#

It would only show in your local server

#

You still saw nothing in local server?

cedar herald
#

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

dreamy forum
#

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

cedar herald
#

Yep I can stay a moment ๐Ÿ™‚

#

PHP 7.4.19

dreamy forum
#

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

cedar herald
#

Yes Apache with Laragon on Windows

dreamy forum
#

Ok I think this is Apache specific

cedar herald
#

grep command not found

dreamy forum
#

Do you have a folder like that?

cedar herald
#

I search because on Windows this is not Linux architecture

dreamy forum
#

Ah right you said windows

#

I tihnk you run php --info | findstr /r /c:"error_log" on windows

#

To find it

cedar herald
#

error_log => C:/laragon/tmp/php_errors.log => C:/laragon/tmp/php_errors.log

#

Great

dreamy forum
#

lol okay

#

So that solves that

#

That should be helpful for future debugging

cedar herald
dreamy forum
#

Didn't realize Apache re-routes your logging

cedar herald
#

I can see the log .... But problem

dreamy forum
#

?

cedar herald
#

[20-May-2022 20:43:11 Europe/Paris] Received unknown event type

dreamy forum
#

Sure that is because you don't have a case for all the event types you are likely triggering

cedar herald
#

But this event 20h43 in Stripe is Payment SUCCESS

#

Ah YES it trigger 3 event at same time

dreamy forum
#

Yeah

cedar herald
#

I remember

dreamy forum
#

You can set the CLI to only forward certain events if you want

cedar herald
#

How ?

dreamy forum
#

Specify events you want with -e flag

#

Otherwise it will forward everything

cedar herald
#

in listening or triggering ?

dreamy forum
#

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

cedar herald
#

Okay thanks a lot for you help !<3

dreamy forum
#

Sure thing! Glad we got it figured out.

cedar herald
#

Last thing (sorry)

#

It works because we disabled the endpoint_secret

#

What when I will enable it ?

dreamy forum
#

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.

cedar herald
#

LOL I forgot to remove the ... of the examplte

#

๐Ÿ˜‚

dreamy forum
#

That'd do it ๐Ÿ˜‚

cedar herald
#

Thanks a lot !

dreamy forum
#

I'm headed out, but @ivory orbit is here to help if you have any other questions!

cedar herald
#

Thanks guys !
I think its Ok for now ๐Ÿ˜‰
I come back if needed