koudoku 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.md +51 -4
  3. data/Rakefile +40 -1
  4. data/app/assets/javascripts/koudoku/application.js +15 -0
  5. data/app/assets/stylesheets/koudoku/application.css +13 -0
  6. data/app/assets/stylesheets/koudoku/pricing-table.scss +77 -0
  7. data/app/concerns/koudoku/plan.rb +20 -0
  8. data/{lib → app/concerns}/koudoku/subscription.rb +26 -6
  9. data/app/controllers/koudoku/application_controller.rb +5 -0
  10. data/app/controllers/koudoku/subscriptions_controller.rb +63 -0
  11. data/app/controllers/koudoku/webhooks_controller.rb +38 -0
  12. data/app/helpers/koudoku/application_helper.rb +4 -0
  13. data/app/views/koudoku/subscriptions/_pricing_table.html.erb +53 -0
  14. data/app/views/koudoku/subscriptions/edit.html.erb +3 -0
  15. data/app/views/koudoku/subscriptions/index.html.erb +2 -0
  16. data/app/views/koudoku/subscriptions/new.html.erb +99 -0
  17. data/app/views/koudoku/subscriptions/show.html.erb +8 -0
  18. data/app/views/layouts/koudoku/application.html.erb +14 -0
  19. data/config/routes.rb +11 -0
  20. data/lib/generators/koudoku/install_generator.rb +7 -3
  21. data/lib/generators/koudoku/views_generator.rb +28 -0
  22. data/lib/koudoku.rb +43 -6
  23. data/lib/koudoku/engine.rb +6 -0
  24. data/lib/koudoku/version.rb +1 -1
  25. data/lib/tasks/koudoku_tasks.rake +12 -0
  26. data/test/dummy/README.rdoc +261 -0
  27. data/test/dummy/Rakefile +7 -0
  28. data/test/dummy/app/assets/javascripts/application.js +15 -0
  29. data/test/dummy/app/assets/stylesheets/application.css +13 -0
  30. data/test/dummy/app/controllers/application_controller.rb +3 -0
  31. data/test/dummy/app/helpers/application_helper.rb +2 -0
  32. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  33. data/test/dummy/config.ru +4 -0
  34. data/test/dummy/config/application.rb +59 -0
  35. data/test/dummy/config/boot.rb +10 -0
  36. data/test/dummy/config/database.yml +25 -0
  37. data/test/dummy/config/environment.rb +5 -0
  38. data/test/dummy/config/environments/development.rb +37 -0
  39. data/test/dummy/config/environments/production.rb +67 -0
  40. data/test/dummy/config/environments/test.rb +37 -0
  41. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  42. data/test/dummy/config/initializers/inflections.rb +15 -0
  43. data/test/dummy/config/initializers/mime_types.rb +5 -0
  44. data/test/dummy/config/initializers/secret_token.rb +7 -0
  45. data/test/dummy/config/initializers/session_store.rb +8 -0
  46. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  47. data/test/dummy/config/locales/en.yml +5 -0
  48. data/test/dummy/config/routes.rb +4 -0
  49. data/test/dummy/public/404.html +26 -0
  50. data/test/dummy/public/422.html +26 -0
  51. data/test/dummy/public/500.html +25 -0
  52. data/{docs/INSTALLATION.md → test/dummy/public/favicon.ico} +0 -0
  53. data/test/dummy/script/rails +6 -0
  54. data/test/integration/navigation_test.rb +10 -0
  55. data/test/koudoku_test.rb +7 -0
  56. data/test/test_helper.rb +15 -0
  57. metadata +113 -17
  58. data/.gitignore +0 -4
  59. data/Gemfile +0 -4
  60. data/koudoku.gemspec +0 -24
  61. data/lib/generators/koudoku/templates/insert_default_plans.rb +0 -0
  62. data/lib/koudoku/webhooks_controller.rb +0 -37
  63. data/lib/tasks/koudoku.rake +0 -8
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2013 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -8,10 +8,55 @@ Include the following in your `Gemfile`:
8
8
 
9
9
  gem 'koudoku'
10
10
 
