如何追踪coffeescript和Stripe中错误的原因?

时间:2012-10-30 00:31:55

标签: ruby-on-rails ruby-on-rails-3 coffeescript stripe-payments

我在我的Rails 3应用程序中使用Stripe进行信用卡付款。我大致跟随railscast on Stripe integration,虽然我的应用程序现在只收取一次费用而不是像Ryan那样的订阅费用。

问题是,我无法启动processCard()功能。我将是第一个承认我不是javascript大师(或任何类型的大师)的人。它没有给出javascript错误,而是将表单提交给rails(没有任何信用卡详细信息)并且由于stripe_card_token为零而失败。我已经在一些非常粗略的调试中放置了一些alert()个对话框,以查看挂断的位置。我的模型名称是Payment,除了临时警报()对话框之外,它实际上是Ryan代码中唯一的偏差。

所以我的问题有两个:我怎样才能可靠地追踪造成问题的原因?我哪里出错了?

这是Javascript:

jQuery ->
  Stripe.setPublishableKey($('meta[name="stripe-key"]').attr('content'))
  payment.setupForm()

payment =
  setupForm: ->
    $('#new_payment').submit ->
      $('input[type=submit]').attr('disabled', true)
      alert("before processCard")
      payment.processCard()
      alert("after processCard")
      false
      # else
      #   true

  processCard: ->
    alert("processCard start")
    card =
      number: $('#credit_card_number').val()
      cvc: $('#cvc').val()
      expMonth: $('#card_month').val()
      expYear: $('#card_year').val()
    alert("card values are set")
    Stripe.createToken(card, payment.handleStripeResponse)
    alert("After createToken")

  handleStripeResponse: (status, response) ->
    if status == 200
      alert(response.id)
      # $('#payment_stripe_card_token').val(response.id)
      # $('#new_payment')[0].submit()
    else
      alert(response.error.message)
      # $('#stripe_error').text(response.error.message)
      # $('input[type=submit]').attr('disabled', false)

相关观点:

<%= simple_form_for @payment, :html => { :class => 'form-horizontal'} do |f| %>
<fieldset>
  <legend>Pay via Credit Card</legend>

    <%= f.input :stripe_card_token, :as => :hidden, :id => 'stripe_card_token' %>

    <div class="well well-small">
      <%= current_user.full_name %><br />
      <%= current_user.email %><br />
      This is the name we have on file for you. If it's not correct, please <%= link_to "change it now", edit_user_registration_path(current_user) %>
    </div>

<div id="stripe_error">
  <noscript>JavaScript is not enabled and is required for this form. First enable it in your web browser settings.</noscript>
</div>

<div id="credit-card">
  <div class="control-group string required">
    <%= label_tag :credit_card_number, "Card Number", :class => "control-label string required" %>
    <div class="controls">
      <%= text_field_tag :credit_card_number, nil, :class => "string required", :name => nil %>
    </div>
  </div>

  <div class="control-group string required">
    <%= label_tag :cvc, "Security code (CVC)", :class => "control-label string required" %>
    <div class="controls">
      <%= text_field_tag :cvc, nil, :class => "string required", :name => nil %>
    </div>
  </div>

  <div class="control-group date required">
    <%= label_tag :card_month, "Expiration Date", :class => "control-label date required"%>
    <div class="controls">
      <%= select_month nil, {add_month_numbers: true}, {name: nil, id: "card_month", :class => "date required span2"} %>
      <%= select_year nil, {start_year: Date.today.year, end_year: Date.today.year+25}, {name: nil, id: "card_year", :class => "date required span2"} %>
    </div>
  </div>

    <h4>Your unpaid courses</h4>
    <% @unpaid_subs.each do |sub| %>
      <%= sub.course.name %>&nbsp;|&nbsp;<%= sub.course.credits %><br />
    <% end %>

<div class="form-actions">
<%= f.submit nil, :class=>"btn btn-primary" %>
<%= link_to "Cancel", courses_path, :class=>"btn" %>
</div>
</fieldset>
<% end %>

相关问题(未接受回答):What is wrong with my CoffeScript in Stripe?

1 个答案:

答案 0 :(得分:1)

我弄明白了这个问题。多谢“@mu太短”。你的建议将其剥离到所需的js和html最终帮助我找到了错误。

事实证明我在带有Stripe公钥的元标记上拼错了name属性。在应用程序布局中,它缺少“stripe-key”中的“r”:

<%= tag :meta, :name => "stipe-key", :content => STRIPE_PUBLIC_KEY %>

我很难找到这个,因为它没有引起任何异常或显示在日志中。为了找出问题的原因,我做了两件事:

  • 我在javascript的关键位置放置了alert()个对话框,以便在脚本运行时暂停该脚本并找到哪个功能无效。原油种类,但它确实有效。
  • 由于@mu太短,在评论中说:
  

您确定没有其他JavaScript妨碍您吗?重新编写   手工编写HTML并删除除条带库之外的所有(Coffee | Java)脚本,   jQuery和你的代码。

在执行此操作时,我发现如果页面上的某些其他javascript中存在错误,则javascript将无法正常运行。通过将视图拆解为基本要素,我不仅最终发现了变量名称的问题,而且还发现了几个无关的错误。

相关问题