如何在本地生成签名的 Stripe rest webhook 请求?
How do I generate a signed Stripe rest webhook request locally?
我尝试创建一个 webhook 请求以在本地进行测试,图书馆
给出了一个错误。我通过发送测试生成了请求的主体
balance.available 网络钩子在这里:
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
您可以将请求转发到本地主机地址。
我尝试创建一个 webhook 请求以在本地进行测试,图书馆 给出了一个错误。我通过发送测试生成了请求的主体 balance.available 网络钩子在这里: 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
您可以将请求转发到本地主机地址。