Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing Stripe with Devise?
    primarykey
    data
    text
    <p>I'm a RoR newbie attempting to integrate Stripe with Devise so that I can register new subscribers of my app, and set them up to be charged monthly. I've followed the Railscast (<a href="http://railscasts.com/episodes/288-billing-with-stripe" rel="nofollow noreferrer">http://railscasts.com/episodes/288-billing-with-stripe</a>) as well as reviewed the QA found here (<a href="https://stackoverflow.com/questions/8440509/using-stripe-with-devise-for-ruby-on-rails-subscriptions">Using Stripe with Devise for Ruby on Rails Subscriptions</a>), but still can't get it to work/fully understand what I'm doing wrong. Essentially I want (if possible) to use my Devise User model and registrations controller, not have to create/use a subscriptions model/controller. </p> <p>I placed the code from the Railscast into the appropriate places, but still receive error(s), for example: </p> <pre><code>NoMethodError in Users/registrations#create Showing /app/views/users/registrations/new.html.erb where line #7 raised: undefined method `errors' for nil:NilClass Extracted source (around line #7): 4: &lt;h2&gt;Sign up&lt;/h2&gt; 5: 6: &lt;%= form_for(resource, :as =&gt; resource_name, :url =&gt; registration_path(resource_name)) do |f| %&gt; 7: &lt;%= devise_error_messages! %&gt; 8: 9: &lt;div&gt;&lt;%= f.label :email %&gt;&lt;br /&gt; 10: &lt;%= f.email_field :email %&gt;&lt;/div&gt; </code></pre> <p>Any help/direction is greatly appreciated. </p> <h2>models/user.rb</h2> <pre><code> class User &lt; ActiveRecord::Base # Include default devise modules. Others available are: # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable,:invitable has_many :org_roles has_many :orgs, :through =&gt; :org_roles, :conditions =&gt; "role_id = 1" accepts_nested_attributes_for :orgs, :allow_destroy =&gt; true has_many :assigned_orgs, :through =&gt; :org_roles, :conditions =&gt; "role_id = 2" , :class_name =&gt; "Org", :source =&gt; :org accepts_nested_attributes_for :assigned_orgs has_one :user_detail accepts_nested_attributes_for :user_detail, :allow_destroy =&gt; true has_one :user_address accepts_nested_attributes_for :user_address, :allow_destroy =&gt; true has_many :subscription_plans # Setup accessible (or protected) attributes for your model attr_accessible :email, :password, :password_confirmation, :remember_me, :user_type_id, :orgs_attributes, :assigned_orgs_attributes, :user_detail_attributes, :user_address_attributes #STRIPE attr_accessor :stripe_card_token def save_with_payment if valid? customer = Stripe::Customer.create(description: email, plan: plan_id, card: stripe_card_token) self.stripe_customer_token = customer.id save! end rescue Stripe::InvalidRequestError =&gt; e logger.error "Stripe error while creating customer: #{e.message}" errors.add :base, "There was a problem with your credit card." false end end </code></pre> <h2>controllers/registrations_controller.rb</h2> <pre><code>def create @subscription = User.new(params[:subscription]) if @subscription.save_with_payment redirect_to @subscription, :notice =&gt; "Thank you for subscribing!" else render :new end super ... </code></pre> <h2>views/users/registrations/new.html.erb</h2> <pre><code>&lt;%= javascript_include_tag "https://js.stripe.com/v1/", "application" %&gt; &lt;%= tag :meta, :name =&gt; "stripe-key", :content =&gt; STRIPE_PUBLIC_KEY %&gt; &lt;h2&gt;Sign up&lt;/h2&gt; &lt;%= form_for(resource, :as =&gt; resource_name, :url =&gt; registration_path(resource_name)) do |f| %&gt; &lt;%= devise_error_messages! %&gt; &lt;div&gt;&lt;%= f.label :email %&gt;&lt;br /&gt; &lt;%= f.email_field :email %&gt;&lt;/div&gt; &lt;div&gt;&lt;%= f.label :password %&gt;&lt;br /&gt; &lt;%= f.password_field :password %&gt;&lt;/div&gt; &lt;div&gt;&lt;%= f.label :password_confirmation %&gt;&lt;br /&gt; &lt;%= f.password_field :password_confirmation %&gt;&lt;/div&gt; &lt;div&gt;Your Business Name:&lt;br /&gt; &lt;%= text_field_tag :org %&gt;&lt;/div&gt; &lt;div&gt;Please select a subscription plan: &lt;br /&gt; &lt;%= radio_button_tag(:subscription_plan, 1, :checked=&gt;"yes") %&gt; &lt;%= label_tag(:subscription_plan_tag, "Small") %&gt; &lt;%= radio_button_tag(:subscription_plan, 2) %&gt; &lt;%= label_tag(:subscription_plan_tag, "Medium") %&gt; &lt;%= radio_button_tag(:subscription_plan, 3) %&gt; &lt;%= label_tag(:subscription_plan_tag, "Large") %&gt; &lt;/div&gt; &lt;%= f.hidden_field :stripe_card_token %&gt; &lt;div class="field"&gt; &lt;%= label_tag :card_number, "Credit Card Number" %&gt; &lt;%= text_field_tag :card_number, nil, name: nil %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= label_tag :card_code, "Security Code on Card (CVV)" %&gt; &lt;%= text_field_tag :card_code, nil, name: nil %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= label_tag :card_month, "Card Expiration" %&gt; &lt;%= select_month nil, {add_month_numbers: true}, {name: nil, id: "card_month"} %&gt; &lt;%= select_year nil, {start_year: Date.today.year, end_year: Date.today.year+15}, {name: nil, id: "card_year"} %&gt; &lt;/div&gt; &lt;div id="stripe_error"&gt; &lt;noscript&gt;JavaScript is not enabled and is required for this form. First enable it in your web browser settings.&lt;/noscript&gt; &lt;/div&gt; &lt;div&gt;&lt;%= f.submit "Sign up" %&gt;&lt;/div&gt; &lt;% end %&gt; &lt;%= render "links" %&gt; </code></pre> <h2>assets/javascripts/users/users.js.coffee</h2> <pre><code>jQuery -&gt; Stripe.setPublishableKey($('meta[name="stripe-key"]').attr('content')) subscription.setupForm() subscription = setupForm: -&gt; $('#new_user').submit -&gt; $('input[type=submit]').attr('disabled', true) if $('#card_number').length subscription.processCard() false else true processCard: -&gt; card = number: $('#card_number').val() cvc: $('#card_code').val() expMonth: $('#card_month').val() expYear: $('#card_year').val() Stripe.createToken(card, subscription.handleStripeResponse) handleStripeResponse: (status, response) -&gt; if status == 200 $('#subscription_stripe_card_token').val(response.id) $('#new_user')[0].submit() #alert(response.id) else $('#stripe_error').text(response.error.message) $('input[type=submit]').attr('disabled', false) #alert(response.error.message) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload