条纹webhook没有以正确的顺序传入错误



我一直在使用stripe_eventgem来获取来自stripe的webhook,今天我发现webhook似乎没有按照正确的顺序进入。我正在存储在stripe上的收费的本地副本,我有以下设置

StripeEvent.configure do |events|
events.subscribe 'payment_intent.created', CreateChargeRecord.new
events.subscribe 'payment_intent.processing', UpdateChargeRecord.new
events.subscribe 'payment_intent.succeeded', UpdateChargeRecord.new
end

查看条纹仪表板,事件payment_intent.created总是在payment_intent.processing之前完成。现在我创建的收费记录如下

class CreateChargeRecord
def call(event)
charge_object = event.data.object
Charge.create(
user: User.find_by(stripe_id: charge_object.customer),
receiver_id: User.find_by(merchant_id: charge_object.transfer_data.destination).id,
amount: charge_object.amount / 100,
currency: 'eur',
application_fee_amount: charge_object.application_fee_amount / 100,
status: charge_object.status,
stripe_id: charge_object.id
)
end
end

和更新如下

class UpdateChargeRecord
def call(event)
charge_object = event.data.object
charge = Charge.find_by(stripe_id: charge_object.id)
charge.update!(status: charge_object.status)
end
end

问题是,从cli看来,webhookpayment_intent.processingpayment_intent.created之前被接收

2021-10-10 22:08:50   --> payment_intent.processing 
2021-10-10 22:08:51   --> payment_intent.created 
2021-10-10 22:08:51  <--  [200] POST http://localhost:3000/webhooks/
2021-10-10 22:08:51  <--  [500] POST http://localhost:3000/webhooks/

导致错误NoMethodError (undefined method更新!' for nil:NilClass): '因为我正在尝试更新一个还不存在的记录。

我能做些什么来防止这种情况,因为这似乎不是一个错误根据条纹?

这是意料之中的,并且是有文档记录的——事件的交付顺序可能与它们生成的顺序不一致。

为了解决这个问题,您的应用程序应该意识到这是可能的,例如,当接收到事件时,您可以直接从API检索最新的对象状态,而不是使用负载。

最新更新