【问题标题】:Rails 5 Stripe::AuthenticationError - create productRails 5 Stripe::AuthenticationError - 创建产品
【发布时间】:2018-09-17 17:11:08
【问题描述】:

我正在使用 Rails 5、Ruby 2.4.0 和 stripe gem 构建应用程序。

我有一个产品脚手架,当我创建一个产品时,如果它保存我希望它然后将产品发送到条带并在那里创建产品,并将条带产品 ID 返回到产品记录。

它自己保存的产品,因为我可以在控制台中查询它,它出现在索引页面上,但是当它发送到条带时,我收到以下错误。

我的 ApplicationController 文件:

class ApplicationController < ActionController::Base

  require "stripe"

  protect_from_forgery with: :exception
end

我的完整条带初始化文件:config/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]

我的产品控制器创建操作:

  def create
    @product = Product.new(product_params)

    respond_to do |format|
      if @product.save
        format.html { redirect_to @product, notice: 'Product was successfully created.' }
        format.json { render :show, status: :created, location: @product }

        product = Stripe::Product.create({
          name: @product.prod_name,
          type: @product.prod_type,
          statement_descriptor: @product.statement_descriptor,
          unit_label: @product.unit_label,
          product_status: @product.product_status
        })

      else
        format.html { render :new }
        format.json { render json: @product.errors, status: :unprocessable_entity }
      end
    end
  end

这里的任何帮助将不胜感激,因为我对条纹的经验很少,而且我似乎无法解决这个问题!

【问题讨论】:

    标签: ruby-on-rails stripe-payments


    【解决方案1】:

    您需要将STRIPE_PUBLISHABLE_KEY & STRIPE_SECRET_KEY 配置到您的config/application.yml 文件中

    您需要设置条带STRIPE_PUBLISHABLE_KEY & STRIPE_SECRET_KEY 以安全地设置这些密钥您可以在安装 Figaro gem 后使用figaro gem 然后将在config 目录中创建一个名为application.yml 的文件,您可以像这样设置您的密钥

    STRIPE_PUBLISHABLE_KEY: pk_test_xxxxxxxxxx
    STRIPE_SECRET_KEY: pk_test_xxxxxxxxxxxxx
    

    然后在config/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]
    

    就是这样,你已经做到了。

    【讨论】:

    • 因此,一旦创建了它,我该如何添加生成的 stripe 生成的 product_id 并将其保存到我在 rails 中创建的产品中?
    【解决方案2】:

    您需要将可发布密钥和秘密密钥设置为环境变量(或硬编码)

    【讨论】:

      猜你喜欢
      • 2017-02-24
      • 2015-11-16
      • 2020-09-15
      • 2021-02-10
      • 1970-01-01
      • 2019-01-27
      • 2021-05-25
      • 2016-04-07
      • 1970-01-01
      相关资源
      最近更新 更多