2012-07-03 35 views
5

我一直在將Stripe整合到我的Web應用程序中,它似乎並沒有工作。爲了幫助我,我一直在使用Ryan Bates的Rails Cast來整合Stripe。每當我嘗試運行付款表單時,我都會收到一個錯誤消息,說「我的信用卡存在問題」。我認爲問題在於我的coffeescript文件,但也許我錯了。我已將條紋用戶令牌包含爲我的用戶模型的一部分,而不是將其放入其自己的訂閱模型中。這裏是CoffeeScript的代碼,我有: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 protected]「,」phone_number「=>」203-xxx-xxxx「,」password 「=>」[FILTERED]「,」password_confirmation「=>」[FILTERED]「},」commit「=>」創建我的帳戶「}

用戶存在(0.2ms)SELECT 1 AS one FROM」users「 WHERE LOWER(「users」。「email」)= LOWER('[email protected]')限制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> 

我給我的控制器的代碼是這樣的創建方法:

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 
浮現在腦海
+0

你回來了什麼'status'和'response',是什麼讓你認爲你的CoffeeScript而不是你的卡信息是問題? –

+0

在條紋中,我的帳戶目前處於測試模式,我使用的是卡代碼:4242424242424242,cvc代碼:123,以及過去一個月的過期日期。我沒有收到應用程序錯誤,但只是卡信息不正確的錯誤。我要將我在終端中遇到的錯誤添加到原始問題中。感謝您的關注! – user1483441

+0

你爲什麼要檢查'status == 500'作爲成功條件?這意味着Stripe服務器上存在服務器錯誤。你應該檢查'if(!response.error)'。你發現服務器上的「stripe_card_token」是空的,對吧? –

回答

0

兩件事:

  1. 你應該做一個狀態檢查200,不500
  2. 您可能需要要求的CoffeeScript文件在您的application.js
    • 例如// =要求用戶
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。