stripe_saas 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (88) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.asc +159 -0
  4. data/Rakefile +37 -0
  5. data/app/assets/javascripts/stripe_saas/application.js +13 -0
  6. data/app/assets/stylesheets/stripe_saas/application.css +15 -0
  7. data/app/concerns/stripe_saas/feature.rb +23 -0
  8. data/app/concerns/stripe_saas/plan.rb +45 -0
  9. data/app/concerns/stripe_saas/plan_feature.rb +25 -0
  10. data/app/concerns/stripe_saas/subscription.rb +182 -0
  11. data/app/controllers/stripe_saas/application_controller.rb +5 -0
  12. data/app/controllers/stripe_saas/subscriptions_controller.rb +200 -0
  13. data/app/helpers/stripe_saas/application_helper.rb +26 -0
  14. data/app/views/stripe_saas/subscriptions/_card.html.erb +51 -0
  15. data/app/views/stripe_saas/subscriptions/_card_form.html.erb +42 -0
  16. data/app/views/stripe_saas/subscriptions/_pricing_table.html.erb +43 -0
  17. data/app/views/stripe_saas/subscriptions/edit.html.erb +7 -0
  18. data/app/views/stripe_saas/subscriptions/index.html.erb +2 -0
  19. data/app/views/stripe_saas/subscriptions/new.html.erb +1 -0
  20. data/app/views/stripe_saas/subscriptions/show.html.erb +15 -0
  21. data/app/views/stripe_saas/subscriptions/unauthorized.html.erb +1 -0
  22. data/config/environment.rb +0 -0
  23. data/config/routes.rb +10 -0
  24. data/lib/generators/stripe_saas/install_generator.rb +60 -0
  25. data/lib/generators/stripe_saas/templates/app/models/feature.rb +8 -0
  26. data/lib/generators/stripe_saas/templates/app/models/plan.rb +9 -0
  27. data/lib/generators/stripe_saas/templates/app/models/plan_feature.rb +6 -0
  28. data/lib/generators/stripe_saas/templates/app/models/subscription.rb +5 -0
  29. data/lib/generators/stripe_saas/templates/config/initializers/stripe_saas.rb +7 -0
  30. data/lib/generators/stripe_saas/views_generator.rb +20 -0
  31. data/lib/stripe_saas.rb +9 -0
  32. data/lib/stripe_saas/configuration.rb +51 -0
  33. data/lib/stripe_saas/engine.rb +41 -0
  34. data/lib/stripe_saas/version.rb +3 -0
  35. data/lib/tasks/stripe_saas_tasks.rake +11 -0
  36. data/spec/concerns/plan_spec.rb +50 -0
  37. data/spec/dummy/README.rdoc +28 -0
  38. data/spec/dummy/Rakefile +6 -0
  39. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  40. data/spec/dummy/app/assets/stylesheets/application.css +15 -0
  41. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  42. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  43. data/spec/dummy/app/models/plan.rb +7 -0
  44. data/spec/dummy/app/models/subscription.rb +5 -0
  45. data/spec/dummy/app/models/user.rb +8 -0
  46. data/spec/dummy/app/views/layouts/application.html.erb +16 -0
  47. data/spec/dummy/bin/bundle +3 -0
  48. data/spec/dummy/bin/rails +4 -0
  49. data/spec/dummy/bin/rake +4 -0
  50. data/spec/dummy/bin/setup +29 -0
  51. data/spec/dummy/config.ru +4 -0
  52. data/spec/dummy/config/application.rb +27 -0
  53. data/spec/dummy/config/boot.rb +5 -0
  54. data/spec/dummy/config/database.yml +25 -0
  55. data/spec/dummy/config/environment.rb +5 -0
  56. data/spec/dummy/config/environments/development.rb +41 -0
  57. data/spec/dummy/config/environments/production.rb +79 -0
  58. data/spec/dummy/config/environments/test.rb +42 -0
  59. data/spec/dummy/config/initializers/assets.rb +11 -0
  60. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  61. data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
  62. data/spec/dummy/config/initializers/devise.rb +259 -0
  63. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  64. data/spec/dummy/config/initializers/inflections.rb +16 -0
  65. data/spec/dummy/config/initializers/mime_types.rb +4 -0
  66. data/spec/dummy/config/initializers/session_store.rb +3 -0
  67. data/spec/dummy/config/initializers/stripe_saas.rb +7 -0
  68. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  69. data/spec/dummy/config/locales/devise.en.yml +60 -0
  70. data/spec/dummy/config/locales/en.yml +23 -0
  71. data/spec/dummy/config/routes.rb +10 -0
  72. data/spec/dummy/config/secrets.yml +22 -0
  73. data/spec/dummy/db/development.sqlite3 +0 -0
  74. data/spec/dummy/db/migrate/20150101233243_devise_create_users.rb +42 -0
  75. data/spec/dummy/db/migrate/20150102001921_create_subscriptions.rb +14 -0
  76. data/spec/dummy/db/migrate/20150102001930_create_plans.rb +19 -0
  77. data/spec/dummy/db/schema.rb +61 -0
  78. data/spec/dummy/db/test.sqlite3 +0 -0
  79. data/spec/dummy/log/development.log +170 -0
  80. data/spec/dummy/log/test.log +110 -0
  81. data/spec/dummy/public/404.html +67 -0
  82. data/spec/dummy/public/422.html +67 -0
  83. data/spec/dummy/public/500.html +66 -0
  84. data/spec/dummy/public/favicon.ico +0 -0
  85. data/spec/integration/navigation_test.rb +9 -0
  86. data/spec/rails_helper.rb +23 -0
  87. data/spec/spec_helper.rb +22 -0
  88. metadata +278 -0
