Skip to content
RailsDev.io

Stripe Subscriptions in Rails with Pay: The Setup That Survives Production

Rails Stripe subscriptions with the Pay gem: checkout, webhooks, plan changes, cancellations and dunning — a production-shaped setup, tested against Pay 12.4.0.

Diagram of Stripe subscriptions in Rails: Customer Portal and Checkout talk to Stripe, webhooks update pay_subscriptions in PostgreSQL via Sidekiq
On this page
  1. The model: Pay’s tables, not your own
  2. Checkout and the Customer Portal
  3. Webhooks are the real backend
  4. Authorization from subscription state
  5. The failure modes tutorials skip
  6. Receipts and compliance
  7. Where this fits your build
  8. Conclusion
  9. Frequently asked questions

Stripe subscriptions in Rails are one of the best-supported paths in the ecosystem — mostly thanks to Pay, the open-source gem that wraps Stripe (and Paddle/Braintree) in a Rails-native model layer. As of November 2026, Pay is at 12.4.0 (MIT, maintained by Jason Charnes, Chris Oliver and Collin Jilbert) and works with Stripe Checkout and the Customer Portal, which removes most of the UI you would otherwise build.

This is the setup I use on SaaS MVPs, including the parts generic tutorials skip: webhooks, plan changes, cancellations and failed payments.

The model: Pay’s tables, not your own

Pay keeps its own tables (pay_customers, pay_subscriptions) linked to your User/Account model, so Stripe stays the source of truth for billing state while your app reads a local cache of it.

bash
bundle add pay
bin/rails pay:install:migrations
bin/rails db:migrate
ruby
# app/models/user.rb
class User < ApplicationRecord
  pay_customer
end

Every subscription-aware query in your app goes through user.payment_processor.subscriptions. Never mirror plan state in your own columns — drift is inevitable.

Checkout and the Customer Portal

The lowest-maintenance path is Stripe-hosted billing UI:

ruby
# app/controllers/checkouts_controller.rb
def create
  session = current_user.payment_processor.checkout(
    mode: :subscription,
    line_items: [{ price: params[:price_id], quantity: 1 }],
    success_url: url_for(root_url) + "?paid=1",
    cancel_url: url_for(root_url),
  )
  redirect_to session.url, allow_other_host: true
end

Customers manage cards, upgrades, downgrades and cancellations in the Stripe-hosted Customer Portal via user.payment_processor.billing_portal_url. You configure products and prices in the Stripe dashboard; the app references price IDs from environment/credentials.

Webhooks are the real backend

Stripe only truly moves through webhooks. With Pay, mount the receiver and let it update its tables:

ruby
# config/routes.rb
mount Pay::Engine, at: "/pay", as: :pay_engine

Register https://your-domain/pay/webhooks/stripe in the Stripe dashboard (and in the Stripe CLI for local development):

bash
stripe listen --forward-to localhost:3000/pay/webhooks/stripe

Pay handles the common events (subscription created/updated/deleted, invoice paid/failed). Anything custom — provisioning side effects, dunning steps — goes in your own listener, inheriting from Pay’s webhook handlers or a plain ActiveJob:

ruby
# app/jobs/invoice_payment_failed_job.rb
class InvoicePaymentFailedJob < ApplicationJob
  def perform(pay_customer_id)
    customer = Pay::Customer.find(pay_customer_id)
    # email the account owner, soften to read-only access after N days
  end
end

Authorization from subscription state

Gate premium features on the synced status, never on the fact of a checkout session:

ruby
# app/models/user.rb
def premium?
  payment_processor.subscriptions.where(status: %w[active trialing]).exists?
end

Feature flags beat booleans: one premium? predicate reused in controllers and views, so a failed payment in Stripe flips access on the next deploy without code changes.

The failure modes tutorials skip

  • trial_end is not a hard cut-off: trials lapse at a timestamp; job ordering and subscription.updated events both matter. Grant trial access from trialing status, not from a flag you set at signup.
  • Plan changes mid-cycle: proration is Stripe’s default; decide explicitly whether upgrades are immediate (bill_immediately) or next cycle.
  • Cancellation timing: cancel_at_period_end = true keeps access until the period closes — make sure your UI says so, or customers think they’ve been charged twice.
  • Dunning: invoice.payment_failed should trigger a grace period (e.g. 7 days read-only), then downgrade. Without it, customers who forgot to update a card churn silently.
  • Test clocks: use Stripe’s test clocks to fast-forward invoices in test mode. Manual webhook poking is how bugs ship.
  • Idempotency: every webhook handler and billing job must be safe to re-run; Stripe retries on non-2xx.

For cost context on the plan you’re supporting, my post on how much a SaaS MVP costs covers the Stripe line item among the others.

Receipts and compliance

Stripe emails receipts and invoices on its own; keep your transactional email for product flows (proposal accepted, onboarding, receipt summary pages). Put a tax solution (Stripe Tax) on checklist if you sell internationally, and never store card data — Checkout and the Portal mean you never touch PANs.

Where this fits your build

Subscription billing is one of the features that defines “MVP complete” for a SaaS: sign-up, teams, Stripe, an admin area. If you’re scoping that build, send me a short brief — I ship exactly this as part of my SaaS MVP development service, with Pay wired, webhooks tested, and the dunning flow included.

Conclusion

With Pay and Stripe Checkout, subscriptions stop being a build project and become a configuration of record: webhooks in, predicates out, dunning handled on day one. If you’d like the full billing surface — checkout, portal, webhooks, dunning and tests — shipped as part of your MVP, send me a short brief and book a discovery call.

Frequently asked questions

Pay gem vs rolling my own Stripe integration — which is better?

Pay is maintained, battle-tested and keeps your schema aligned with upstream; rolling your own means owning every webhook edge case forever. Use Pay unless you need a very unusual billing model.

Does Pay work with Stripe Customer Portal?

Yes — payment_processor.billing_portal_url redirects to it. You can still hand-roll pricing pages if you prefer the marketing control.

How do I test webhooks locally?

stripe listen --forward-to localhost:3000/pay/webhooks/stripe and stripe trigger invoice.payment_failed. Also consider request-spec coverage against the Pay controllers.

How do trials work with Pay?

Stripe manages trial timestamps; Pay syncs them. Check the subscription’s status and trial_ends_at in your authorization predicate rather than storing your own trial flag.

Can Pay handle metered/usage-based billing?

Stripe supports metered billing; Pay maps subscriptions but usage reporting is usually custom — record usage yourself and report metered events to Stripe.

What about VAT and taxes?

Enable Stripe Tax (per-country registration is on you) and let Stripe calculate, collect and remit. Keep invoice records for your accountant from day one.

Keep reading

Seven steps to build a SaaS MVP on Rails 8, each with its command: new app with Tailwind, sign-in and sign-up with the authentication generator, core workflow with a scaffold, scoping to the account, billing with pay_customer, testing with bin/ci, and deploying with bin/kamal setup
9 min read

Build Your SaaS MVP Faster with Ruby on Rails

Build a Ruby on Rails MVP faster: a tested Rails 8.1 setup with sign-up, accounts, a scoped core workflow, tests and CI, plus what to leave out of version one.