tellimus 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +152 -0
- data/Rakefile +10 -0
- data/app/assets/javascripts/tellimus/application.js +13 -0
- data/app/assets/stylesheets/tellimus/application.css +13 -0
- data/app/assets/stylesheets/tellimus/pricing-table.scss +78 -0
- data/app/concerns/tellimus/plan.rb +8 -0
- data/app/concerns/tellimus/subscription.rb +275 -0
- data/app/controllers/tellimus/application_controller.rb +6 -0
- data/app/controllers/tellimus/subscriptions_controller.rb +189 -0
- data/app/helpers/tellimus/application_helper.rb +20 -0
- data/app/views/layouts/tellimus/application.html.erb +13 -0
- data/app/views/tellimus/subscriptions/_braintree_js.html.erb +8 -0
- data/app/views/tellimus/subscriptions/_card.html.erb +26 -0
- data/app/views/tellimus/subscriptions/_pricing_table.html.erb +40 -0
- data/app/views/tellimus/subscriptions/_social_proof.html.erb +11 -0
- data/app/views/tellimus/subscriptions/edit.html.erb +7 -0
- data/app/views/tellimus/subscriptions/index.html.erb +2 -0
- data/app/views/tellimus/subscriptions/new.html.erb +1 -0
- data/app/views/tellimus/subscriptions/show.html.erb +15 -0
- data/app/views/tellimus/subscriptions/unauthorized.html.erb +1 -0
- data/config/environment.rb +0 -0
- data/config/locales/en.yml +58 -0
- data/config/routes.rb +10 -0
- data/lib/generators/tellimus/install_generator.rb +66 -0
- data/lib/generators/tellimus/templates/app/models/plan.rb +6 -0
- data/lib/generators/tellimus/templates/app/models/subscription.rb +7 -0
- data/lib/generators/tellimus/templates/config/initializers/tellimus.rb +14 -0
- data/lib/generators/tellimus/views_generator.rb +31 -0
- data/lib/tasks/tellimus_tasks.rake +12 -0
- data/lib/tellimus/engine.rb +20 -0
- data/lib/tellimus/errors.rb +4 -0
- data/lib/tellimus/version.rb +3 -0
- data/lib/tellimus.rb +83 -0
- data/spec/concerns/tellimus/plan_spec.rb +39 -0
- data/spec/concerns/tellimus/subscription_spec.rb +0 -0
- data/spec/controllers/tellimus/subscriptions_controller.rb +21 -0
- data/spec/dummy/README.rdoc +261 -0
- data/spec/dummy/Rakefile +7 -0
- data/spec/dummy/app/assets/javascripts/application.js +15 -0
- data/spec/dummy/app/assets/stylesheets/application.css +13 -0
- data/spec/dummy/app/controllers/application_controller.rb +3 -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 +6 -0
- data/spec/dummy/app/models/user.rb +5 -0
- data/spec/dummy/app/views/layouts/application.html.erb +14 -0
- data/spec/dummy/app/views/tellimus/subscriptions/_social_proof.html.erb +11 -0
- data/spec/dummy/config/application.rb +55 -0
- data/spec/dummy/config/boot.rb +10 -0
- data/spec/dummy/config/database.yml +25 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +32 -0
- data/spec/dummy/config/environments/production.rb +69 -0
- data/spec/dummy/config/environments/test.rb +36 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/devise.rb +0 -0
- data/spec/dummy/config/initializers/inflections.rb +15 -0
- data/spec/dummy/config/initializers/mime_types.rb +5 -0
- data/spec/dummy/config/initializers/secret_token.rb +7 -0
- data/spec/dummy/config/initializers/session_store.rb +8 -0
- data/spec/dummy/config/initializers/tellimus.rb +14 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/config/locales/devise.en.yml +59 -0
- data/spec/dummy/config/locales/en.yml +5 -0
- data/spec/dummy/config/routes.rb +6 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/db/development.sqlite3 +0 -0
- data/spec/dummy/db/migrate/20130318201927_create_customers.rb +8 -0
- data/spec/dummy/db/migrate/20130318204455_create_subscriptions.rb +15 -0
- data/spec/dummy/db/migrate/20130318204458_create_plans.rb +14 -0
- data/spec/dummy/db/migrate/20130520163946_add_interval_to_plan.rb +5 -0
- data/spec/dummy/db/schema.rb +44 -0
- data/spec/dummy/db/test.sqlite3 +0 -0
- data/spec/dummy/log/development.log +59 -0
- data/spec/dummy/log/test.log +48 -0
- data/spec/dummy/public/404.html +26 -0
- data/spec/dummy/public/422.html +26 -0
- data/spec/dummy/public/500.html +25 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/script/rails +6 -0
- data/spec/dummy/test/fixtures/customers.yml +7 -0
- data/spec/dummy/test/unit/customer_test.rb +7 -0
- data/spec/dummy/tmp/development_secret.txt +1 -0
- data/spec/spec_helper.rb +30 -0
- metadata +306 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 1b9357bda0c37119e06e966a8c4c646ce8657fdc9b4d2fd3ecc6315b5ecbbfca
|
|
4
|
+
data.tar.gz: e7999a21034571119aaf8b4f4ed324f6d0d18f008edfc515fe81629ac12a13a5
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 3381634d0f210a10b1095cb0211bc6b25dbdaf5f1122b30540aca77b292a7f6bf01de4d0e4ff5eced5cf9b4b0f99395330b3c249e82034e7a024dd730969e79d
|
|
7
|
+
data.tar.gz: c2ec10112720a00d9b411103cac24a9e9b9d174be2a6b6109487336e8122bd7b049bee0ee1f424facd7bdef8f580a7c499d831eddcf7caa1edef852e1ac71757
|
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
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
# Tellimus
|
|
2
|
+
|
|
3
|
+
Robust subscription support for Ruby on Rails apps using [Braintree](https://braintreepayments.com), including out-of-the-box pricing pages, payment pages, and subscription management for your customers. This is a fork of [Koudoku](https://github.com/andrewculver/koudoku) specifically for Braintree.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Include the following in your `Gemfile`:
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
gem 'tellimus'
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
After running `bundle install`, you can run a Rails generator to do the rest. You should have a user model.
|
|
14
|
+
|
|
15
|
+
```ruby
|
|
16
|
+
rails g tellimus:install
|
|
17
|
+
rake db:migrate
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Add the following to `app/views/layouts/application.html.erb` before your `<head>` tag closes:
|
|
21
|
+
|
|
22
|
+
```ruby
|
|
23
|
+
<script src="https://js.braintreegateway.com/js/braintree-2.32.1.min.js"></script>
|
|
24
|
+
<%= yield :tellimus %>
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
(This allows us to inject a Braintree `<script>` tag in the correct place. If you don't, the payment form will not work.)
|
|
28
|
+
|
|
29
|
+
After installing, you'll need to add some subscription plans. (You can see an explanation of each of the attributes in the table below.)
|
|
30
|
+
|
|
31
|
+
**Note:** You need to create the plans in your [Braintree Dashboard](https://braintreepayments.com) separately.
|
|
32
|
+
|
|
33
|
+
```ruby
|
|
34
|
+
Plan.create({
|
|
35
|
+
name: 'Personal',
|
|
36
|
+
price: 10.00,
|
|
37
|
+
interval: 'month',
|
|
38
|
+
braintree_id: '1',
|
|
39
|
+
features: ['1 Project', '1 Page', '1 User', '1 Organization'].join("\n\n"),
|
|
40
|
+
display_order: 1
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
Plan.create({
|
|
44
|
+
name: 'Team',
|
|
45
|
+
highlight: true, # This highlights the plan on the pricing page.
|
|
46
|
+
price: 30.00,
|
|
47
|
+
interval: 'month',
|
|
48
|
+
braintree_id: '2',
|
|
49
|
+
features: ['3 Projects', '3 Pages', '3 Users', '3 Organizations'].join("\n\n"),
|
|
50
|
+
display_order: 2
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
Plan.create({
|
|
54
|
+
name: 'Enterprise',
|
|
55
|
+
price: 100.00,
|
|
56
|
+
interval: 'month',
|
|
57
|
+
braintree_id: '3',
|
|
58
|
+
features: ['10 Projects', '10 Pages', '10 Users', '10 Organizations'].join("\n\n"),
|
|
59
|
+
display_order: 3
|
|
60
|
+
})
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
To help you understand the attributes:
|
|
64
|
+
|
|
65
|
+
| Attribute | Type | Function |
|
|
66
|
+
| --------------- | ------- | -------- |
|
|
67
|
+
| `name` | string | Name for the plan to be presented to customers. |
|
|
68
|
+
| `price` | float | Price per billing cycle. |
|
|
69
|
+
| `interval` | string | *Optional.* What is the billing cycle? Valid options are `month`, `year`, `week`, `3-month`, `6-month`. Defaults to `month`. |
|
|
70
|
+
| `braintree_id` | string | The Plan ID in Braintree. |
|
|
71
|
+
| `features` | string | A list of features. Supports Markdown syntax. |
|
|
72
|
+
| `display_order` | integer | Order in which to display plans. |
|
|
73
|
+
| `highlight` | boolean | *Optional.* Whether to highlight the plan on the pricing page. |
|
|
74
|
+
|
|
75
|
+
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.
|
|
76
|
+
|
|
77
|
+
### Configuring Braintree API Keys
|
|
78
|
+
|
|
79
|
+
You can supply your publishable and secret API keys in `config/initializers/tellimus.rb`. However, by default it will use the `BRAINTREE_PUBLISHABLE_KEY` and `BRAINTREE_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.
|
|
80
|
+
|
|
81
|
+
In a bash shell, you can set them in `~/.bash_profile` like so:
|
|
82
|
+
|
|
83
|
+
```bash
|
|
84
|
+
export BRAINTREE_PUBLIC_KEY=pk_0CJwDH9sdh98f79FDHDOjdiOxQob0
|
|
85
|
+
export BRAINTREE_PRIVATE_KEY=sk_0CJwFDIUshdfh97JDJOjZ5OIDjOCH
|
|
86
|
+
export BRAINTREE_MERCHANT_ID=2dj90d2j3dhnxn2
|
|
87
|
+
export BRAINTREE_ENVIRONMENT="sandbox"
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
(Reload your terminal for these settings to take effect.)
|
|
91
|
+
|
|
92
|
+
## User-Facing Subscription Management
|
|
93
|
+
|
|
94
|
+
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:
|
|
95
|
+
|
|
96
|
+
```ruby
|
|
97
|
+
<%= link_to 'Pricing', main_app.pricing_path %>
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
(Note: Tellimus 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 Tellimus engine instead of your application.)
|
|
101
|
+
|
|
102
|
+
Existing users can view available plans, select a plan, enter credit card details, review their subscription, change plans, and cancel at the following route:
|
|
103
|
+
|
|
104
|
+
```ruby
|
|
105
|
+
tellimus.owner_subscriptions_path(@user)
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
In these paths, `owner` refers to `User` by default, or whatever model has been configured to be the owner of the `Subscription` model.
|
|
109
|
+
|
|
110
|
+
A number of views are provided by default. To customize the views, use the following generator:
|
|
111
|
+
|
|
112
|
+
```ruby
|
|
113
|
+
rails g tellimus:views
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
### Pricing Table
|
|
117
|
+
|
|
118
|
+
Tellimus 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`:
|
|
119
|
+
|
|
120
|
+
```css
|
|
121
|
+
*= require 'tellimus/pricing-table'
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
Or, if you've replaced your `application.css` with an `application.scss` (like I always do):
|
|
125
|
+
|
|
126
|
+
```css
|
|
127
|
+
@import "tellimus/pricing-table"
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## Implementing Logging, Notifications, etc.
|
|
131
|
+
|
|
132
|
+
The included module defines the following empty "template methods" which you're able to provide an implementation for in `Subscription`:
|
|
133
|
+
|
|
134
|
+
- `prepare_for_plan_change`
|
|
135
|
+
- `prepare_for_new_subscription`
|
|
136
|
+
- `prepare_for_upgrade`
|
|
137
|
+
- `prepare_for_downgrade`
|
|
138
|
+
- `prepare_for_cancelation`
|
|
139
|
+
- `prepare_for_card_update`
|
|
140
|
+
- `finalize_plan_change!`
|
|
141
|
+
- `finalize_new_subscription!`
|
|
142
|
+
- `finalize_upgrade!`
|
|
143
|
+
- `finalize_downgrade!`
|
|
144
|
+
- `finalize_cancelation!`
|
|
145
|
+
- `finalize_card_update!`
|
|
146
|
+
- `card_was_declined`
|
|
147
|
+
|
|
148
|
+
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.
|
|
149
|
+
|
|
150
|
+
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.
|
|
151
|
+
|
|
152
|
+
```
|
data/Rakefile
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
#!/usr/bin/env rake
|
|
2
|
+
require "bundler/gem_tasks"
|
|
3
|
+
|
|
4
|
+
APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
|
|
5
|
+
load 'rails/tasks/engine.rake'
|
|
6
|
+
|
|
7
|
+
require 'rspec/core/rake_task'
|
|
8
|
+
RSpec::Core::RakeTask.new(spec: 'app:db:test:prepare')
|
|
9
|
+
|
|
10
|
+
task default: :spec
|
|
@@ -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 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_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,78 @@
|
|
|
1
|
+
ul.tellimus-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
|
+
&.plan-primary {
|
|
21
|
+
background-color: #f9f9f9;
|
|
22
|
+
margin-top: 0px;
|
|
23
|
+
|
|
24
|
+
margin-left: -5px;
|
|
25
|
+
margin-right: -5px;
|
|
26
|
+
|
|
27
|
+
z-index: 100;
|
|
28
|
+
|
|
29
|
+
h3 {
|
|
30
|
+
font-size: 32.5px;
|
|
31
|
+
line-height: 51px;
|
|
32
|
+
}
|
|
33
|
+
h4 {
|
|
34
|
+
line-height: 22px;
|
|
35
|
+
}
|
|
36
|
+
a, input[type=submit] {
|
|
37
|
+
font-size: 21.5px;
|
|
38
|
+
line-height: 26px;
|
|
39
|
+
}
|
|
40
|
+
ul.features {
|
|
41
|
+
margin-bottom: 12px;
|
|
42
|
+
li {
|
|
43
|
+
font-size: 12pt;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
.call-to-action {
|
|
49
|
+
margin: 10px 0;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
.features {
|
|
53
|
+
margin: 0;
|
|
54
|
+
li {
|
|
55
|
+
list-style: none;
|
|
56
|
+
padding: 8px;
|
|
57
|
+
line-height: 18px;
|
|
58
|
+
vertical-align: top;
|
|
59
|
+
border-top: 1px solid #dddddd;
|
|
60
|
+
}
|
|
61
|
+
li:first-child {
|
|
62
|
+
border-top: none;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
.social-proof {
|
|
70
|
+
padding: 17px 0;
|
|
71
|
+
blockquote {
|
|
72
|
+
h2, h3, h4 {
|
|
73
|
+
padding: 5px 0;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
}
|
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
module Tellimus::Subscription
|
|
2
|
+
extend ActiveSupport::Concern
|
|
3
|
+
|
|
4
|
+
included do
|
|
5
|
+
|
|
6
|
+
# We don't store these one-time use tokens, but this is what Braintree provides
|
|
7
|
+
# client-side after storing the credit card information.
|
|
8
|
+
attr_accessor :payment_method_nonce
|
|
9
|
+
|
|
10
|
+
belongs_to :plan, optional: true
|
|
11
|
+
|
|
12
|
+
# update details.
|
|
13
|
+
before_save :processing!
|
|
14
|
+
def processing!
|
|
15
|
+
# if their package level has changed ..
|
|
16
|
+
if changing_plans?
|
|
17
|
+
|
|
18
|
+
prepare_for_plan_change
|
|
19
|
+
|
|
20
|
+
# and a subscription exists in stripe ..
|
|
21
|
+
if braintree_id.present?
|
|
22
|
+
|
|
23
|
+
# if a new plan has been selected
|
|
24
|
+
if self.plan.present?
|
|
25
|
+
|
|
26
|
+
# Record the new plan pricing.
|
|
27
|
+
self.current_price = self.plan.price
|
|
28
|
+
|
|
29
|
+
prepare_for_downgrade if downgrading?
|
|
30
|
+
prepare_for_upgrade if upgrading?
|
|
31
|
+
|
|
32
|
+
# update the package level with braintree.
|
|
33
|
+
Tellimus.gateway.subscription.update(
|
|
34
|
+
self.braintree_id,
|
|
35
|
+
plan_id: self.plan.braintree_id
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
finalize_downgrade! if downgrading?
|
|
39
|
+
finalize_upgrade! if upgrading?
|
|
40
|
+
|
|
41
|
+
# if no plan has been selected.
|
|
42
|
+
else
|
|
43
|
+
|
|
44
|
+
prepare_for_cancelation
|
|
45
|
+
|
|
46
|
+
# Remove the current pricing.
|
|
47
|
+
self.current_price = nil
|
|
48
|
+
|
|
49
|
+
# delete the subscription.
|
|
50
|
+
Tellimus.gateway.subscription.cancel(self.braintree_id)
|
|
51
|
+
|
|
52
|
+
finalize_cancelation!
|
|
53
|
+
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# when customer DOES NOT exist in braintree ..
|
|
57
|
+
else
|
|
58
|
+
|
|
59
|
+
# if a new plan has been selected
|
|
60
|
+
if self.plan.present?
|
|
61
|
+
|
|
62
|
+
# Record the new plan pricing.
|
|
63
|
+
self.current_price = self.plan.price
|
|
64
|
+
|
|
65
|
+
prepare_for_new_subscription
|
|
66
|
+
prepare_for_upgrade
|
|
67
|
+
begin
|
|
68
|
+
raise Tellimus::NilCardToken, "No card token received. Check for JavaScript errors breaking Braintree.js on the previous page." unless payment_method_nonce.present?
|
|
69
|
+
customer_attributes = {
|
|
70
|
+
email: subscription_owner_email,
|
|
71
|
+
payment_method_nonce: payment_method_nonce
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
# create a customer at that package level.
|
|
75
|
+
result = Tellimus.gateway.customer.create(customer_attributes)
|
|
76
|
+
unless result.success?
|
|
77
|
+
errors[:base] << result.errors
|
|
78
|
+
card_was_declined
|
|
79
|
+
return false
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
finalize_new_customer!(result.customer.id, plan.price)
|
|
83
|
+
payment_method = result.customer.payment_methods.last
|
|
84
|
+
|
|
85
|
+
subscription_result = Tellimus.gateway.subscription.create(
|
|
86
|
+
payment_method_token: payment_method.token,
|
|
87
|
+
plan_id: self.plan.braintree_id
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
unless subscription_result.success?
|
|
91
|
+
errors[:base] << subscription.errors
|
|
92
|
+
card_was_declined
|
|
93
|
+
return false
|
|
94
|
+
end
|
|
95
|
+
# store the customer id.
|
|
96
|
+
self.braintree_customer_id = result.customer.id
|
|
97
|
+
self.braintree_id = subscription_result.subscription.id
|
|
98
|
+
|
|
99
|
+
if payment_method.class == Braintree::PayPalAccount
|
|
100
|
+
self.payment_signature = payment_method.email
|
|
101
|
+
self.payment_type = "Paypal"
|
|
102
|
+
else
|
|
103
|
+
self.payment_signature = payment_method.last_4
|
|
104
|
+
self.payment_type = payment_method.card_type
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
finalize_new_subscription!
|
|
108
|
+
finalize_upgrade!
|
|
109
|
+
rescue StandardError => e
|
|
110
|
+
errors[:base] << e
|
|
111
|
+
return false
|
|
112
|
+
end
|
|
113
|
+
else
|
|
114
|
+
|
|
115
|
+
# This should never happen.
|
|
116
|
+
|
|
117
|
+
self.plan_id = nil
|
|
118
|
+
|
|
119
|
+
# Remove any plan pricing.
|
|
120
|
+
self.current_price = nil
|
|
121
|
+
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
finalize_plan_change!
|
|
127
|
+
|
|
128
|
+
# if they're updating their credit card details.
|
|
129
|
+
elsif self.payment_method_nonce.present?
|
|
130
|
+
|
|
131
|
+
prepare_for_card_update
|
|
132
|
+
|
|
133
|
+
# fetch the customer.
|
|
134
|
+
customer = Tellimus.gateway.customer.find(self.braintree_customer_id)
|
|
135
|
+
payment_method_response = Tellimus.gateway.payment_method.create(
|
|
136
|
+
:customer_id => self.braintree_customer_id,
|
|
137
|
+
:payment_method_nonce => self.payment_method_nonce
|
|
138
|
+
)
|
|
139
|
+
subscription_response = Tellimus.gateway.subscription.update(
|
|
140
|
+
self.braintree_id,
|
|
141
|
+
payment_method_token: payment_method_response.payment_method.token,
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
unless subscription_response.success?
|
|
145
|
+
errors[:base] << subscription_response.errors
|
|
146
|
+
card_was_declined
|
|
147
|
+
return false
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
payment_method_token = subscription_response.subscription.payment_method_token
|
|
151
|
+
payment_method = Tellimus.gateway.payment_method.find(payment_method_token)
|
|
152
|
+
if payment_method.class == Braintree::PayPalAccount
|
|
153
|
+
self.payment_signature = payment_method.email
|
|
154
|
+
self.payment_type = "Paypal"
|
|
155
|
+
else
|
|
156
|
+
self.payment_signature = payment_method.last_4
|
|
157
|
+
self.payment_type = payment_method.card_type
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
finalize_card_update!
|
|
162
|
+
|
|
163
|
+
end
|
|
164
|
+
end
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def describe_difference(plan_to_describe)
|
|
169
|
+
if plan.nil?
|
|
170
|
+
if persisted?
|
|
171
|
+
I18n.t('tellimus.plan_difference.upgrade')
|
|
172
|
+
else
|
|
173
|
+
if Tellimus.free_trial?
|
|
174
|
+
I18n.t('tellimus.plan_difference.start_trial')
|
|
175
|
+
else
|
|
176
|
+
I18n.t('tellimus.plan_difference.upgrade')
|
|
177
|
+
end
|
|
178
|
+
end
|
|
179
|
+
else
|
|
180
|
+
if plan_to_describe.is_upgrade_from?(plan)
|
|
181
|
+
I18n.t('tellimus.plan_difference.upgrade')
|
|
182
|
+
else
|
|
183
|
+
I18n.t('tellimus.plan_difference.downgrade')
|
|
184
|
+
end
|
|
185
|
+
end
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
# Pretty sure this wouldn't conflict with anything someone would put in their model
|
|
189
|
+
def subscription_owner
|
|
190
|
+
# Return whatever we belong to.
|
|
191
|
+
# If this object doesn't respond to 'name', please update owner_description.
|
|
192
|
+
send Tellimus.subscriptions_owned_by
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
def subscription_owner=(owner)
|
|
196
|
+
# e.g. @subscription.user = @owner
|
|
197
|
+
send Tellimus.owner_assignment_sym, owner
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
def subscription_owner_description
|
|
201
|
+
# assuming owner responds to name.
|
|
202
|
+
# we should check for whether it responds to this or not.
|
|
203
|
+
"#{subscription_owner.try(:name) || subscription_owner.try(:id)}"
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
def subscription_owner_email
|
|
207
|
+
"#{subscription_owner.try(:email)}"
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
def changing_plans?
|
|
211
|
+
plan_id_changed?
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
def downgrading?
|
|
215
|
+
plan.present? and plan_id_was.present? and plan_id_was > self.plan_id
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
def upgrading?
|
|
219
|
+
(plan_id_was.present? and plan_id_was < plan_id) or plan_id_was.nil?
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
# Template methods.
|
|
223
|
+
def prepare_for_plan_change
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
def prepare_for_new_subscription
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
def prepare_for_upgrade
|
|
230
|
+
end
|
|
231
|
+
|
|
232
|
+
def prepare_for_downgrade
|
|
233
|
+
end
|
|
234
|
+
|
|
235
|
+
def prepare_for_cancelation
|
|
236
|
+
end
|
|
237
|
+
|
|
238
|
+
def prepare_for_card_update
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
def finalize_plan_change!
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
def finalize_new_subscription!
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
def finalize_new_customer!(customer_id, amount)
|
|
248
|
+
end
|
|
249
|
+
|
|
250
|
+
def finalize_upgrade!
|
|
251
|
+
end
|
|
252
|
+
|
|
253
|
+
def finalize_downgrade!
|
|
254
|
+
end
|
|
255
|
+
|
|
256
|
+
def finalize_cancelation!
|
|
257
|
+
end
|
|
258
|
+
|
|
259
|
+
def finalize_card_update!
|
|
260
|
+
end
|
|
261
|
+
|
|
262
|
+
def card_was_declined
|
|
263
|
+
end
|
|
264
|
+
|
|
265
|
+
# stripe web-hook callbacks.
|
|
266
|
+
def payment_succeeded(amount)
|
|
267
|
+
end
|
|
268
|
+
|
|
269
|
+
def charge_failed
|
|
270
|
+
end
|
|
271
|
+
|
|
272
|
+
def charge_disputed
|
|
273
|
+
end
|
|
274
|
+
|
|
275
|
+
end
|