11
- 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`".)
11
+ 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.)
12
12
 
13
13
  rails g koudoku:install user
14
+
15
+ After installing, you'll need to add some subscription plans.
16
+
17
+ Plan.create(name: 'Personal', price: 10.00, stripe_id: '1')
18
+ Plan.create(name: 'Team', price: 30.00, stripe_id: '2')
19
+ Plan.create(name: 'Enterprise', price: 100.00, stripe_id: '3')
20
+
21
+ ### Configuring Stripe API Keys
22
+
23
+ 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.
24
+
25
+ In a bash shell, you can set them in `~/.bash_profile` like so:
26
+
27
+ export STRIPE_PUBLISHABLE_KEY=pk_0CJwDH9sdh98f79FDHDOjdiOxQob0
28
+ export STRIPE_SECRET_KEY=sk_0CJwFDIUshdfh97JDJOjZ5OIDjOCH
29
+
30
+ (Reload your terminal for these settings to take effect.)
31
+
32
+ On Heroku you accomplish this same effect with [Config Vars](https://devcenter.heroku.com/articles/config-vars):
33
+
34
+ heroku config:add STRIPE_PUBLISHABLE_KEY=pk_0CJwDH9sdh98f79FDHDOjdiOxQob0
35
+ heroku config:add STRIPE_SECRET_KEY=sk_0CJwFDIUshdfh97JDJOjZ5OIDjOCH
36
+
37
+ ## User-Facing Subscription Management
38
+
39
+ Users can view available plans, select a plan, enter credit card details, review their subscription, change plans, and cancel at the following route:
14
40
 
41
+ koudoku.owner_subscriptions_path(@user)
42
+
43
+ In these paths, `owner` refers to `User` by default, or whatever model has been configured to be the owner of the `Subscription` model.
44
+
45
+ A number of views are provided by default. To customize the views, use the following generator:
46
+
47
+ rails g koudoku:views
48
+
49
+ ### Pricing Table
50
+
51
+ 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`:
52
+
53
+ *= require 'koudoku/pricing-table'
54
+
55
+ Or, if you've replaced your `application.css` with an `application.scss` (like I always do):
56
+
57
+ @import "koudoku/pricing-table"
58
+
59
+
15
60
  ## Using Coupons
16
61
 
17
62
  While more robust coupon support is expected in the future, the simple way to use a coupon is to first create it:
@@ -26,7 +71,7 @@ Then assign it to a _new_ subscription before saving:
26
71
 
27
72
  ## Implementing Logging, Notifications, etc.
28
73
 
29
- The included module defined the following empty "template methods" which you're able to provide an implementation for:
74
+ The included module defines the following empty "template methods" which you're able to provide an implementation for in `Subscription`:
30
75
 
31
76
  - `prepare_for_plan_change`
32
77
  - `prepare_for_new_subscription`
@@ -46,10 +91,12 @@ Between `prepare_for_*` and `finalize_*`, so far I've used `finalize_*` almost e
46
91
 
47
92
  ### Stripe Webhooks
48
93
 
49
- Support for Stripe Webhooks is still quite limited. To react to some of the webhooks that are currently handled, you can implement the following template methods of the `Subscription` class:
94
+ During the installation process, a path and API key will be printed out that you can configure in the Stripe account panel that will allow your application to receive information about recurring transactions as they happen. (These are useful for reacting to expired credit cards on file and revenue tracking.)
95
+
96
+ Support for Stripe Webhooks is still quite limited. You can implement the following template methods of the `Subscription`:
50
97
 
51
98
  - `payment_succeeded(amount)`
52
99
  - `charge_failed`
53
100
  - `charge_disputed`
54
101
 
55
- No additional information is currently handled from the webhook request body.
102
+ As mentioned before, be sure to call `super` in your implementations. No additional information from Stripe is currently handled from the webhook request body.
data/Rakefile CHANGED
@@ -1 +1,40 @@
1
- require "bundler/gem_tasks"
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'Koudoku'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+ APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
24
+ load 'rails/tasks/engine.rake'
25
+
26
+
27
+
28
+ Bundler::GemHelper.install_tasks
29
+
30
+ require 'rake/testtask'
31
+
32
+ Rake::TestTask.new(:test) do |t|
33
+ t.libs << 'lib'
34
+ t.libs << 'test'
35
+ t.pattern = 'test/**/*_test.rb'
36
+ t.verbose = false
37
+ end
38
+
39
+
40
+ task :default => :test
@@ -0,0 +1,15 @@
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 vendor/assets/javascripts of plugins, if any, 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
+ // the compiled file.
9
+ //
10
+ // WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
11
+ // GO AFTER THE REQUIRES BELOW.
12
+ //
13
+ //= require jquery
14
+ //= require jquery_ujs
15
+ //= require_tree .
@@ -0,0 +1,13 @@
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 vendor/assets/stylesheets of plugins, if any, 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 top of the
9
+ * compiled file, but it's generally better to create a new file per style scope.
10
+ *
11
+ *= require_self
12
+ *= require_tree .
13
+ */
@@ -0,0 +1,77 @@
1
+ ul.koudoku-pricing-table {
2
+
3
+ position: relative;
4
+ margin: 20px 0;
5
+ margin-left: 25px;
6
+
7
+
8
+ li.plan {
9
+
10
+ text-align: center;
11
+
12
+ background-color: #f5f5f5;
13
+ margin-top: 17px;
14
+
15
+ margin-left: 0;
16
+
17
+ position: relative;
18
+ z-index: 99;
19
+
20
+
21
+ &.plan-primary {
22
+ background-color: #f9f9f9;
23
+ margin-top: 0px;
24
+
25
+ margin-left: -5px;
26
+ margin-right: -5px;
27
+
28
+ z-index: 100;
29
+
30
+ h2 {
31
+ margin-top: 5px;
32
+ padding-bottom: 7px;
33
+ font-size: 2.5em;
34
+ }
35
+ a, input[type=submit] {
36
+ font-size: 2em;
37
+ font-size: 14pt;
38
+ }
39
+ ul.features {
40
+ margin-bottom: 12px;
41
+ li {
42
+ font-size: 12pt;
43
+ }
44
+ }
45
+ }
46
+
47
+ .call-to-action {
48
+ margin: 10px 0;
49
+ }
50
+
51
+ .features {
52
+ margin: 0;
53
+ li {
54
+ list-style: none;
55
+ padding: 8px;
56
+ line-height: 18px;
57
+ vertical-align: top;
58
+ border-top: 1px solid #dddddd;
59
+ }
60
+ li:first-child {
61
+ border-top: none;
62
+ }
63
+ }
64
+
65
+ }
66
+
67
+
68
+ .social-proof {
69
+ padding: 17px 0;
70
+ blockquote {
71
+ h2, h3, h4 {
72
+ padding: 5px 0;
73
+ }
74
+ }
75
+ }
76
+
77
+ }
@@ -0,0 +1,20 @@
1
+ module Koudoku::Plan
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+
6
+ end
7
+
8
+ module ClassMethods
9
+
10
+ end
11
+
12
+ module InstanceMethods
13
+
14
+ def is_upgrade_from?(plan)
15
+ self.price >= plan.price
16
+ end
17
+
18
+ end
19
+
20
+ end
@@ -3,12 +3,12 @@ module Koudoku::Subscription
3
3
 
4
4
  included do
5
5
 
6
- attr_accessible :plan_id, :stripe_id, :current_price
6
+ attr_accessible :plan_id, :stripe_id, :current_price, :credit_card_token, :card_type, :last_four
7
7
 
8
- # We don't store these one-time use tokens, but this is what Strie provides
8
+ # We don't store these one-time use tokens, but this is what Stripe provides
9
9
  # client-side after storing the credit card information.
10
10
  attr_accessor :credit_card_token
11
-
11
+
12
12
  belongs_to :plan
13
13
 
14
14
  # update details.
@@ -84,7 +84,7 @@ module Koudoku::Subscription
84
84
 
85
85
  # create a customer at that package level.
86
86
  customer = Stripe::Customer.create(customer_attributes)
87
-
87
+
88
88
  rescue Stripe::CardError => card_error
89
89
  errors[:base] << card_error.message
90
90
  card_was_declined
@@ -123,18 +123,38 @@ module Koudoku::Subscription
123
123
  end
124
124
 
125
125
  module InstanceMethods
126
+
127
+ def describe_difference(plan_to_describe)
128
+ if plan.nil?
129
+ if persisted?
130
+ "Upgrade"
131
+ else
132
+ if Koudoku.free_trial?
133
+ "Start Trial"
134
+ else
135
+ "Upgrade"
136
+ end
137
+ end
138
+ else
139
+ if plan_to_describe.is_upgrade_from?(plan)
140
+ "Upgrade"
141
+ else
142
+ "Downgrade"
143
+ end
144
+ end
145
+ end
126
146
 
127
147
  # Pretty sure this wouldn't conflict with anything someone would put in their model
128
148
  def subscription_owner
129
149
  # Return whatever we belong to.
130
150
  # If this object doesn't respond to 'name', please update owner_description.
131
- user
151
+ send Koudoku.subscriptions_owned_by
132
152
  end
