Ruby on rails 如何在验证之前等待条带API调用返回
我正在将Stripe与Desive集成。我想用stripe注册一个用户,并在保存该用户之前存储该stripe ID。该方法在API调用返回客户哈希之前完成。我怎样才能解决这个问题Ruby on rails 如何在验证之前等待条带API调用返回,ruby-on-rails,ruby,stripe-payments,Ruby On Rails,Ruby,Stripe Payments,我正在将Stripe与Desive集成。我想用stripe注册一个用户,并在保存该用户之前存储该stripe ID。该方法在API调用返回客户哈希之前完成。我怎样才能解决这个问题 class User < ActiveRecord::Base validates_presence_of :stripe_id before_validation :create_stripe_customer def create_stripe_customer cu
class User < ActiveRecord::Base
validates_presence_of :stripe_id
before_validation :create_stripe_customer
def create_stripe_customer
customer = Stripe::Customer.create(
:email => email,
:card => stripe_card_token
)
self.stripe_id = customer.id
end
end
class用户email,
:card=>stripe\u card\u令牌
)
self.stripe\u id=customer.id
结束
结束
当我检查用户时,stripe_id为零,验证失败。实现这一点的最佳方法是使用
活动记录事务
用保护块包装它
事务是保护块,其中SQL语句只有在它们都可以作为一个原子操作成功时才是永久的
这将强制语句一起执行或根本不执行
begin
@user = User.new(strong_params)
User.transaction do
@user.save!
customer = Stripe::Customer.create(
:email => email,
:card => stripe_card_token
)
@user.update_attributes!(stripe_id: customer.id)
end
rescue Exception => ex
flash[:danger] = "#{ex}"
render 'new'
end