A Rails Engine for integrating with Stripe
This gem can help your rails application integrate with Stripe in the following ways
Professionally supported stripe-rails is coming soon
Setup your payment configuration
Add this line to your application's Gemfile:
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:
or, you can render it as a partial:
'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.
config.stripe.debug_js = true # use stripe-debug.js
config.stripe.debug_js = false # use stripe.jsBy default the helper renders the v3 version of stripe.js. You can provide an
alternate version to the helper to generate the appropriate tag:
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:
export STRIPE_SECRET_KEY=sk_test_xxyyzzor if you are on heroku:
heroku config:add STRIPE_SECRET_KEY=sk_test_xxyyzzYou can also set this value from inside ruby configuration code:
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:
rake stripe:verifyIf 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:
# config/application.rb
# ...
config.stripe.publishable_key = 'pk_test_XXXYYYZZZ'And then override it to use your live key in production only
# 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:
export STRIPE_PUBLISHABLE_KEY=pk_test_XXXYYYZZZIf 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:
# config/environments/production.rb
# ...
config.stripe.ignore_missing_secret_key = trueIf you need to test a new API version in development, you can override the version number manually.
# config/environments/development.rb
# ...
config.stripe.api_version = '2015-10-16'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:
rails generate stripe:installthis will generate the configuration files containing your plan and coupon definitions:
create config/stripe/products.rb
create config/stripe/plans.rb
create config/stripe/prices.rb
create config/stripe/coupons.rbUse 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.
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:
Stripe.plan "Silver-Plan".to_sym do |plan|
plan.constant_name = 'SILVER_PLAN' # will be defined
# Will map to plan :id => "Silver-Plan" on StripeNote - 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:
Stripe.coupon :super_elite_free_vip do |coupon|
coupon.duration = 'forever'
coupon.percent_off = 100
coupon.max_redemptions = 5
endAs are Products:
Stripe.product :primo do |product|
product.name = 'PRIMO as a service'
product.type = 'service'
product.statement_descriptor = 'PRIMO'
endAnd Prices:
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'
endTo upload your plans, products, prices and coupons onto stripe.com, run:
rake stripe:prepareThis 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::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:
Additionally, you can pass a block containing custom form elements to stripe_elements_tag:
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:
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:
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
# config/locales/en.yml
en:
stripe_rails:
elements:
label_text: Your label text
submit_button_text: Your button textStripe::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.
config.stripe.endpoint = '/payment/stripe-integration'Your new webhook URL would then be http://myproductionapp/payment/stripe-integration/events
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.
# 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:
export STRIPE_SIGNING_SECRET=whsec_XXXYYYZZZ
export STRIPE_CONNECT_SIGNING_SECRET=whsec_AAABBBCCCconfig.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.
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:
# config/application.rb
# ...
config.stripe.signing_secrets = ['whsec_XXXYYYZZZ']And you'll need to restart your rails server with:
rails restartNow you're ready to click Send test webhook, and trigger whichever events you'd like to test from Stripe itself.
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
No open issues yet, or sync has not completed.