RailsApps / rails-stripe-membership-saas

An example Rails 4.2 app with Stripe and the Payola gem for a membership or subscription site.

Home Page:http://railsapps.github.io/rails-stripe-membership-saas

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Stripe token not found

jbankston opened this issue · comments

Hello,

I am experiencing an issue with stripe token not found. Any help is appreciated.

RuntimeError in RegistrationsController#create

Stripe token not present. Can't create account.

app/models/user.rb:34:in `update_stripe'

{"utf8"=>"✓",
"authenticity_token"=>"ytT7L0+Ulg035PbIgzjSO2RtxUcjlz0/cXumwzVticg=",
"plan"=>"starter",
"user"=>{"name"=>"jeff test",
"email"=>"testme@changehealthcare.com",
"password"=>"[FILTERED]",
"password_confirmation"=>"[FILTERED]",
"coupon"=>"",
"stripe_token"=>""},
"commit"=>"Sign up"}

routes file

Britevid::Application.routes.draw do
mount StripeEvent::Engine => '/stripe'
get "content/starter"
get "content/intermediate"
get "content/professional"
unauthenticated do
root :to => 'external#index'
match '/features' => 'external#features'
match '/pricing' => 'external#pricing'
end

devise_for :users, :controllers => { :registrations => 'registrations' }
devise_scope :user do
put 'update_plan', :to => 'registrations#update_plan'
put 'update_card', :to => 'registrations#update_card'
match '/sign_in' => 'devise/sessions#new'
end

authenticated :user do
root :to => "dashboard#index"
match 'dashboard/videos' => 'videos#index'
match 'dashboard/statistics' => 'statistics#index'
match 'dashboard/analytics' => 'analytics#index'
end

resources :users
post "zencoder-callback" => "zencoder_callback#create", :as => "zencoder_callback"

end

user.rb

class User < ActiveRecord::Base
rolify

Include default devise modules. Others available are:

:token_authenticatable, :confirmable,

:lockable, :timeoutable and :omniauthable

devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable

Setup accessible (or protected) attributes for your model

attr_accessible :name, :email, :password, :password_confirmation, :remember_me, :stripe_token, :coupon, :last_4_digits
attr_accessor :stripe_token, :coupon
before_save :update_stripe
before_destroy :cancel_subscription

def update_plan(role)
self.role_ids = []
self.add_role(role.name)
unless customer_id.nil?
customer = Stripe::Customer.retrieve(customer_id)
customer.update_subscription(:plan => role.name)
end
true
rescue Stripe::StripeError => e
logger.error "Stripe Error: " + e.message
errors.add :base, "Unable to update your subscription. #{e.message}."
false
end

def update_stripe
return if email.include?(ENV['ADMIN_EMAIL'])
return if email.include?('@example.com') #and not Rails.env.production?
if customer_id.nil?
if !stripe_token.present?
raise "Stripe token not present. Can't create account."
end
if coupon.blank?
customer = Stripe::Customer.create(
:email => email,
:description => name,
:card => stripe_token,
:plan => roles.first.name
)
else
customer = Stripe::Customer.create(
:email => email,
:description => name,
:card => stripe_token,
:plan => roles.first.name,
:coupon => coupon
)
end
else
customer = Stripe::Customer.retrieve(customer_id)
if stripe_token.present?
customer.card = stripe_token
end
customer.email = email
customer.description = name
customer.save
end
self.last_4_digits = customer.active_card.last4
self.customer_id = customer.id
self.stripe_token = nil
rescue Stripe::StripeError => e
logger.error "Stripe Error: " + e.message
errors.add :base, "#{e.message}."
self.stripe_token = nil
false
end

def cancel_subscription
unless customer_id.nil?
customer = Stripe::Customer.retrieve(customer_id)
unless customer.nil? or customer.respond_to?('deleted')
if customer.subscription.status == 'active'
customer.cancel_subscription
end
end
end
rescue Stripe::StripeError => e
logger.error "Stripe Error: " + e.message
errors.add :base, "Unable to cancel your subscription. #{e.message}."
false
end

def expire
UserMailer.expire_email(self).deliver
destroy
end

def linked_with_viewer(viewer)
UsersViewers.where(user_id: self.id, viewer_id: viewer.id).any?
end
end

This page : https://github.com/RailsApps/rails-stripe-membership-saas
Shows these two definition lines as

def update_stripe
return if email.include?(ENV['ADMIN_EMAIL'])
return if email.include?('@example.com') and not Rails.env.production?

Yours are not the same.
You do not say if you are in development or production.

kathyonu,

I am in development on my local machine.

You need to change this line, then it should work

return if email.include?('@example.com') and not Rails.env.production?
You have it partially commented out.

That is what I had originally. I still receive the error when i try to purchase one of the plans. I also noticed I get an internal 500 error in the console. It seems like the transaction never reaches stripe as I do not see any activity in their logs. Thanks.

You are showing this email : "email"=>"testme@changehealthcare.com",
If that is your admin email, and your Configurations variables show that, then it should work.
You are using this in the user model :
return if email.include?('@example.com') and not Rails.env.production?
You should be testing with the db seeded users@example.com addresses.
If you try signing up using an @example.com address, it should work.
If this doesn't help, you will need to wait for another to answer you further.

What happens if you clone the example app from the Github repo, set your Stripe key, and run the Cucumber test suite?

Daniel,

I cloned the example app and ran the migrations.
Failing Scenarios:
cucumber features/users/sign_in.feature:6 # Scenario: User is not signed up

Kathyonu,

The seed file works fine. I am trying to test a transaction with the fake card number stripe provides and the token is missing. I am also running the app in development on my local pc. Thanks

From the README:

Before you can use the application, you must prepare your Stripe account to recognize the subscription plans you’ll offer. To match the example application, tell Stripe that you have three plans named “Silver”, “Gold”, and “Platinum” that will be billed monthly at rates of $9, $19, and $29 ... Go to the Stripe plan management page to create a subscription plan. Stripe offers documentation about creating a plan...

Thanks guy for both of your help. I had modified the example app and didnt add the stripe js to the second application layout form. Thanks again.