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'

订单控制器.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

订单.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

4

1 回答 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",
于 2012-12-29T18:27:39.827 回答