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

Error message when loading "Sign Up" View

DNSH opened this issue · comments

I have cloned the app from the repo and have run "rails s"

The homepage loads, but then when I click "Sign Up" I receive the following error message:

NoMethodError at /users/sign_up: undefined method `plan_class' for nil:NilClass,

The output says the error is occurring on line 8 in "app/views/devise/registrations/new.html.erb":

'data-payola-plan-type' => resource.plan.plan_class,

I was wondering how I should proceed? Thanks

Please wait a very short while ..
I am putting the fixes in place and will be submitting the Pull Requests to RailsApps momentarily.
After Danie Kehoe approves and merges the changes into the master, this error will go away.

On Apr 24, 2015, at 3:48 PM, DNSH notifications@github.com wrote:

I have cloned the app from the repo and have run "rails s"

The homepage loads, but then when I click "Sign Up" I receive the following error message:

NoMethodError at /users/sign_up: undefined method `plan_class' for nil:NilClass,

The output says the error is occurring on line 8 in "app/views/devise/registrations/new.html.erb":

'data-payola-plan-type' => resource.plan.plan_class,

I was wondering how I should proceed? Thanks


Reply to this email directly or view it on GitHub #127.

So a few possibilities. Most likely your resource doesn't have a plan. You can check by inspecting the resource as that page is loaded.

On Apr 24, 2015 5:48 PM, DNSH notifications@github.com wrote:

I have cloned the app from the repo and have run "rails s"

The homepage loads, but then when I click "Sign Up" I receive the following error message:

