Skip to content

agiangrant/stripe_saas

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

StripeSaas

A Rails 4 Engine providing Stripe subscription management for SaaS applications. Based on the work of Andrew Culver in Koudoku (https://github.com/andrewculver/koudoku).

Status

CI Status Code Climate Test Coverage

Installation

Add gem dependency

Include the stripe_saas gem in your Gemfile and bundle (install):

gem 'stripe_saas'

Install subscriptions management on a model.

A rails generator is provided to install the StripeSaas models:

rails g stripe_saas:install user

Stripe Subscriptions

A model that mirrors a Stripe subscription (https://stripe.com/docs/api/ruby#subscriptions) is generated and a one-to-one relationship between it and one of your application’s models as the owner of the subscription.

In the example above the generated StripeSaas::Subscription 'belongs to' your application’s User:

class Subscription < ActiveRecord::Base
  include StripeSaas::Subscription

  belongs_to :user
end

and the User class will have one (has_one) subscription:

has_one :subscription

Stripe Plans

A model that mirrors a Stripe plan (https://stripe.com/docs/api/ruby#plans) is generated.

class Plan < ActiveRecord::Base
  has_many :subscriptions
  has_many :plan_features
  has_many :features, through: :plan_features

  default_scope { order(:display_order) }

  include StripeSaas::Plan
end

Plans have PlanFeatures which in turn are join table/model between Plan and Feature:

class Feature < ActiveRecord::Base
  has_many :plan_features
  has_many :plans, through: :plan_features

  default_scope { order(:display_order) }

  include StripeSaas::Feature
end

To create a Feature for example, you could use:

Feature.find_or_create_by(name: 'signals').update({
  description: "Inbound Signals",
  feature_type: :number,
  unit: "signals",
  display_order: 1
})

Where the feature type can be one of:

FEATURE_TYPES = {
  boolean: 'Boolean',
  interval: 'Interval (in seconds)',
  filesize: 'Filesize (in bytes)',
  number: 'Number',
  percentage: 'Percentage (%)'
}

To create a plan (in your seeds for example) with a set of features you could use something like:

developer_plan = Plan.find_or_create_by(stripe_id: 'developer')
developer_plan.update({
  name: 'Developer',
  price: 0.0,
  interval: 'month',
  interval_count: 1,
  statement_descriptor: 'Binnacle Developer Plan',
  trial_period_days: 30,
  display_order: 1
})

developer_plan.add_feature(:signals, 50000)

Any plan with a price of 0.0 is considered a free plan in StripeSaas which will not require the user to enter credit card information.

After running the installer you will have to migrate your database:

rake db:migrate

Configuration

As part of the installation procedure an initializer is generated under config/initializers/stripe_saas.rb:

StripeSaas.setup do |config|
  config.subscriptions_owned_by = :user
  # config.devise_scope = :user
  config.stripe_publishable_key = ENV['STRIPE_PUBLISHABLE_KEY']
  config.stripe_secret_key = ENV['STRIPE_SECRET_KEY']
  config.create_plans_in_stripe = false
end
  • subscriptions_owned_by: The symbol of the class that owns the subscription

  • devise_scope: If using Devise and the subscription is not owned by the devise class (user/customer). For example, if users have accounts, and accounts have subscriptions. Then config.subscriptions_owned_by = :account and config.devise_scope = :user

  • stripe_publishable_key: Your Stripe Publishable Key https://stripe.com/docs/tutorials/dashboard#api-keys

  • stripe_secret_key: Your Stripe Secret Key https://stripe.com/docs/tutorials/dashboard#api-keys

  • create_plans_in_stripe: Whether to autogenerate the local Plans in Stripe and keep then in synch

About

Stripe SAAS Rails Engine

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Ruby 83.3%
  • HTML 13.6%
  • CSS 1.7%
  • JavaScript 1.4%