2017-10-28 144 views
2

我試圖創建一個webhook請求來測試本地,庫 給出了一個錯誤。我通過發送測試生成請求的主體 balance.available webhook here: https://dashboard.stripe.com/test/webhooks/we_1BI2E2IYOmXNPhc1uOyyRvHg 我複製正文並將其放入文件/tmp/stripe.webhook.json.tmp。 該文檔描述瞭如何生成簽名: https://stripe.com/docs/webhooks#signatures如何在本地生成簽名的Stripe休息webhook請求?


$ 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 

回答

1

我認爲這個問題已與curl調用來完成。 -d/--data參數將剝離json中的任何換行符,並且由Stripe::Webhook.construct_event計算的結果摘要與您在終端中計算的結果不同。

生成摘要之後,我在我的網絡掛接端點捲曲:

使用標準-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 
+0

哇,工作!我不知道'-d'剝掉了字符! – Chloe

+0

對不對?這不是很明顯。我正在努力解決類似問題,只通過檢查發送的原始請求來了解這一點。 – duck