将Stripe密钥保存到数据库Rails时出现NoMethodError



在我的网站上创建用户的托管Stripe帐户时,我正试图将用户的Stripe机密和可发布密钥保存到我的数据库中。

stripe_controller.rb

def create
  Stripe.api_key = Rails.configuration.stripe[:secret_key]
  @account = Stripe::Account.create(
    managed: true,
    country: params[:artist_payment_setting][:country],
    email: @artist.email,
    tos_acceptance: {
      ip: request.remote_ip,
      date: Time.now.to_i
    },
    legal_entity: {
      dob: {
        month: params[:artist_payment_setting][:month],
        day: params[:artist_payment_setting][:day],
        year: params[:artist_payment_setting][:year]
      },
      first_name: params[:artist_payment_setting][:first_name],
      last_name: params[:artist_payment_setting][:last_name],
      type: 'individual',
    }
  )
  if @account.save
    @payment = @artist.create_artist_payment_setting(
        currency: @account.default_currency,
        country: @account.country,
        stripe_id: @account.id,
        stripe_publishable_key: @account.keys.publishable, ***********problem********
        stripe_secret_key: @account.keys.secret            ***********problem********
      )
  end
  redirect_to artist_path(@artist)
end

不幸的是,我一直得到NoMethodError (undefined method 'publishable' for #<Array:0x007f57d83edaf0>)secret

API响应为

{
  keys:
    {
      secret: "secret_key"
      publishable: "publish_key"
    }
 }

不知道怎么拿到那些钥匙。

我认为这是Stripe的一个错误。看起来@account的作用就像一个Hash,所以@account.keys为对象返回一个键数组(具有讽刺意味的是,在这种情况下是[:keys]),而不是您所期望的。

文档中没有这一点,但将对象视为哈希格式可能会解决您的问题:

@account[:keys][:publishable]

请注意,Stripe::Payment中的#capture方法也有类似的错误,因为这也是Ruby保留字。不知道Stripe为什么习惯在回复中使用保留词。

最新更新