【发布时间】:2017-06-30 13:20:48
【问题描述】:
我只是想知道我一般如何处理来自第三方 API 的 webhook。
就我而言,我需要处理来自 Stripe 的 Webhooks。
所以我用
- StripeEvent 处理和监听webhook handlers的入口。它提供了一个易于使用的界面来处理来自 Stripe 的事件。
主要实现是:
- 从 POSTed 事件数据中获取 ID
- stripe 不签署事件,so to verify by fetching event from Stripe API。
- 存储事件 (id) 并拒绝我们已经看到的 ID 以防止重放攻击。
到目前为止一切正常。
但是,让我们假设
- 在 webhook 处理程序中处理复杂的小逻辑
- 侦听许多 webhook 请求
在这种情况下,我觉得我需要考虑使用后台工作。
如果您的 webhook 脚本执行复杂的逻辑或进行网络调用,则脚本可能会在 Stripe 看到其完整执行之前超时。出于这个原因,您可能希望通过返回 2xx HTTP 状态代码让您的 webhook 端点立即确认收到,>然后执行其其余职责。
这是我的代码, 我只是想知道我应该捆绑哪个部分并入队?
StripeEvent.event_retriever = lambda do |params|
return nil if StripeWebhook.exists?(stripe_id: params[:id])
StripeWebhook.create!(stripe_id: params[:id])
return Stripe::Event.construct_from(params.deep_symbolize_keys) if Rails.env.test? # fetching the event from Stripe API
return Stripe::Event.retrieve(params[:id])
end
StripeEvent.configure do |events|
events.subscribe 'invoice.created', InvoiceCreated.new # handling the invoice.created event in service object
events.subscribe 'invoice.payment_succeeded', InvoicePaymentSucceeded.new
...
end
【问题讨论】:
-
我猜都是
标签: ruby-on-rails stripe-payments webhooks