【发布时间】: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