【问题标题】:Stripe RailsJS, Cannot charge a customer that has no active cardStripe RailsJS,无法向没有活动卡的客户收费
【发布时间】:2017-03-17 18:57:07
【问题描述】:

在尝试向测试用户收费时,我不断收到“无法向没有活动卡的用户收费”错误消息。

该应用程序可以让用户在输入 CC 凭据后在注册时收取 10 美元的费用。

我知道正在创建客户,但没有发送费用

这是条纹错误

我的 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;
	};

});
	

还有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

还有一个设计注册 new.html.erb 的 sn-p,这是用户将被收取费用的地方

<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>

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

【问题讨论】:

  • 在 Stripe 中创建的客户是否附有信用卡?
  • 信用卡附在注册时,您可以从最后一个代码 sn-p 中看到。但是 javascript 在将信用卡保存给用户之前会转储信用卡,因此它不会命中数据库
  • 我的意思是,如果您进入 Stripe Dashboard 并导航到其中的一位客户,您会在其中看到信用卡吗?
  • 对不起,我没有
  • 酷。你的 JS 生成的所有调试信息怎么样——你能确定在出现问题之前它已经走了多远吗?此外,card_token 是否存在于发送到您的服务器的请求的 params 中?

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


【解决方案1】:

Jack 和我 had a chat 发现 Stripe.js 没有加载,因此请求中没有发送令牌。

问题是&lt;%= javascript_include_tag 'https://js/stripe.com/v2' %&gt;application.html.erb 中输入错误的js 链接

解决方法是&lt;%= javascript_include_tag 'https://js.stripe.com/v2/' %&gt;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-05
    • 1970-01-01
    • 2017-11-05
    • 2022-01-27
    • 2015-05-25
    • 2021-09-19
    • 2016-12-15
    相关资源
    最近更新 更多