2012-07-03 32 views
5

Tôi đã làm việc để tích hợp Stripe vào ứng dụng web của mình và dường như nó không hoạt động. Để giúp tôi, tôi đã sử dụng Rails Cast của Ryan Bates khi tích hợp Stripe. Bất cứ khi nào tôi cố gắng chạy biểu mẫu thanh toán, tôi nhận được thông báo lỗi "Đã xảy ra sự cố với thẻ tín dụng của tôi". Tôi nghĩ rằng vấn đề nằm trong tập tin coffeescript của tôi, nhưng có lẽ tôi sai. Tôi đã bao gồm mã thông báo người dùng sọc như là một phần của mô hình người dùng của tôi thay vì đặt nó vào mô hình đăng ký của riêng mình. Đây là mã coffeescript tôi có:Điều gì là sai với coffeescript của tôi trong 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) 

Tôi là người mới bắt đầu khi nói đến lập trình, vì vậy bất kỳ trợ giúp nào bạn có thể cho tôi đều tuyệt vời.

Đây là lỗi tôi nhận được trong thiết bị đầu cuối của tôi khi tôi cố gắng đăng ký:

Tham số: { "utf8" => "✓", "authenticity_token" => "XAS + iA + a3op7jUi57qTr7XWQSClPscA7fR19rkclkEE =", " "=" {"," name "=>" Jack "," email "=>" [email protected] "," phone_number "=>" 203-xxx-xxxx "," mật khẩu "," password_card_token "=>" "=>" [FILTERED] "," password_confirmation "=>" [FILTERED] "}," commit "=>" Tạo tài khoản của tôi "}

Người dùng tồn tại (0.2ms) CHỌN 1 AS một FROM" người dùng " WHERE LOWER ("người dùng". "Email") = LOWER ('[email protected] ') LIMIT 1 Lỗi sọc khi tạo khách hàng: Mã thông báo không hợp lệ:

Quan điểm của tôi cho đăng ký là thế này:

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

Mã của tôi cho điều khiển của tôi là thế này đối với phương pháp tạo:

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 

Mã của tôi cho sự di cư cơ sở dữ liệu cho các token của user là thế này:

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

Và mã cho phương pháp save_with_payment trong mô hình của tôi là thế này:

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

'Trạng thái' và' phản hồi' là gì bạn đang quay lại và điều gì khiến bạn nghĩ rằng CoffeeScript của bạn thay vì thông tin thẻ của bạn là vấn đề? –

+0

In sọc, tài khoản của tôi hiện đang ở chế độ thử nghiệm và tôi đang sử dụng mã thẻ: 4242424242424242, mã cvc: 123 và ngày hết hạn trong tháng này. Tôi không gặp lỗi ứng dụng, nhưng chỉ là lỗi mà thông tin thẻ không chính xác. Tôi sẽ thêm lỗi tôi nhận được trong thiết bị đầu cuối của tôi cho câu hỏi ban đầu. Cảm ơn sự nhiệt tình của bạn! – user1483441

+0

Tại sao bạn kiểm tra 'status == 500' là một điều kiện thành công? Điều đó có nghĩa là đã xảy ra lỗi máy chủ trên máy chủ của Stripe. Bạn nên kiểm tra 'if (! Response.error)'. Bạn nhận thấy rằng 'stripe_card_token' trống trên máy chủ của bạn, phải không? –

Trả lời

0

2 điều mà tôi suy nghĩ:

  1. Bạn nên làm một kiểm tra trạng thái cho 200, chứ không phải 500
  2. Bạn có thể cần phải yêu cầu các tập tin coffeescript trong application.js bạn
    • ví dụ // = yêu cầu người dùng
0

Tôi có thể sai, nhưng vào thời điểm này:

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

Ngoài việc thay đổi if status == 500 để if status == 200, dòng này $('#user_stripe_card_token').val(response.id) có thể cần phải $('#new_user_stripe_card_token').val(response.id). Đảm bảo kiểm tra ID đầu vào.

Các vấn đề liên quan