What is wrong with my coffeescript in Stripe?

Question!

I've been working on integrating Stripe into my web application, and it doesn't seem to be working. To help me along, I've been using Ryan Bates's Rails Cast on integrating Stripe. Whenever I try to run the payment form, I get an error saying that "There was a problem with my credit card". I think the problem lies in my coffeescript file, but perhaps I'm wrong. I've included the stripe user token as a part of my user model instead of placing it into its own subscription model. Here is the coffeescript code I have:

jQuery ->
  Stripe.setPublishableKey($('meta[name="stripe-key"]').attr('content'))
  subscription.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 == 500
      $('#user_stripe_card_token').val(response.id)
      $('#new_user')[0].submit()
    else
      $('#stripe_error').text(response.error.message)
      $('input[type=submit]').attr('disabled', false)

I'm a beginner when it comes to programming, so any help you can give me would be great.

Here's the error I get in my terminal when I try to sign up:

Parameters: {"utf8"=>"✓", "authenticity_token"=>"Xas+iA+a3op7jUi57qTr7XWQSClPscA7fR19rkclkEE=", "user"=>{"stripe_card_token"=>"", "name"=>"Jack", "email"=>"[email protected]", "phone_number"=>"203-xxx-xxxx", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Create my account"}

User Exists (0.2ms) SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('[email protected]') LIMIT 1 Stripe error while creating customer: Invalid token id:

My view for the signup is this:

<% provide(:title, 'Sign up') %>
<h1>Sign up</h1>

<div class="row">
    <div class="span6 offset3">
        <%= form_for(@user) do |f| %>
            <%= render 'shared/error_messages' %>

            <%= f.hidden_field :stripe_card_token %>

            <%= f.label :name %>
            <%= f.text_field :name %>

            <%= f.label :email %>
            <%= f.text_field :email %>

            <%= f.label :phone_number, "Your cell phone number" %>
            <%= f.text_field :phone_number %>

            <%= f.label :password %>
            <%= f.password_field :password %>

            <%= f.label :password_confirmation, "Password confirmation" %>
            <%= f.password_field :password_confirmation %>

            <%= label_tag :card_number, "Credit Card Number" %>
            <%= text_field_tag :card_number, nil, name: nil %>

            <%= label_tag :card_code, "Security Code on Card (CVV)" %>
            <%= text_field_tag :card_code, nil, name: nil %>

            <%= label_tag :card_month, "Card Expiration" %>
            <%= select_month nil, {add_month_numbers: true}, {name: nil, id: "card_month"}%>
        <%= select_year nil, {start_year: Date.today.year, end_year: Date.today.year+15}, {name: nil, id: "card_year"} %>

    <%= f.submit "Create my account", class: "btn btn-large btn-primary" %>
    <% end %>
</div>
</div>

<div id="stripe_error">
  <noscript>JavaScript is not enabled and is required for this form. First enable it in your web browser settings.</noscript>
</div>

My code for my controller is this for the create method:

  def create
    @user = User.new(params[:user])
    if @user.save_with_payment
     sign_in @user
      flash[:success] = "Welcome to the Sample App!"
     redirect_to edit_user_path(current_user)
     UserMailer.welcome_email(@user).deliver
    else
      render 'new'
    end
  end

My code for the database migration for the user token is this:

class AddStripeToUsers < ActiveRecord::Migration
  def change
    add_column :users, :stripe_customer_token, :string
  end
end

And the code for the save_with_payment method in my model is this:

 def save_with_payment
     if valid?
       customer = Stripe::Customer.create(description: email, plan: 1, card: stripe_card_token)
       self.stripe_customer_token = customer.id
       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


Answers

2 things that come to mind:

  1. You should be doing a status check for 200, not 500
  2. You may need to require the coffeescript file in your application.js
    • e.g. //= require users


I could be wrong, but at this point:

handleStripeResponse: (status, response) -


This video can help you solving your question :)
By: admin