对Stripe的javascript进行故障排除

时间:2013-09-13 19:45:28

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

我在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();
   }
};

});

感谢您的反馈!

1 个答案:

答案 0 :(得分:0)

我正在回答我自己的问题。事实证明代码没有任何问题。这段代码在我的视图中不起作用,但是如果我将js移动到application.js,那么使用Stripe的事务就可以完美地运行。不知道为什么。