Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9057119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T14:22:21+00:00 2026-06-16T14:22:21+00:00

My rails app is successfully taking payments using Stripe but I’m getting an undefined

  • 0

My rails app is successfully taking payments using Stripe but I’m getting an undefined method error when trying to retrieve the last 4 digits of the credit card from a successful charge.

error:

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

Here’s the Stripe docs that show how to retrieve a charge, you can see that ‘last4’ is correct so why does it come up as undefined method?

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T14:22:22+00:00Added an answer on June 16, 2026 at 2:22 pm

    The response will return a card which itself has a last4. So the card is its own object.

    charge.card.last4
    

    Here’s the docs:

    #<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",
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the respond_to and respond_with in a rails app, but in one use
I'm trying to deploy a Rails app (2.3.8) on AppFog using the commands: af
I successfully deployed my rails app to my VPS, but when I try to
I am using the paypal-recurring gem to handle recurring payments in a Rails app.
I'm having trouble getting rubygems to work in my Rails app. Specifically, I'm trying
I'm using Devise in a Rails 3 app, and I successfully configured it so
I am trying to deploy my app on Rails for the first time using
I have a Rails app deployed on Heroku with the Heroku scheduler add-on successfully
My rails app is set up using a mysql database. When I do a
In my Rails app, I'm trying to use jQuery ajax to create a new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.