Fedora 38 上的 Stripe Webhooks 连接问题

问题描述 投票:0回答:1

我在 Fedora 38 系统上使用 Stripe Webhooks 时面临一些挑战。我已按照说明操作并在终端中成功使用了

stripe login
命令。

接下来,我使用命令

stripe listen --forward-to localhost:4242/webhook
将事件转发到本地服务器的 webhook,这似乎工作正常。

但是,当我尝试使用 stripe 触发器 payment_intent.succeeded 在另一个终端实例中触发事件时,遇到以下错误:

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

其他背景

我正在运行 Fedora 38,我相信可能需要一些配置来解决此问题。如果有人遇到类似的问题或知道如何解决它,我将非常感谢您的帮助。

我尝试解决 Fedora 38 上的 Stripe Webhooks 连接问题。我预计在我的计算机上启用端口 4242 权限后,Webhook 连接将成功建立。然而,尽管进行了此配置更改,但实际结果仍然没有变化。与

localhost:4242/webhook
上的 Webhook 端点的连接继续被拒绝,并且错误消息仍然存在。

总而言之,我通过启用端口权限来解决问题的努力并未产生建立成功的 Webhook 连接的预期结果。实际结果是持续拒绝连接,阻止了预期的功能。

terminal stripe-payments webhooks fedora
1个回答
0
投票

哎呀

好吧,我成功解决了我的问题!事实证明,缺少的步骤是通过 Node.js 运行 webhook 代码。在我关注的 Stripe 视频教程中,他们没有明确提及这一关键步骤(与书面文档不同)。经验教训 - 务必彻底阅读文档!

解决方案如下:您需要使用以下代码创建一个 server.js 文件:

// 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'))

创建 server.js 文件后,在终端实例中执行

node server.js
来运行服务器。此外,请确保您正在另一个终端实例中运行
stripe listen --forward-to localhost:4242/webhook
。最后,使用
stripe trigger payment_intent.succeeded
在另一个终端实例上触发事件。

对于我之前的困惑,我深表歉意,但我希望这个详细的解决方案可以帮助其他面临类似问题的人。

© www.soinside.com 2019 - 2024. All rights reserved.