Marko I.
Marko I.

Reputation: 562

How to save customer card updates in Stripe, Rails?

I want customers to be able to update their credit card details in my Rails app. Stripe has documentation on how to achieve this, but the article is showing an example in PHP, but I need an example for Rails: https://stripe.com/docs/recipes/updating-customer-cards

Basically, I need to save a customer's credit card without charging it.

This is subscribers_controller.rb:

class SubscribersController < ApplicationController        
  before_filter :authenticate_user!

  def new
  end

  def update
    token = params[:stripeToken]

    customer = Stripe::Customer.create(
      card: token,
      plan: 1212,
      email: current_user.email
    )

    current_user.subscribed = true
    current_user.stripeid = customer.id
    current_user.save

    redirect_to profiles_user_path
  end
end

Upvotes: 3

Views: 2335

Answers (2)

Michael Gaskill
Michael Gaskill

Reputation: 8042

You might also want to check out this SO answer How to create a charge and a customer in Stripe ( Rails) for more details on using Stripe in a Rails application.

For Ruby documentation, you can find great examples at on the Stripe Ruby API. In Stripe terminology, a card is called a source for the customer. You can create a source from a token, but once it's created, you deal with source and default_source elements on the Customer object, and retrieve card objects from the customer's source. Also note that you should never try to use the token beyond creating the source (or for one-time charges).

The Stripe Ruby API for Customers shows that you can create a customer and assign the source at the same time:

customer = Stripe::Customer.create(
    source: token,
    email: current_user.email
)

You do not have to assign a source to create a customer. However, if you set the customer up on a subscription, they will require a source to be available, and the charges will be made to the customer's default_source. If a customer has only one source, it is automatically the default_source.

The Stripe Ruby API for Cards, shows that you can also add a new card to an existing customer, using a token:

customer = Stripe::Customer.retrieve(customer_id)
customer.sources.create({source: token_id})

Once you have a card assigned to the customer, you can make it the default_source, using this:

customer.default_source = customer.sources.retrieve(card_id)

And that's what it takes to get setup and ready to start charging customers. Happy billing!

Upvotes: 4

Ywain
Ywain

Reputation: 17503

To update the card for an existing customer, the relevant snippet from the PHP recipe you mentioned is:

$cu = \Stripe\Customer::retrieve($customer_id); // stored in your application
$cu->source = $_POST['stripeToken']; // obtained with Checkout
$cu->save();

In Ruby, this would be:

cu = Stripe::Customer.retrieve(customer_id)
cu.source = params[:stripeToken]
cu.save

This will update the existing customer with the card from the token contained in the stripeToken parameter.

Upvotes: 1

Related Questions