@@ -0,0 +1,8 @@
1
+ class Feature < ActiveRecord::Base
2
+ has_many :plan_features
3
+ has_many :plans, through: :plan_features
4
+
5
+ default_scope { order(:display_order) }
6
+
7
+ include StripeSaas::Feature
8
+ end
@@ -0,0 +1,9 @@
1
+ class Plan < ActiveRecord::Base
2
+ has_many :subscriptions
3
+ has_many :plan_features
4
+ has_many :features, through: :plan_features
5
+
6
+ default_scope { order(:display_order) }
7
+
8
+ include StripeSaas::Plan
9
+ end
@@ -0,0 +1,6 @@
1
+ class PlanFeature < ActiveRecord::Base
2
+ belongs_to :plan
3
+ belongs_to :feature
4
+
5
+ include StripeSaas::PlanFeature
6
+ end
@@ -0,0 +1,5 @@
1
+ class Subscription < ActiveRecord::Base
2
+ include StripeSaas::Subscription
3
+
4
+ belongs_to :<%= subscription_owner_model %>
5
+ end
@@ -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
@@ -0,0 +1,9 @@
1
+ require_relative 'stripe_saas/engine'
2
+ require_relative 'generators/stripe_saas/install_generator'
3
+ require_relative 'generators/stripe_saas/views_generator'
4
+
5
+ require 'money-rails'
6
+ require 'stripe'
7
+
8
+ module StripeSaas
9
+ end
@@ -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,3 @@
1
+ module StripeSaas
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,11 @@
1
+ namespace :stripe_saas do
2
+ desc "Install StripeSaas"
3
+ task :install do
4
+ system 'rails g stripe_saas:install'
5
+ end
6
+
7
+ desc "Install StripeSaas views"
8
+ task :views do
9
+ system 'rails g stripe_saas:views'
10
+ end
11
+ 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>.
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+
6
+ Rails.application.load_tasks
@@ -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,5 @@
1
+ class ApplicationController < ActionController::Base
2
+ # Prevent CSRF attacks by raising an exception.
3
+ # For APIs, you may want to use :null_session instead.
4
+ protect_from_forgery with: :exception
5
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,7 @@
1
+ class Plan < ActiveRecord::Base
2
+ has_many :subscriptions
3
+
4
+ include StripeSaas::Plan
5
+
6
+ belongs_to :user
7
+ end
@@ -0,0 +1,5 @@
1
+ class Subscription < ActiveRecord::Base
2
+ include StripeSaas::Subscription
3
+
4
+ belongs_to :user
5
+ end
@@ -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>
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
3
+ load Gem.bin_path('bundler', 'bundle')
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
3
+ require_relative '../config/boot'
4
+ require 'rails/commands'
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../config/boot'
3
+ require 'rake'
4
+ Rake.application.run
@@ -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,4 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require ::File.expand_path('../config/environment', __FILE__)
4
+ run Rails.application
@@ -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