您不能多次使用 Stripe 令牌

You cannot use a Stripe token more than once

我似乎无法在 Rails 4.

中对卡进行收费然后即时创建客户
def charge
 token = params[:stripeToken] # can only be used once.
 begin
  charge = Stripe::Charge.create(
    :amount => 5000,
    :currency => "gbp",
    :source => token,
    :description => "Example charge"
  )
 rescue Stripe::CardError => e
  # The card has been declined
 end

 if current_user.stripeid == nil
  customer = Stripe::Customer.create(card: token, ...)
  current_user.stripeid = customer.id
  current_user.save
 end
end

但没有 token.id 这样的东西,因为 token 只是一个 String.

您似乎在两个位置使用令牌:

charge = Stripe::Charge.create(
    :amount => 5000,
    :currency => "gbp",
    :source => token,
    :description => "Example charge"
  )

还有这里:

customer = Stripe::Customer.create(card: token, ...)

事实上,从令牌创建 Stripe 费用也应该与卡一起创建客户,如果它还不存在的话。您创建客户的步骤是不必要的。因此,只需从源中获取 Stripe 客户:

current_user.update_attribute(:stripeid, charge.source.customer)

相关条纹文档: https://stripe.com/docs/api/ruby#create_charge

编辑

如果您想更好地控制充电过程,请独立创建每个对象:

customer = Stripe::Customer.create(
  description: "Example customer",
  email: current_user.email
)

card = customer.sources.create(
  source: "<stripe token>"
  customer: customer.id
)

Stripe::Charge.create(
  amount: 5000,
  currency: "gbp",
  source: card.id,
  customer: customer.id
)