Rails:不能多次使用 Stripe 令牌 - [Stripe::InvalidRequestError]

Rails: Cannot use Stripe token more than once - [Stripe::InvalidRequestError]

我在 Rails 4x 应用程序中使用条带结帐。我的目标是提供每月订阅。我收到以下信息。

Stripe::InvalidRequestError 不能多次使用 stripeToken。当在 subscriptions_controller#create 中调用 create_stripe_subscription 时会发生这种情况。

尽管有此错误消息,但我的 stripe 控制面板中仍显示有客户收费。

我想了解我在哪里复制这个一次性使用令牌,并了解此订阅需要什么条带。

这里有相关的文件和图像来展示我的实施工作到目前为止的样子。

initializers/stripe.rb:

Rails.configuration.stripe = {
  publishable_key: ENV["STRIPE_PUBLISHABLE_KEY"],
  secret_key: ENV["STRIPE_SECRET_KEY"]
}

Stripe.api_key = Rails.configuration.stripe[:secret_key]

subscriptions/new("small" 计划的条纹结帐按钮)

<script src="https://checkout.stripe.com/checkout.js" 
class="stripe-button",
  data-key="<%= ENV["STRIPE_PUBLISHABLE_KEY"] %>",
  data-email="<%= current_employee.email %>",
  data-image="app/assets/images/mascot_favicon.ico",
  data-name="Small Group Home",
  data-description="Monthly subcription plan",
  data-amount="<%= @small_plan.amount %>",
  data-id="<%= @small_plan.id %>",
  data-label="Subscribe!">
</script>

subscriptions_controller.rb

def create
  create_stripe_subscription

  if create_stripe_subscription.valid?
    AdminMailer.welcome_email(@admin).deliver_now
    flash[:success] = "#{ @admin.full_name.pluralize } created!"
    redirect_to root_path
  else
    flash.now[:notice] = "There was a problem with the form"
    render :new
  end
end
.    
.
.
def create_stripe_subscription 
  plan_id = params[:plan_id]
  plan = Stripe::Plan.retrieve(plan_id)
  token = params[:stripeToken]
  email = params[:stripeEmail] 

  customer = Stripe::Customer.create(
    source: token,
    email: email,
    plan: plan
  )

  subscription = Subscription.new
  subscription.stripe_card_token = customer.id

  same_customer = Stripe::Customer.retrieve(subscription.stripe_card_token)
  same_customer.subscriptions.create(plan: params[:plan_id])
end

叶柄破折号

我通过像这样处理 create_stripe_subscription method 中的 Stripe::InvalidRequestError 弄明白了:

subscriptions_controller.rb

def create_stripe_subscription
.
.
.
rescue Stripe::InvalidRequestError => e
  logger.error "Stripe error: #{e.message}"
end

这导致我的应用出现 noMethodError。然后错误来自我 subscriptions_controller 的创建方法中的这一行。

if create_stripe_subscription.valid?

删除 valid? 允许创建方法完成,发送电子邮件并按原样重定向。