#Reitrac
1 messages ยท Page 1 of 1 (latest)
Hi there
Could you please provide an event Id evt_123 and a webhookId ?
Where can i found these information ?
Is this helped you :
"id": "evt_1M3zGSC1ozQJGJccexQCGMkZ"
And Webhook id : we_1M3z8eC1ozQJGJccmpCNmVNN
Yep, looks like we're having trouble reaching your endpoint/server. This is generally indicative of a firewall blocking the connection. There's IP addresses that you should add to your server safelist: https://stripe.com/docs/ips#webhook-notifications
I dont understand how i can add my IP to safelist
I guess that would depend on your server/host! Ultimately this is a server configuration issue, not a Stripe issue
I'n using local server :/
Yep, but Stripe can't reach your local server without some kind of external address (i.e. DNS/IP)
You should use the CLI to forward events to your local endpoint rather than configuring a Dashboard endpoint
And if i'm working on my dev server this will solve the problem ?
i'm sorry i'm a little lost. I just want to use Webhook to sync payment status with my wordpress back end
Yep! See: https://stripe.com/docs/cli/listen
Receive webhook events from Stripe on your local machine via a direct connection to Stripe's API. The listen command can receive events based on your account's default API version or the latest version, filter by type of event, or forward events to an application running on a given port.
The issue you have is you're using a localhost URL in your Stripe Dashboard, which is not reachable from anything external to your machine (i.e. Stripe)
You should either:
- Use the Stripe CLI to listen and forward events.
- Expose your local dev environment via a 'tunnel' (like ngrok) which is than reachable externally.
i can use ngrok for live link, but require login / password to access the website
Your webhook endpoint requires a login?
nope, ngrok to access my local website
I tried with ngrok and webbook return 401 ERR
Did you try the CLI yet?
With stripe listen ?
in my console, everythin works, but not in my dashboard, always the same error
Ignore the Dashboard, that's the unreachable webhook (in fact you can just delete that)
The CLI will forward the events your account is emitting and log any responses sent from your webhook handler code
Yup, so your webhook code has received those events and returned a 200 response (indicating succes)
And now whats the next step to sync status with my wordpress ?
Yeah so I'd guess you'd write the logic in your webhook handler to handle whatever post payment actions you want
"webhook handler" wich file ?
Whatever file whisy-district.local resolves to
I'd recommend starting here: https://stripe.com/docs/webhooks/quickstart
i create webhook.php in root stripe folder
i installed stripe manually and vendor/autoload.php doesn't exist
tried to replace by require_once('./init.php');
But there are lot of errors
Ok, and what errors do you see
i added stripe-php folder on root if mu project
I added manually server.php and webhook.php
Which version of PHP are you using and which version of stripe-php did you use?
How are you including it in your project?
by using :
whisky-district.local/stripe-php/public/checkout.html
I try to make it work outside of my project before including it
Hmm, you don't add the server.php file(s) into the stripe-php directory
They live in your project directory
And then in those file(s), you include stripe-php:
require_once('/path/to/stripe-php/init.php');
I must therefore put the server.php file at the root of my project?
I have read the doc several times, I do not understand ...
and where i include server.php ?
And whats the difference between webhook.php and server.php ?
and how theses 2 files are called ?
There's no real difference, you can name the file whatever you like. This would be the file that the webhook endpoint points to, and that handles any events from your Stripe account
So where i can call this file ?
I'm not sure what you mean
i use "https://whisky-district.local/stripe-php/public/checkout.html" to simulate and order and it works but the file "server.php" or "webhook.php" are never called (well i think)
well checkout.html would be the frontend page. You should have something in that html that contacts the server.php script, like a <form action> or some Javascript making a HTTP request.
webhook.php would get triggered from Stripe sending you a request separately.
checkout.html > create-checkout-session.php > and then cancel / success. I installed this from the documentation
sounds right enough yes
My form action call create-checkout-session.php
It works yes but now, need to use webhooks to sync payment statut with my wordpress and i'm lost. I add server.php to root of my project and webhook.php in strip-php folder and then ? Whats the next step ? as i said, I have read the doc several times, I do not understand ...
I stopped working locally and moved to an online dev server. My webhook returns me a 200 response in the "successful" column
Now I need to add SQL queries in my php with the conditions "if ok, if ko" but I have absolutely no idea where to add this data
I think what is asking is not complicated but I am so lost and desperate that I lose my mind...
- the customer makes the payment
- we send you a webhook to the URL you set up
- your code at that URL runs(like the webhook.php script)
- that code inspects Event object we send in the webhook request body
- that code can run your business logic like adding entries to an SQL database
- we send you a webhook to the URL you set up -> the url is just success.html or cancel.html :/
So i have to replace this by "succes.php" (for example) and wich code i have to put in ?
no
that is not the webhook
that is just the success page. It's separate.
there's two things.
The customer pays
- Checkout redirects them to your
success_urlwhere you can show them some generic "thanks for paying" page - Separately, Stripe send your backend server a
checkout.session.completedwebhook. You update your records and databases on the backend
they're two separate things
For the first point, its OK
For the second where is located the script ?
not sure what you mean by "located"
you can call it anything you want, the code itself wold look like https://stripe.com/docs/webhooks/quickstart. You put the URL to it like "https://whisky-district.local/the_file_you_created.php" in the stripe listen --forward-to=<url> command for example.
ineed to call my script manually to sync my webhook ?
I don't know what you mean
You said :
You put the URL to it like "https://whisky-district.local/the_file_you_created.php" in the stripe listen --forward-to=<url> command for example.
But stripe listen --forward-to=<url> command need to be write manually in console ?
for testing yes
when you go live you won't be using stripe-cli
you will set up a webhook URL in your dashboard (https://stripe.com/docs/webhooks/go-live) and we automatically will send events to that URL
my webhook works
But where i have to add my SQL queries to sync webhook with my data base ?
somewhere in the script that you wrote that handles the webhook.
what does that script look like today, can you share the full code?
i have no script
except webhook.php
I took the code on documentation and replace require 'vendor/autoload.php'; by require_once('./init.php');
and how launch this script after customer paid ?
Stripe calls it, via the webhook. That's what --forward-to=<mypage/webhook.php> is doing for example
and in production later we'll do it automatically like I mentioned here
But where i have to add my SQL queries to sync webhook with my data base ?
in that script. For example you have
case 'checkout.session.async_payment_succeeded':
$session = $event->data->object;
in that case of the switch statement, you now have the knowledge that a payment has succeeded, you can inspect the details of the $session object and rwite code there to update your database or so on.
Can i ask another question ?
Hi! I'm taking over this thread.
Hello ๐
Sure, what's your question?
--forward-to=<mypage/webhook.php> can be launch on local project, but how launch on online server when we dont have access to console ?
If you can't use the Stripe CLI, then you should use real webhook endpoints. You can create them in the dashbaord (https://dashboard.stripe.com/test/webhooks) or with the API (https://stripe.com/docs/api/webhook_endpoints/create)
Ok Thanks.
And why, for each event, after customer paid, i have first status "failed" or "succeeded" and then "expired" ?
Can you give me an example of PaymentIntent ID where you saw this?
Humm writtent in my data base
This is my switch case
and after the script, this add 2 lines in my database
"Succeeded" and "Expired"
is "expired" launched after others statuts ?
Can you share a PaymentIntent ID (pi_xxx) so I can check the events it generated?
yes i'll find it
pi_3M42hjC1ozQJGJcc17faON5g
or this :
pi_3M42dKC1ozQJGJcc04crMDb9
For pi_3M42hjC1ozQJGJcc17faON5g, there where multiple charge attempts:
- https://dashboard.stripe.com/test/logs/req_NCWTaaO8AfvOTU with card_declined
- https://dashboard.stripe.com/test/logs/req_BcA95kjvqB9YMr with card_declined
- and finally https://dashboard.stripe.com/test/logs/req_JF39N3FcGytaPJ that succeeded
So it's expected to get mutiple webhook events. But since you are using Checkout Session you should only listen to thecheckout.session.completedevent, that will be fired only once.
Yes i tried multiple card to try rejected card
but this : pi_3M42dKC1ozQJGJcc04crMDb9
Have only 1 entry
but 2 in my database
Here we sent 2 events related to PaymentIntent: payment_intent.created and payment_intent.succeeded
so why "expired" created for each event ? ๐ข
You can see all events for this PaymentIntent in your dashboard here: https://dashboard.stripe.com/test/payments/pi_3M42dKC1ozQJGJcc04crMDb9
And there's no expired event here.
Can you share the event ID of the expired event you see?
I already send you : pi_3M42dKC1ozQJGJcc04crMDb9
I cant explain why ๐
how i can retrieve name and price via $session?
I'm asking for an event ID (evt_xxx). This PaymmentIntent ID didn't send any expired event.
ho sorry
also your PHP code is missing break statements @distant tiger , that's why you're confused I think and seeing multiple things happen
https://www.php.net/manual/en/control-structures.switch.php
you need to put breaks in each case, or the execution will just run through every case so the code for every event type will run regardless of what type it actually was. We have that in our example code, you must have removed them when editing the code.
hoooooooooo
i'm stupid
of course yes !
how i can retrieve name and price via $session?
you can't unfortuantely, you have to make an extra API call at that point to get that information
https://dev.to/stripe/purchase-fulfilment-with-checkout-or-wait-what-was-i-paid-for-335d is a good blog post about it
to summarise
- https://stripe.com/docs/payments/checkout/fulfill-orders#handle-the-event
- when handling the checkout.session.completed event, you would retrieve the CheckoutSession object from the API and crucially, expand the line_items of the CheckoutSession : https://stripe.com/docs/expand#includable-properties
- The line_items contains details of the specific items the customer bought such as the related Price and Product objects. https://stripe.com/docs/api/checkout/sessions/object#checkout_session_object-line_items-data-price
In "line_items" is it possible to add "reference" ?
or "transaction_id"
when declare $checkout_session
and where can i add custom field or something like this ?
Add a custom field to the Checkout page? Unfortunately that is not possible at the moment though it is on the roadmap. Right now, the best way to take custom info is to collect if from your user before or after redirecting them to checkout