billing_logic 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (30) hide show
  1. checksums.yaml +7 -0
  2. data/README.rdoc +89 -0
  3. data/config/cruise/run_cruise +10 -0
  4. data/features/change_periodicity_on_anniversary_day.feature +34 -0
  5. data/features/change_periodicity_on_signup_day.feature +34 -0
  6. data/features/independent_payment_strategy.feature +93 -0
  7. data/features/same_day_cancellation_policy.feature +57 -0
  8. data/features/step_definitions/cancellation_steps.rb +52 -0
  9. data/features/step_definitions/implementation_specific_steps.rb +41 -0
  10. data/features/step_definitions/independent_payment_strategy_steps.rb +4 -0
  11. data/features/step_definitions/steps.rb +15 -0
  12. data/features/support/env.rb +13 -0
  13. data/features/support/helpers.rb +73 -0
  14. data/features/support/parameter_types.rb +42 -0
  15. data/lib/billing_logic/billing_cycle.rb +77 -0
  16. data/lib/billing_logic/command_builders/command_builders.rb +179 -0
  17. data/lib/billing_logic/current_state.rb +18 -0
  18. data/lib/billing_logic/current_state_mixin.rb +32 -0
  19. data/lib/billing_logic/payment_command_builder.rb +54 -0
  20. data/lib/billing_logic/proration_calculator.rb +25 -0
  21. data/lib/billing_logic/strategies/independent_payment_strategy.rb +322 -0
  22. data/lib/billing_logic/version.rb +3 -0
  23. data/lib/billing_logic.rb +23 -0
  24. data/spec/billing_info/billing_cycle_spec.rb +35 -0
  25. data/spec/billing_info/command_builders/command_builder_spec.rb +47 -0
  26. data/spec/billing_info/independent_payment_strategy_spec.rb +234 -0
  27. data/spec/billing_info/payment_command_builder_spec.rb +41 -0
  28. data/spec/billing_info/proration_calculator_spec.rb +65 -0
  29. data/spec/spec_helper.rb +61 -0
  30. metadata +227 -0
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+ module BillingLogic
3
+ describe PaymentCommandBuilder do
4
+ let(:monthly_cycle) do
5
+ BillingCycle.new(:period => :month,
6
+ :frequency => 1,
7
+ :anniversary => Date.current - 7)
8
+ end
9
+
10
+ let(:yearly_cycle) do
11
+ BillingCycle.new(:period => :year,
12
+ :frequency => 1,
13
+ :anniversary => Date.current - 7)
14
+ end
15
+ let(:product_a) { double('Product A', :name => 'A', :price => 10, :billing_cycle => monthly_cycle) }
16
+ let(:product_b) { double('Product B', :name => 'B', :price => 10, :billing_cycle => monthly_cycle) }
17
+ let(:yearly_product) { double('Product Yearly', :name => 'Yearly', :price => 10, :billing_cycle => yearly_cycle) }
18
+
19
+ describe "#group_products_by_billing_cycle" do
20
+ context "when 1 billing cycle is present" do
21
+ it "should return an array with 1 element" do
22
+ builder = PaymentCommandBuilder.new([product_a])
23
+ builder.group_products_by_billing_cycle.size.should == 1
24
+ end
25
+
26
+ it "should return an array with 1 element even if it has 2 different products" do
27
+ builder = PaymentCommandBuilder.new([product_a, product_b])
28
+ builder.group_products_by_billing_cycle.size.should == 1
29
+ end
30
+ end
31
+
32
+ context "when 2 different billing cycle is present" do
33
+ it "should return an array with 1 element" do
34
+ builder = PaymentCommandBuilder.new([product_a, yearly_product])
35
+ builder.group_products_by_billing_cycle.size.should == 2
36
+ end
37
+ end
38
+ end
39
+
40
+ end
41
+ end
@@ -0,0 +1,65 @@
1
+ require 'spec_helper'
2
+ module Helper
3
+ def self.date(d)
4
+ Date.strptime(d, '%d/%m/%Y')
5
+ end
6
+ end
7
+ module BillingLogic
8
+
9
+ describe ProrationCalculator do
10
+ context "calculating monthly prorations" do
11
+ before do
12
+ @anniversary_date = Helper.date('1/2/2012')
13
+ @cycle = BillingCycle.new(:period => :month,
14
+ :frequency => 1,
15
+ :anniversary => @anniversary_date)
16
+ @price = 29.0
17
+ @calculator = ProrationCalculator.new(:billing_cycle => @cycle,
18
+ :price => @price)
19
+ end
20
+ it "should recall price & cycles" do
21
+ @calculator.price.should == @price
22
+ @calculator.billing_cycle.should == @cycle
23
+ end
24
+
25
+ context "from a date prior to the anniversary date" do
26
+ it "should be able to calculate a proration" do
27
+ @calculator.price = 31 #adjusting for simplicity
28
+ @calculator.prorate_from(Helper.date('15/1/2012')).should == 17
29
+ end
30
+ end
31
+
32
+ context "from a date equal to the anniversary date the proration" do
33
+ it "should be the full price" do
34
+ @calculator.prorate_from(@anniversary_date).should == @price
35
+ end
36
+ end
37
+
38
+ context "from a date posterior to the anniversary date the proration" do
39
+ it "should be calculated just fine" do
40
+ @calculator.prorate_from(Helper.date('15/2/2012')).should == 14
41
+ end
42
+
43
+ it "should return the full price if the future date is the next anniversary date" do
44
+ @calculator.prorate_from(Helper.date('1/3/2012')).should == 29
45
+ end
46
+ end
47
+
48
+ end
49
+ context "calculating a yearly proration" do
50
+ before do
51
+ @anniversary_date = Helper.date('1/1/2013')
52
+ @cycle = BillingCycle.new(:period => :year,
53
+ :frequency => 1,
54
+ :anniversary => @anniversary_date)
55
+ @price = 36600
56
+ @calculator = ProrationCalculator.new(:billing_cycle => @cycle,
57
+ :price => @price)
58
+ end
59
+ it "should calculate a proration of $350 on the 1/16" do
60
+ @calculator.date = Helper.date('16/1/2012')
61
+ @calculator.prorate.should == 35100
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,61 @@
1
+ require 'billing_logic'
2
+ # This file was generated by the `rspec --init` command. Conventionally, all
3
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
4
+ # Require this file using `require "spec_helper.rb"` to ensure that it is only
5
+ # loaded once.
6
+ #
7
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
8
+ RSpec.configure do |config|
9
+ config.treat_symbols_as_metadata_keys_with_true_values = true
10
+ config.run_all_when_everything_filtered = true
11
+ config.filter_run :focus
12
+ config.color_enabled = true
13
+ config.formatter = 'documentation'
14
+ end
15
+
16
+ class MockProduct
17
+
18
+ include BillingLogic::CommandBuilders::BuilderHelpers
19
+
20
+ attr_accessor :identifier, :name, :price, :billing_cycle, :initial_payment
21
+
22
+ # Example:
23
+ # MockProduct.new(:identifier => 1,
24
+ # :name => 'A',
25
+ # :price => 10,
26
+ # :billing_cycle => monthly_cycle,
27
+ # :initial_payment => 0.0)
28
+
29
+ def initialize(opts ={})
30
+ opts.each do |k, v|
31
+ self.send("#{k}=", v)
32
+ end
33
+ end
34
+
35
+ end
36
+
37
+ class MockProfile
38
+ attr_accessor :identifier, :products, :price, :paid_until_date, :billing_cycle, :active_or_pending,
39
+ :current_products, :active_products, :billing_start_date
40
+
41
+ # Example:
42
+ # MockProfile.new(:products => [product_d],
43
+ # :price => 40,
44
+ # :identifier => 'i-4',
45
+ # :paid_until_date => monthly_cycle.next_payment_date,
46
+ # :active_or_pending => false)
47
+
48
+ def initialize(opts ={})
49
+ opts.each do |k, v|
50
+ self.send("#{k}=", v)
51
+ end
52
+ end
53
+
54
+ def active_or_pending?
55
+ active_or_pending
56
+ end
57
+
58
+ def refundable_payment_amount(foo)
59
+ 0.0
60
+ end
61
+ end
metadata ADDED
@@ -0,0 +1,227 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: billing_logic
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Diego Scataglini
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-09-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: awesome_print
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.12'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.12'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: cucumber
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.2'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.2'
83
+ - !ruby/object:Gem::Dependency
84
+ name: timecop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.6'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.6'
97
+ - !ruby/object:Gem::Dependency
98
+ name: guard
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '1.6'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '1.6'
111
+ - !ruby/object:Gem::Dependency
112
+ name: guard-rspec
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '2.4'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '2.4'
125
+ - !ruby/object:Gem::Dependency
126
+ name: guard-cucumber
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '1.3'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '1.3'
139
+ - !ruby/object:Gem::Dependency
140
+ name: yard
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ description: There are only a few way to calculate prorations & manage recurring payments.
154
+ email:
155
+ - diego@junivi.com
156
+ executables: []
157
+ extensions: []
158
+ extra_rdoc_files: []
159
+ files:
160
+ - README.rdoc
161
+ - config/cruise/run_cruise
162
+ - features/change_periodicity_on_anniversary_day.feature
163
+ - features/change_periodicity_on_signup_day.feature
164
+ - features/independent_payment_strategy.feature
165
+ - features/same_day_cancellation_policy.feature
166
+ - features/step_definitions/cancellation_steps.rb
167
+ - features/step_definitions/implementation_specific_steps.rb
168
+ - features/step_definitions/independent_payment_strategy_steps.rb
169
+ - features/step_definitions/steps.rb
170
+ - features/support/env.rb
171
+ - features/support/helpers.rb
172
+ - features/support/parameter_types.rb
173
+ - lib/billing_logic.rb
174
+ - lib/billing_logic/billing_cycle.rb
175
+ - lib/billing_logic/command_builders/command_builders.rb
176
+ - lib/billing_logic/current_state.rb
177
+ - lib/billing_logic/current_state_mixin.rb
178
+ - lib/billing_logic/payment_command_builder.rb
179
+ - lib/billing_logic/proration_calculator.rb
180
+ - lib/billing_logic/strategies/independent_payment_strategy.rb
181
+ - lib/billing_logic/version.rb
182
+ - spec/billing_info/billing_cycle_spec.rb
183
+ - spec/billing_info/command_builders/command_builder_spec.rb
184
+ - spec/billing_info/independent_payment_strategy_spec.rb
185
+ - spec/billing_info/payment_command_builder_spec.rb
186
+ - spec/billing_info/proration_calculator_spec.rb
187
+ - spec/spec_helper.rb
188
+ homepage: ''
189
+ licenses: []
190
+ metadata: {}
191
+ post_install_message:
192
+ rdoc_options: []
193
+ require_paths:
194
+ - lib
195
+ required_ruby_version: !ruby/object:Gem::Requirement
196
+ requirements:
197
+ - - ">="
198
+ - !ruby/object:Gem::Version
199
+ version: '0'
200
+ required_rubygems_version: !ruby/object:Gem::Requirement
201
+ requirements:
202
+ - - ">="
203
+ - !ruby/object:Gem::Version
204
+ version: '0'
205
+ requirements: []
206
+ rubygems_version: 3.0.9
207
+ signing_key:
208
+ specification_version: 4
209
+ summary: The only recurring billing logic you'll need
210
+ test_files:
211
+ - features/same_day_cancellation_policy.feature
212
+ - features/change_periodicity_on_anniversary_day.feature
213
+ - features/independent_payment_strategy.feature
214
+ - features/support/helpers.rb
215
+ - features/support/parameter_types.rb
216
+ - features/support/env.rb
217
+ - features/step_definitions/cancellation_steps.rb
218
+ - features/step_definitions/implementation_specific_steps.rb
219
+ - features/step_definitions/steps.rb
220
+ - features/step_definitions/independent_payment_strategy_steps.rb
221
+ - features/change_periodicity_on_signup_day.feature
222
+ - spec/spec_helper.rb
223
+ - spec/billing_info/independent_payment_strategy_spec.rb
224
+ - spec/billing_info/billing_cycle_spec.rb
225
+ - spec/billing_info/command_builders/command_builder_spec.rb
226
+ - spec/billing_info/proration_calculator_spec.rb
227
+ - spec/billing_info/payment_command_builder_spec.rb