Quickstart Accounts Authentication Teams Billing Subscriptions DevExp Scaffolding Admin Blog Deployment Kamal

Stripe subscriptions

Here’s how to understand and configure Stripe subscriptions in Business Class.

Stripe

To set up Stripe, create an account for the Stripe Dashboard. Then head to Developers -> API keys to create your API keys.

To update your credentials:

$ VISUAL="code --wait" bin/rails credentials:edit --environment=development
$ VISUAL="code --wait" bin/rails credentials:edit --environment=production

Here’s what you’ll need to provide:

stripe:
  public_key: pk_test_510..
  private_key: sk_test_510..
  signing_secret: whsec_8383..

The signing_secret is related to incoming webhooks. The test environment doesn’t need any credentials set, only development and production need that.

Alternatively to Rails credentials, you can also set credentials using the environment variables:

ENV["STRIPE_PUBLIC_KEY"] = ""
ENV["STRIPE_PRIVATE_KEY"] = ""
ENV["STRIPE_SIGNING_SECRET"] = ""

You can find test cards for checkout here.

Plans

Head over to Create product -> basic -> recurring to add new subscription plans in your Stripe dashboard and save the price IDs.

Add your Stripe plans in the stripe.rb initializer:

# Fill this in with your own plans. Only the price ID (`id`) and trial period
# (`trial_period`) are used in subscription management, the rest is for UI/UX.
# Stripe requires minimum of 1 day trial.
STRIPE_PLANS = [
  # {
  #   id: "price_1OKYbOIwGbapH49i3RpyY6la",
  #   name: "My first plan",
  #   trial_period: "14",
  #   price: "$10/mo"
  # },
  # {
  #   id: "price_1OKYbOIwGbapH49i3RpyY6la",
  #   name: "My second plan",
  #   trial_period: "14",
  #   price: "$19/mo"
  # }
]

Apart from the price ID, fill in whatever you want to show up in the UI. Prices are not fetched automatically to avoid dealing with slow requests or caches.

Webhooks

To set up webhooks, go to Developers -> Webhooks in the Stripe dashboard and add the following destination:

https://example.org/pay/webhooks/stripe

To set up webhooks in development, you can use Stripe CLI either natively or with Docker:

$ brew install stripe/stripe-cli/stripe
$ stripe listen --forward-to localhost:3000/pay/webhooks/stripe

You’ll get signing_secret for development.

If you use the Docker version, you’ll need to run commands like this:

$ docker run --rm -it stripe/stripe-cli:latest "login"
$ docker run --rm -it stripe/stripe-cli:latest "listen --forward-to localhost:3000/pay/webhooks/stripe"

Sync

Pay webhooks should ensure that subscriptions are always synced. Nevertheless, you can always run the following Rake task to synchronize subscriptions’s state:

$ bin/rails stripe:sync