Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
S

stripe-rails

> 后端框架
Open source

A Rails Engine for integrating with Stripe

760 stars0 likes2 views
WebsiteGitHub

About

A Rails Engine for integrating with Stripe

Stripe::Rails: A Rails Engine for use with stripe.com

This gem can help your rails application integrate with Stripe in the following ways

  • manage stripe configurations in a single place.
  • makes stripe.js available from the asset pipeline.
  • manage product, prices, plans and coupons from within your app.
  • painlessly receive and validate webhooks from stripe.

Professionally supported stripe-rails is coming soon


Installation

  • Setup your API keys
  • Manually set your API version (optional)

Setup your payment configuration

  • Configuring your plans and coupons

Stripe Elements

  • Custom Elements

Webhooks

  • Signed Webhooks
    • Testing Signed Webhooks Locally
  • Disabling auto mount
  • Responding to webhooks
  • Critical and non-critical hooks
  • Filtering Callbacks
  • Catchall Callback

Unit testing

Thanks

Code of Conduct

Installation

Add this line to your application's Gemfile:

ruby
gem 'stripe-rails'

If you are going to be using [stripe.js][1] to securely collect credit card information on the client, then you will need to add the stripe javascript tags into your template. stripe-rails provides a helper to make this easy:

erb

or, you can render it as a partial:

erb
 'stripe/js' %>

In both cases, stripe-rails will choose a version of stripe.js appropriate for your development environment and automatically configure it to use your publishable API key. By default it uses stripe-debug.js for your development environment and stripe.js for everything else, but you can manually configure it per environment.

ruby
config.stripe.debug_js = true  # use stripe-debug.js
config.stripe.debug_js = false # use stripe.js

By default the helper renders the v3 version of stripe.js. You can provide an alternate version to the helper to generate the appropriate tag:

erb

Setup your API keys.

You will need to configure your application to authenticate with stripe.com using [your api key][1]. There are two methods to do this, you can either set the environment variable STRIPE_SECRET_KEY:

bash
export STRIPE_SECRET_KEY=sk_test_xxyyzz

or if you are on heroku:

bash
heroku config:add STRIPE_SECRET_KEY=sk_test_xxyyzz

You can also set this value from inside ruby configuration code:

ruby
config.stripe.secret_key = "sk_test_xxyyzz"

In either case, it is recommended that you not check in this value into source control.

You can verify that your api is set up and functioning properly by running the following command:

bash
rake stripe:verify

If you are going to be using stripe.js, then you will also need to set the value of your publishable key. A nice way to do it is to set your test publishable for all environments:

ruby
# config/application.rb
# ...
config.stripe.publishable_key = 'pk_test_XXXYYYZZZ'

And then override it to use your live key in production only

ruby
# config/environments/production.rb
# ...
config.stripe.publishable_key = 'pk_live_XXXYYYZZZ'

This key will be publicly visible on the internet, so it is ok to put in your source. If you prefer to environment variables, you can also set STRIPE_PUBLISHABLE_KEY:

bash
export STRIPE_PUBLISHABLE_KEY=pk_test_XXXYYYZZZ

If no API key is provided, stripe-rails will show a warning: "No stripe.com API key was configured ...". You can silence this warning by setting the ignore_missing_secret_key option to true:

ruby
# config/environments/production.rb
# ...
config.stripe.ignore_missing_secret_key = true

Manually set your API version (optional)

If you need to test a new API version in development, you can override the version number manually.

ruby
# config/environments/development.rb
# ...
config.stripe.api_version = '2015-10-16'

Setup your payment configuration

If you're using subscriptions, then you'll need to set up your application's payment plans and discounts. Stripe::Rails lets you automate the management of these definitions from within the application itself. To get started:

bash
rails generate stripe:install

this will generate the configuration files containing your plan and coupon definitions:

bash
create  config/stripe/products.rb
create  config/stripe/plans.rb
create  config/stripe/prices.rb
create  config/stripe/coupons.rb

Configuring your plans and coupons

Use the plan builder to define as many plans as you want in config/stripe/plans.rb

…

This will define constants for these plans in the Stripe::Plans module so that you can refer to them by reference as opposed to an id string.

ruby
Stripe::Plans::SILVER # => 'silver: ACME Silver'
Stripe::Plans::GOLD # => 'gold: ACME Gold'

If you have to support an existing plan with a Stripe plan id that can not be used as a Ruby constant, provide the plan id as a symbol when defining the plan, but provide the name for the constant to define with constant_name:

ruby
Stripe.plan "Silver-Plan".to_sym do |plan|
  plan.constant_name = 'SILVER_PLAN' #  will be defined
# Will map to plan :id => "Silver-Plan" on Stripe

Note - If you're planning on running rake stripe:prepare to create your subscription plans, Stripe will restrict plan ids to match this regexp (/\A[a-zA-Z0-9_\-]+\z/) when created via API but still allows creation of plan ids that don't follow this restriction when manually created on stripe.com.

Coupons are created in much the same way:

ruby
Stripe.coupon :super_elite_free_vip do |coupon|
  coupon.duration = 'forever'
  coupon.percent_off = 100
  coupon.max_redemptions = 5
end

