首页 文章

条带:未定义的方法'last4'检索Stripe charge - Rails的最后4位数

提问于
浏览
0

我的rails应用程序成功使用Stripe进行付款,但在尝试从成功收费中检索信用卡的最后4位数时,我收到了未定义的方法错误 .

错误:

undefined method `last4' for #<Stripe::Charge:0x007ff2704febc8>

app/models/order.rb:33:in `stripe_retrieve'
app/controllers/orders_controller.rb:54:in `block in create'
app/controllers/orders_controller.rb:52:in `create'

orders_controller.rb

def create
if current_user
  @order = current_user.orders.new(params[:order])
else
  @order = Order.new(params[:order])
end
respond_to do |format|
  if @order.save_with_payment
    @order.stripe_retrieve

    format.html { redirect_to auctions_path, :notice => 'Your payment of $1 has been successfully processed and your credit card has been linked to your account.' }
    format.json { render json: @order, status: :created, location: @order }
    format.js
  else
    format.html { render action: "new" }
    format.json { render json: @order.errors, status: :unprocessable_entity }
    format.js
  end
end
end

order.rb

def save_with_payment
if valid?
  customer = Stripe::Customer.create(description: email, card: stripe_card_token)
  self.stripe_customer_token = customer.id
  self.user.update_column(:customer_id, customer.id)
  save!

  Stripe::Charge.create(
      :amount => (total * 100).to_i, # in cents
      :currency => "usd",
      :customer => customer.id
  )
 end
rescue Stripe::InvalidRequestError => e
logger.error "Stripe error while creating customer: #{e.message}"
errors.add :base, "There was a problem with your credit card."
false
end

def stripe_retrieve
charge = Stripe::Charge.retrieve("ch_10U9oojbTJN535")
self.last_4_digits = charge.last4
self.user.update_column(:last_4_digits, charge.last4)
save!
end

这是显示如何检索费用的Stripe文档,您可以看到'last4'是正确的,那么为什么它会以未定义的方法出现?

https://stripe.com/docs/api?lang=ruby#retrieve_charge

1 回答

  • 3

    响应将返回一张本身有last4的卡片 . 所以卡片是它自己的对象 .

    charge.card.last4
    

    这是文档:

    #<Stripe::Charge id=ch_0ZHWhWO0DKQ9tX 0x00000a> JSON: {
      "card": {
        "type": "Visa",
        "address_line1_check": null,
        "address_city": null,
        "country": "US",
        "exp_month": 3,
        "address_zip": null,
        "exp_year": 2015,
        "address_state": null,
        "object": "card",
        "address_country": null,
        "cvc_check": "unchecked",
        "address_line1": null,
        "name": null,
        "last4": "1111",
    

相关问题