5

我一直在努力将 Stripe 集成到我的 Web 应用程序中,但它似乎不起作用。为了帮助我,我一直在使用 Ryan Bates 的 Rails Cast 来集成 Stripe。每当我尝试运行付款表单时,都会收到一条错误消息,提示“我的信用卡有问题”。我认为问题出在我的咖啡脚本文件中,但也许我错了。我已将条带用户令牌作为我的用户模型的一部分,而不是将其放入自己的订阅模型中。这是我的咖啡脚本代码:

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

user =
  setupForm: ->
    $('#new_user').submit ->
      $('input[type=submit]').attr('disabled', true)
      if $('#card_number').length
        user.processCard()
        false
      else
        true

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

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

在编程方面,我是初学者,所以你能给我的任何帮助都会很棒。

这是我尝试注册时在终端中遇到的错误:

参数:{"utf8"=>"✓", "authenticity_token"=>"Xas+iA+a3op7jUi57qTr7XWQSClPscA7fR19rkclkEE=", "user"=>{"stripe_card_token"=>"", "name"=>"Jack", " email"=>"email@example.com", "phone_number"=>"203-xxx-xxxx", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit "=>"创建我的帐户"}

用户存在 (0.2ms) SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('jjets718@yahoo.com') LIMIT 1 创建客户时出现条带错误:令牌 ID 无效:

我对注册的看法是这样的:

<% provide(:title, 'Sign up') %>
<h1>Sign up</h1>

<div class="row">
    <div class="span6 offset3">
        <%= form_for(@user) do |f| %>
            <%= render 'shared/error_messages' %>

            <%= f.hidden_field :stripe_card_token %>

            <%= f.label :name %>
            <%= f.text_field :name %>

            <%= f.label :email %>
            <%= f.text_field :email %>

            <%= f.label :phone_number, "Your cell phone number" %>
            <%= f.text_field :phone_number %>

            <%= f.label :password %>
            <%= f.password_field :password %>

            <%= f.label :password_confirmation, "Password confirmation" %>
            <%= f.password_field :password_confirmation %>

            <%= label_tag :card_number, "Credit Card Number" %>
            <%= text_field_tag :card_number, nil, name: nil %>

            <%= label_tag :card_code, "Security Code on Card (CVV)" %>
            <%= text_field_tag :card_code, nil, name: nil %>

            <%= 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"} %>

    <%= f.submit "Create my account", class: "btn btn-large btn-primary" %>
    <% end %>
</div>
</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>

我的控制器代码是用于 create 方法的:

  def create
    @user = User.new(params[:user])
    if @user.save_with_payment
     sign_in @user
      flash[:success] = "Welcome to the Sample App!"
     redirect_to edit_user_path(current_user)
     UserMailer.welcome_email(@user).deliver
    else
      render 'new'
    end
  end

我的用户令牌数据库迁移代码是这样的:

class AddStripeToUsers < ActiveRecord::Migration
  def change
    add_column :users, :stripe_customer_token, :string
  end
end

我的模型中 save_with_payment 方法的代码是这样的:

 def save_with_payment
     if valid?
       customer = Stripe::Customer.create(description: email, plan: 1, card: stripe_card_token)
       self.stripe_customer_token = customer.id
       save!
     end

   rescue Stripe::InvalidRequestError => e
     logger.error "Stripe error while creating customer: #{e.message}"
     errors.add :base, "There was a problem with your credit card."
     false
   end
4

2 回答 2

0

我可能是错的,但在这一点上:

handleStripeResponse: (status, response) ->
  if status == 500
    $('#user_stripe_card_token').val(response.id)

除了改成if status == 500if status == 200这条线$('#user_stripe_card_token').val(response.id)可能还需要$('#new_user_stripe_card_token').val(response.id)。确保检查输入 ID。

于 2013-12-07T19:39:52.933 回答
0

想到的2件事:

  1. 您应该检查 200 的状态,而不是 500
  2. 您可能需要在 application.js 中使用 coffeescript 文件
    • 例如 //= 要求用户
于 2012-10-04T01:59:47.663 回答