简体   繁体   English

如何在 firebase 函数中检查 Stripe webhook 签名

[英]How to check Stripe webhook signatures in firebase functions

Hi 👋🏻I'm trying to verify a Stripe signature in Firebase functions.嗨👋🏻我正在尝试验证 Firebase 函数中的 Stripe 签名。 But when i try stripe.webhooks.constructEvent it catches an error saying:但是当我尝试stripe.webhooks.constructEvent它捕获了一个错误:

No signatures found matching the expected signature for payload. Are you passing the raw request body you received from Stripe? https://github.com/stripe/stripe-node#webhook-signing No signatures found matching the expected signature for payload. Are you passing the raw request body you received from Stripe? https://github.com/stripe/stripe-node#webhook-signing 😔 No signatures found matching the expected signature for payload. Are you passing the raw request body you received from Stripe? https://github.com/stripe/stripe-node#webhook-signing 😔

I've logged out the raw body, and it looks fine 🤖我已经注销了 raw body,看起来还不错 🤖

<Buffer 7b 0a 20 20 22 69 64 22 3a 20 22 65 76 74 5f 31 46 72 6b 4d 73 41 55 73 34 77 79 52 42 49 73 63 6d 66 72 43 39 7a 37 22 2c 0a 20 20 22 6f 62 6a 65 63 ... >

Here is the relevant code:这是相关的代码:

  // A webhook called by stripe
  const sig = req.headers['stripe-signature']
  let event
  // 1. construct event and validate
  try {
    event = stripe.webhooks.constructEvent(req.rawBody, sig, functions.config().stripe.mytestkey)
    assert(event)
  } catch (err) {
    console.log(`Error when constructing Stripe event: ${err} - ${req.body}`)
    res.status(400).send({ error: `Stripe webhook error: ${err}` })
    return
  }

  // 2. Handle webhook
  res.status(200).send(`successfully handled webhook ${hook}`)
})

Any thoughts?有什么想法吗? 💭 💭

Doing正在做

let sig = req.get('stripe-signature');

instead of代替

const sig = req.headers['stripe-signature']

should do the trick.应该做的伎俩。

According to the Express documentation :根据Express 文档

req.get(field)

Returns the specified HTTP request header field (case-insensitive match).返回指定的 HTTP 请求头字段(不区分大小写匹配)。

Finally, I figured out I had to write req.rawBody.toString('utf8') .最后,我发现我必须写req.rawBody.toString('utf8') Source: https://github.com/stripe/stripe-node/issues/341来源: https : //github.com/stripe/stripe-node/issues/341

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Firebase 云函数 - Stripe Connect Webhook 未触发 - Firebase Cloud Functions - Stripe Connect Webhook not firing 使用 Google Cloud Functions 的 Stripe webhook 不断给出 Webhook 错误:未找到与有效负载的预期签名匹配的签名 - Stripe webhook with Google Cloud Functions keeps giving Webhook Error: No signatures found matching the expected signature for payload Stripe webhook 在 firebase 功能中不起作用 - Stripe webhook not working in firebase function GCP:Stripe webhook 错误:未找到与有效负载的预期签名匹配的签名 - GCP: Stripe webhook error: No signatures found matching the expected signature for payload Stripe webhook 错误:未找到与有效负载的预期签名匹配的签名 - Stripe webhook error: No signatures found matching the expected signature for payload 条带连接和 Firebase 功能 - Stripe Connect and Firebase Functions 使用 firebase 函数处理 gocardless webhook - Handling gocardless webhook with firebase functions 条带 webhook 的 firebase 函数不适用于 async-await - firebase function for stripe webhook does not work with async-await 使用未触发的 Stripe Firebase 扩展 Webhook 运行订阅付款 - Run Subscription Payments with Stripe Firebase Extension Webhook Not Firing 条纹 Webhook 签名检查 - 节点 js - IBM Functions / OpenWhisk - Stripe Webhook Signature Checks - Node js - IBM Functions / OpenWhisk
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM