Ask A Question

Notifications

You’re not receiving notifications from this thread.

Switching Between Live and Test Modes in Stripe Connect with Pay Gem

Sandhiya Thangaraj asked in Rails

We have set up Stripe Connect in our Rails application to allow customers to sell their products. Currently, we support purchasing products from connected accounts only in live mode. However, we received a request to enable purchasing products in test mode as well for testing purpose.

We are using the pay gem for payment processing. By default, the pay gem automatically fetches Stripe credentials (public and secret keys) from the environment using the following pattern:
credentials&.dig(env, scope, name)

In our setup, it fetches the keys using Rails.application.credentials.production.stripe.public_key and Rails.application.credentials.production.stripe.secret_key. However, even when we specify test credentials, the env value resolves to Rails.env.to_sym, which is :production in our production environment. As a result, the gem always fetches the live credentials, ignoring the test credentials.

We discovered that overriding the env method in the pay gem in a initializer allows us to use the test credentials, but this approach is global. It doesn’t allow switching between live and test modes based on the user’s preference during runtime.

Challenges:

  1. The pay gem seems to lack built-in support for dynamically switching between test and live environments.
  2. Overriding the env method in the initializer file applies globally, making it impossible to serve both live and test environments simultaneously based on user selection.

Question:
Is there a way to configure the pay gem (version 2.2.2) to support both live and test modes in production? Specifically, can we dynamically switch between live and test credentials based on the user's preference during runtime without relying solely on the initializer file?

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 87,400+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.