我在Stripe中的coffeescript出了什么问题?

use*_*441 5 coffeescript stripe-payments

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

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)
Run Code Online (Sandbox Code Playgroud)

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

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

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

用户存在(0.2ms)SELECT 1 AS一个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>
Run Code Online (Sandbox Code Playgroud)

我的控制器代码是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
Run Code Online (Sandbox Code Playgroud)

我的用户令牌数据库迁移代码如下:

class AddStripeToUsers < ActiveRecord::Migration
  def change
    add_column :users, :stripe_customer_token, :string
  end
end
Run Code Online (Sandbox Code Playgroud)

我的模型中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
Run Code Online (Sandbox Code Playgroud)

小智 0

我想到了两件事:

  1. 您应该对 200 个而不是 500 个进行状态检查
  2. 您可能需要在 application.js 中添加 CoffeeScript 文件
    • 例如 //= 需要用户