我想从Stripe Webhook获取数据(客户ID),然后将其保存到我的数据库中,我得到了数据,但问题是在我的数据库中customer_id仍然为零。这是我的代码
StripeEvent.configure do |events|
events.all do |event|
if event.type == 'customer.created'
customer = event.data.object
user = User.where('customer_id LIKE ?', "%#{customer['id']}%").first
if user
customer = Stripe::Customer.retrieve(JSON.parse(user.customer_id)['id'])
user.customer_id = customer.to_json
user.save!
end
end
end
end
此代码:
user = User.where('customer_id LIKE ?', "%#{customer['id']}%").first
提供以下输出:
SELECT "users".* FROM "users" WHERE (customer_id LIKE '%cus_EEIReNX3M4je2U%') ORDER BY "users"."id" ASC LIMIT
当我进入数据库时,我的customer_id字段仍然为nil,这是输出:
<User id: 3, email: "achielinda44@yahoo.com", created_at: "2018-12-27 03:47:41", updated_at: "2018-12-27 03:47:41", first_name: "Achie", last_name: nil, gender: nil, gender_preferences: nil, description: "trial stripe", photo: nil, provider: nil, uid: nil, name: nil, image: nil, customer_id: nil>
任何人都可以看到我缺少的东西吗?我将非常感谢您的帮助。谢谢!
答案 0 :(得分:1)
我的代码全都错了,虽然不是全部。用户对象为nil,因为我试图获取一个尚不可用的customer_id,因此我让用户使用电子邮件,然后将条带化的客户ID传递给他们,最后保存了它们。奏效了!
StripeEvent.configure do |events|
events.all do |event|
if event.type == 'customer.created'
customer = event.data.object
user = User.find_by(email: customer.email)
if user
user.customer_id = customer.id
user.save!
end
end
end