【问题标题】:NoMethodError `sources' for #<Stripe::Customer with Rails#<Stripe::Customer with Rails 的 NoMethodError `sources'
【发布时间】:2021-06-27 00:15:28
【问题描述】:

当我尝试从 Stripe 客户接收数据时,我收到错误 undefined method 'sources'。

这是我使用的代码:

    def update_payment 
    
    if !current_user.stripe_id
      customer = Stripe::Customer.create(
        email: current_user.email,
        source: params[:stripeToken]
      )
    else
      customer = Stripe::Customer.update(
        current_user.stripe_id,
        source: params[:stripeToken]
      )
    end


    if current_user.update(stripe_id: customer.id, stripe_last_4: customer.sources.data.first["last4"])
      flash[:notice] = "New card is saved"
    else
      flash[:alert] = "Invalid card"
    end
    redirect_to request.referrer
  rescue Stripe::CardError => e
    flash[:alert] = e.message
    redirect_to request.referrer
  end

客户变量的输出不是空的,所以我不明白这个错误是怎么可能的。

【问题讨论】:

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


    【解决方案1】:

    默认情况下,Stripe API 中不再提供sources,因为它是一个已弃用的集成路径:https://stripe.com/docs/api/customers/object?lang=ruby#customer_object-sources

    您需要明确声明您希望通过扩展返回sources 参数。例如

    cust = Stripe::Customer.create({
      email: 'example@test.com',
      source: 'tok_visa',
      expand: ['sources']
    })
    
    // cust.sources is now populated
    puts cust.sources
    

    【讨论】:

      【解决方案2】:

      sources 属性现在是可选的。 为了让您将来源作为响应的一部分,您需要对其进行扩展。 将来源字段添加到 Stripe Create Customer。

      例子:

      customer = Stripe::Customer.create(
       email: current_user.email,
       source: params[:stripeToken],
       expand: ['sources']
      )
      

      【讨论】:

        猜你喜欢
        • 2021-01-19
        • 2019-02-07
        • 2014-11-04
        • 2021-01-05
        • 2016-02-18
        • 1970-01-01
        • 1970-01-01
        • 2016-07-20
        • 1970-01-01
        相关资源
        最近更新 更多