2016-07-19 10 views
0

Ich habe versucht, Streifen zu erhalten einen Token auf meine Rails-Anwendung zurückzukehren, aber ich erhalte immer diese Fehlermeldung:Stripe/Rails: "No such Token: undefined"

Streifen :: InvalidRequestError in Users :: RegistrationsController # erstellen Keine solche Token: undefined

Chrom unterstreicht diese Linie:

customer = Stripe::Customer.create(description: email, plan: plan_id, card: stripe_card_token) 

ich auch versucht haben, "Karte" auf "Quelle" zu ändern und es kommt immer noch zurück, als Token undefined:

{"utf8"=>"✓", 
"authenticity_token"=>"returns a token :)", 
"plan"=>"2", 
"user"=>{"email"=>"[email protected];klfgjbearfgvkujb.com", 
"password"=>"[FILTERED]", 
"password_confirmation"=>"[FILTERED]", 
"stripe_card_token"=>"undefined"}} 

Ich habe auch ein & 'create' in der Trace-Anwendung 'Block in erstellen'

app/models/user.rb: 11: in save_with_payment' app/controllers/users/registrations_controller.rb:8:in Block in erstellen‘ app/controllers/users/registrations_controller .rb: 4: in `create‘

registrations_controller.rb

class Users::RegistrationsController < Devise::RegistrationsController #given to us by Devise 
    #creating a class Users and inheriting devise 
    def create #When the user submits their form 
     super do |resource| #importing to create action code that is inside the devise gem 
      if params[:plan] #when user submits basic or pro form 
       resource.plan_id = params[:plan] #were going to cath that form and see if param :plan was submitted and if yes then we save to resource.plan_id 
       if resource.plan_id == 2 # if User.first_plan.id = 2 
        resource.save_with_payment # if id does = 2 then we save_with_payment defined in models/user.rb otherwise 
       else #if id = 1 
        resource.save #just do a regular save to resources 
       end 
      end 
     end 
    end 

end 

ich habe versucht, den Server neu gestartet habe ich versucht, ein Bündel Update Hier sind einige Beiträge, die ich gelesen habe:

Stripe Integration - Dynamically adding attr value in to the hidden_field

https://github.com/tyler-johnson/stripe-meteor/issues/25

ich auch 4 verschiedene Karte versucht haben # 's nach

https://stripe.com/docs/testing

Jede Hilfe wäre sehr geschätzt :)

FÜR REFERENZ:

application.yml

stripe_api_key: test key 
stripe_publishable_key: pk test key 
# 
production: 
    stripe_api_key: test key 
    stripe_publishable_key: pk test key 

users.js

/* global $ */ 
$(document).ready(function() {  //The .ready just means that the document will get loaded before teh JS runs 
    Stripe.setPublishableKey($('meta[name="stripe-key"]').attr('content')); //Lets grab generic meta tag contents. We can use it because we loaded the JS Stripe file 
    // Watch for a form submission: 
    $("#form-submit-btn").click(function(event) { //When the user clicks on submit, we add an event listener to "next line" 
     event.preventDefault(); //Dont send anything to the server just yet 
     $('input[type=submit]').prop('disabled', true); //This disables the button 
     var error = false; //establishing a variable false 
     var ccNum = $('#card-number').val(), //storing variables 
      cvcNum = $('#card_code').val(), 
      expMonth = $('#card_month').val(), 
      expYear = $('#card_year').val(); 

     if (!error) { //if there are no errors 
      //Get the Stripe token: 
      Stripe.createToken({ //Ship off the data entered to STRIPE 
       number: ccNum, 
       cvc: cvcNum, 
       exp_month: expMonth, 
       exp_year: expYear 
      }, stripeResponseHandler); //Once stripe returns TOKEN 
     } 
     return false; 
    }); //form submission 

    function stripeResponseHandler(status, response) { //response coming back from server 
     //Get a reference to the form: 
     var f = $("#new_user"); //Lets use this form we made 

     //Get the token from the response: 
     var token = response.id; //coming from response above and gives us card TOKEN 

     //Add the token to the form: 
     f.append('<input type="hidden" name="user[stripe_card_token]" value="' + token + '" />'); //and append the value for the field 

     //Submit the form: 
     f.get(0).submit(); //f.get(0) means we only want to the first forn incase there are multiple forms returned and submit 
    } 
}); 

user.rb

class User < ActiveRecord::Base 
    # Include default devise modules. Others available are: 
    # :confirmable, :lockable, :timeoutable and :omniauthable 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable 
    belongs_to :plan 
    attr_accessor :stripe_card_token #stripe_card_token hidden field value 

    def save_with_payment #method used in /app/controllers/users/registrations_controller.rb 
    if valid? #If user filled out form properly 
     customer = Stripe::Customer.create(description: email, plan: plan_id, source: stripe_card_token) 
     self.stripe_customer_token = customer.id #setting property of user to user id returned from stripe 
     save! #saving entire user object to database by creating new migration file 
    end 
    end 
