bank_teller 0.1.0
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/.gitignore +10 -0
- data/Gemfile +4 -0
- data/README.html +1065 -0
- data/README.md +156 -0
- data/Rakefile +2 -0
- data/bank_teller.gemspec +32 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/bank_teller/engine.rb +5 -0
- data/lib/bank_teller/version.rb +3 -0
- data/lib/bank_teller.rb +70 -0
- data/lib/billable.rb +204 -0
- data/lib/generators/bank_teller/install_generator.rb +31 -0
- data/lib/generators/bank_teller/templates/add_bank_teller_fields_to_users.rb +30 -0
- data/lib/generators/bank_teller/templates/create_subscriptions.rb +16 -0
- data/lib/invoice.rb +115 -0
- data/lib/invoice_item.rb +38 -0
- data/lib/subscription.rb +118 -0
- data/lib/subscription_builder.rb +79 -0
- metadata +106 -0
data/lib/invoice_item.rb
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
class InvoiceItem
|
|
2
|
+
def initialize(user, item)
|
|
3
|
+
@user = user
|
|
4
|
+
@item = item
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
def total
|
|
8
|
+
format_amount(amount)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def start_date
|
|
12
|
+
if is_subscription?
|
|
13
|
+
item.period.start
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def end_date
|
|
18
|
+
if is_subscription?
|
|
19
|
+
item.period.end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def is_subscription?
|
|
24
|
+
item.type === 'subscription'
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def as_stripe_invoice_item
|
|
28
|
+
item
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
protected
|
|
32
|
+
|
|
33
|
+
attr_accessor :user, :item
|
|
34
|
+
|
|
35
|
+
def format_amount(amount)
|
|
36
|
+
BankTeller::format_amount(amount)
|
|
37
|
+
end
|
|
38
|
+
end
|
data/lib/subscription.rb
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
class Subscription < ActiveRecord::Base
|
|
2
|
+
belongs_to :user
|
|
3
|
+
|
|
4
|
+
def valid
|
|
5
|
+
active? || on_trial? || on_grace_period?
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def active?
|
|
9
|
+
ends_at.nil? || on_grace_period?
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def cancelled?
|
|
13
|
+
!ends_at.nil?
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def on_trial?
|
|
17
|
+
if trial_ends_at.nil?
|
|
18
|
+
false
|
|
19
|
+
else
|
|
20
|
+
DateTime.now < trial_ends_at.to_datetime
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def on_grace_period?
|
|
25
|
+
if ends_at.nil?
|
|
26
|
+
return false
|
|
27
|
+
else
|
|
28
|
+
DateTime.now < ends_at.to_datetime
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def increment_quantity(count = 1)
|
|
33
|
+
update_quantity(quantity + count)
|
|
34
|
+
self
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def increment_and_invoice(count = 1)
|
|
38
|
+
increment_quantity(count)
|
|
39
|
+
user.invoice
|
|
40
|
+
self
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def decrement_quantity(count = 1)
|
|
44
|
+
quantity = [1, (self.quantity - count)].max
|
|
45
|
+
update_quantity(quantity)
|
|
46
|
+
self
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def update_quantity(quantity)
|
|
50
|
+
subscription = stripe_subscription
|
|
51
|
+
subscription.quantity = quantity
|
|
52
|
+
subscription.save
|
|
53
|
+
self.quantity = quantity
|
|
54
|
+
self.save
|
|
55
|
+
self
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def swap(plan, *args)
|
|
59
|
+
subscription = stripe_subscription
|
|
60
|
+
subscription.plan = plan
|
|
61
|
+
subscription.prorate = false
|
|
62
|
+
additional_options = args[0]
|
|
63
|
+
|
|
64
|
+
if additional_options
|
|
65
|
+
subscription.prorate = additional_options[:prorate] || false
|
|
66
|
+
anchor = additional_options[:billing_cycle_anchor] || nil
|
|
67
|
+
subscription.billing_cycle_anchor = anchor if anchor
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
if on_trial?
|
|
71
|
+
subscription.trial_end = trial_ends_at.to_time
|
|
72
|
+
else
|
|
73
|
+
subscription.trial_end = 'now'
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
if quantity
|
|
77
|
+
subscription.quantity = quantity
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
subscription.save
|
|
81
|
+
user.invoice
|
|
82
|
+
|
|
83
|
+
self.stripe_plan = plan
|
|
84
|
+
self.ends_at = nil
|
|
85
|
+
self.save
|
|
86
|
+
self
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def cancel
|
|
90
|
+
subscription = stripe_subscription
|
|
91
|
+
subscription.delete(at_period_end: true)
|
|
92
|
+
|
|
93
|
+
if on_trial?
|
|
94
|
+
self.ends_at = trial_ends_at
|
|
95
|
+
else
|
|
96
|
+
self.ends_at = current_period_end.to_time
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
self.save
|
|
100
|
+
self
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def cancel_now
|
|
104
|
+
subscription = stripe_subscription
|
|
105
|
+
subscription.delete
|
|
106
|
+
mark_as_cancelled
|
|
107
|
+
self
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def mark_as_cancelled
|
|
111
|
+
self.ends_at = DateTime.now
|
|
112
|
+
self.save
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def stripe_subscription
|
|
116
|
+
user.as_stripe_customer.subscriptions.retrieve(stripe_id)
|
|
117
|
+
end
|
|
118
|
+
end
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
class SubscriptionBuilder
|
|
2
|
+
def initialize(user, name, plan, *args)
|
|
3
|
+
@user = user
|
|
4
|
+
@name = name
|
|
5
|
+
@plan = plan
|
|
6
|
+
@trial_days = args[0][:trial_days] || 0
|
|
7
|
+
@quantity = args[0][:quantity] || 1
|
|
8
|
+
@skip_trial = args[0][:skip_trial] || false
|
|
9
|
+
@coupon = args[0][:coupon]
|
|
10
|
+
@metadata = args[0][:metadata]
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def add(options = {})
|
|
14
|
+
create(nil, options)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def create(token = nil, options = {})
|
|
18
|
+
customer = get_stripe_customer(token, options)
|
|
19
|
+
stripe_subscription = customer.subscriptions.create(pay_load)
|
|
20
|
+
|
|
21
|
+
if skip_trial
|
|
22
|
+
trial_ends_at = nil
|
|
23
|
+
else
|
|
24
|
+
trial_ends_at = trial_days ? trial_days.days.from_now.to_time.to_i : nil
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
user.subscriptions.create do |subscription|
|
|
28
|
+
subscription.name = name
|
|
29
|
+
subscription.stripe_id = stripe_subscription.id
|
|
30
|
+
subscription.stripe_plan = plan
|
|
31
|
+
subscription.quantity = quantity
|
|
32
|
+
subscription.trial_ends_at = trial_ends_at
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
protected
|
|
37
|
+
|
|
38
|
+
attr_accessor :user
|
|
39
|
+
attr_accessor :name
|
|
40
|
+
attr_accessor :plan
|
|
41
|
+
attr_accessor :quantity
|
|
42
|
+
attr_accessor :trial_days
|
|
43
|
+
attr_accessor :skip_trial
|
|
44
|
+
attr_accessor :coupon
|
|
45
|
+
attr_accessor :metadata
|
|
46
|
+
|
|
47
|
+
def get_stripe_customer(token = nil, options = {})
|
|
48
|
+
if !user.stripe_id.nil?
|
|
49
|
+
customer = user.as_stripe_customer
|
|
50
|
+
user.update_card(token) if token
|
|
51
|
+
else
|
|
52
|
+
options = options.merge!({ coupon: coupon })
|
|
53
|
+
customer = user.create_as_stripe_customer(token, options)
|
|
54
|
+
end
|
|
55
|
+
customer
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def pay_load
|
|
59
|
+
{
|
|
60
|
+
plan: plan,
|
|
61
|
+
quantity: quantity,
|
|
62
|
+
trial_end: get_trial_end_for_pay_load,
|
|
63
|
+
tax_percent: get_tax_percent_for_pay_load,
|
|
64
|
+
metadata: metadata
|
|
65
|
+
}
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def get_trial_end_for_pay_load
|
|
69
|
+
if skip_trial
|
|
70
|
+
'now'
|
|
71
|
+
else
|
|
72
|
+
trial_days.days.from_now.to_time.to_i if trial_days
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def get_tax_percent_for_pay_load
|
|
77
|
+
user.try(:tax_percentage)
|
|
78
|
+
end
|
|
79
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: bank_teller
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Jason Charnes
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2016-05-06 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: bundler
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '1.11'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '1.11'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: rake
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '10.0'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '10.0'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: stripe
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: 1.42.0
|
|
48
|
+
type: :runtime
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: 1.42.0
|
|
55
|
+
description: A subscription billing interface modeled after Laravel Cashier
|
|
56
|
+
email:
|
|
57
|
+
- jason@jasoncharnes.com
|
|
58
|
+
executables: []
|
|
59
|
+
extensions: []
|
|
60
|
+
extra_rdoc_files: []
|
|
61
|
+
files:
|
|
62
|
+
- ".gitignore"
|
|
63
|
+
- Gemfile
|
|
64
|
+
- README.html
|
|
65
|
+
- README.md
|
|
66
|
+
- Rakefile
|
|
67
|
+
- bank_teller.gemspec
|
|
68
|
+
- bin/console
|
|
69
|
+
- bin/setup
|
|
70
|
+
- lib/bank_teller.rb
|
|
71
|
+
- lib/bank_teller/engine.rb
|
|
72
|
+
- lib/bank_teller/version.rb
|
|
73
|
+
- lib/billable.rb
|
|
74
|
+
- lib/generators/bank_teller/install_generator.rb
|
|
75
|
+
- lib/generators/bank_teller/templates/add_bank_teller_fields_to_users.rb
|
|
76
|
+
- lib/generators/bank_teller/templates/create_subscriptions.rb
|
|
77
|
+
- lib/invoice.rb
|
|
78
|
+
- lib/invoice_item.rb
|
|
79
|
+
- lib/subscription.rb
|
|
80
|
+
- lib/subscription_builder.rb
|
|
81
|
+
homepage: http://www.github.com/jasoncharnes/bank_teller
|
|
82
|
+
licenses: []
|
|
83
|
+
metadata:
|
|
84
|
+
allowed_push_host: https://rubygems.org
|
|
85
|
+
post_install_message:
|
|
86
|
+
rdoc_options: []
|
|
87
|
+
require_paths:
|
|
88
|
+
- lib
|
|
89
|
+
- app
|
|
90
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
91
|
+
requirements:
|
|
92
|
+
- - ">="
|
|
93
|
+
- !ruby/object:Gem::Version
|
|
94
|
+
version: '0'
|
|
95
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
96
|
+
requirements:
|
|
97
|
+
- - ">="
|
|
98
|
+
- !ruby/object:Gem::Version
|
|
99
|
+
version: '0'
|
|
100
|
+
requirements: []
|
|
101
|
+
rubyforge_project:
|
|
102
|
+
rubygems_version: 2.4.5.1
|
|
103
|
+
signing_key:
|
|
104
|
+
specification_version: 4
|
|
105
|
+
summary: A subscription billing interface modeled after Laravel Cashier
|
|
106
|
+
test_files: []
|