【问题标题】:Locally test event handler for Stripe webhooks in Rails在 Rails 中本地测试 Stripe webhook 的事件处理程序
【发布时间】:2014-01-12 08:56:03
【问题描述】:

我正在使用 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 相同。很奇怪。

我有一种感觉,我不应该将请求发送到开发服务器,而是可能发送到其他地方?有什么建议吗?提前谢谢!

【问题讨论】:

  • 您的目标是编写自动化测试,还是在本地使用 webhook 进行试验?有关编写自动化测试的示例,请参阅 stripe_event 自述文件的测试部分。在 irb 或 rails 控制台中运行 Stripe::Event.construct_from(fake_webhook_event)(假设 fake_webhook_event 是带有符号键的哈希)来调试 active/past_due 问题。
  • @dwhalen 感谢您的建议,我按照github.com/invisiblefunnel/test-hooks 中的规范解决了这个问题。

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


【解决方案1】:

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

特点

  • 无需访问条带服务器
  • 轻松测试条带错误
  • 模拟和自定义条纹 webhook
  • 拨动开关以针对 Stripe 的实时测试服务器运行测试

【讨论】:

    猜你喜欢
    • 2019-04-14
    • 2015-10-24
    • 2017-10-25
    • 2020-04-26
    • 2021-05-14
    • 2017-03-04
    • 2021-10-24
    • 1970-01-01
    • 2020-05-16
    相关资源
    最近更新 更多