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」が正しいことがわかりますが、なぜ未定義のメソッドとして表示されるのですか?