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

koudoku

> 编程语言
Open source

Robust subscription support for Rails with Stripe.

1.2K stars0 likes2 views
WebsiteGitHub

About

Robust subscription support for Rails with Stripe.

Koudoku

Robust subscription support for Ruby on Rails apps using Stripe, including out-of-the-box pricing pages, payment pages, and subscription management for your customers. Also makes it easy to manage logic related to new subscriptions, upgrades, downgrades, cancellations, payment failures, and streamlines hooking up notifications, metrics logging, etc.

To see an example of Koudoku in action, please visit Koudoku.org.

Koudoku is authored and maintained by Andrew Culver and Christoph Engelhardt. If you find it useful, consider checking out Churn Buster. It's designed to help you handle the pain points you'll run into when doing payments at scale.

Installation

Include the following in your Gemfile:

ruby
    gem 'koudoku'

After running bundle install, you can run a Rails generator to do the rest. Before installing, the model you'd like to have own subscriptions must already exist. (In many cases this will be user. It may also be something like company, etc.)

ruby
    rails g koudoku:install user
    rake db:migrate

Add the following to app/views/layouts/application.html.erb before your `` tag closes:

ruby
    

(This allows us to inject a Stripe `` tag in the correct place. If you don't, the payment form will not work.)

After installing, you'll need to add some subscription plans. (You can see an explanation of each of the attributes in the table below.)

Note: You need to create the plans in your Stripe Dashboard separately.

…

To help you understand the attributes:

Attribute Type Function
name string Name for the plan to be presented to customers.
price float Price per billing cycle.
interval string Optional. What is the billing cycle? Valid options are month, year, week, 3-month, 6-month. Defaults to month.
stripe_id string The Plan ID in Stripe.
features string A list of features. Supports Markdown syntax.
display_order integer Order in which to display plans.
highlight boolean Optional. Whether to highlight the plan on the pricing page.

The only view installed locally into your app by default is the koudoku/subscriptions/_social_proof.html.erb partial which is displayed alongside the pricing table. It's designed as a placeholder where you can provide quotes about your product from customers that could positively influence your visitors.

Configuring Stripe API Keys

You can supply your publishable and secret API keys in config/initializers/koudoku.rb. However, by default it will use the STRIPE_PUBLISHABLE_KEY and STRIPE_SECRET_KEY shell environment variables. This encourages people to keep these API keys out of version control. You may want to rename these environment variables to be more application specific.

In a bash shell, you can set them in ~/.bash_profile like so:

bash
    export STRIPE_PUBLISHABLE_KEY=pk_0CJwDH9sdh98f79FDHDOjdiOxQob0
    export STRIPE_SECRET_KEY=sk_0CJwFDIUshdfh97JDJOjZ5OIDjOCH

(Reload your terminal for these settings to take effect.)

On Heroku you accomplish this same effect with Config Vars:

bash
    heroku config:add STRIPE_PUBLISHABLE_KEY=pk_0CJwDH9sdh98f79FDHDOjdiOxQob0
    heroku config:add STRIPE_SECRET_KEY=sk_0CJwFDIUshdfh97JDJOjZ5OIDjOCH

User-Facing Subscription Management

By default a pricing_path route is defined which you can link to in order to show visitors a pricing table. If a user is signed in, this pricing table will take into account their current plan. For example, you can link to this page like so:

ruby
    

(Note: Koudoku uses the application layout, so it's important that application paths referenced in that layout are prefixed with "main_app." like you see above or Rails will try to look the paths up in the Koudoku engine instead of your application.)

Existing users can view available plans, select a plan, enter credit card details, review their subscription, change plans, and cancel at the following route:

ruby
    koudoku.owner_subscriptions_path(@user)

In these paths, owner refers to User by default, or whatever model has been configured to be the owner of the Subscription model.

A number of views are provided by default. To customize the views, use the following generator:

ruby
    rails g koudoku:views

Pricing Table

Koudoku ships with a stock pricing table. By default it depends on Twitter Bootstrap, but also has some additional styles required. In order to import these styles, add the following to your app/assets/stylesheets/application.css:

css
    *= require 'koudoku/pricing-table'

Or, if you've replaced your application.css with an application.scss (like I always do):

css
    @import "koudoku/pricing-table"

Using Coupons

While more robust coupon support is expected in the future, the simple way to use a coupon is to first create it:

ruby
    coupon = Coupon.create(code: '30-days-free', free_trial_length: 30)

Then assign it to a new subscription before saving:

ruby
    subscription = Subscription.new(...)
    subscription.coupon = coupon
    subscription.save

It should be noted that these coupons are different from the coupons provided natively by Stripe.

Implementing Logging, Notifications, etc.

The included module defines the following empty "template methods" which you're able to provide an implementation for in Subscription:

  • prepare_for_plan_change
  • prepare_for_new_subscription
  • prepare_for_upgrade
  • prepare_for_downgrade
  • prepare_for_cancelation
  • prepare_for_card_update
  • finalize_plan_change!
  • finalize_new_subscription!
  • finalize_upgrade!
  • finalize_downgrade!
  • finalize_cancelation!
  • finalize_card_update!
  • card_was_declined

Be sure to include a call to super in each of your implementations, especially if you're using multiple concerns to break all this logic into smaller pieces.

Between prepare_for_* and finalize_*, so far I've used finalize_* almost exclusively. The difference is that prepare_for_* runs before we settle things with Stripe, and finalize_* runs after everything is settled in Stripe. For that reason, please be sure not to implement anything in finalize_* implementations that might cause issues with ActiveRecord saving the updated state of the subscription.

Webhooks

We use stripe_event under the hood to support webhooks. The default webhooks URL is /koudoku/webhooks.

You can add your own webhooks using the (reduced) stripe_event syntax in the config/initializers/koudoku.rb file:

# /config/initializers/koudoku.rb
Koudoku.setup do |config|
  config.subscriptions_owned_by = :user
  config.stripe_publishable_key = ENV['STRIPE_PUBLISHABLE_KEY']
  config.stripe_secret_key = ENV['STRIPE_SECRET_KEY']
  
  # add webhooks
  config.subscribe 'charge.failed', YourChargeFailed
end

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Ruby

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言