Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stripe - No API key provided?

I'm struggling with this error No API key provided. Set your API key using "Stripe.api_key = ". You can generate API keys from the Stripe web interface in a Rails app after following step by step Stripe's guide.

From what I see, everything looks fine, but it keeps returning that notice. Any advice?

Charges Controller:

  class ChargesController < ApplicationController

    def new
    end

    def create
      # Amount in cents
      @amount = 500

      customer = Stripe::Customer.create(
        :email => '[email protected]',
        :card  => 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 charges_path
    end

  end

config/initializers/stripe.rb

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

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

Terminal trace Started POST "/charges" for 127.0.0.1 at 2014-12-12 22:15:08 +0100

    Processing by ChargesController#create as HTML

      Parameters: {"utf8"=>"✓", "authenticity_token"=>"XXX", "stripeToken"=>"tok_1590kf2NNSl5uX0kXE9XXX", "stripeTokenType"=>"card", "stripeEmail"=>"[email protected]"}

    Completed 500 Internal Server Error in 2ms



    Stripe::AuthenticationError - No API key provided. Set your API key using "Stripe.api_key = <API-KEY>". You can generate API keys from the Stripe web interface. See https://stripe.com/api for details, or email [email protected] if you have any questions.:

       () Users/javier/.rvm/gems/ruby-2.1.2/bundler/gems/stripe-ruby-9c7ebd21c973/lib/stripe.rb:71:in `request'

       () Users/javier/.rvm/gems/ruby-2.1.2/bundler/gems/stripe-ruby-9c7ebd21c973/lib/stripe/api_operations/create.rb:6:in `create'

       () Users/javier/Desktop/definitive/app/controllers/charges_controller.rb:10:in `create'

Tested including the keys in secrets.yml as @sealocal suggests in comments, but still the same issue:

development:
 secret_key_base: key
 publishable_key: anotherkey
 secret_key: anotherkey

test:
 secret_key_base:key
production:
 secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
 publishable_key: <%= ENV["publishable_key"] %>
 secret_key: <%= ENV["secret_key"] %>
like image 496
malditojavi Avatar asked Dec 12 '14 21:12

malditojavi


People also ask

How do I find my API key?

The new API key is listed on the Credentials page under API keys. (Remember to restrict the API key before using it in production.)


1 Answers

You need to store your Stripe keys in environment variables so that config/initializers/stripe.rb can read them.

In Rails 4.1+ you can use secrets.yml:

development:
  secret_key_base: key
  publishable_key: pk_test_lkasjdflkajsd
  secret_key: sk_test_lkasjdflkajsd

NOTE: Use exactly two spaces when defining nested key-value pairs in YAML. That is, the keys under development should be indented by two spaces, and not with a tab character. This is because YAML files strictly depend on indentation.

In config/initializers/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]
like image 60
sealocal Avatar answered Oct 06 '22 10:10

sealocal