What is wrong with my stripe configuration?
Stripe isn't working anymore. When trying to create a new charge returns
the server responded with status 401
I followed stripes checkout docs to set up stripe. Stripe checkout
I have the stripe PUBLISHABLE_KEY: and SECRET_KEY: set up in application.yml
resources: charges
charges ctrl
def new
end
def create
# Amount in cents
@amount = 500
customer = Stripe::Customer.create(
:email => params[:stripeEmail],
:source => params[:stripeToken]
)
charge = Stripe::Charge.create(
:customer => customer.id,
:amount => @amount,
:description => 'Rails Stripe customer',
:currency => 'usd'
)
rescue Stripe::CardError => e
flash[:error] = e.message
redirect_to new_charge_path
end
stripe.rb
Rails.configuration.stripe = {
:publishable_key => ENV['PUBLISHABLE_KEY'],
:secret_key => ENV['SECRET_KEY']
}
Stripe.api_key = Rails.configuration.stripe[:secret_key]
charges/new.html.erb
<%= form_tag charges_path do %>
<article>
<% if flash[:error].present? %>
<div id="error_explanation">
<p><%= flash[:error] %></p>
</div>
<% end %>
<label class="amount">
<span>Amount: $5.00</span>
</label>
</article>
<script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="<%= Rails.configuration.stripe[:publishable_key] %>"
data-description="A month's subscription"
data-amount="500"
data-locale="auto"></script>
<% end %>
401 errors are authorization errors, see: https://httpstatuses.com/401
Do you mean you store your keys in secrets.yml
? If so then you don't read those values from the environment, instead you'd do something like Rails.application.secrets.publishable_key
So in secrets.yml
publishable_key: pk_your_publishable_key
secret_key: sk_your_secret_key
then in stripe.rb
:
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]
Env variables are setup in application.yml. All my applications use this set up and have worked. All of a sudden it's giving me trouble.
application.yml
STRIPE_PUBLISHABLE_KEY: pk_test_publishable_key
STRIPE_SECRET_KEY: sk_test_secret_key
On your Rails box where you're getting the error, if you go to the console and execute printenv
, does STRIPE_PUBLISHABLE_KEY
and STRIPE_SECRET_KEY
show up?
Unless I'm missing something here, when you put key/values into a yml file, that doesn't mean they're available via the ENV['foo']
method. The only way I know to convert your variables from a yml file into ENV variables is to put this into your config/application.rb
:
config.before_configuration do
env_file = File.join(Rails.root, 'config', 'custom_yml_file.yml')
YAML.load(File.open(env_file)).each do |key, value|
ENV[key.to_s] = value.to_s
end if File.exists?(env_file)
end
There could be a gem that does this as well, so maybe that's what you're using to get your yml entries into an ENV variable?
In any case, your app isn't authenticating... so have you verified all the keys are correct and they haven't been rolled over? Try hard coding your keys in your stripe.rb
and charges/new.html.erb
file and see if it authenticates:
stripe.rb
Rails.configuration.stripe = {
:publishable_key => pk_test_asdfasdfasdfasdf,
:secret_key => sk_test_asdfadsfadsfadsf
}
Stripe.api_key = Rails.configuration.stripe[:secret_key]
charges/new.html.erb
<script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="pk_test_asdfasdfasdfasdf"
data-description="A month's subscription"
data-amount="500"
data-locale="auto">
</script>
Thank you. I'm using figaro gem. The ENV var are in application.yml
There was an issue with the key. The key they provided worked for weeks then stopped working. They said the key was correct after chatting back and forth they generated a new one (without telling me) and it works now. Very frustrating. I rolled the keys previously hoping would fix the issue.
Ahh yeah, Figaro - I forgot all about that one
Glad you were able to get it sorted out - too many cooks in the kitchen is always a headache!