stripe_saas 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.asc +159 -0
- data/Rakefile +37 -0
- data/app/assets/javascripts/stripe_saas/application.js +13 -0
- data/app/assets/stylesheets/stripe_saas/application.css +15 -0
- data/app/concerns/stripe_saas/feature.rb +23 -0
- data/app/concerns/stripe_saas/plan.rb +45 -0
- data/app/concerns/stripe_saas/plan_feature.rb +25 -0
- data/app/concerns/stripe_saas/subscription.rb +182 -0
- data/app/controllers/stripe_saas/application_controller.rb +5 -0
- data/app/controllers/stripe_saas/subscriptions_controller.rb +200 -0
- data/app/helpers/stripe_saas/application_helper.rb +26 -0
- data/app/views/stripe_saas/subscriptions/_card.html.erb +51 -0
- data/app/views/stripe_saas/subscriptions/_card_form.html.erb +42 -0
- data/app/views/stripe_saas/subscriptions/_pricing_table.html.erb +43 -0
- data/app/views/stripe_saas/subscriptions/edit.html.erb +7 -0
- data/app/views/stripe_saas/subscriptions/index.html.erb +2 -0
- data/app/views/stripe_saas/subscriptions/new.html.erb +1 -0
- data/app/views/stripe_saas/subscriptions/show.html.erb +15 -0
- data/app/views/stripe_saas/subscriptions/unauthorized.html.erb +1 -0
- data/config/environment.rb +0 -0
- data/config/routes.rb +10 -0
- data/lib/generators/stripe_saas/install_generator.rb +60 -0
- data/lib/generators/stripe_saas/templates/app/models/feature.rb +8 -0
- data/lib/generators/stripe_saas/templates/app/models/plan.rb +9 -0
- data/lib/generators/stripe_saas/templates/app/models/plan_feature.rb +6 -0
- data/lib/generators/stripe_saas/templates/app/models/subscription.rb +5 -0
- data/lib/generators/stripe_saas/templates/config/initializers/stripe_saas.rb +7 -0
- data/lib/generators/stripe_saas/views_generator.rb +20 -0
- data/lib/stripe_saas.rb +9 -0
- data/lib/stripe_saas/configuration.rb +51 -0
- data/lib/stripe_saas/engine.rb +41 -0
- data/lib/stripe_saas/version.rb +3 -0
- data/lib/tasks/stripe_saas_tasks.rake +11 -0
- data/spec/concerns/plan_spec.rb +50 -0
- data/spec/dummy/README.rdoc +28 -0
- data/spec/dummy/Rakefile +6 -0
- data/spec/dummy/app/assets/javascripts/application.js +13 -0
- data/spec/dummy/app/assets/stylesheets/application.css +15 -0
- data/spec/dummy/app/controllers/application_controller.rb +5 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/models/plan.rb +7 -0
- data/spec/dummy/app/models/subscription.rb +5 -0
- data/spec/dummy/app/models/user.rb +8 -0
- data/spec/dummy/app/views/layouts/application.html.erb +16 -0
- data/spec/dummy/bin/bundle +3 -0
- data/spec/dummy/bin/rails +4 -0
- data/spec/dummy/bin/rake +4 -0
- data/spec/dummy/bin/setup +29 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/config/application.rb +27 -0
- data/spec/dummy/config/boot.rb +5 -0
- data/spec/dummy/config/database.yml +25 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +41 -0
- data/spec/dummy/config/environments/production.rb +79 -0
- data/spec/dummy/config/environments/test.rb +42 -0
- data/spec/dummy/config/initializers/assets.rb +11 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
- data/spec/dummy/config/initializers/devise.rb +259 -0
- data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/dummy/config/initializers/inflections.rb +16 -0
- data/spec/dummy/config/initializers/mime_types.rb +4 -0
- data/spec/dummy/config/initializers/session_store.rb +3 -0
- data/spec/dummy/config/initializers/stripe_saas.rb +7 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/config/locales/devise.en.yml +60 -0
- data/spec/dummy/config/locales/en.yml +23 -0
- data/spec/dummy/config/routes.rb +10 -0
- data/spec/dummy/config/secrets.yml +22 -0
- data/spec/dummy/db/development.sqlite3 +0 -0
- data/spec/dummy/db/migrate/20150101233243_devise_create_users.rb +42 -0
- data/spec/dummy/db/migrate/20150102001921_create_subscriptions.rb +14 -0
- data/spec/dummy/db/migrate/20150102001930_create_plans.rb +19 -0
- data/spec/dummy/db/schema.rb +61 -0
- data/spec/dummy/db/test.sqlite3 +0 -0
- data/spec/dummy/log/development.log +170 -0
- data/spec/dummy/log/test.log +110 -0
- data/spec/dummy/public/404.html +67 -0
- data/spec/dummy/public/422.html +67 -0
- data/spec/dummy/public/500.html +66 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/integration/navigation_test.rb +9 -0
- data/spec/rails_helper.rb +23 -0
- data/spec/spec_helper.rb +22 -0
- metadata +278 -0
@@ -0,0 +1,7 @@
|
|
1
|
+
StripeSaas.setup do |config|
|
2
|
+
config.subscriptions_owned_by = :<%= subscription_owner_model %>
|
3
|
+
# config.devise_scope = :<%= subscription_owner_model %>
|
4
|
+
config.stripe_publishable_key = ENV['STRIPE_PUBLISHABLE_KEY']
|
5
|
+
config.stripe_secret_key = ENV['STRIPE_SECRET_KEY']
|
6
|
+
config.create_plans_in_stripe = false
|
7
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
|
3
|
+
module StripeSaas
|
4
|
+
class ViewsGenerator < Rails::Generators::Base
|
5
|
+
# # Not sure what this does.
|
6
|
+
# source_root "#{StripeSaas::Engine.root}/app/views/StripeSaas/subscriptions"
|
7
|
+
|
8
|
+
include Rails::Generators::Migration
|
9
|
+
|
10
|
+
desc "StripeSaas installation generator"
|
11
|
+
|
12
|
+
def install
|
13
|
+
files_to_copy = Dir.entries("#{StripeSaas::Engine.root}/app/views/stripe_saas/subscriptions") - %w[. ..]
|
14
|
+
files_to_copy.each do |file|
|
15
|
+
copy_file file, "app/views/stripe_saas/subscriptions/#{file}"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
data/lib/stripe_saas.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
module StripeSaas
|
2
|
+
mattr_accessor :subscriptions_owned_by
|
3
|
+
@@subscriptions_owned_by = nil
|
4
|
+
|
5
|
+
mattr_accessor :devise_scope
|
6
|
+
@@devise_scope = nil
|
7
|
+
|
8
|
+
mattr_accessor :customer_accessor
|
9
|
+
@@customer_accessor = nil
|
10
|
+
|
11
|
+
mattr_accessor :stripe_publishable_key
|
12
|
+
@@stripe_publishable_key = nil
|
13
|
+
|
14
|
+
mattr_accessor :stripe_secret_key
|
15
|
+
@@stripe_secret_key = nil
|
16
|
+
|
17
|
+
mattr_accessor :create_plans_in_stripe
|
18
|
+
@@create_plans_in_stripe = false
|
19
|
+
|
20
|
+
def self.setup
|
21
|
+
yield self
|
22
|
+
|
23
|
+
# Configure the Stripe gem.
|
24
|
+
::Stripe.api_key = stripe_secret_key
|
25
|
+
end
|
26
|
+
|
27
|
+
# e.g. :users
|
28
|
+
def self.owner_resource
|
29
|
+
subscriptions_owned_by.to_s.pluralize.to_sym
|
30
|
+
end
|
31
|
+
|
32
|
+
# e.g. :user_id
|
33
|
+
def self.owner_id_sym
|
34
|
+
:"#{StripeSaas.subscriptions_owned_by}_id"
|
35
|
+
end
|
36
|
+
|
37
|
+
# e.g. :user=
|
38
|
+
def self.owner_assignment_sym
|
39
|
+
:"#{StripeSaas.subscriptions_owned_by}="
|
40
|
+
end
|
41
|
+
|
42
|
+
# e.g. User
|
43
|
+
def self.owner_class
|
44
|
+
StripeSaas.subscriptions_owned_by.to_s.classify.constantize
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.create_plans_in_stripe?
|
48
|
+
StripeSaas.create_plans_in_stripe
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require_relative 'configuration'
|
2
|
+
|
3
|
+
module StripeSaas
|
4
|
+
class Engine < ::Rails::Engine
|
5
|
+
isolate_namespace StripeSaas
|
6
|
+
|
7
|
+
initializer 'stripe_saas.action_controller' do |app|
|
8
|
+
ActiveSupport.on_load :action_controller do
|
9
|
+
helper StripeSaas::ApplicationHelper
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
initializer 'stripe_saas.create_plans' do |app|
|
14
|
+
if StripeSaas.create_plans_in_stripe?
|
15
|
+
begin
|
16
|
+
::Plan.all.each do |plan|
|
17
|
+
begin
|
18
|
+
stripe_plan = Stripe::Plan.retrieve(plan.stripe_id)
|
19
|
+
rescue Stripe::InvalidRequestError => ire
|
20
|
+
if ire.message == "No such plan: #{plan.stripe_id}"
|
21
|
+
Stripe::Plan.create(
|
22
|
+
id: plan.stripe_id,
|
23
|
+
name: plan.name,
|
24
|
+
amount: plan.price_cents,
|
25
|
+
interval: plan.interval,
|
26
|
+
interval_count: plan.interval_count,
|
27
|
+
trial_period_days: plan.trial_period_days,
|
28
|
+
statement_descriptor: plan.statement_descriptor,
|
29
|
+
currency: 'usd',
|
30
|
+
metadata: plan.metadata_as_json
|
31
|
+
)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
rescue NameError, ActiveRecord::StatementInvalid
|
36
|
+
# ignore: Plan model is not defined yet (migration might not have run)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'rails_helper'
|
2
|
+
|
3
|
+
describe StripeSaas::Plan do
|
4
|
+
describe '#is_upgrade_from?' do
|
5
|
+
|
6
|
+
it 'returns true if the price is higher' do
|
7
|
+
plan = Plan.new
|
8
|
+
plan.price = 123.23
|
9
|
+
cheaper_plan = Plan.new
|
10
|
+
cheaper_plan.price = 61.61
|
11
|
+
expect(plan.is_upgrade_from?(cheaper_plan)).to be true
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'returns true if the price is the same' do
|
15
|
+
plan = Plan.new
|
16
|
+
plan.price = 123.23
|
17
|
+
expect(plan.is_upgrade_from?(plan)).to be true
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'returns false if the price is the same or higher' do
|
21
|
+
plan = Plan.new
|
22
|
+
plan.price = 61.61
|
23
|
+
more_expensive_plan = Plan.new
|
24
|
+
more_expensive_plan.price = 123.23
|
25
|
+
expect(plan.is_upgrade_from?(more_expensive_plan)).to be false
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'handles a nil value gracefully' do
|
29
|
+
plan = Plan.new
|
30
|
+
plan.price = 123.23
|
31
|
+
cheaper_plan = Plan.new
|
32
|
+
|
33
|
+
expect(plan.is_upgrade_from?(cheaper_plan)).to be true
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'returns whether the plan is a free plan' do
|
37
|
+
plan = Plan.new
|
38
|
+
plan.price = 0.0
|
39
|
+
expect(plan).to be_free
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
describe 'plan features' do
|
45
|
+
# before {
|
46
|
+
# feature_a = Feature.new
|
47
|
+
# feature_b = Feature.new
|
48
|
+
# }
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
== README
|
2
|
+
|
3
|
+
This README would normally document whatever steps are necessary to get the
|
4
|
+
application up and running.
|
5
|
+
|
6
|
+
Things you may want to cover:
|
7
|
+
|
8
|
+
* Ruby version
|
9
|
+
|
10
|
+
* System dependencies
|
11
|
+
|
12
|
+
* Configuration
|
13
|
+
|
14
|
+
* Database creation
|
15
|
+
|
16
|
+
* Database initialization
|
17
|
+
|
18
|
+
* How to run the test suite
|
19
|
+
|
20
|
+
* Services (job queues, cache servers, search engines, etc.)
|
21
|
+
|
22
|
+
* Deployment instructions
|
23
|
+
|
24
|
+
* ...
|
25
|
+
|
26
|
+
|
27
|
+
Please feel free to use a different markup language if you do not plan to run
|
28
|
+
<tt>rake doc:app</tt>.
|
data/spec/dummy/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
// This is a manifest file that'll be compiled into application.js, which will include all the files
|
2
|
+
// listed below.
|
3
|
+
//
|
4
|
+
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
|
5
|
+
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
|
6
|
+
//
|
7
|
+
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
|
8
|
+
// compiled file.
|
9
|
+
//
|
10
|
+
// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
|
11
|
+
// about supported directives.
|
12
|
+
//
|
13
|
+
//= require_tree .
|
@@ -0,0 +1,15 @@
|
|
1
|
+
/*
|
2
|
+
* This is a manifest file that'll be compiled into application.css, which will include all the files
|
3
|
+
* listed below.
|
4
|
+
*
|
5
|
+
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
|
6
|
+
* or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
|
7
|
+
*
|
8
|
+
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
|
9
|
+
* compiled file so the styles you add here take precedence over styles defined in any styles
|
10
|
+
* defined in the other CSS/SCSS files in this directory. It is generally better to create a new
|
11
|
+
* file per style scope.
|
12
|
+
*
|
13
|
+
*= require_tree .
|
14
|
+
*= require_self
|
15
|
+
*/
|
@@ -0,0 +1,8 @@
|
|
1
|
+
class User < ActiveRecord::Base
|
2
|
+
# Include default devise modules. Others available are:
|
3
|
+
# :confirmable, :lockable, :timeoutable and :omniauthable
|
4
|
+
devise :database_authenticatable, :registerable,
|
5
|
+
:recoverable, :rememberable, :trackable, :validatable
|
6
|
+
|
7
|
+
has_one :subscription
|
8
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title>Dummy</title>
|
5
|
+
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
|
6
|
+
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
|
7
|
+
<%= csrf_meta_tags %>
|
8
|
+
</head>
|
9
|
+
<body>
|
10
|
+
<p class="notice"><%= notice %></p>
|
11
|
+
<p class="alert"><%= alert %></p>
|
12
|
+
|
13
|
+
<%= yield %>
|
14
|
+
|
15
|
+
</body>
|
16
|
+
</html>
|
data/spec/dummy/bin/rake
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'pathname'
|
3
|
+
|
4
|
+
# path to your application root.
|
5
|
+
APP_ROOT = Pathname.new File.expand_path('../../', __FILE__)
|
6
|
+
|
7
|
+
Dir.chdir APP_ROOT do
|
8
|
+
# This script is a starting point to setup your application.
|
9
|
+
# Add necessary setup steps to this file:
|
10
|
+
|
11
|
+
puts "== Installing dependencies =="
|
12
|
+
system "gem install bundler --conservative"
|
13
|
+
system "bundle check || bundle install"
|
14
|
+
|
15
|
+
# puts "\n== Copying sample files =="
|
16
|
+
# unless File.exist?("config/database.yml")
|
17
|
+
# system "cp config/database.yml.sample config/database.yml"
|
18
|
+
# end
|
19
|
+
|
20
|
+
puts "\n== Preparing database =="
|
21
|
+
system "bin/rake db:setup"
|
22
|
+
|
23
|
+
puts "\n== Removing old logs and tempfiles =="
|
24
|
+
system "rm -f log/*"
|
25
|
+
system "rm -rf tmp/cache"
|
26
|
+
|
27
|
+
puts "\n== Restarting application server =="
|
28
|
+
system "touch tmp/restart.txt"
|
29
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require File.expand_path('../boot', __FILE__)
|
2
|
+
|
3
|
+
require 'rails/all'
|
4
|
+
|
5
|
+
Bundler.require(*Rails.groups)
|
6
|
+
|
7
|
+
require 'stripe_saas'
|
8
|
+
require 'devise'
|
9
|
+
|
10
|
+
module Dummy
|
11
|
+
class Application < Rails::Application
|
12
|
+
# Settings in config/environments/* take precedence over those specified here.
|
13
|
+
# Application configuration should go into files in config/initializers
|
14
|
+
# -- all .rb files in that directory are automatically loaded.
|
15
|
+
|
16
|
+
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
|
17
|
+
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
|
18
|
+
# config.time_zone = 'Central Time (US & Canada)'
|
19
|
+
|
20
|
+
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
|
21
|
+
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
|
22
|
+
# config.i18n.default_locale = :de
|
23
|
+
|
24
|
+
# Do not swallow errors in after_commit/after_rollback callbacks.
|
25
|
+
config.active_record.raise_in_transactional_callbacks = true
|
26
|
+
end
|
27
|
+
end
|