Jack Jack - 1 month ago 28
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.



$(document).ready(function(){
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
if(Stripe){
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);

// RESPONSE HANDLER
stripeResponseHandler = function (status, response) {
var token, $form;
$form = $('.cc_form');
if (response.error) {
console.log(response.error.message);
show_error(response.error.message);
$form.find("input[type=submit]").prop("disabled", false);
} else {
token = response.id;
$form.append($("<input type=\"hidden\" name=\"payment[token]\" />").val(token));
$("[data-stripe=number]").remove();
$("[data-stripe=cvv]").remove();
$("[data-stripe=exp-year]").remove();
$("[data-stripe=exp-month]").remove();
$("[data-stripe=label]").remove();
$form.get(0).submit();
}

return false;

};

//ERROR HANDLING
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>');
$('.alert').delay(5000).fadeOut(3000);

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]}
end

def self.year_options
(Date.today.year..(Date.today.year+10)).to_a
end

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


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

end





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>
<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%>
</div>





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

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/' %>

Comments