Sandhiya Thangaraj

Joined

50 Experience
0 Lessons Completed
0 Questions Solved

Activity

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?