133
153
 
134
154
  def subscription_owner_description
135
155
  # assuming owner responds to name.
136
156
  # we should check for whether it responds to this or not.
137
- "#{subscription_owner.name} (#{subscription_owner.id})"
157
+ "#{subscription_owner.id}"
138
158
  end
139
159
 
140
160
  def changing_plans?
@@ -0,0 +1,5 @@
1
+ module Koudoku
2
+ class ApplicationController < ActionController::Base
3
+ layout 'application'
4
+ end
5
+ end
@@ -0,0 +1,63 @@
1
+ module Koudoku
2
+ class SubscriptionsController < ApplicationController
3
+ before_filter :load_owner
4
+ before_filter :show_existing_subscription, only: [:index, :new, :create]
5
+ before_filter :load_subscription, only: [:show, :cancel, :edit, :update]
6
+ before_filter :load_plans, only: [:index, :edit]
7
+ def load_plans
8
+ @plans = ::Plan.order(:price)
9
+ end
10
+ def load_owner
11
+ # e.g. User.find(params[:user_id])
12
+ @owner = Koudoku.owner_class.find(params[:owner_id])
13
+ end
14
+ def load_subscription
15
+ @subscription = ::Subscription.find(params[:id])
16
+ end
17
+ def index
18
+ # we should also set the owner of the subscription here.
19
+ @subscription = ::Subscription.new({Koudoku.owner_id_sym => @owner.id})
20
+ # e.g. @subscription.user = @owner
21
+ @subscription.send Koudoku.owner_assignment_sym, @owner
22
+ end
23
+ def new
24
+ @subscription = ::Subscription.new
25
+ @subscription.plan = ::Plan.find(params[:plan])
26
+ end
27
+ def show_existing_subscription
28
+ if @owner.subscription.present?
29
+ redirect_to owner_subscription_path(@owner, @owner.subscription)
30
+ end
31
+ end
32
+ def create
33
+ @subscription = ::Subscription.new(params[:subscription])
34
+ @subscription.user = @owner
35
+ if @subscription.save
36
+ flash[:success] = "You've been successfully upgraded."
37
+ redirect_to owner_subscription_path(@owner, @subscription)
38
+ else
39
+ flash[:error] = 'There was a problem processing this transaction.'
40
+ render :new
41
+ end
42
+ end
43
+ def show
44
+ end
45
+ def cancel
46
+ flash[:success] = "You've successfully cancelled your subscription."
47
+ @subscription.plan_id = nil
48
+ @subscription.save
49
+ redirect_to owner_subscription_path(@owner, @subscription)
50
+ end
51
+ def edit
52
+ end
53
+ def update
54
+ if @subscription.update_attributes(params[:subscription])
55
+ flash[:success] = "You've been successfully upgraded."
56
+ redirect_to owner_subscription_path(@owner, @subscription)
57
+ else
58
+ flash[:error] = 'There was a problem processing this transaction.'
59
+ render :edit
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,38 @@
1
+ module Koudoku
2
+ class WebhooksController < ApplicationController
3
+
4
+ def create
5
+
6
+ raise "API key not configured. For security reasons you must configure this in 'config/koudoku.rb'." unless Koudoku.webhooks_api_key.present?
7
+ raise "Invalid API key. Be sure the webhooks URL Stripe is configured with includes ?api_key= and the correct key." unless params[:api_key] == Koudoku.webhooks_api_key
8
+
9
+ data_json = JSON.parse request.body.read
10
+
11
+ if data_json['type'] == "invoice.payment_succeeded"
12
+
13
+ stripe_id = data_json['data']['object']['customer']
14
+ amount = data_json['data']['object']['total'].to_f / 100.0
15
+ subscription = ::Subscription.find_by_stripe_id(stripe_id)
16
+ subscription.payment_succeeded(amount)
17
+
18
+ elsif data_json['type'] == "charge.failed"
19
+
20
+ stripe_id = data_json['data']['object']['customer']
21
+
22
+ subscription = ::Subscription.find_by_stripe_id(stripe_id)
23
+ subscription.charge_failed
24
+
25
+ elsif data_json['type'] == "charge.dispute.created"
26
+
27
+ stripe_id = data_json['data']['object']['customer']
28
+
29
+ subscription = ::Subscription.find_by_stripe_id(stripe_id)
30
+ listing = subscription.listing
31
+ subscription.charge_disputed
32
+
33
+ end
34
+
35
+ end
36
+
37
+ end
38
+ end