NoMethodError at /users/sign_up: undefined method `plan_class' for nil:NilClass,

The output says the error is occurring on line 8 in "app/views/devise/registrations/new.html.erb":

'data-payola-plan-type' => resource.plan.plan_class,

I was wondering how I should proceed? Thanks


Reply to this email directly or view it on GitHubhttps://github.com//issues/127.

I can tell you this line is correct : 'data-payola-plan-type' => resource.plan.plan_class,
I can also tell you this has been tested by me and the passing test exists, but ..
I cannot tell you exactly what to do to fix your app yet ..
I can't pull out each of the exact bits required to pull it off, from amongst all the test works that are done and are now being Pull Requested up to master in an orderly way.
I can also tell you that the Stripe Test API documentation is (was last time i looked) itself out of date, and I can tell you the main change you need to make is this :
Anywhere you have .cards. in the stripe codes, change that to .sources.
Example : a stripe_card_spec.rb working line is :
card = customer.sources.create(card: card_token)
it is no longer this :
card = customer.cards.create(card: card_token)
Here is one of the test helpers soon to be on the master that will give you a good feel for it, and notice where the word sources is, for that is the key change in the Stripe update :
spec/support/helpers/stripe_helpers.rb
. . .
describe 'Card Error Prep' do
it "prepares a card error" do
StripeMock.prepare_card_error(:card_declined, :new_charge)
cus = Stripe::Customer.create email: 'alice@example.com',
:card => stripe_helper.generate_card_token({ number:'4242424242424242', brand: 'Visa' })
expect { charge = Stripe::Charge.create({
amount: 900,
currency: 'usd',
customer: cus,
card: cus.sources.first,
description: 'hello',
})
}.to raise_error Stripe::CardError
end
end

You can see more of the set up for this helper here :
https://github.com/kathyonu/rails-stripe-membership-saas/blob/testsyetagain/spec/support/helpers/stripe_helpers.rb
Please do not use that branch commit as a copy and paste resource ..
There are no guarantees on that code until it is Pull Requested and merged into the master.
The working stripe test's will be arriving in the master in sure and working fashion, fairly quickly.

I doubt this has anything to do with stripe.

Can you open up your rails console and type Plan.pluck(:stripe_id)

Let me know the result. If it doesn't output ["platinum", "gold", "silver"] then try running CreatePlanService.new.call and see if that fixes your problem.

When we get to this point in the Pull Requests of more tests merged to master,
You will see this file :

app/models/plan.rb

 class Plan < ActiveRecord::Base
   include Payola::Plan

   has_many :users
   validates :stripe_id, inclusion: { in: Plan.pluck('DISTINCT stripe_id'), message: "not a valid subscription plan" }

   def redirect_path(subscription)
     '/‘
   end

 end

Has changed to this :

class Plan < ActiveRecord::Base
  include Payola::Plan

  has_many :users

  def redirect_path(subscription)
    ‘/'
  end
end

Feel free to make that one change, and see what happens.

The reason this vaidation can be removed is this method definition in this file below,
forces a choice of a plan on the after_initialize call :

app/models/user.rb
—————————————
def set_default_plan
self.plan ||= Plan.last

end

If you have any problem after that, let me know what it is.
Chances are I have already fixed it.
Please show me the error, if any, and if I can, I will show the fix.

On Apr 25, 2015, at 10:00 AM, Taylor Mock notifications@github.com wrote:

I doubt this has anything to do with stripe.

Can you open up your rails console and type Plan.pluck(:stripe_id)

Let me know the result. If it doesn't output ["platinum", "gold", "silver"] then try running CreatePlanService.new.call and see if that fixes your problem.


Reply to this email directly or view it on GitHub #127 (comment).

The membership app is being prepared for Rails 5.
The steps used to make and keep your app healthy have been moved here :
https://gist.github.com/kathyonu/c9ef8190e50422bc0edc
I moved it there because it is off topic here.

@kathyonu , forgive me if I'm missing something. But how does the inclusion validation

validates :stripe_id, inclusion: { in: Plan.pluck('DISTINCT stripe_id'),
    message: "not a valid subscription plan" }

have anything to do with the after initialize call of:

self.plan ||= Plan.last

They are doing completely different things. If the user doesn't select a default plan, the users plan is set to Plan.last.

Whereas the inclusion validation runs after the creation of a Plan. It looks at the current database, grabs the unique stripe_id's. And makes sure the new Plan you are creating has a stripe_id currently in the database. While I agree that line is totally unnecessary as there is a uniqueness validation on stripe_id , what does that have to do with setting the initial plan on the user?

@tmock12 : bowing in your presence as an early contributor ..
I can safely say, chances are I will be the incorrect describer of a detail ..
hard as I work to be accurate, I do make errors. just look at the
Contributors/Network commits for a long blazing trail of them.

I can explain how, while working up the tests for our in service production app, and now doing the same for the rails-stripe-membershp-saas update to Rails 4.2.1, that the pluck code was among the bugaboos that ran me round in circles. I thought, by virtue of the plan being set when the visitor arrives at the sign_up window .. that validation is pointless. That for me, is the connection. And why removing it was okay, and why removing it would be safe, and allow the app to fire up.

Am I amiss ? in this connection, though nebulous ? Eye am always open to correction.

@kathyonu I tried removing the lines you suggested from app/models/plan.rb but the error still persists

@tmock12 I tried your suggestion and the output is below:

Loading development environment (Rails 4.2.1)
2.2.2 :001 > Plan.pluck(:stripe_id)
(0.2ms) SELECT DISTINCT stripe_id FROM "plans"
(0.1ms) SELECT "plans"."stripe_id" FROM "plans"
=> []
2.2.2 :002 > CreatePlanService.new.call
Plan Load (0.6ms) SELECT "plans".* FROM "plans" WHERE "plans"."name" = ? ORDER BY "plans"."id" ASC LIMIT 1 ["name", "Platinum"] begin transaction
(0.3ms) rollback transaction
Stripe::AuthenticationError: (Status 401) You did not provide an API key, though you did set your Authorization header to "Bearer". Using Bearer auth, your Authorization header should look something like 'Authorization: Bearer YOUR_SECRET_KEY'. See https://stripe.com/docs/api#authentication for details, or we can help at https://support.stripe.com/.
from /home/dinesh/.rvm/gems/ruby-2.2.2@rails-stripe-membership-saas/gems/stripe-1.20.1/lib/stripe.rb:247:in handle_api_error' from /home/dinesh/.rvm/gems/ruby-2.2.2@rails-stripe-membership-saas/gems/stripe-1.20.1/lib/stripe.rb:139:inrescue in request'
from /home/dinesh/.rvm/gems/ruby-2.2.2@rails-stripe-membership-saas/gems/stripe-1.20.1/lib/stripe.rb:125:in request' from /home/dinesh/.rvm/gems/ruby-2.2.2@rails-stripe-membership-saas/gems/stripe-1.20.1/lib/stripe/api_operations/request.rb:15:inrequest'
from /home/dinesh/.rvm/gems/ruby-2.2.2@rails-stripe-membership-saas/gems/stripe-1.20.1/lib/stripe/api_operations/request.rb:37:in request' from /home/dinesh/.rvm/gems/ruby-2.2.2@rails-stripe-membership-saas/gems/stripe-1.20.1/lib/stripe/api_resource.rb:24:inrefresh'
from /home/dinesh/.rvm/gems/ruby-2.2.2@rails-stripe-membership-saas/gems/stripe-1.20.1/lib/stripe/api_resource.rb:31:in retrieve' from /home/dinesh/.rvm/gems/ruby-2.2.2@rails-stripe-membership-saas/gems/payola-payments-1.3.1/app/services/payola/create_plan.rb:7:incall'
from /home/dinesh/.rvm/gems/ruby-2.2.2@rails-stripe-membership-saas/gems/payola-payments-1.3.1/app/models/concerns/payola/plan.rb:23:in create_stripe_plan' from /home/dinesh/.rvm/gems/ruby-2.2.2@rails-stripe-membership-saas/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:432:inblock in make_lambda'
from /home/dinesh/.rvm/gems/ruby-2.2.2@rails-stripe-membership-saas/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:164:in call' from /home/dinesh/.rvm/gems/ruby-2.2.2@rails-stripe-membership-saas/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:164:inblock in halting'
from /home/dinesh/.rvm/gems/ruby-2.2.2@rails-stripe-membership-saas/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:504:in call' from /home/dinesh/.rvm/gems/ruby-2.2.2@rails-stripe-membership-saas/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:504:inblock in call'
from /home/dinesh/.rvm/gems/ruby-2.2.2@rails-stripe-membership-saas/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:504:in each' from /home/dinesh/.rvm/gems/ruby-2.2.2@rails-stripe-membership-saas/gems/activesupport-4.2.1/lib/active_support/callbacks.rb:504:incall'
... 28 levels...
from /home/dinesh/.rvm/gems/ruby-2.2.2@rails-stripe-membership-saas/gems/railties-4.2.1/lib/rails/commands/console.rb:9:in start' from /home/dinesh/.rvm/gems/ruby-2.2.2@rails-stripe-membership-saas/gems/railties-4.2.1/lib/rails/commands/commands_tasks.rb:68:inconsole'
from /home/dinesh/.rvm/gems/ruby-2.2.2@rails-stripe-membership-saas/gems/railties-4.2.1/lib/rails/commands/commands_tasks.rb:39:in run_command!' from /home/dinesh/.rvm/gems/ruby-2.2.2@rails-stripe-membership-saas/gems/railties-4.2.1/lib/rails/commands.rb:17:in<top (required)>'
from /home/dinesh/.rvm/gems/ruby-2.2.2@rails-stripe-membership-saas/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in require' from /home/dinesh/.rvm/gems/ruby-2.2.2@rails-stripe-membership-saas/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:inblock in require'
from /home/dinesh/.rvm/gems/ruby-2.2.2@rails-stripe-membership-saas/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:240:in load_dependency' from /home/dinesh/.rvm/gems/ruby-2.2.2@rails-stripe-membership-saas/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:inrequire'
from /home/dinesh/bigsolve/arnold/bin/rails:8:in <top (required)>' from /home/dinesh/.rvm/gems/ruby-2.2.2@rails-stripe-membership-saas/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:inload'
from /home/dinesh/.rvm/gems/ruby-2.2.2@rails-stripe-membership-saas/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in block in load' from /home/dinesh/.rvm/gems/ruby-2.2.2@rails-stripe-membership-saas/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:240:inload_dependency'
from /home/dinesh/.rvm/gems/ruby-2.2.2@rails-stripe-membership-saas/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in load' from /home/dinesh/.rvm/rubies/ruby-2.2.2/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:inrequire'
from /home/dinesh/.rvm/rubies/ruby-2.2.2/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from -e:1:in

@tmock12 , Look at the error :

On Apr 26, 2015, at 3:27 PM, Dinesh Ramdhayan notifications@github.com wrote:
Stripe::AuthenticationError: (Status 401)
You did not provide an API key,
though you did set your Authorization header to "Bearer”.
Using Bearer auth, your Authorization header should look something like
'Authorization: Bearer YOUR_SECRET_KEY’.
See https://stripe.com/docs/api#authentication https://stripe.com/docs/api#authentication
for details, or we can help at https://support.stripe.com/ https://support.stripe.com/.

So, the question becomes, is the Stripe API Key set in your environmental variables ?
( the way your system is asking for “Bearer” is where it wants to see the key )

Normal answer to not having an api key set would be this :
In the working directory, type just env and look for your STRIPE_API_KEY.
in the Terminal :

$ env

You also want to make sure your STRIPE_PUBLISHABLE_KEY is set, so look for that one too.
If they are showing up in your env result, and they are correct .. next do this ..
Open your config/secrets.yml file and make sure you added these two in development :

development:
admin_name: First User
admin_email: user@example.com
admin_password: changeme
email_provider_username: <%= ENV["GMAIL_USERNAME"] %>
email_provider_password: <%= ENV["GMAIL_PASSWORD"] %>
domain_name: example.com
mailchimp_api_key: <%= ENV["MAILCHIMP_API_KEY"] %>
mailchimp_list_id: <%= ENV["MAILCHIMP_LIST_ID"] %>
stripe_api_key: <%= ENV["STRIPE_API_KEY"] %>
stripe_publishable_key: <%= ENV["STRIPE_PUBLISHABLE_KEY"] %>
secret_key_base: very_long_random_string

Add those two if need be, close the file, and restart your server and or console.
If they are NOT showing up in your env results, do this in your directory terminal :

$ export STRIPE_API_KEY=sk_test_( fill in your real key sequence )
$ export STRIPE_PUBLISHABLE_KEY=pk_test_( fill in your real key sequence )

Now try it.

Sorry I was driving all day and don't have much time to respond. But yes the problem is your API key isn't set, which is why the plans aren't being set, which is causing the error on the registrations new page.

like @DNSH reported the error is on line 8 of the registrations/new page. plan is null. Which is why I had him run Plan.pluck(:stripe_id). And sure enough the return was []. So he had no plans. So thats why I had him run the create plan service and thats where the problem lies. The ENV variables aren't set so the Plans can't be created.

So to fix this its as simple as providing your API keys and rerunning CreatePlanService.new.call

You can't just start your server after providing your API keys as Plan.count will still return 0. and the original error will still exist. You have to run the service above to create the plans.

@DNSH please let us know if setting the API keys and running $ rake db:reset solves your issue. If so, you can close the issue.

@tmock12 , How correct you are.

@DNSH , I missed the step I like to do often while development changes occur ..

$ rake db:reset

If that does not work, you instantly know you have a problem.
If that does work, the Plans are there.
If that does not work, and you have never migrated and seeded, the Plans are not there.

I use the reset command often because it covers all of the database creations,
no matter how large the schema.rb becomes.

Let us leave no stone unturned here ..
This is what the $ rake db:reset command results in, with the Plans being created last :

directory_app-prompt$ rake db:reset
-- create_table("payola_affiliates", {:force=>:cascade})
-> 0.0034s

-- create_table("payola_coupons", {:force=>:cascade})
-> 0.0229s
-- create_table("payola_sales", {:force=>:cascade})
-> 0.0020s
-- add_index("payola_sales", ["coupon_id"], {:name=>"index_payola_sales_on_coupon_id"})
-> 0.0018s
-- add_index("payola_sales", ["email"], {:name=>"index_payola_sales_on_email"})
-> 0.0016s
-- add_index("payola_sales", ["guid"], {:name=>"index_payola_sales_on_guid"})
-> 0.0018s
-- add_index("payola_sales", ["owner_id", "owner_type"], {:name=>"index_payola_sales_on_owner_id_and_owner_type"})
-> 0.0020s
-- add_index("payola_sales", ["product_id", "product_type"], {:name=>"index_payola_sales_on_product"})
-> 0.0022s
-- add_index("payola_sales", ["stripe_customer_id"], {:name=>"index_payola_sales_on_stripe_customer_id"})
-> 0.0023s
-- create_table("payola_stripe_webhooks", {:force=>:cascade})
-> 0.0013s
-- create_table("payola_subscriptions", {:force=>:cascade})
-> 0.0015s
-- add_index("payola_subscriptions", ["guid"], {:name=>"index_payola_subscriptions_on_guid"})
-> 0.0012s
-- create_table("plans", {:force=>:cascade})
-> 0.0013s
-- create_table("users", {:force=>:cascade})
-> 0.0016s
-- add_index("users", ["email"], {:name=>"index_users_on_email", :unique=>true})
-> 0.0012s
-- add_index("users", ["plan_id"], {:name=>"index_users_on_plan_id"})
-> 0.0015s

-- add_index("users", ["reset_password_token"], {:name=>"index_users_on_reset_password_token", :unique=>true})
-> 0.0016s
-- initialize_schema_migrations_table()
-> 0.0064s
-- create_table("payola_affiliates", {:force=>:cascade})
-> 0.0017s
-- create_table("payola_coupons", {:force=>:cascade})
-> 0.0026s
-- create_table("payola_sales", {:force=>:cascade})
-> 0.0015s
-- add_index("payola_sales", ["coupon_id"], {:name=>"index_payola_sales_on_coupon_id"})
-> 0.0013s
-- add_index("payola_sales", ["email"], {:name=>"index_payola_sales_on_email"})
-> 0.0014s
-- add_index("payola_sales", ["guid"], {:name=>"index_payola_sales_on_guid"})
-> 0.0016s
-- add_index("payola_sales", ["owner_id", "owner_type"], {:name=>"index_payola_sales_on_owner_id_and_owner_type"})
-> 0.0018s
-- add_index("payola_sales", ["product_id", "product_type"], {:name=>"index_payola_sales_on_product"})
-> 0.0020s
-- add_index("payola_sales", ["stripe_customer_id"], {:name=>"index_payola_sales_on_stripe_customer_id"})
-> 0.0033s
-- create_table("payola_stripe_webhooks", {:force=>:cascade})
-> 0.0012s
-- create_table("payola_subscriptions", {:force=>:cascade})
-> 0.0016s
-- add_index("payola_subscriptions", ["guid"], {:name=>"index_payola_subscriptions_on_guid"})
-> 0.0015s
-- create_table("plans", {:force=>:cascade})
-> 0.0013s
-- create_table("users", {:force=>:cascade})
-> 0.0027s
-- add_index("users", ["email"], {:name=>"index_users_on_email", :unique=>true})
-> 0.0014s
-- add_index("users", ["plan_id"], {:name=>"index_users_on_plan_id"})
-> 0.0022s
-- add_index("users", ["reset_password_token"], {:name=>"index_users_on_reset_password_token", :unique=>true})
-> 0.0021s
-- initialize_schema_migrations_table()
-> 0.0042s
CREATED ADMIN USER: user@example.com
CREATED PLANS

If the end of yours does not look like that, put these four lines in your db/seeds.rb file :

user = CreateAdminService.new.call
puts 'CREATED ADMIN USER: ' << user.email
CreatePlanService.new.call
puts 'CREATED PLANS'

Now enter the console, then enter this command > silver = Plan.where(:name => "Silver")

directory_app-prompt$ rails c
Loading development environment (Rails 4.2.1)
2.2.2 :001 > silver = Plan.where(:name => "Silver")
Plan Load (0.1ms) SELECT "plans".* FROM "plans" WHERE "plans"."name" = ? [["name", "Silver"]]
=> <ActiveRecord::Relation [#<Plan id: 3, name: "Silver", stripe_id: "silver", interval: "month", amount: 900, created_at: "2015-04-27 02:53:56", updated_at: "2015-04-27 02:53:56">]>
2.2.2 :002 > silver
=> <ActiveRecord::Relation [#<Plan id: 3, name: "Silver", stripe_id: "silver", interval: "month", amount: 900, created_at: "2015-04-27 02:53:56", updated_at: "2015-04-27 02:53:56">]>
2.2.2 :003 >

You now have your Plan available to you. These commands now work in the console :

silver = Plan.where(:name => "Silver")
gold = Plan.where(:name => "Gold")
platinum = Plan.where(:name => "Platinum")

Proofs :
$ rails console
2.2.2 :001> silver = Plan.where(:name => "Silver")
Plan Load (0.1ms) SELECT "plans".* FROM "plans" WHERE "plans"."name" = ? [["name", "Silver"]]
=> #<ActiveRecord::Relation [#<Plan id: 3, name: "Silver", stripe_id: "silver", interval: "month", amount: 900, created_at: "2015-04-27 02:53:56", updated_at: "2015-04-27 02:53:56">]>

2.2.2 :002> gold = Plan.where(:name => "Gold")
Plan Load (0.1ms) SELECT "plans".* FROM "plans" WHERE "plans"."name" = ? [["name", "Gold"]]
=> #<ActiveRecord::Relation [#<Plan id: 2, name: "Gold", stripe_id: "gold", interval: "month", amount: 1900, created_at: "2015-04-27 02:53:56", updated_at: "2015-04-27 02:53:56">]>

2.2.2 :003> platinum = Plan.where(:name => "Platinum")
Plan Load (0.1ms) SELECT "plans".* FROM "plans" WHERE "plans"."name" = ? [["name", "Platinum"]]
=> #<ActiveRecord::Relation [#<Plan id: 1, name: "Platinum", stripe_id: "platinum", interval: "month", amount: 2900, created_at: "2015-04-27 02:53:55", updated_at: "2015-04-27 02:53:55">]>
2.2.2 :004 >

How is it working now, @DNSH ?
Any further problem on this issue ?
Ask clear as Yee can, Eye shall answer clear as I can muster.

Please Note :

If you do not have the database migrated and seeded ..
and/or if the $ rake db:reset command cannot be run ..
the call command will not work. Here is what it looks like,
first we need to drop the database to get rid of Plans :

$ rake db:drop

Now go into the console and run the command :

$ rails c
Loading development environment (Rails 4.2.1)
2.2.2 :001 > CreatePlanService.new.call
ActiveRecord::StatementInvalid: Could not find table 'plans'

To fix this, run either $ rake db:migrate or $ rake db:reset and it will work.

$ rake db:reset

I am not showing results for brevity, they are same as above.
Now enter the console and run the call :

$ rails c
Loading development environment (Rails 4.2.1)
2.2.2 :001 > CreatePlanService.new.call
Plan Load (0.1ms) SELECT "plans".* FROM "plans" WHERE "plans"."name" = ? ORDER BY "plans"."id" ASC LIMIT 1 [["name", "Platinum"]]
(0.1ms) begin transaction
(0.0ms) commit transaction
Plan Load (0.1ms) SELECT "plans".* FROM "plans" WHERE "plans"."name" = ? ORDER BY "plans"."id" ASC LIMIT 1 [["name", "Gold"]]
(0.0ms) begin transaction
(0.1ms) commit transaction
Plan Load (0.1ms) SELECT "plans".* FROM "plans" WHERE "plans"."name" = ? ORDER BY "plans"."id" ASC LIMIT 1 [["name", "Silver"]]
(0.0ms) begin transaction
(0.1ms) commit transaction
=> true

There you have it.

$ rake -T
^^ a developer's friend, that one is.
this is where the rake db:drop command is found, and explained
Run the command and look for this in the results : rake db:drop
Drops the database from DATABASE_URL or config/database.yml for the current RAILS_ENV (use db:drop:all to drop all databases in the config)

No reply from @DNSH so I am closing this issue and assuming it was resolved to his satisfaction.

I had the same error, running these two commands in the terminal solved the issue for me:

$ export STRIPE_API_KEY=sk_test_( fill in your real key sequence )
$ export STRIPE_PUBLISHABLE_KEY=pk_test_( fill in your real key sequence )

Think it was an issue with me not setting the environment variables properly using the Figaro gem.

May I refer you to one of the first tests I put in place when working with Stripe in an app new to me :

spec/stripe/stripe_confiig_spec.rb

describe "Config Variables" do

describe "STRIPE_API_KEY" do
# this tests env variables against config/secrets.yml which reads from your env variables $ env
it "a STRIPE_API_KEY is set" do
expect(ENV['STRIPE_API_KEY']).to eq(Rails.application.secrets.stripe_api_key),
"Your STRIPE_API_KEY is not set, Please refer to the 'Configure the Stripe Initializer' section of the README"
end
end

describe "STRIPE_PUBLISHABLE_KEY" do
it "a STRIPE_PUBLISHING_KEY is set" do
expect(ENV['STRIPE_PUBLISHABLE_KEY']).to eq(Rails.application.secrets.stripe_publishable_key),
"Your STRIPE_PUBLISHABLE_KEY is not set, Please refer to the 'Configure the Stripe Initializer' section of the README"
end
end

end
——

I just pushed this commit showing the same tests :

https://github.com/kathyonu/rails-stripe-membership-saas/blob/20150520usersignup/spec/stripe/stripe_config_spec.rb https://github.com/kathyonu/rails-stripe-membership-saas/blob/20150520usersignup/spec/stripe/stripe_config_spec.rb

On Jun 6, 2015, at 3:58 AM, Ben Matthews notifications@github.com wrote:

I had the same error, running these two commands in the terminal solved the issue for me:

$ export STRIPE_API_KEY=sk_test_( fill in your real key sequence )
$ export STRIPE_PUBLISHABLE_KEY=pk_test_( fill in your real key sequence )

Think it was an issue with me not setting the environment variables properly using the Figaro gem.


Reply to this email directly or view it on GitHub #127 (comment).

Thanks @kathyonu. The app is working for me locally, but when I push to Heroku it is unable to migrate the databases due to the following error:

Stripe::AuthenticationError: You did not provide an API key, though you did set your Authorization
header to "Bearer". Using Bearer auth, your Authorization header should lo
ok something like 'Authorization: Bearer YOUR_SECRET_KEY'.

I also see the following error in the Heroku logs:

RestClient::Unauthorized: 401 Unauthorized 

I've checked the Heroku dashboard and the Stripe API and Publishable keys have been set, so I'm not sure what is wrong.

You can set your ENV variables with a heroku command.

heroku config:add STRIPE_API_KEY=<your API key>
heroku config:add STRIPE_PUBLISHABLE_KEY=<your publishable key>

You can read more about it here: http://railsapps.github.io/rails-environment-variables.html

@archonic Thanks - I checked my heroku dashboard (e.g. http://dashboard.heroku.com) and the ENV variables, but for whatever reason it was only when following your heroku commands that the app worked in production. Problem solved!

@benrmatthews , i presume that means you have run this command in your terminal,

$ heroku config --app yourappname

from your app’s directory, and your Stripe Keys are showing ?

If not, set them both with :

$ heroku config:add STRIPE_API_KEY=sk_live_yourlongstring STRIPE_PUBLISHABLE_KEY=pk_live_yourlongstring

Or separately :

$ heroku config:add STRIPE_API_KEY=sk_live_yourlongstring
$ heroku config:add STRIPE_PUBLISHABLE_KEY=pk_live_yourlongstring

Also, I don’t think this has anything to do with it, but ..
Heroku requires this command to be run before pushing to production :

$ rake rails:update:bin

Taken from : Rails-applications-health-codes.md : https://gist.github.com/kathyonu/c9ef8190e50422bc0edc

On Jun 7, 2015, at 1:07 PM, Ben Matthews notifications@github.com wrote:

Thanks @kathyonu https://github.com/kathyonu. The app is working for me locally, but when I push to Heroku it is unable to migrate the databases due to the following error:

Stripe::AuthenticationError: You did not provide an API key, though you did set your Authorization
header to "Bearer". Using Bearer auth, your Authorization header should lo
ok something like 'Authorization: Bearer YOUR_SECRET_KEY'.
I also see the following error in the Heroku logs:

RestClient::Unauthorized: 401 Unauthorized
I've checked the Heroku dashboard and the Stripe API and Publishable keys have been set, so I'm not sure what is wrong.


Reply to this email directly or view it on GitHub #127 (comment).

commented

I cannot create a plan in Heroku. I am able to successfully create run rake db:seed, create plans, and sign up users in development mode. In production on heroku I am unable to create a plan. I receive the same errors others are getting. For example:

irb(main):001:0> s1 = Plan.new(name:'Silver', stripe_id:'silver', interval:'monthly', amount: 900)
=> #<Plan id: nil, name: "Silver", stripe_id: "silver", interval: "monthly", amount: 900, created_at: nil, updated_at: nil>
irb(main):002:0> s1.save(:validate => false)
(1.5ms) BEGIN
(1.5ms) BEGIN
(1.4ms) ROLLBACK
(1.4ms) ROLLBACK
Stripe::AuthenticationError: (Status 401) You did not provide an API key, though you did set your Authorization header to "Bearer". Using Bearer auth, your Authorization header should look something like 'Authorization: Bearer YOUR_SECRET_KEY'. See https://stripe.com/docs/api#authentication for details, or we can help at https://support.stripe.com/.

I set my test keys in heroku using the previous answer:
heroku config:add STRIPE_API_KEY=
heroku config:add STRIPE_PUBLISHABLE_KEY=

I am not able to run db:drop because I have important information in the database.

I am using the following gems which I believe to be the most recent:

stripe (1.20.1)
  json (~> 1.8.1)
  mime-types (>= 1.25, < 3.0)
  rest-client (~> 1.4)

payola-payments (1.3.2)
  aasm (>= 4.0.7)
  jquery-rails
  rails (>= 4.1)
  stripe (= 1.20.1)
  stripe_event (>= 1.3.0)

I removed the following line from the model Plan.rb:
validates :stripe_id, inclusion: { in: Plan.pluck('DISTINCT stripe_id'),
message: "not a valid subscription plan" }

Where the are the Bearer authorization header environmental variables called from by Payola Payments?

Help troubleshooting this problem as to why it works in development but not on Heroku would be wonderful. Please let me know if you need any other information.

Thanks,

Ty

commented

I read the Stripe documentation and set the Heroku ENV variables using:

heroku config:set PUBLISHABLE_KEY=pk_test_xxxxxxxxxxxxxxxxx SECRET_KEY=xxxxxxxxxxxxxxxxxxx

Now I am getting a new error that says:
{"guid":null,"status":"pending","error":"Email can't be blank and Stripe token can't be blank"}

One step closer =)

commented

mrusson's comment on Apr 15 worked:
#116