Issue
Hello Stack Overflow community! 👋 I am currently facing some challenges while using Stripe Webhooks on my Fedora 38 system. I have followed the instructions and used the stripe login
command successfully in my terminal.
Next, I forwarded the events to my local server's webhook using the command stripe listen --forward-to localhost:4242/webhook
, which seems to be working fine.
However, when I try to trigger an event in another terminal instance using stripe trigger payment_intent.succeeded, I encounter the following error:
2023-07-20 20:55:41 --> charge.succeeded [evt_3NW7d9ElhJRNJy1r1mjvP0WG]
2023-07-20 20:55:41 [ERROR] Failed to POST: Post "http://localhost:4242/webhook": dial tcp [::1]:4242: connect: connection refused
2023-07-20 20:55:41 --> payment_intent.succeeded [evt_3NW7d9ElhJRNJy1r13bYAcrf]
2023-07-20 20:55:41 [ERROR] Failed to POST: Post "http://localhost:4242/webhook": dial tcp [::1]:4242: connect: connection refused
2023-07-20 20:55:41 --> payment_intent.created [evt_3NW7d9ElhJRNJy1r17JzkvDh]
2023-07-20 20:55:41 [ERROR] Failed to POST: Post "http://localhost:4242/webhook": dial tcp [::1]:4242: connect: connection refused
Additional Context
I am running Fedora 38, and I believe there might be some configuration required to resolve this issue. If anyone has encountered a similar problem or knows how to fix it, I would greatly appreciate your assistance.
Thank you all for your support! 😇
I attempted to troubleshoot the Stripe Webhooks connection issue on Fedora 38. I expected that after enabling port 4242 permissions on my computer, the webhook connections would be established successfully. However, despite making this configuration change, the actual result remained unchanged. The connection to the webhook endpoint on localhost:4242/webhook
continued to be refused, and the error message persisted.
In summary, my efforts to resolve the issue by enabling port permissions did not yield the expected outcome of establishing successful webhook connections. The actual result was an ongoing connection refusal, preventing the expected functionality.
Solution
Whoops
Alright, I managed to resolve my issue! It turned out that the missing step was to have the webhook code running via Node.js. In the Stripe video tutorial I followed, they didn't explicitly mention this crucial step (unlike in the written documentation). Lesson learned - always read the docs thoroughly!
Here's the solution: You need to create a server.js file with the following code:
// server.js
//
// Use this sample code to handle webhook events in your integration.
//
// 1) Paste this code into a new file (server.js)
//
// 2) Install dependencies
// npm install stripe
// npm install express
//
// 3) Run the server on http://localhost:4242
// node server.js
// The library needs to be configured with your account's secret key.
// Ensure the key is kept out of any version control system you might be using.
const stripe = require('stripe')('sk_test_...')
const express = require('express')
const app = express()
// This is your Stripe CLI webhook secret for testing your endpoint locally.
const endpointSecret =
'your endpoint secret here'
app.post(
'/webhook',
express.raw({ type: 'application/json' }),
(request, response) => {
const sig = request.headers['stripe-signature']
let event
try {
event = stripe.webhooks.constructEvent(request.body, sig, endpointSecret)
} catch (err) {
response.status(400).send(`Webhook Error: ${err.message}`)
return
}
// Handle the event
switch (event.type) {
case 'payment_intent.succeeded':
const paymentIntentSucceeded = event.data.object
// Then define and call a function to handle the event payment_intent.succeeded
break
// ... handle other event types
default:
console.log(`Unhandled event type ${event.type}`)
}
// Return a 200 response to acknowledge receipt of the event
response.send()
}
)
app.listen(4242, () => console.log('Running on port 4242'))
Once you have created the server.js file, execute node server.js
in a terminal instance to run the server. Additionally, ensure that you are running stripe listen --forward-to localhost:4242/webhook
in another terminal instance. Finally, trigger the event on yet another terminal instance using stripe trigger payment_intent.succeeded
.
I apologize for my earlier confusion, but I hope this detailed solution helps others facing similar issues.
Answered By - ysaeldev Answer Checked By - David Goodson (WPSolving Volunteer)