Stripe API key missing Rails - ruby-on-rails

We're facing a problem with Stripe API keys. What I've done so far is:
set environment/*.rb to utilise appropriate Stripe keys using constants
created a config/initializers/stripe.rb with the line Stripe.api_key = STRIPE_SECRET
using the rails console, both STRIPE_PUBLIC and STRIPE_SECRET constants are set and visible. STRIPE_PUBLIC => "pk_test_xxxxxxxxx"
However, once these things are in place, making a call to Stripe's API using the browser results in:
Stripe::AuthenticationError in some_controller#some_action
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 support#stripe.com if you have any questions.
Using the web-console gem, we can tell that STRIPE_SECRET and STRIPE_PUBLIC are nil in the website. But every time we run rails console we get our keys from our constants.

I fixed my issue. The terminal running rails server had been opened since before the environment variables had been set.
Therefore, running source ~/.bashrc (for me) or using a new terminal worked for me.
Thank you, sincerely, to everyone who tried to help!

I think the issue you are encountering is happening because if the way you are setting the key.
You cannot expect something you set using the rails console to be available to the web application. The Rails console started up as its own unique process that does not share anything with the web process started and accessed by the browser.
If you want the key to be available to the web application try setting it as an environment variable and reading it accordingly in your stripe.rb file

Try restarting the rails server to make these values available to the rails app since the changes were made in the initializers directory.

Related

Rails 5.1 secrets and Stripe using wrong key

I am using
rails 5.1.2
gem 'stripe-rails
and when I deploy to Heroku I am double checking that the Heroku environment is using the correct Stripe Keys for production by doing the following:
heroku run rails console --remote heroku
This opens a ruby console on heroku for me: irb(main):009:0> where I then check what Stripe Keys it has by doing the following:
Rails.application.secrets.STRIPE_SECRET_KEY => sk_Live_***************
Rails.application.secrets.STRIPE_PUBLISHABLE_KEY => pk_Live_**************
I'm assuming that the production environment has the right keys set because of the prefixes sk_Live and pk_Live but when I try run the following:
Stripe::BillingPortal::Session.create(customer: 'cus_**LiveID*****',return_url: "http://someurl.com") it fails and says that a similar object exists in live mode, but a test mode key was used to make this request. The weird thing is that all my other Stripe actions like creating subscriptions are working as expected in production so I am entirely at a loss why this one action creating a BillingPortal is always using a test key and failing. Obviously it works when I test it locally because that is a test environment and stripe doesn't error. This error exists only on production.
There are a few debug steps:
Go to your Dashboard request log: Live mode log or Test mode
log and find the exact request that is failing
Confirm that it is using a Test mode key
On the console before calling the problematic Billing Portal, double check if the Stripe instance is using correct key by
p Stripe.api_key
Stripe::BillingPortal::Session.create(customer:'cus_**LiveID*****',return_url: "http://someurl.com")
Despite your Rails application could have correct secrets, it looks like your Stripe instance might still use the test key value from somewhere else.

ActiveAdmin taking wrong http method for update and destroy actions

Rails version - 5.2
Active admin version - 2.9.0
I have installed and configured active admin in my rails API application. Everything is working fine, except for the update, delete action of any controller, and logout of the admin user.
Here is my applicaiton.rb file
I have added method override in application.rb file though it is taking the POST request method for any update or delete request. It is working fine in my local even though it is taking POST request but when I deployed the code on the staging environment. I have found this thing. On my staging environment, that route is not present hence it is giving 404 error.
Below is the screenshot of the Update admin user request.
Can someone please help me to fix this issue?
I have finally fixed the issue. I am assuming the issue might be with my staging web server configuration otherwise it was working fine in my local in both the environments local and staging.
Post the answer here so it might help people in future.
By default the browser only supports for GET and POST requests. If we want to use any other request methods then we need to pass that request method in the parameter _method. You can read more about it here.
That wasn't happening in my case though i have added config.middleware.use Rack::MethodOverride in application.rb.
For resolving the issue, I have added the use Rack::MethodOverride in my config.ru file. It means before running the rails application it will use this method. I have added this code and that's it everything is working fine now.

Internal server error response during rails omniauth docusign callback

I am connecting to DocuSign via oauth with Rails 6 and Omniauth - it seems I have it configured correctly since visiting /auth/docusign takes me to the DocuSign login screen. After logging in, it takes me to the callback. The callback is failing because the omniauth-docusign gem is failing with the user_info hash. Where it should be getting user_info['accounts'] it is getting this:
user_info #=> {"error"=>"internal_server_error", "reference_id"=>"a0f0e8c9-2faa-4b70-90b8-875ae40f13cf"}
My API Dashboard for the App shows no log or request info. 0 total requests. Is there a log area for oauth attempts, or where to use this reference ID?
Can someone at DocuSign help with this? Your developer support page seems to say to ask here on S/O.
It appears that you didn't configure everything correctly for your Integration Key (IK which is like clientId) as well as clientSecret and the rest of the configuration that you can do for your IK.
I would suggest that you can get a pre-built Ruby application that already had this all done for you and save you some work by going to the Quickstart. This would enable you to go get a ZIP file pre-configurated with all the required gems and everything needed for auth already set based on your own developer account.
Update: if you then want to take this code into your own app, you need to also get the authentication code supplied lib/docusign.rb instead of a gem like omniauth-docusign.
As Inbar Gazit said, it's important to use the Quickstart app, which I was doing. However, not only do I already have my own existing rails app, even if I didn't, I wouldn't want to have to clean out all the extra stuff in the quickstart app that I don't need. It would be nice to have a minimal script for installing just the bare minimum into an existing rails app to get authenticated and start using the API.
In the end, the solution for me was to notice and copy the lib/docusign.rb file from the Quickstart app, instead of using an omniauth-docusign gem. The base omniauth gem had advised me to find and install that gem because there was no strategy defined for docusign. The quickstart lib file is what I needed.

Initializing object within devolopment.rb envirorment using gem within rails mountable engine

Completely lost.
I'm following this sample app tutorial https://www.wepay.com/developer/resources/wefarm-tutorial
which seems like a simple tutorial to follow except I'm building it inside a rails engine. I'm currently attempting to follow the tutorial and initialize the new object.
initialize a new WePay object. add these variables to config/development.rb:
wefarm / config / environments / development.rb
App specific information
CLIENT_ID = 32636
CLIENT_SECRET = "180c800c62"
USE_STAGE = true
WEPAY = WePay.new(CLIENT_ID, CLIENT_SECRET, USE_STAGE)
The issue I think I'm having is the gem is within my core engine allong with user's and the rest of the application and I'm adding these lines of code into the empty shell app. How would I make sure my engine uses this in development I'm also assuming I will come across this issue again when I'm set for production.
In another question a user specified I'm putting this code into the wrong area if I'm using an engine it should be in the initializer folder, but within the documents they just specify putting the code within the config/environments file so where/how exactly do I translate this over to an engine. If it goes into the initializer folder how would I make that file to just include the code specified?
Any help would be amazingly helpful.
Ps. The client Id and secret our just test information
In Rails 4 engine, define the app specific information in secrets.yml
Specifying secrete keys and ids according to environment will solve environment related configuration for keys.
Example :
development:
CLIENT_ID: 123
Make engine configurable
Example in config/initializer/wefarm.rb
Weform.CLIENT_ID = Rails.application.secrets.CLIENT_ID
In lib/engine.rb (engine)
mattr_accessor :CLIENT_ID
Now you can access client_id in engine as WeForm.CLIENT_ID
For more information Rails Engine

Some environmental variables accessible, others not?

I have a rails app and am trying to use an environmental variable (API key) inside of a controller and it is failing. Debugging shows it's value to be nil. Weird thing is that other keys from that file are accessible, so I am not really understanding why. They are all in my secrets.yml file.
I have tried accessing it using both ENV["STRIPE_TEST_SECRET_KEY"] and Rails.application.secrets.stripe_test_secret_key and both come back nil. I get the error:
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 support#stripe.com if you have any questions.
using the better errors gem. How do I make these available throughout my app?
You need to set STRIPE_TEST_SECRET_KEY in the environment running the Rails app.
Check out the Choices gem, it makes overriding env vars pretty easy.
You can set the env vars on the CLI in development like this:
$ STRIPE_TEST_SECRET_KEY=abc123 rails s
If you are using Rbenv, you can create a .rbenv-vars file in the root of your project that contains one env var per line like this:
DATABASE_URL=mysql://db_user:db_pass#localhost:3306/dev_db
STRIPE_TEST_SECRET_KEY=abc123
SOME_OTHER_VAR=foo

Resources