Stripe RailsJS,无法向没有活动卡的客户收取费用

时间:2016-11-04 02:08:05

标签: javascript ruby-on-rails devise stripe-payments

我不断收到"无法向没有有效卡的用户收取费用#34;尝试向测试用户收费时出错。

该应用程序的工作方式是,用户在输入CC凭据后,在注册时收取10美元的费用。

我知道客户正在创建,但收费并未发送

enter image description here

这是条纹错误

enter image description here

我的credit_card_form.js文件收集信用卡信息并将其发送到条带而不会访问我的数据库。



$(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;
	};

});
	
&#13;
&#13;
&#13;

和payment.rb

&#13;
&#13;
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
&#13;
&#13;
&#13;

还有一个设计注册片段new.html.erb,用户将从中收取费用

&#13;
&#13;
<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>
&#13;
&#13;
&#13;

我知道客户正在创建,但我不知道为什么我的客户在使用测试信用卡号码时没有付款。到目前为止,没有关于堆栈溢出的答案对我有所帮助。

1 个答案:

答案 0 :(得分:2)

杰克和我had a chat并发现Stripe.js没有加载,所以令牌没有在请求中发送。

问题是<%= javascript_include_tag 'https://js/stripe.com/v2' %>application.html.erb

中错误的js链接

修复程序正在使其<%= javascript_include_tag 'https://js.stripe.com/v2/' %>

相关问题