在Rails中为Stripe webhooks本地测试事件处理程序

时间:2013-12-24 05:34:53

标签: ruby-on-rails event-handling rspec-rails stripe-payments webhooks

我正在使用stripe_event gem为Stripe webhooks设置我的事件处理程序。我想发送一封电子邮件,通知客户他们的订阅是否未续订。一个这样的设置如下:

# config/initializers/stripe.rb

StripeEvent.setup do

  subscribe 'customer.subscription.updated' do |event|
    subscription = event.data.object
    customer_id = subscription.customer
    user = User.find_by_customer_id(customer_id)

    UserMailer.delay.past_due_email(user) if subscription.status == 'past_due'
  end

end

现在我的目标是在本地测试上面的事件处理程序(不依赖于ngrok或ultrahook等)。

在我的规范中,我构建了一个类型为"customer.subscription.updated"的虚假Stripe webhook事件。它还包含对localhost:3000/stripe的POST请求:

RestClient.post('localhost:3000/stripe', fake_webhook_event)

我在本地启动rails服务器并运行规范。服务器正在接收POST请求并且正在运行事件处理程序,但请求中包含的订阅对象的状态为active而不是past_due。其他属性(例如事件ID)与我编写的虚假webhook相同。很奇怪。

我有一种感觉我不应该将请求发送到开发服务器,但也许在其他地方?有什么建议?提前谢谢!

1 个答案:

答案 0 :(得分:1)

享受使用这颗宝石 https://github.com/stripe-ruby-mock/stripe-ruby-mock

<块引用>

特点

  • 无需访问条带服务器
  • 轻松测试条带错误
  • 模拟和自定义条纹网络钩子
  • 翻转开关,在 Stripe 的实时测试服务器上运行您的测试