条纹检索客户 Rails5
Stripe retrieve customer Rails5
我希望 Stripe 检查客户是否已经存在,如果是,则将其关联到结帐会话。
class OrdersController < ApplicationController
@session = Stripe::Checkout::Session.create(
payment_method_types: ['card'],
shipping_address_collection: {
allowed_countries: ['GB']
},
customer_email: current_user.email,
line_items: line_items_order,
success_url: new_order_message_url(@order),
cancel_url: order_url(@order)
)
if @user.stripe_id != nil
Stripe::Customer.retrieve(@user.stripe_id)
@session.customer = customer.id
@session.customer_email =customer.email
else
customer = Stripe::Customer.create({
email: current_user.email,
name: current_user.username,
metadata: {
},
})
@user.stripe_id = customer.id
@user.save
end
@order.checkout_session_id = @session.id
@order.save
end
我检查了 byebug,发现 Stripe::Customer.retrieve(@user.stripe_id) 正在运行,Stripe API 能够找到正确的客户,但仍然创建了一个新客户对于每个结帐会话。我已经阅读了关于会话 objetc 的文档,发现关于客户属性:
customer attribute:
The ID of the customer for this Session. For Checkout Sessions in payment or subscription mode, Checkout will create a new customer object based on information provided during the payment flow unless an existing customer was provided when the Session was created.
我在这里想念什么?
问题是您在未传递客户的情况下调用 Stripe::Checkout::Session.create
。如客户文档中所述,如果要使用现有客户,则应在创建会话时传递它
customer string EXPANDABLE
The ID of the customer for this Session. For Checkout Sessions in
payment or subscription mode, Checkout will create a new customer
object based on information provided during the payment flow unless an
existing customer was provided when the Session was created.
所以只要确保你在创建方法中有客户
@session = Stripe::Checkout::Session.create(
customer: customer.id
...
)
我希望 Stripe 检查客户是否已经存在,如果是,则将其关联到结帐会话。
class OrdersController < ApplicationController
@session = Stripe::Checkout::Session.create(
payment_method_types: ['card'],
shipping_address_collection: {
allowed_countries: ['GB']
},
customer_email: current_user.email,
line_items: line_items_order,
success_url: new_order_message_url(@order),
cancel_url: order_url(@order)
)
if @user.stripe_id != nil
Stripe::Customer.retrieve(@user.stripe_id)
@session.customer = customer.id
@session.customer_email =customer.email
else
customer = Stripe::Customer.create({
email: current_user.email,
name: current_user.username,
metadata: {
},
})
@user.stripe_id = customer.id
@user.save
end
@order.checkout_session_id = @session.id
@order.save
end
我检查了 byebug,发现 Stripe::Customer.retrieve(@user.stripe_id) 正在运行,Stripe API 能够找到正确的客户,但仍然创建了一个新客户对于每个结帐会话。我已经阅读了关于会话 objetc 的文档,发现关于客户属性:
customer attribute: The ID of the customer for this Session. For Checkout Sessions in payment or subscription mode, Checkout will create a new customer object based on information provided during the payment flow unless an existing customer was provided when the Session was created.
我在这里想念什么?
问题是您在未传递客户的情况下调用 Stripe::Checkout::Session.create
。如客户文档中所述,如果要使用现有客户,则应在创建会话时传递它
customer string EXPANDABLE
The ID of the customer for this Session. For Checkout Sessions in payment or subscription mode, Checkout will create a new customer object based on information provided during the payment flow unless an existing customer was provided when the Session was created.
所以只要确保你在创建方法中有客户
@session = Stripe::Checkout::Session.create(
customer: customer.id
...
)