Correct way for Storing API keys to credentials.yml.enc?

Viewed 1163

I'm trying to store my API keys for Stripe Elements..You can see my previous post here: Using javascript for Stripe Elements

As you can see, the Stripe Elements isn't showing up with the JS from Stripe or my stripejs.js file.

I am assuming its the https://js.stripe.com/v3/ that's not showing up somehow due to my API keys not storing correctly. I currently have my API keys stored in my .env file managed by the dotenv gem

I'm trying to store my API keys with credentials.yml.enc but not sure if I am doing it correctly.

What I do:

Call "EDITOR=atom rails credentials:edit" in CMD. Atom pops up. I store API keys the following way: PUBLISHABLE_KEY = pk_test_1111111111111111111 SECRET_KEY = sk_test_11111111111111111111

Save the file. Exit.

Although, it won't work. Am i not storing it correctly? Doing something wrong?

In my stripe.rb file under initializers is:

Rails.configuration.stripe = {
  :publishable_key => ENV['PUBLISHABLE_KEY'],
  :secret_key      => ENV['SECRET_KEY']
}

Stripe.api_key = Rails.configuration.stripe[:secret_key]

Edit:

I have my stripe.rb file structured like this:

Rails.configuration.stripe = {
  :publishable_key => Rails.application.credentials.stripe[:publishable_key],
  :secret_key      => Rails.application.credentials.stripe[:secret_key]
}

Stripe.api_key = Rails.configuration.stripe[:secret_key]

and then saved my credentials files like this:

stripe:   
    publishable_key: 123456789
    secret_key: 123456789

I then receive the error when starting my servier:

undefined method \credentials' for Rails:Module (NoMethodError)`

Is there something wrong I'm doing?

Attempt: 2:

I tried doing in the stripe.rb file...

Rails.configuration.stripe = {
  Rails.application.credentials.stripe[:publishable_key],
  Rails.application.credentials.stripe[:secret_key]
}

Stripe.api_key = Rails.configuration.stripe[:secret_key]

with and withou tthe last time... I get the error: "syntax error, unexpected '}', expecting end-of-input"

UPDATE:

in case anyone comes across this with a similar issue...

For me, it was bootstrap causing the problems. All you need to do is use class="form-control" within the Stripe Elements form for it to display correctly - if you're using Bootstrap.

2 Answers

What you read through the credentials from Rails 5.1 isn't stored in ENV variable, but through the Rails helper.

With file like

aws:
  access_key_id: 123
  secret_access_key: 345
secret_key_base: 2fdea1259c6660852864f9726616df64c8cd

You access them like this.

Rails.application.credentials.aws[:access_key_id]     # => "123"
Rails.application.credentials.aws[:secret_access_key] # => "345"
Rails.application.credentials.secret_key_base         # => "2fdea..

From what your have written it looks like your doing this in Rails 5.2

I was able to fix this Rails 5.2 in my app by changing the last line of the stripe.rb file to:

Stripe.api_key = Rails.application.credentials.stripe[:secret_key]

The key is stored in the credentials.yml.enc file.

Related