user2911232
user2911232

Reputation:

If I have a Stripe card token how do i retrieve customer id?

My app uses devise for creating new users and I integrated that with Stripe.

When I create a new customer I get back (as a response) a token which is the card's token. Is there a way to retrieve customer's id by using that token?

I have a related question that shows my current implementation. I made this question as I now understand that the problem is that I don't retrieve / store the customer's id during the creation of the new customer.

Any tip is valuable. Thank you.

My JS (coffee script)

jQuery ->
  Stripe.setPublishableKey($('meta[name="stripe-key"]').attr('content'))
  user.setupForm()

user =
  setupForm: ->
    $('#new_user').submit ->
      $('input[type=submit]').attr('disabled', true)
      if $('#card_number').length
        user.processCard()
        false
      else
        true

  processCard: ->
    card =
      number: $('#card_number').val()
      cvc: $('#card_code').val()
      expMonth: $('#card_month').val()
      expYear: $('#card_year').val()
    Stripe.createToken(card, user.handleStripeResponse)

  handleStripeResponse: (status, response) ->
    if status == 200
      $('#user_stripe_card_token').val(response.id)
      $('#new_user')[0].submit()
    else
      $('#stripe_error').text(response.error.message)
      $('#stripe_error').show()
      $('input[type=submit]').attr('disabled', false)

I don't do anything in a controller. I only use this in my user.rb:

 after_create :create_a_customer

 def create_a_customer
    token = self.stripe_card_token
    customer = Stripe::Customer.create(
      :card => token,
      :plan => 1,
      :email => self.email
      )         
  end

Upvotes: 3

Views: 3227

Answers (1)

frenchloaf
frenchloaf

Reputation: 1054

Our JS looks identical, so I thought I would post my form and controller action as well as my model that does some processing and saving of the user. Let me know If any of this helps! Sorry I can't seem to find the exact error, but I know, for me, having a working example to look at always helps!

FORM --

<%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => {:class => 'form card_form' }) do |f| %>

  <%= f.email_field :email, :required => true, :class => "deviseFormInput", :placeholder => "EMAIL" %><br />
  <i>PASSWORD MINIMUM OF 6 CHARACTERS</i>
  <%= f.password_field :password, :required => true, :class => "deviseFormInput", :placeholder => "PASSWORD" %>
  <%= f.password_field :password_confirmation, :required => true, :class => "deviseFormInput", :placeholder => "PASSWORD CONFIRMATION" %>
  <div class="field">
    <%= f.input :meatless, as: :boolean, :class => "deviseFormInput", :label => "I AM A VEGETARIAN" %>
  </div>
  <%= f.input :comments, as: :text, :placeholder => "ALLERGIES OR INTOLERANCES", :label => false, input_html: { class: 'deviseFormInput', style: 'width: 280px; height: 100px;' } %>

  <% if @user.stripe_customer_token %>
    <p>Credit card acceptance is pending.</p>
  <% else %>
    <div class="field">
      <%= text_field_tag :card_number, nil, name: nil, :class => "deviseFormInput", :placeholder => "CREDIT CARD NUMBER" %>
    </div>
    <div class="field">
      <%= text_field_tag :card_code, nil, name: nil, :class => "deviseFormInput", :placeholder => "CARD SECURITY CODE (CVV)" %>
    </div>
    <div class="field">
      <%= label_tag :card_month, "CARD EXPIRATION" %>
      <br>
      <div class="presser presserDeep">
        <%= select_month nil, {add_month_numbers: true}, {name: nil, id: "card_month"} %>
      </div>
      <div class="presser">
        <%= select_year nil, {start_year: Date.today.year, end_year: Date.today.year+10}, {name: nil, id: "card_year"}%>
      </div>
    </div>
  <% end %>
  <%= f.hidden_field :stripe_card_token %>
  <%= f.button :submit, 'SIGN UP', :class => 'deviseBtn' %>

<% end %>

CONTROLLER --

def create
  shopping_cart_id = session[:shopping_cart_id]
  @shopping_cart = session[:shopping_cart_id] ? ShoppingCart.find(shopping_cart_id) : ShoppingCart.create
  session[:shopping_cart_id] = @shopping_cart.id
  @user = User.new(params[:user])
  if @user.save_with_payment
    sign_in @user
    redirect_to root_path, :notice => "Thank you for signing up!"
  else
    render :new
  end
end

MODEL (for save_with_payment method) --

def save_with_payment
  if valid?
    customer = Stripe::Customer.create(description: email, card: stripe_card_token)
    self.stripe_customer_token = customer.id
    self.last_4_digits = customer.cards.data.first["last4"]
    save!
  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 

Upvotes: 2

Related Questions