【问题标题】:Rails: Cannot use Stripe token more than once - [Stripe::InvalidRequestError]Rails:不能多次使用 Stripe 令牌 - [Stripe::InvalidRequestError]
【发布时间】:2016-02-05 06:19:43
【问题描述】:

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

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

尽管有此错误消息,但我的条带仪表板中显示了已付款的客户。

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

以下是相关文件和图像,以展示我目前的实施工作情况。

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]

订阅/新(“小”计划的条纹结帐按钮)

<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

短划线

【问题讨论】:

  • 您好,我们很难调试您的代码的口头描述...请编辑您的问题并添加处理条纹的代码的相关部分。
  • 您好,感谢您的及时回复!我已经进行了编辑,以更清楚地说明我的问题所在。期待您的回复。
  • 看起来您将 API 密钥作为元标记包含在客户端代码中。这是不必要的,而且是一个巨大的安全漏洞——使用您的 API 密钥,任何人基本上都可以对您的帐户执行任何操作(创建费用、退款、删除数据等)
  • 如果你有一堆直接相关的代码,而不是使用不同的代码块来格式化它。您可以使用--- 来分隔代码块,或者只是不缩进文件的描述。滚动一个多行的代码块真的很难。
  • 您的initializers/stripe.rb 也很愚蠢。您不需要 if Rails.env == "production" - 只需在开发和生产中使用不同的环境变量 - 这就是使用它们的全部意义所在。

标签: ruby-on-rails ruby-on-rails-4 stripe-payments


【解决方案1】:

我通过像这样处理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。然后错误来自我的订阅控制器的创建方法中的这一行。

if create_stripe_subscription.valid?

删除 valid? 允许 create 方法完成,发送电子邮件并按应有的重定向。

【讨论】:

  • 刚刚注意到我仍然在我的服务器日志中看到这个 Stripe 错误。
  • 我调用了 create_stripe_subscription 方法两次。一次在我的 subscriptions_controller#create 方法的开头,并在它下面的条件中。删除第一个调用完全从我的日志中删除了错误。
猜你喜欢
  • 2016-08-21
  • 2013-11-04
  • 1970-01-01
  • 1970-01-01
  • 2016-02-16
  • 2015-06-21
  • 2015-03-04
  • 2019-03-31
  • 1970-01-01
相关资源
最近更新 更多