我在我的 Rails 3 应用程序中使用 Stripe 进行信用卡支付。尽管我的应用程序现在只收取一次费用,而不是像 Ryan 节目那样的订阅计费,但我大致遵循了关于 Stripe 集成的 railscast 。
问题是,我无法启动该processCard()
功能。我将是第一个承认我不是 javascript 大师(或任何类型的大师)的人。它没有给出 javascript 错误,而是将表单提交到 rails(没有任何信用卡详细信息)并且失败,因为stripe_card_token
它是 nil。我已经放置了一些alert()
对话框,用于一些非常粗略的调试,以查看挂断的位置。除了临时的 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 %> | <%= 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 %>
相关问题(没有接受的答案):我在 Stripe 中的 CoffeScript 有什么问题?