【发布时间】: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