As are Products:

ruby
Stripe.product :primo do |product|
  product.name = 'PRIMO as a service'
  product.type = 'service'
  product.statement_descriptor = 'PRIMO'
end

And Prices:

ruby
Stripe.price :bronze do |price|
  # Use an existing product id to prevent a new product from
  # getting created
  price.product_id = Stripe::Products::PRIMO.id
  price.billing_scheme = 'tiered'
  price.recurring = {
    interval: 'month',
    usage_type: 'metered'
  }

  # Use graduated pricing tiers
  # ref: https://stripe.com/docs/api/prices/object#price_object-tiers
  price.tiers = [
    {
      unit_amount: 1500,
      up_to: 10
    },
    {
      unit_amount: 1000,
      up_to: 'inf'
    }
  ]
  price.tiers_mode = 'graduated'
end

To upload your plans, products, prices and coupons onto stripe.com, run:

bash
rake stripe:prepare

This will create any plans, products, prices and coupons that do not currently exist, and treat as a NOOP any objects that already exist, so you can run this command safely as many times as you wish. Now you can use any of these objects in your application.

NOTE: You must destroy plans and prices manually from your stripe dashboard.

Stripe Elements

Stripe::Rails allows you to easily include Stripe Elements in your application.

Stripe Elements are rich, pre-built UI components that help you create your own pixel-perfect checkout flows across desktop and mobile.

Simply include the stripe_elements_tag anywhere below the stripe_javascript_tag and pass it the path to the controller action which will handle the Stripe token once the form is submitted:

erb

Additionally, you can pass a block containing custom form elements to stripe_elements_tag:

Custom Elements

Stripe::Rails allows you to easily include your own custom form elements within the Stripe form by including those form elements in a block passed to stripe_elements_tag:

erb

  
  

Configuration options

Stripe::Rails comes bundled with default CSS and Javascript for Stripe elements, making it easy to drop in to your app. You can also specify your own assets paths:

erb

If you decide to use your own CSS and Javascript for Stripe Elements, please refer to the Stripe elements docs.

To change the form text you can add the following keys to your locale files

yaml
# config/locales/en.yml
en:
  stripe_rails:
    elements:
      label_text: Your label text
      submit_button_text: Your button text

Webhooks

Stripe::Rails automatically sets up your application to receive webhooks from stripe.com whenever a payment event is generated. To enable this, you will need to configure your [stripe webhooks][3] to point back to your application. By default, the webhook controller is mounted at '/stripe/events' so you would want to enter in http://myproductionapp.com/stripe/events as your url for live mode, and http://mystagingapp.com/stripe/events for your test mode.

If you want to mount the stripe engine somewhere else, you can do so by setting the stripe.endpoint parameter. E.g.

ruby
config.stripe.endpoint = '/payment/stripe-integration'

Your new webhook URL would then be http://myproductionapp/payment/stripe-integration/events

Signed Webhooks

Validation of your webhook's signature uses your webhook endpoint signing secret. Before you can verify signatures, you need to retrieve your endpoint’s secret from your Stripe Dashboard. Select an endpoint for which you want to obtain the secret, then select the Click to reveal button.

ruby
# config/application.rb
# ...
config.stripe.signing_secrets = ['whsec_XXXYYYZZZ']

Each secret is unique to the endpoint to which it corresponds. If you use multiple endpoint, you must obtain a secret for each one. After this setup, Stripe starts to sign each webhook it sends to the endpoint. Because of this, we recommend setting this variable with environment variables:

bash
export STRIPE_SIGNING_SECRET=whsec_XXXYYYZZZ
export STRIPE_CONNECT_SIGNING_SECRET=whsec_AAABBBCCC
ruby
config.stripe.signing_secrets = [ENV.fetch('STRIPE_SIGNING_SECRET'), ENV.fetch('STRIPE_CONNECT_SIGNING_SECRET')]

The first secret that successfully matches for each incoming webhook will be used to verify the incoming events.

Testing Signed Webhooks Locally

In order to test signed webhooks, you'll need to trigger test webhooks from your Stripe dashboard, and configure your local environment to receive remote network requests. To do so, we recommend using ngrok to configure a secure tunnel to localhost.

Once configured and running, ngrok will give you a unique URL which can be used to set up a webhook endpoint. Webhook endpoints are configured in your Dashboard's Webhook settings section. Make sure you are in Test mode and click Add endpoint, and provide your ngrok URL along with the stripe.endpoint suffix.

An example webhook URL would then be https://bf2a5d21.ngrok.io/stripe/events.

Once your endpoint is configured, you can reveal the Signing secret. This will need to be set as documented above:

ruby
# config/application.rb
# ...
config.stripe.signing_secrets = ['whsec_XXXYYYZZZ']

And you'll need to restart your rails server with:

bash
rails restart

Now you're ready to click Send test webhook, and trigger whichever events you'd like to test from Stripe itself.

Disabling auto mount

Sometimes, you don't want the stripe engine to be auto-mounted so that you control exactly what priority it will take in your routing table. This is especially important if you have a catch-all r

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Rubyhacktoberfestrails-enginerubystripe

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category后端框架
PricingOpen source

> Related tools

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架