2

I am doing some development involving the Stripe API. In my development environment I am using my stripe test key. In production I am using the real api key so that I can process real transactions of course.

I am currently just changing out the test api key immediately before I deploy to my production environment; this does not feel very good. A strategy that I am pretty sure would work is to just make a development branch with a gitignore (ignoring my initializer that loads the api key) and then just merging it with the master branch before I deploy; this way the api keys would always be correct in their respective environments. I don't really like this approach though. Is there some sort of way to configure these api keys somewhere so that the app just knows which one to use when in dev/prod?

Spencer Cooley
  • 8,471
  • 16
  • 48
  • 63

4 Answers4

9

In rails 4.1 we have the config/secrets.yml file so you can set the api keys there like so:

development:
  secret_key_base: 'xxx'
  publishable_key: xxx
  secret_key: xxx
production:
  secret_key_base: 'xxx'
  publishable_key: xxx
  secret_key: xxx

And in your stripe.rb file you can do this:

Rails.configuration.stripe = {
  :publishable_key => Rails.application.secrets.publishable_key,
  :secret_key      => Rails.application.secrets.secret_key
}

Stripe.api_key = Rails.configuration.stripe[:secret_key]
David Mohundro
  • 11,922
  • 5
  • 40
  • 44
alexsmn
  • 1,736
  • 11
  • 18
2

I prefer using yetting.

Though adding API keys in to the environments files gets the job done, adding them to a seperate file seems a whole lot of a cleaner and abstracted way to me.

Once the gem is installed. you can make a yetting.yml file in the config directory.

development:
  facebook_app_id: xxxxxx
staging:
  facebook_app_id: xxxxxx
production:
  facebook_app_id: xxxxxx
sohaibbbhatti
  • 2,672
  • 22
  • 21
1

There are many ways to skin this cat.

Quick and simple, look at Rails.env.

It will return the environment the server is running in. You just need to set the key accordingly.

The way I would really recommend is to create application config variables with a yaml file or using Rails.application.config

config/environments/development.rb

config.api_key = "my dev api key"

config/environments/production.rb

config.api_key = "my prod api key"

To access your key

MyApp::Application.config.api_key

Check out this question for other examples.

Community
  • 1
  • 1
Kyle
  • 21,978
  • 2
  • 60
  • 61
1

This is how I do it :

In config/initializers/stripe.rb

if(Rails.env == 'development' || Rails.env == 'staging')
  Stripe.api_key = "XXXXXXXXXXXXXXXXXXXXXXXXXXX"
  STRIPE_PUBLIC_KEY = "XXXXXXXXXXXXXXXXXXXXXXXXXXX"
elsif(Rails.env == 'production')
  Stripe.api_key = "XXXXXXXXXXXXXXXXXXXXXXXXXXX"
  STRIPE_PUBLIC_KEY = "XXXXXXXXXXXXXXXXXXXXXXXXXXX"
end
az7ar
  • 5,187
  • 2
  • 19
  • 23
  • 7
    That works, but it would not be following the best practices: what happens when you commit your code to a shared repo? The production keys would become available to your entire team. Production keys should be set in the environment (ENV). – Marco Lazzeri Apr 10 '13 at 15:09