我已经在hooks_controller中设置了我的webhook,并且我成功地接收了webhook。
但是由于某种原因,来自'invoice.payment_succeeded'的更改不想工作。
class HooksController < ApplicationController
protect_from_forgery :except => :receiver
require 'json'
Stripe.api_key = "key"
def receiver
data_json = JSON.parse request.body.read
p data_json['data']['object']['customer']
if data_json[:type] == "invoice.payment_succeeded"
make_active(data_event)
end
if data_json[:type] == "invoice.payment_failed"
make_inactive(data_event)
end
end
def make_active(data_event)
@user = User.find_by_stripe_customer_id(data['data']['object']['customer'])
if @user.service_id.nil?
@user.service_id = "test"
@user.save!
end
end
end
我收到了,整个webhook似乎工作正常。
[2015-01-13 16:47:39] POST http://localhost:3000/hooks/receiver - 200
我还在rails控制台中重新创建了make_active方法,我输入了User.find_by_stripe_customer_id(“value”)并成功保存。
回复日志: 在2015-01-13 16:47:39 -0800开始POST“/ hooks / receiver”for 127.0.0.1 由HooksController#receiver作为XML处理 参数:{“id”=&gt;“evt_15KfJsKSH0PjiOYJ1MSTQxdi”,“created”=&gt; 1421196456,“livemode”=&gt; false,“type”=&gt;“invoice.payment_succeeded”,“data”=&gt; {“对象“=&gt; {”date“=&gt; 1421196455,”id“=&gt;”in_15KfJrKSH0PjiOYJJKOb0p1F“,”period_start“=&gt; 1421196455,”period_end“=&gt; 1421196455,”lines“=&gt; {”object“= &gt;“list”,“total_count”=&gt; 1,“has_more”=&gt; false,“url”=&gt;“/ v1 / invoices / in_15KfJrKSH0PjiOYJJKOb0p1F / lines”,“data”=&gt; [{“id” =&gt;“sub_5VghThK2pwN5kM”,“object”=&gt;“line_item”,“type”=&gt;“订阅”,“livemode”=&gt; false,“amount”=&gt; 500,“currency”=&gt;“ usd“,”proration“=&gt; false,”period“=&gt; {”start“=&gt; 1421196455,”end“=&gt; 1423874855},”subscription“=&gt; nil,”quantity“=&gt; 1 ,“plan”=&gt; {“interval”=&gt;“month”,“name”=&gt;“monkeyjr”,“created”=&gt; 1421185125,“amount”=&gt; 500,“currency”=&gt; “usd”,“id”=&gt;“1”,“object”=&gt;“plan”,“livemode”=&gt; false,“interval_count”=&gt; 1,“trial_period_days”=&gt; nil,“元数据“=&gt; {},”statement_descriptor“=&gt;”monke ywork - monkeyjr“},”description“=&gt; nil,”metadata“=&gt; {}}]},”subtotal“=&gt; 500,”total“=&gt; 500,”customer“=&gt;”cus_5VghMM74h7yUFG “,”object“=&gt;”invoice“,”attempts“=&gt; true,”closed“=&gt; true,”forgiven“=&gt; false,”paid“=&gt; true,”livemode“=&gt; false,“attempt_count”=&gt; 1,“amount_due”=&gt; 500,“currency”=&gt;“usd”,“starting_balance”=&gt; 0,“ending_balance”=&gt; 0,“next_payment_attempt”=&gt; nil,“webhooks_delivered_at”=&gt; nil,“charge”=&gt;“ch_15KfJrKSH0PjiOYJ03y6dXP1”,“discount”=&gt; nil,“application_fee”=&gt; nil,“subscription”=&gt;“sub_5VghThK2pwN5kM”,“tax_percent”= &gt; nil,“metadata”=&gt; {},“statement_descriptor”=&gt; nil,“description”=&gt; nil,“receipt_number”=&gt; nil}},“object”=&gt;“event”,“ pending_webhooks“=&gt; 1,”request“=&gt;”iar_5VghmqLO5Fy6Rt“,”api_version“=&gt;”2014-12-22“,”hook“=&gt; {”id“=&gt;”evt_15KfJsKSH0PjiOYJ1MSTQxdi“,”已创建“=&gt; 1421196456,”livemode“=&gt; false,”type“=&gt;”invoice.payment_succeeded“,”data“=&gt; {”object“=&gt; {”date“=&gt; 14 21196455,“id”=&gt;“in_15KfJrKSH0PjiOYJJKOb0p1F”,“period_start”=&gt; 1421196455,“period_end”=&gt; 1421196455,“lines”=&gt; {“object”=&gt;“list”,“total_count”=&gt; ; 1,“has_more”=&gt; false,“url”=&gt;“/ v1 / invoices / in_15KfJrKSH0PjiOYJJKOb0p1F / lines”,“data”=&gt; [{“id”=&gt;“sub_5FghThK2pwN5kM”,“object”= &gt;“line_item”,“type”=&gt;“订阅”,“livemode”=&gt; false,“amount”=&gt; 500,“currency”=&gt;“usd”,“proration”=&gt; false, “period”=&gt; {“start”=&gt; 1421196455,“end”=&gt; 1423874855},“subscription”=&gt; nil,“quantity”=&gt; 1,“plan”=&gt; {“interval” =&gt;“month”,“name”=&gt;“monkeyjr”,“created”=&gt; 1421185125,“amount”=&gt; 500,“currency”=&gt;“usd”,“id”=&gt;“ 1“,”object“=&gt;”plan“,”livemode“=&gt; false,”interval_count“=&gt; 1,”trial_period_days“=&gt; nil,”metadata“=&gt; {},”statement_descriptor“= &gt;“monkeywork - monkeyjr”},“description”=&gt; nil,“metadata”=&gt; {}}]},“subtotal”=&gt; 500,“total”=&gt; 500,“customer”=&gt; ;“cus_5VghMF74h7yUFG”,“object”=&gt;“invoice”,“attempts”=&gt; tr ue,“closed”=&gt; true,“forgiven”=&gt; false,“paid”=&gt; true,“livemode”=&gt; false,“attempt_count”=&gt; 1,“amount_due”=&gt; 500, “currency”=&gt;“usd”,“starting_balance”=&gt; 0,“ending_balance”=&gt; 0,“next_payment_attempt”=&gt; nil,“webhooks_delivered_at”=&gt; nil,“charge”=&gt;“ch_15KfJrKSHPjiOYJ03y6dXP1 “,”discount“=&gt; nil,”application_fee“=&gt; nil,”subscription“=&gt;”sub_5VghhK2pwN5kM“,”tax_percent“=&gt; nil,”metadata“=&gt; {},”statement_descriptor“=&gt ; nil,“description”=&gt; nil,“receipt_number”=&gt; nil}},“object”=&gt;“event”,“pending_webhooks”=&gt; 1,“request”=&gt;“iar_5VghmqLO5Fy6Rt”,“ API_VERSION “=&gt;” 中2014年12" 月22日}} “cus_5VghMF74h7yUFG” 在layouts / application(0.1ms)内渲染hooks / receiver.html.erb 渲染布局/ nav / _default.html.erb(0.8ms) 渲染布局/ _header.html.erb(12.2ms) 渲染布局/ _alerts.html.erb(0.1ms) 渲染布局/ _footer.html.erb(0.1ms) 在183ms完成200 OK(浏览次数:182.4ms | ActiveRecord:0.0ms)
答案 0 :(得分:0)
<强> 1。在这里,您试图获取data_json [:type]。而是使用data_json ['type']。使用下面提到的接收器操作。
def receiver
data_json = JSON.parse(request.body.read)
data_event = data_json['data']['object']['customer']
if data_json['type'] == "invoice.payment_succeeded"
make_active(data_event)
end
if data_json['type'] == "invoice.payment_failed"
make_inactive(data_event)
end
end
2:您在make_active方法中尝试使用数据哈希(仅限接收方本地)。而只是将该值赋给变量data_event并将其传递给make_active方法。请检查以下代码:
def make_active(data_event)
user = User.find_by_stripe_customer_id(data_event)
if user.service_id.nil?
user.service_id = "test"
user.save!
end
end
检查我的控制台:
我创建了我的示例应用&amp;将它连接到条带以测试webhook。 您可以在此gist link。
中引用我的代码