Home > database >  Rails 5.1 secrets and Stripe using wrong key
Rails 5.1 secrets and Stripe using wrong key

Time:07-25

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.

CodePudding user response:

There are a few debug steps:

  1. Go to your Dashboard request log: Live mode log or Test mode log and find the exact request that is failing
  2. Confirm that it is using a Test mode key
  3. 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.

  • Related