Reputation: 51
I'm trying out this Stripe quick checkout for my webshop, form appears fine but when redirected and run the charges#create I get an error saying No API key provided. Set your API key using "Stripe.api_key = ". as far as I've learned I have set it up in the stripe.rb
Rails.configuration.stripe = {
:publishable_key => Rails.application.secrets.stripe_publishable_key,
:secret_key => Rails.application.secrets.stripe_secret_key
}
Stripe.api_key = Rails.application.secrets.stripe_secret_key
The error comes in charges_controller.rb
class ChargesController < ApplicationController
require 'stripe'
def index
render :new
end
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
end
Here is also how I setup my secrets.yml
shared:
stripe_api_key: sk_test_####################
stripe_publishable_key: pk_test_###############
secret_key_base: ##############################
Here is a gif showing what happens when I click pay in the stripe payment session: imgur gif
Upvotes: 4
Views: 1154
Reputation: 7777
Try to the following in stripe.rb
Rails.configuration.stripe = {
:publishable_key => ENV['stripe_publishable_key'],
:secret_key => ENV['stripe_api_key']
}
Stripe.api_key = Rails.configuration.stripe[:secret_key]
Additional Which I Have Used
Look you need to set stripe STRIPE_PUBLISHABLE_KEY
& STRIPE_SECRET_KEY
for setting these keys securely you can use figaro gem
after installing Figaro gem then a file will create inside config
directory which name application.yml
you can set your keys like this
STRIPE_PUBLISHABLE_KEY: pk_xxxxxxxxxxxxxxxxxxxx
STRIPE_SECRET_KEY: sk_xxxxxxxxxxxxxxxxxxxxxxx
Then update your stripe.rb
inside config/initializers/
Rails.configuration.stripe = {
:publishable_key => ENV['STRIPE_PUBLISHABLE_KEY'],
:secret_key => ENV['STRIPE_SECRET_KEY']
}
Stripe.api_key = Rails.configuration.stripe[:secret_key]
That's it, you have done this.
Upvotes: 2
Reputation: 6952
Why don't you just put the API key in an initializer? Better yet, store it as an environment variable and reference that. Create config/initializers/stripe.rb
with the following content
Stripe.api_key = ENV['STRIPE_SECRET']
STRIPE_PUBLIC_KEY = ENV['STRIPE_PUBLIC_KEY']
Upvotes: 0