#stripe_controller.rb
class StripeController < ApplicationController
protect_from_forgery except: :stripe_webook
skip_before_action :verify_authenticity_token
def stripe_webook
data_json = JSON.parse(request.body.read)
event_id = data_json['id']
data = Stripe::Event.retrieve(event_id)
handle_webhook(data)
head :ok
end
通过Stripe信息中心测试webhooks时只会出现500错误,但使用curl
curl -i -H "Content-Type: application/json"
-X POST -'{"id":"evt_0000000000000001","object":"event","api_version":"2016-03-07","created":1475273919,"data":{"object":{"id":"in_xxxxxxxxxxxxxxxxxxx","object":"invoice","amount_due":2900,"application_fee":null,"attempt_count":1,"attempted":true,"charge":"ch_xxxxxxxxxxxxxx","closed":false,"currency":"usd","customer":"cus_xxxxxxxxxxxx","date":1475270308,"description":null,"discount":null,"ending_balance":0,"forgiven":false,"lines":{"object":"list","data":[{"id":"sub_xxxxxxxxxxx","object":"line_item","amount":2900,"currency":"usd","description":null,"discountable":true,"livemode":false,"metadata":{},"period":{"start":1475270272,"end":1477862272},"plan":{"id":"tier_a_plan","object":"plan","amount":2900,"created":1475099850,"currency":"usd","interval":"month","interval_count":1,"livemode":false,"metadata":{},"name":"Startup","statement_descriptor":null,"trial_period_days":15},"proration":false,"quantity":1,"subscription":null,"type":"subscription"}],"has_more":false,"total_count":1,"url":"/v1/invoices/in_xxxxxxxxxxxxxxxx/lines"},"livemode":false,"metadata":{},"next_payment_attempt":1475360318,"paid":false,"period_end":1475270272,"period_start":1475269805,"receipt_number":null,"starting_balance":0,"statement_descriptor":null,"subscription":"sub_xxxxxxxxxxxxxxx","subtotal":2900,"tax":null,"tax_percent":null,"total":2900,"webhooks_delivered_at":1475270308}},"livemode":false,"pending_webhooks":1,"request":null,"type":"invoice.payment_failed"}' https://myurl.com/stripe/webhook
我正在200
在寻找答案后 - 我迷路了。
答案 0 :(得分:0)
那个500来自你的代码某处,只有你可以弄清楚在哪里。在调试器中单步执行它,或者引入一些日志语句以查看它在爆炸之前会有多远。
答案 1 :(得分:0)
您正在尝试获取一个不存在的Event
(因为它是一个标识为evt_0000000001
的测试webhook)
注释掉这一行,它应该有效:data = Stripe::Event.retrieve(event_id)
Stripe应该使这个测试事件可以检索,以便更好地进行测试。
此外,最佳做法是在顶部设置head: ok