#CipherDev17
1 messages ยท Page 1 of 1 (latest)
So when I go live with mine, the url will just be https://mywebsiteurl/webhook
And the enabled events will just be the events I handle in the webhook event handler?
for example, one of the events I am handling is 'charge.succeeded' so that will go in the enabled_events array?
The enabled events will be those events you register with Stripe
Yup
We only send the events you specify in the enabled_events parameter
I assume also that this has to be instantiated first before everything?
here is the file in which I am handling the webhook events
so the endpoint instantiation would go at the top? and then I would use that "endpoint" variable where you see "endpointSecret" on line 13?
It doesn't have to be in place first, just your webhooks will fail if it isn't. This code would need to be inside of a running webserver that responds to requests made to the URL you registered with the webhook endpoint
So in your case you would have (likely) an Express webserver running with a URL that matches the webhook endpoint
and the Webhook secret from that endpoint
This code would then handle the requests coming from Stripe
The code above is a screenshot of my node server controller file that handles an endpoint "/webhook".
So the server accepts "/webhook" requests, goes through my routes file and lands here in this controller file.
Okay
So then that endpoint code goes at the top with my correct information and it should work fine?
Yes that should work just fine. You can see an example of the a basic Express app handling this here: https://stripe.com/docs/webhooks/quickstart?lang=node
Thank you, and that example doesn't show the webhookEndpoints.create({}) function, it only shows creating the webhook event.
No
Because you would only create the Webhook endpoint once
So that code does not need to be repeated. This example is specific to handling the webhook responses
so does the "endpoint" constant that is created with that go in the exports.handleWebhooks function on line 19 in replacemet on endpoint secret?
my apologies, clearly I have never set this up before
No
You would not do that
You would create the webhook, retrieve the secret from Stripe, add it to your environment configuration (out of version control) and use that value in your webhook handler
I guess I am confused on what you are supposed to do with the endpoint variable that is created on line 6
We don't put those together for a reason. You would use the returned value to store whatever information you want/need for your integration but it would be used separate from the webhook handler
So it should probably go in my app.js file then? if it has nothing to do with handling the webhooks?
That's up to you
So as is, this would work?
After a successful payment, this webhookEndpoints.create({}) function would run, telling stripe what my endpoint url is and the enabled events I have for that endpoint
It would then send the request to that endpoint where it would start in my app.js file, be filtered to my routes where I handle "/webhook" requests and then redirects it to this controller file that I have sent you screenshots of
And then I would be able to handle the events how I have them?
๐ stepping in as Snufkin needs to step away
No problem ๐
Hmm so maybe I don't have full context yet, but no you would not be creating an endpoint after a successful payment
You create the endpoint up front
And it stays static
right, I have my endpoint as "/webhook"
I know that will always stay the same
I have already been able to handle and test the endpoint with the CLI
Gotcha, can you clarify the outstanding question?
when you go live with your app
and you are not actively testing your application with the CLI testing
this image that I just sent is the how stripe knows where to send the webhook?
if so, where does this need to be implemented
Yes
You just do that once
You create an endpoint using your secret key or via the Dashboard
Once you set up your Webhook endpoint URL then Stripe will attempt to send any events that you have configured to that endpoint while the endpoint is active. It will only become inactive if there are many response failures or if you de-activate it.
So after you have instantiated that "endpoint" variable on line 5 of the screenshot I sent, you dont need to do anything with it?
the screenshot of the docs
Correct
And it doesnt matter where this is instantiated? For example, I currently have it in my webhook controller file. But it sounds like it needs to be in my app.js file.
I have a node/express app
No this is a one-time thing
You could just use a cURL request or you can run that same request via the Dashboard
You don't need that specific code to live in your project. It should only ever be run once for an endpoint
I'd recommend deleting that picture as it contains a Webhook secret which is sensitive
this is my webhook controller file. All requests too "/webhook" go here. I will register an endpoint on the stripe dashboard with the events I am going to handle. is that all i need to do?
Yes, that is all you need to do. You register the endpoint via the Dashboard, you remove the webhooks.create code from your file, then you can test that you are successfully hitting that endpoint by triggering events.
so I dont even need this "const endpoint = await stripe.webhookEndpoints.create" function??
all i need to do is create the endpoint in the dashboard along with my events and when my app goes live, it will properly handle all webhook events?
Correct, though you shouldn't just assume your app will properly handle the Events. You do need to test it! The handleWebhooks code is what you need to test
Right of course, okay I think I understand now, thank you for your help today!