0

在过去的 2 天里,我一直在尝试让以下 railscast 中描述的计费系统正常工作,但到目前为止完全没有运气。代码已稍作更改以适用于我的用户模型。只要有足够的时间,我通常能够解决这些问题,但我肯定遇到了障碍。

值得一提的是,在提交表单时,部分响应如下:一个空的“stripe_card_token”(Stripe 服务器没有返回任何内容)并且它很快就回来了,这让我觉得甚至从未联系过 Stripe 服务器...

Processing by UsersController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"4H+rHOsU+0A55+QoaoWw27vbctDIVK3G9gNZHaB5KVY=", "user"=>{"stripe_card_token"=>"", "email"=>""}, "commit"=>"Subscribe"}

http://railscasts.com/episodes/288-billing-with-stripe


users.js.coffee:

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

subscription =
  setupForm: ->
      $('#new_user').submit ->
      $('input[type=submit]').attr('disabled', true)
      subscription.processCard()

  processCard: ->
    card =
      {number: $('#card_number').val()
      cvc: $('#card_code').val()
      expMonth: $('#card_month').val()
      expYear: $('#card_year').val()}
    Stripe.createToken(card, subscription.handleStripeResponse)

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

新的.html.erb:

<%= form_for @user do |f| %>


<%= f.hidden_field :stripe_card_token %>

 <% if @user.errors.any? %>
    <div class="error_messages">
      <h2><%= pluralize(@user.errors.count, "error") %> prohibited this subscription from being saved:</h2>
      <ul>
      <% @user.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

<div class="field">
  <%= f.label :email %>
  <%= f.text_field :email %>
</div>
<% if @user.stripe_card_token.present? %>
  Credit card has been provided.
<% else %>
  <div class="field">
    <%= label_tag :card_number, "Credit Card Number" %>
    <%= text_field_tag :card_number, nil, name: nil %>
  </div>
  <div class="field">
    <%= label_tag :card_code, "Security Code on Card (CVV)" %>
    <%= text_field_tag :card_code, nil, name: nil %>
  </div>
  <div class="field">
    <%= label_tag :card_month, "Card Expiration" %>
    <%= select_month nil, {add_month_numbers: true}, {name: nil, id: "card_month"}%>
    <%= select_year nil, {start_year: Date.today.year, end_year: Date.today.year+15}, {name: nil, id: "card_year"}%>
  </div>

 <div class="actions"><%= f.submit "Subscribe" %></div>


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

<% end %>
4

1 回答 1

3

注意事项:

1)您的缩进似乎已关闭,尽管这可能是复制/粘贴问题。

handleStripeResponse: (status, response) ->

if status == 200 $('#user_stripe_card_token').val(response.id) $('#new_user')[0].submit() else $('#stripe_error').text(response.error.message) $('input[type=submit]').attr('disabled', false)

应该

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

2) 您是否尝试在您的 Coffeescript 中放入 debugger 或 console.log 语句?鉴于您的回答,我只是猜测不是:

handleStripeResponse: (status, response) ->
  console.log status
  console.log response
  ...

您可以在浏览器的开发者控制台中查看这些内容。这至少可以说明您是否收到了 Stripe 的回复。

于 2013-08-07T21:00:36.640 回答