Stripe Integration with Rails: Pay Gem Tips and Fixes
12 Mar 2024After building my app on Rails, I integrated Stripe using the Pay gem to make the process simpler. I documented the roadblocks I faced, hoping it helps someone.
Turbo Breaks “redirect_to” Stripe Checkout URL
Turbo attempts to pre-fetch the checkout page, which breaks the redirect. Disable Turbo by passing data-turbo=false
to the link_to
helper. StackOverflow’s question#70733756 provides more insight into the issue.
Default URL Options
I didn’t find a reference to this in the docs. And if not configured the integration will error our with a reasonable error message. But, if you’re new to rails, it may not be so obvious. The integration requires that Rails.application.routes.default_url_options
is set and available. I choose to set the value in both config/environments/development.rb
and config/environments/production.rb
, and used include Rails.application.routes.url_helpers
in the class where I generate the checkout url.
Set the Stripe.api_key in stripe.rb
When using the Pay gem, the recommended way to configure Stripe client is by setting up config/initializers/stripe.rb
. But for the stripe gem to pick up the private key, it is also necessary to set Stripe.api_key
in the initializer.
Disable Telemetry
By default, the Stripe Ruby gem sends telemetry to Stripe. Disable telemetry if you’re in a resource-starved environment. While configuring Stripe, also set Stripe.api_version—it’s important to be aware of the API version you’re using. Here’s the stripe initializer I used in my app.
Stripe Webhooks
When you are configuring a webhook endpoint in Stripe, you will be asked to select the events for which you want to receive webhooks. While you can choose to subscribe to all events, if you have limited resources, it is best to only subscribe to the events that you actually need. A list of the events that the Pay gem expects is available in this gist.