我如何在本地生成带签名的Stripe rest webhook请求?

7
我尝试创建一个本地测试Webhook请求,但是库出现了错误。我通过发送一个测试balance.available Webhook来生成请求的主体,链接在这里:https://dashboard.stripe.com/test/webhooks/we_1BI2E2IYOmXNPhc1uOyyRvHg。我将主体复制并放入了文件 /tmp/stripe.webhook.json.tmp 中。文档描述了如何生成签名:https://stripe.com/docs/webhooks#signatures
$ date +%s
1509229775
$ cat /tmp/stripe.webhook.tmp | openssl dgst -hmac whsec_nRZzpzBajM5zBLxnyFAHNZLkLLEu5Xlj -sha256
(stdin)= de2da72d739f0bdf0e2289eab5ac131f51cdd35af8f9c1f1224333b53abde9f7
$ curl -s -X POST http://localhost:3000/stripe/webhook -H "Stripe-Signature: t=1509229775,v1=de2da72d739f0bdf0e2289eab5ac131f51cdd35af8f9c1f1224333b53abde9f7" -d @/tmp/stripe.webhook.json.tmp | head -2         
Invalid signature.
$ head -2 /tmp/stripe.webhook.tmp
1509229775.{
  "created": 1326853478,
$ head -2 /tmp/stripe.webhook.json.tmp
{
  "created": 1326853478,

  def webhook
    payload = request.body.read
    sig_header = request.env['HTTP_STRIPE_SIGNATURE']
    endpoint_secret = ENV['STRIPE_WEBHOOK']
    event = nil
    begin
      event = Stripe::Webhook.construct_event(payload, sig_header,
endpoint_secret)
    rescue JSON::ParserError => e
      # Invalid payload
      render plain: "Invalid JSON.", status: 400
      return
    rescue Stripe::SignatureVerificationError => e
      # Invalid signature
      render plain: "Invalid signature.", status: 400
      return
    end

2个回答

4
我认为问题与curl调用有关。 -d/--data参数从你的json中剥离了任何换行符,导致由Stripe::Webhook.construct_event计算的摘要与你在终端中计算的不同。
生成摘要后,我在我的webhook端点上进行了curl:
使用标准的-d,抛出错误,说签名无效。
curl -s -X POST http://localhost:3000/webhook  -H "Stripe-Signature: t=1509309309,v1=a2e2776cd5a57ba60355f7cfa3bcdd1d69e773373a0da" -d @./webhook.json.tmp

然而,指定--data-binary返回了有效的签名。

curl -s -X POST http://localhost:3000/webhook  -H "Stripe-Signature: t=1509309309,v1=a2e2776cd5a57ba60355f7cfa3bcdd1d69e773373a0da" --data-binary @./webhook.json.tmp

哇,太棒了!我不知道“-d”可以去掉字符! - Chloe
对吧?这不是非常明显。我曾经遇到过类似的问题,只有通过检查发送的原始请求才学会了这个。 - duck

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接