【问题标题】:NoMethodError when saving Stripe keys to database, Rails将Stripe键保存到数据库Rails时出现NoMethodError
【发布时间】:2016-07-20 15:17:51
【问题描述】:

我正在尝试在我的网站上创建他们的托管 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"
    }
 }

不知道如何获得这些密钥。

【问题讨论】:

  • 您查看过@account.keys.inspect 以了解其中包含的内容吗?错误消息表明它是Array...
  • 是的,我不明白为什么它会以Array 的形式返回。 API 的响应不是。
  • 尝试使用byebug 在您的控制台中操作@account.keys。你能发布 @account 在保存之前的样子吗?
  • @account 只是我用来创建新 Stripe 帐户的变量。而且 Stripe 帐户运行 if @account.save 部分没有任何问题。它可以毫无问题地检索stripe_idcurrencycountry,但这些不是嵌套的。我认为将secretpublishable 嵌套在keys 中会导致它被抛弃。

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


【解决方案1】:

我认为这是 Stripe 的一个错误。似乎@account 的行为类似于哈希,因此@account.keys 为对象返回了一个键数组(在这种情况下具有讽刺意味的是[:keys]),而不是您所期望的。

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

@account[:keys][:publishable]

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

【讨论】:

  • 刚刚尝试过 - 以NoMethodError (undefined method '[] ' for nil:NilClass)返回
  • 你能试试@account.to_h吗?它会给你上面提供的 API 响应吗?
猜你喜欢
  • 2019-05-25
  • 1970-01-01
  • 2016-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-14
  • 2019-04-04
  • 2021-07-21
相关资源
最近更新 更多