我在 Rails 4 应用程序中使用 Devise 3 和 Stripe 1.8.4。如果有人可以查看我的 javascript,我将不胜感激。我有一个我无法找到的错误。最近几天一直在吃我。我很确定这是一个 javascript 错误,因为来自条带的错误是“为卡提供的空字符串”。我假设没有提交卡详细信息,因为 javascript 出了点问题。预先感谢您的帮助。
我的表格如下:
<%= simple_form_for(resource, :as => resource_name, :url =>
registration_path(resource_name), :id => 'payment-form', method: :post) do |f| %>
<%= f.error_notification %>
<!-- Create Account -->
<div class="inputs">
<%= f.input :first_name, :autofocus => true, :placeholder => "First Name", label:false %>
<%= f.input :last_name, :placeholder => "Last Name", label:false, :required => true %>
<%= f.input :email, :placeholder => "Email Address", label:false, :required => true %>
<%= f.input :password, :placeholder => "Password", label:false, :required => true %>
<%= f.input :password_confirmation, :placeholder => "Password Confirmation", label:false, :required => true %>
<!-- Terms -->
<%= f.input :terms, as: :boolean, label: 'I agree to the terms of service.' %>
<%= f.input :autorenew, as: :boolean, label: 'Automatically renew my account each year.' %>
</div>
<!-- Payment Information -->
<span class="payment-errors"></span>
<div class="row">
<div class="small-8 columns">
<input type="text" size="20" data-stripe="number" placeholder="Credit Card Number"/>
</div>
</div>
<div class="row">
<div class="small-3 columns">
<input type="text" size="4" data-stripe="cvc" placeholder="CVC Code"/>
</div>
</div>
<div class="row">
<div class="small-4 columns">
<div class="row">
<div class="small-5 columns">
<input type="text" size="2" data-stripe="exp-month" placeholder ="MM"/>
</div>
<div class="small-6 columns">
<input type="text" size="4" data-stripe="exp-year" placeholder="YYYY"/>
</div>
</div>
</div>
</div>
<div class="row">
<div class="small-4 columns">
<input type="text" size="10" data-stripe="coupon" placeholder="Coupon Code"/>
</div>
</div>
<%= f.input :stripe_token, as: :hidden %>
<div class="actions">
<%= f.submit "Subscribe", :class => "button radius" %>
</div>
<% end %>
javascript是:
<script type="text/javascript" src="https://js.stripe.com/v2/"></script>
<script type="text/javascript">
$(function($){
Stripe.setPublishableKey('<%= Rails.configuration.stripe[:publishable_key] %>');
$('#payment-form').submit(function(event) {
var form = $('#payment-form');
form.find('button').prop('disabled', true);
Stripe.createToken(form, stripeResponseHandler);
return false;
});
function stripeResponseHandler = function(status, response) {
var form = $('#payment-form');
if (response.error) {
// Show the errors on the form
form.find('.payment-errors').text(response.error.message);
form.find('button').prop('disabled', false);
} else {
// token contains id, last4, and card type
var token = response.id;
// Insert the token into the form so it gets submitted to the server
form.append($('<input type="hidden" name="stripeToken" />').val(token));
// and submit
form.get(0).submit();
}
};
});
感谢您的反馈!