我的 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”是正确的,那么为什么它会以未定义的方法出现?