Brian
Brian

Reputation: 83

Stripe on Ruby on Rails - Creating config/initializers/stripe.rb

Following the Stripe setup document for Ruby on Rails (https://stripe.com/docs/checkout/rails), it says that the config/initializers/stripe.rb will be created when the app is started.

I have shutdown the server and the restarted the server several times, but this file isn't being created under the path identified in the documentation.

What am I doing wrong? Appreciate the help.

Upvotes: 2

Views: 2206

Answers (2)

dandush03
dandush03

Reputation: 255

I really disliked the anti-pattern that Stripe Gem has, instead I end up overriding the Stripe::StripeConfiguration (even here we can see naming redundancy 😞) Here is my solution:

## Stripe Initializer
## {Rails.root}/config/initializers/stripe.rb

# frozen_string_literal: true

require 'stripe'

module Stripe
  class StripeConfiguration
    def api_key=(key_value)
      @api_key = key_value || Rails.application.credentials.stripe.secret_key
    end
  end
end

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

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

NOTE: for the Stripe team, their implementation in no way gives an extra security layer, anyone who can have access to the console/credentials will be able to read it so it makes no sense that they override it on each request. You can always assign an attribute with an operator.

PS: I'm working on an app that would improve that security (Comming Soon)

Upvotes: 0

SacWebDeveloper
SacWebDeveloper

Reputation: 2812

Create this file manually. Initializers are not generated on application start. They are read by Rails to configure your specific application.

Create config/initializers/stripe.rb and fill it with the following.

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

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

Set your secret key and publishable key in your ENVs. Restart your application after the change or you won't see any difference.

I can see how you would be confused, Stripe documentation says "An initializer is a good place to set these values, which will be provided when the application is started." They mean the values you set in that file will be provided to the application instance.

Upvotes: 9

Related Questions