end 

stripe.rb

Stripe.api_key = ENV["stripe_api_key"] 
STRIPE_PUBLIC_KEY = ENV["stripe_publishable_key"] #The ENV comes from the figaro file 

Migration Datei (Schienen erzeugt)

class AddStripeCustomerTokenToUsers < ActiveRecord::Migration 
    def change 
     add_column :users, :stripe_customer_token, :string 
    end #add a column to the user database and call it stripe ciutomer token as a string 
end #bundle exec rake db:migrate after you created this 

application.html.erb

<!DOCTYPE html> 
<html> 
<head> 
    <title>DevMatch</title> 
    <%= stylesheet_link_tag 'application', media: 'all' %> 
    <%= javascript_include_tag "https://js.stripe.com/v2/", type: 'text/javascript' %> 
    <%= javascript_include_tag 'application'%> 
    <%= tag :meta, :name => "stripe-key", :content => STRIPE_PUBLIC_KEY %> 
    <%= csrf_meta_tags %> 
</head> 
<body> 
    <nav class="navbar navbar-inverse navbar-static-top" role="navigation"> 
    <div class='container'> 
     <div class='navbar-header'> 
     <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#main-nav-collapse"> 
      <span class="sr-only">Toggle navigation</span> 
      <span class="icon-bar"></span> 
      <span class="icon-bar"></span> 
      <span class="icon-bar"></span> 
     </button> 
     <%= link_to root_path, class: 'navbar-brand' do %> 
      <i class="fa fa-users"></i> 
      DevMatch 
     <% end %> 
     </div> 
     <div class="collapse navbar-collapse" id="main-nav-collapse"> 
     <div class="navbar-nav navbar-right"> 
      <% if current_user %> 
      <%= button_to "Sign Out", destroy_user_session_path, method: :delete, class: 'btn btn-primary navbar-btn' %> 
      <% else %> 
      <%= link_to "Log In", new_user_session_path, class: 'btn btn-primary navbar-btn', method: :get %> 
     </div> 
     <ul class="nav navbar-nav navbar-right"> 
      <li><%= link_to "About", about_path %></li> 
      <li><%= link_to "New Contact", new_contact_path %></li> 
     </ul> 
     </div><!-- /.navbar-collapse --> 
    </div> 
    </nav> 

<div class="container"> 
    <% flash.each do |key, value| %> 
    <%= content_tag :div, value, class: "alert alert-#{key}" %> 
    <% end %> 

    <%= yield %> 
    <% end %> 
</div> 

</body> 

</html> 

Antwort

1

Es scheint, dass wir an einem ähnlichen Projekt gearbeitet, weil ich das gleiche Problem mit der Methode hatten Sie verwenden. Nachdem ich die gleichen Fehlerbehebungen ausprobiert habe (und immer noch den gleichen Fehler erhalte), habe ich schließlich einen kleinen Fehler in den Anweisungen entdeckt, die wir beide zum Einrichten von Stripe benutzt haben. Der erste Hinweis ist, die Variablenwerte in Ihrer users.js Datei zu betrachten. Ich habe sofort einen Tippfehler bei dir bemerkt.Sie haben

var ccNum = $('#card-number').val(), //storing variables 
     cvcNum = $('#card_code').val(), 
     expMonth = $('#card_month').val(), 
     expYear = $('#card_year').val(); 

Beachten Sie, dass Sie #card-number haben, wenn es #card_number sein sollte (mit einem _ kein -). Während Sie es nicht aufgelistet haben, müssen Sie auch die _pro_form.html.erb Datei überprüfen, oder es ist äquivalent. In meinem Fall hatte ich

<%= label_tag :card_code, "Credit Card Number" %> 

wenn es

gewesen sein sollte
<%= label_tag :card_number, "Credit Card Number" %> 

Stellen Sie sicher, dass diese Werte in der _pro_form.html.erb (oder gleichwertig) Datei, die die in der users.js Datei passen. Ich hoffe, das hilft.

+0

Gutes Auge! Ich bin diesen Code ein paar Mal durchgegangen, aber ich denke, es war nicht genug. Danke für die Lösung. –

+0

Wenn Sie könnten, geben Sie meiner Antwort eine Stimme! Vielen Dank. – jcbridwe

+0

Habe ich gerade gemacht, aber ich bin so neu, dass es keine öffentliche Ansicht meiner Abstimmung zeigt. –