Stripe Webhook on Rails

我知道还有另外一个与此类似的问题,但我认为它没有得到很好的回答。

基本上我有一个工作轨道应用程序,用户可以注册我的订阅,输入信用卡信息等。这一切都有效。 但我需要处理在此定期订阅期间用户卡被拒绝的情况。

他们发送的事件类型如下: https : //stripe.com/docs/api?lang = ruby​​ #event_types 。

我在访问应用中的charge.failed对象时遇到问题。

关于webhooks的文档也在这里: https ://stripe.com/docs/webhooks,任何帮助将不胜感激。

您需要创建一个控制器来基本上接受和处理请求。 这是非常直接的,虽然不是最初直接包装你的想法。 这是我的hooks_controller.rb的一个例子:

class HooksController < ApplicationController require 'json' Stripe.api_key = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 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) @profile = Profile.find(User.find_by_stripe_customer_token(data['data']['object']['customer']).profile) if @profile.payment_received == false @profile.payment_received = true @profile.save! end end def make_inactive(data_event) @profile = Profile.find(User.find_by_stripe_customer_token(data['data']['object']['customer']).profile) if @profile.payment_received == true @profile.payment_received = false @profile.save! end end end 

def接收器是您必须将webhook指向条带接口的视图。 视图接收json,我正在使用它来更新用户的配置文件,以防付款失败或成功。

现在使用stripe_event gem要容易stripe_event

https://github.com/integrallis/stripe_event

这是一个不太理想的测试情况……

Stripe需要一种“强制”webhook用于测试目的的方法。 目前,您可以进行的最短订阅为期一周(在测试模式下); 如果您可以将其设置为1分钟,1小时,甚至只是简单地使回调实时发生,那将更有帮助,因此您可以测试您的API响应系统。

本地测试很棒,但没有什么可以取代现实世界,现场,互联网,webhooks /回调。 等待一周(!)会严重减慢项目速度。