Jack Jack - 1 year ago 262
Javascript Question

Stripe RailsJS, Cannot charge a customer that has no active card

I keep getting the "Cannot charge a user that has no active card" error when trying to bill a test user.

The app works so that a user gets charged $10 on sign up after they input their CC credentials.

I know the customers are being created but the charges aren't being sent

enter image description here

Here is the Stripe error

enter image description here

My credit_card_form.js file which is what collects the credit card info and sends it to stripe without it hitting my database.

var show_error, stripeResponseHandler, submitHandler;

submitHandler = function (event) {
var $form = $(event.target);
$form.find("input[type=submit]").prop("disabled", true);
//If Stripe was initialized correctly this will create a token using the credit card info
Stripe.card.createToken($form, stripeResponseHandler);
show_error("CC token generated")
} else {
show_error("Failed to load credit card processing functionality. Please reload this page in your browser.")

return false;

// calling the SUBMIT
$(".cc_form").on('submit', submitHandler);

stripeResponseHandler = function (status, response) {
var token, $form;
$form = $('.cc_form');
if (response.error) {
$form.find("input[type=submit]").prop("disabled", false);
} else {
token = response.id;
$form.append($("<input type=\"hidden\" name=\"payment[token]\" />").val(token));

return false;


show_error = function (message) {
if($("#flash-messages").size() < 1){
$('div.container.main div:first').prepend("<div id='flash-messages'></div>")
$("#flash-messages").html('<div class="alert alert-warning"><a class="close" data-dismiss="alert">×</a><div id="flash_alert">' + message + '</div></div>');

return false;


And payment.rb

class Payment < ApplicationRecord
attr_accessor :card_number, :card_cvv, :card_expires_month, :card_expires_year
belongs_to :user

def self.month_options
Date::MONTHNAMES.compact.each_with_index.map{|name,i| ["#{i+1} - #{name}", i+1]}

def self.year_options

def process_payment
customer = Stripe::Customer.create email:email, card:token

Stripe::Charge.create customer:customer.id, amount: 1000, description: "Premium", currency: "usd"


And a snippet of the devise registrations new.html.erb which is where the user will be charged from

<div class="col-md-3">
<%=p .select :card_expires_month, options_for_select(Payment.month_options), {include_blank: "Month"}, "data-stripe"=>"exp-month", class: "form-control", required: true%>
<div class="col-md-3">
<%=p .select :card_expires_year, options_for_select(Payment.year_options.push), {include_blank: "Year"}, class: "form-control", data: {stripe: "exp-year"}, required: true%>

I know that the customers are being created but I have no clue why my customers aren't paying when I use the test credit card numbers. No answers on stack overflow have helped me as of yet.

Answer Source

Jack and I had a chat and discovered that Stripe.js wasn't loading, so the token wasn't being sent in the request.

The issue was <%= javascript_include_tag 'https://js/stripe.com/v2' %> being a mistyped js link in application.html.erb

The fix was making it <%= javascript_include_tag 'https://js.stripe.com/v2/' %>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download