簡體   English   中英

如何在本地生成簽名的Stripe rest webhook請求?

[英]How do I generate a signed Stripe rest webhook request locally?

我試圖創建一個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

我認為這個問題與curl調用有關。 -d / --data參數從您的json剝離任何換行符,並且由Stripe::Webhook.construct_event計算得到的摘要與您在終端中計算的摘要不同。

生成摘要后,我蜷縮在我的webhook端點:

使用標准-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

最好的方法是使用Stripe CLI: https//github.com/stripe/stripe-cli

您可以將請求轉發到localhost地址。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM