Stripe webhooks 的錯誤率約為 90%,反應無法預測。伺服器問題?

Stripe webhooks 的錯誤率約為 90%,反應無法預測。伺服器問題?

我是第一次設定 Stripe webhooks。我已經發出了數百個測試請求,錯誤率約為 90%。失敗發生的原因是無法預測的。

Stripe 儀表板上的失敗反應是:

Timed out connecting to remote host

或者

Failed to connect to remote host

我的網路鉤子(我已經簡化了它以進行測試)。大約 10% 的情況下,我會收到 200 的回覆{received: true}

expressRouter.route('/hooks').post( async (req, res) => {

  const event = req.body;

  console.log("Event:");
  console.log(event);

  // Handle the event
  switch (event.type) {
    case 'payment_intent.succeeded':
      const paymentIntent = event.data.object;
      // Then define and call a method to handle the successful payment intent.
      // handlePaymentIntentSucceeded(paymentIntent);
      break;
    case 'payment_method.attached':
      const paymentMethod = event.data.object;
      // Then define and call a method to handle the successful attachment of a PaymentMethod.
      // handlePaymentMethodAttached(paymentMethod);
      break;
    // ... handle other event types
    default:
      console.log(`Unhandled event type ${event.type}`);
  }

  // Return a response to acknowledge receipt of the event
  res.json({received: true});
})

我嘗試過這兩種定義鉤子的方法,以及有和沒有異步的:

expressRouter.route('/hooks').post( async (req, res) => {
  ...
})
// and 
app.post("/hooks", async (req, res) => {
  ...
})

我已與 Stripe 支援人員聯繫並嘗試了他們的所有建議。他們給我留下了以下可能性:

可能存在網路緩慢或路由的其他問題。

主機提供者可能還需要允許 Stripe 的傳送 IP 位址,僅供參考,他們可能在到達您的伺服器之前被阻止

我已將 Stripe 的 IP 新增至 iptables 中,例如:

iptables -I INPUT -p tcp -s 3.18.12.63 -j ACCEPT

我正在 Ubuntu 18.04 上使用 Caddy 運行 Hostinger VPS 伺服器。這可能是我的伺服器設定的問題嗎?任何意見都是極大的讚賞。

答案1

我似乎有它的工作。

我透過 apt 安裝了 ngrok (https://ngrok.com/download

然後按照以下內容:youtube.com/watch?v=S1uExj7mMgM&ab_channel=Twilio,並設定ngrok轉發到localhost:4000:

ngrok http 4000

我只是發送了大量請求,它們都成功了。我不太清楚這是如何工作的/為什麼我需要它,但我很高興它能工作。如果有人可以解釋為什麼沒有這個就無法工作,請告訴我!

相關內容