#jules-php-webhook

1 messages ยท Page 1 of 1 (latest)

rugged muralBOT
cursive roost
#

Hello, happy to help. Can you send me your code that tries to access the data in the event object?

#

And this is for all events or just a certain kind of event?

rose pollen
#

I just tried with payment_intent.succeeded event

cursive roost
#

Thank you for the code. Quick question, how are you sending this event? The Stripe CLI or actually making a payment?

rose pollen
#

The CLI

#

But I tried with a payment to

cursive roost
#

Gotcha, and the signature verification works, and $event->type has the right string, but $event->data is empty?

rose pollen
#

I'm sorry I'm french and my english is bad

#

The $payload is an empty string

cursive roost
#

It is easy for me to understand you. Let me know if I need to rephrase something

#

Ah thank you for the clarification. So it is an issue with $payload = @file_get_contents('php://input'); probably

rose pollen
#

Yes I think

cursive roost
rose pollen
#

No I just copied the code from the Stripe documentation about webhooks

#

My goal is to record all successful payments in my database

#

However I have access to the data of each event on my dashboard but it is impossible for me to access it via the PHP script of my webhook

cursive roost
#

Are you using stripe listen to get these events on your personal machine? Or does this server have a public URL that you are sending the events to?

rose pollen
#

I use stripe listen on my personal machine

cursive roost
#

Thank you. That should work here as far as I know. Looking in to what might cause this

rose pollen
#

var_dump($payload);

cursive roost
#

Thank you

#

I am still trying to think of what is going wrong here.

#

Hey I am still having trouble figuring this out. I will ask a colleague to help look in to this, they may know more about what could be going wrong here

rose pollen
#

Okay thank you very much

digital tusk
#

hey @rose pollen the problem is that you are visiting the URL in your web browser, which doesn't make sense. That's a HTTP GET, not a HTTP POST.

#

to test this, you need to actually POST something to the endpoint, for example by using stripe trigger charge.succeeded to have Stripe send an event to you, or taking some test mode action on your account to generate an event.

rose pollen
#

I sent these requests. Does this match what you are telling me?

digital tusk
#

yes

#

are you also running stripe listen --forward-to http://localhost/Odyssee/maquette/stripe/gestionnaire.php at the same time?

rose pollen
#

Yes

digital tusk
#

you need to have one console window that is listening, and use another to trigger

rose pollen
#

I have both

digital tusk
#

cool, then you should have seen in the listen window a log saying that it sent the event to your URL. Correct?

rose pollen
digital tusk
#

awesome! So it's working? We sent you the event and your code returned a HTTP 200 status.

#

that means the code executed and it reached the http_response_code(200); line.

rose pollen
#

Yes but when I try to use the event data in my php script, it doesn't work

digital tusk
#

are you sure?

#

I really think the problem is you visit your script in your web browser and you are expecting an event to exist, which it won't since you're just visiting the script in a browser, not sending an event to it from a webhook.

#

can you say more about the exact code you run, when you run it, and what part exactly does not work?

rose pollen
#

If I understand correctly, when an event of type payment_intent.succeeded is sent, the php script of my endpoint is executed.
In this script I send a query to my database which is supposed to record successful payments.
However, even if a 200 code is displayed on my console, nothing is recorded in my database.
I checked and the problem is not with my query.

#

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

#

This code is testing if the event is type of payment_intent.succeeded

#

But $paymentIntent is empty

digital tusk
#

If I understand correctly, when an event of type payment_intent.succeeded is sent, the php script of my endpoint is executed.
yes, correct
In this script I send a query to my database which is supposed to record successful payments.
ok! I don't see anything in the code you shared earlier which does that though, so I'm unable to say much about that part as it's your code.
however, even if a 200 code is displayed on my console, nothing is recorded in my database.
again, it's your database and your code that writes to your database, I'm not sure how I can debug that better than you can.
I checked and the problem is not with my query.
but is it a Stripe problem? We sent you the event successfully and presumably you're able to access the event and the payload. So that part is all fine?
But $paymentIntent is empty
that should be impossible.

can you add a lot more logging to your code and log the exact values of everything like $event and $event->data and share those logs here?

rose pollen
#

Ok i will do it

#

How can I log something in the stripe console ?

digital tusk
#

you can't, you have to log it from your PHP code.

#

for example maybe try using print_r or writing to your server's error_log or so on, however you would normally log internal details of your PHP scripts.

rose pollen
digital tusk
#

that code all looks correct to me, I see no immediate problem with it.

#

it doesn't seem related to what you said though, you said you are accessing a database and there's nothing related to that in that code.

#

you claimed "But $paymentIntent is empty". So I'd need to ask you to add more debugging and logs to check what the values of $event are in the line before that, check if any errors are thrown, how the code executes, and so on, because, to me, that code combined with the information about how you use stripe listen and stripe trigger will work perfectly.

rose pollen
#

And if I add a query to my database in the case 'payment_intent.succeeded', I should only have successful payments in my database.
Do we agree?

digital tusk
#

I have no idea unfortunately, I don't know what your query is trying to do.

This webhook endpoint is just code that runs when a successful payment happens. You would use it to update (write to) your database about that payment now existing, not really query(search for) existing successful payments.

rose pollen
#

I understand better. I thought that this code was executed every time an event was launched.

digital tusk
#

well, it is. An event happens in your Stripe account. We send that event to your webhook endpoint, and that code you posted is what your endpoint is doing.

#

all that code does right now is accept the event and return a HTTP 200 status(which you showed me earlier it does correctly without error). It doesn't do anything with a database unless there is some code you are not showing us. So I'm not sure what direct problem you're facing beyond that.

sweet lily
#

jules-php-webhook

rose pollen
#

II added the part related to my database in the switch case

sweet lily
#

We can't speak to the database part, that's your own system/code but as long as you listen to the right Events on your endpoint then yes

#

right now you access $paymentIntent = $event->data->object; but you don't do anything with it right?

rose pollen
#

Yes

sweet lily
#

My advice is to remove the database code for now and debug with text. Like write the data you want in a file, and then look at the file and see if the right information appears there.

#

way easier to debug this way to start, that's what I do at least. And once you have all you need, then start adding the database code and debugging that

rose pollen
#

Ok thanks a lot for all the help you gave me

#

I will do this

sweet lily
#
    file_put_contents("./log.txt", "Invoice paid for customer : $customerId", FILE_APPEND);
    file_put_contents("./log.txt", "Invoice total : $invoiceTotal", FILE_APPEND);
    file_put_contents("./log.txt", "Invoice nb Lines : $nbLines", FILE_APPEND);
    file_put_contents("./log.txt", "Invoice sub id : $subscriptionId", FILE_APPEND);
    file_put_contents("./log.txt", "Invoice sub amt : $subscriptionAmount", FILE_APPEND);``` I do things like this when I want to debug my webhook handler