stripe-ruby-mock 1.10.1.5 → 1.10.1.6

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -8,7 +8,7 @@
8
8
 
9
9
  In your gemfile:
10
10
 
11
- gem 'stripe-ruby-mock', '~> 1.10.1.5'
11
+ gem 'stripe-ruby-mock', '~> 1.10.1.6'
12
12
 
13
13
  ## Features
14
14
 
@@ -29,7 +29,11 @@ module StripeMock
29
29
  raise Stripe::InvalidRequestError.new('You must supply a valid card', nil, 400)
30
30
  end
31
31
 
32
- add_subscription_to_customer(plan, customers[params[:id]] )
32
+ subscription = Data.mock_subscription({ id: new_id('su') })
33
+ subscription.merge!(custom_subscription_params(plan, customers[ params[:id] ], params))
34
+ add_subscription_to_customer(customers[ params[:id] ], subscription)
35
+ elsif params[:trial_end]
36
+ raise Stripe::InvalidRequestError.new('Received unknown parameter: trial_end', nil, 400)
33
37
  end
34
38
 
35
39
  customers[ params[:id] ]
@@ -6,25 +6,32 @@ module StripeMock
6
6
  customer[:subscriptions][:data].find{|sub| sub[:id] == sub_id }
7
7
  end
8
8
 
9
- def add_subscription_to_customer(plan, cus, start_time = nil)
10
- start_time ||= Time.now.utc.to_i
11
- params = { id: new_id('su'), plan: plan, customer: cus[:id], current_period_start: start_time, current_period_end: get_ending_time(start_time, plan) }
9
+ def custom_subscription_params(plan, cus, options = {})
10
+ verify_trial_end(options[:trial_end]) if options[:trial_end]
12
11
 
13
- if plan[:trial_period_days].nil?
14
- params.merge!({status: 'active', trial_start: nil, trial_end: nil})
12
+ start_time = options[:current_period_start] || Time.now.utc.to_i
13
+ params = { plan: plan, customer: cus[:id], current_period_start: start_time }
14
+ params.merge! options.select {|k,v| k =~ /application_fee_percent|quantity/}
15
+ # TODO: Implement coupon logic
16
+
17
+ if (plan[:trial_period_days].nil? && options[:trial_end].nil?) || options[:trial_end] == "now"
18
+ end_time = get_ending_time(start_time, plan)
19
+ params.merge!({status: 'active', current_period_end: end_time, trial_start: nil, trial_end: nil})
15
20
  else
16
- params.merge!({status: 'trialing', trial_start: Time.now.utc.to_i, trial_end: (Time.now.utc.to_i + plan[:trial_period_days]*86400) })
21
+ end_time = options[:trial_end] || (Time.now.utc.to_i + plan[:trial_period_days]*86400)
22
+ params.merge!({status: 'trialing', current_period_end: end_time, trial_start: start_time, trial_end: end_time})
17
23
  end
18
24
 
19
- subscription = Data.mock_subscription params
25
+ params
26
+ end
20
27
 
21
- cus[:subscriptions][:count] = (cus[:subscriptions][:count] ? cus[:subscriptions][:count]+1 : 1 )
22
- cus[:subscriptions][:data] << subscription
23
- subscription
28
+ def add_subscription_to_customer(cus, sub)
29
+ cus[:subscriptions][:count] = (cus[:subscriptions][:count] || 0) + 1
30
+ cus[:subscriptions][:data] << sub
24
31
  end
25
32
 
26
- # intervals variable is set to 1 when calculating current_period_end from current_period_start & plan
27
- # intervals variable is set to 2 when calculating Stripe::Invoice.upcoming end from current_period_start & plan
33
+ # `intervals` is set to 1 when calculating current_period_end from current_period_start & plan
34
+ # `intervals` is set to 2 when calculating Stripe::Invoice.upcoming end from current_period_start & plan
28
35
  def get_ending_time(start_time, plan, intervals = 1)
29
36
  case plan[:interval]
30
37
  when "week"
@@ -38,6 +45,18 @@ module StripeMock
38
45
  end
39
46
  end
40
47
 
48
+ def verify_trial_end(trial_end)
49
+ if trial_end != "now"
50
+ if !trial_end.is_a? Integer
51
+ raise Stripe::InvalidRequestError.new('Invalid timestamp: must be an integer', nil, 400)
52
+ elsif trial_end < Time.now.utc.to_i
53
+ raise Stripe::InvalidRequestError.new('Invalid timestamp: must be an integer Unix timestamp in the future', nil, 400)
54
+ elsif trial_end > Time.now.utc.to_i + 31557600*5 # five years
55
+ raise Stripe::InvalidRequestError.new('Invalid timestamp: can be no more than five years in the future', nil, 400)
56
+ end
57
+ end
58
+ end
59
+
41
60
  end
42
61
  end
43
62
  end
@@ -22,7 +22,9 @@ module StripeMock
22
22
  # Ensure customer has card to charge if plan has no trial and is not free
23
23
  verify_card_present(customer, plan)
24
24
 
25
- subscription = add_subscription_to_customer(plan, customer, params[:current_period_start])
25
+ subscription = Data.mock_subscription({ id: (params[:id] || new_id('su')) })
26
+ subscription.merge!(custom_subscription_params(plan, customer, params))
27
+ add_subscription_to_customer(customer, subscription)
26
28
 
27
29
  # oddly, subscription returned from 'create_subscription' does not expand plan
28
30
  subscription.merge(plan: params[:plan])
@@ -63,17 +65,14 @@ module StripeMock
63
65
  end
64
66
 
65
67
  # expand the plan for addition to the customer object
66
- if params[:plan]
67
- plan_name = params[:plan]
68
- plan = plans[plan_name]
69
- assert_existance :plan, params[:plan], plan
70
- params[:plan] = plan
71
- end
68
+ plan_name = params[:plan] || subscription[:plan][:id]
69
+ plan = plans[plan_name]
72
70
 
73
- # Ensure customer has card to charge if plan has no trial and is not free
71
+ assert_existance :plan, plan_name, plan
72
+ params[:plan] = plan if params[:plan]
74
73
  verify_card_present(customer, plan)
75
74
 
76
- subscription.merge!(params)
75
+ subscription.merge!(custom_subscription_params(plan, customer, params))
77
76
 
78
77
  # delete the old subscription, replace with the new subscription
79
78
  customer[:subscriptions][:data].reject! { |sub| sub[:id] == subscription[:id] }
@@ -1,4 +1,4 @@
1
1
  module StripeMock
2
2
  # stripe-ruby-mock version
3
- VERSION = "1.10.1.5"
3
+ VERSION = "1.10.1.6"
4
4
  end
@@ -47,6 +47,75 @@ shared_examples 'Customer API' do
47
47
  expect(customer.subscriptions.first.customer).to eq(customer.id)
48
48
  end
49
49
 
50
+ context "create customer" do
51
+
52
+ it "with a trial when trial_end is set" do
53
+ plan = Stripe::Plan.create(id: 'no_trial', amount: 999)
54
+ trial_end = Time.now.utc.to_i + 3600
55
+ customer = Stripe::Customer.create(id: 'test_cus_trial_end', card: 'tk', plan: 'no_trial', trial_end: trial_end)
56
+
57
+ customer = Stripe::Customer.retrieve('test_cus_trial_end')
58
+ expect(customer.subscriptions.count).to eq(1)
59
+ expect(customer.subscriptions.data.length).to eq(1)
60
+
61
+ expect(customer.subscriptions).to_not be_nil
62
+ expect(customer.subscriptions.first.plan.id).to eq('no_trial')
63
+ expect(customer.subscriptions.first.status).to eq('trialing')
64
+ expect(customer.subscriptions.first.current_period_end).to eq(trial_end)
65
+ expect(customer.subscriptions.first.trial_end).to eq(trial_end)
66
+ end
67
+
68
+ it 'overrides trial period length when trial_end is set' do
69
+ plan = Stripe::Plan.create(id: 'silver', amount: 999, trial_period_days: 14)
70
+ trial_end = Time.now.utc.to_i + 3600
71
+ customer = Stripe::Customer.create(id: 'test_cus_trial_end', card: 'tk', plan: 'silver', trial_end: trial_end)
72
+
73
+ customer = Stripe::Customer.retrieve('test_cus_trial_end')
74
+ expect(customer.subscriptions.count).to eq(1)
75
+ expect(customer.subscriptions.data.length).to eq(1)
76
+
77
+ expect(customer.subscriptions).to_not be_nil
78
+ expect(customer.subscriptions.first.plan.id).to eq('silver')
79
+ expect(customer.subscriptions.first.current_period_end).to eq(trial_end)
80
+ expect(customer.subscriptions.first.trial_end).to eq(trial_end)
81
+ end
82
+
83
+ it "returns no trial when trial_end is set to 'now'" do
84
+ plan = Stripe::Plan.create(id: 'silver', amount: 999, trial_period_days: 14)
85
+ customer = Stripe::Customer.create(id: 'test_cus_trial_end', card: 'tk', plan: 'silver', trial_end: "now")
86
+
87
+ customer = Stripe::Customer.retrieve('test_cus_trial_end')
88
+ expect(customer.subscriptions.count).to eq(1)
89
+ expect(customer.subscriptions.data.length).to eq(1)
90
+
91
+ expect(customer.subscriptions).to_not be_nil
92
+ expect(customer.subscriptions.first.plan.id).to eq('silver')
93
+ expect(customer.subscriptions.first.status).to eq('active')
94
+ expect(customer.subscriptions.first.trial_start).to be_nil
95
+ expect(customer.subscriptions.first.trial_end).to be_nil
96
+ end
97
+
98
+ it "returns an error if trial_end is set to a past time" do
99
+ plan = Stripe::Plan.create(id: 'silver', amount: 999)
100
+ expect {
101
+ Stripe::Customer.create(id: 'test_cus_trial_end', card: 'tk', plan: 'silver', trial_end: Time.now.utc.to_i - 3600)
102
+ }.to raise_error {|e|
103
+ expect(e).to be_a(Stripe::InvalidRequestError)
104
+ expect(e.message).to eq('Invalid timestamp: must be an integer Unix timestamp in the future')
105
+ }
106
+ end
107
+
108
+ it "returns an error if trial_end is set without a plan" do
109
+ expect {
110
+ Stripe::Customer.create(id: 'test_cus_trial_end', card: 'tk', trial_end: "now")
111
+ }.to raise_error {|e|
112
+ expect(e).to be_a(Stripe::InvalidRequestError)
113
+ expect(e.message).to eq('Received unknown parameter: trial_end')
114
+ }
115
+ end
116
+
117
+ end
118
+
50
119
  it 'cannot create a customer with a plan that does not exist' do
51
120
  expect {
52
121
  customer = Stripe::Customer.create(id: 'test_cus_no_plan', card: 'tk', :plan => 'non-existant')
@@ -26,6 +26,23 @@ shared_examples 'Customer Subscriptions' do
26
26
 
27
27
  end
28
28
 
29
+ it "correctly sets quantity and application_fee_percent" do
30
+ Stripe::Plan.create(
31
+ :amount => 2500,
32
+ :interval => 'month',
33
+ :name => 'Test plan',
34
+ :currency => 'usd',
35
+ :id => 'silver',
36
+ :statement_description => "testPlan"
37
+ )
38
+ customer = Stripe::Customer.create(id: 'test_customer_sub', card: 'tk')
39
+
40
+ subscription = customer.subscriptions.create({
41
+ :plan => "silver", quantity: 2, application_fee_percent: 10})
42
+ expect(subscription.quantity).to eq(2)
43
+ expect(subscription.application_fee_percent).to eq(10)
44
+ end
45
+
29
46
  it "adds additional subscription to customer with existing subscription" do
30
47
  silver = Stripe::Plan.create(id: 'silver')
31
48
  gold = Stripe::Plan.create(id: 'gold')
@@ -114,6 +131,67 @@ shared_examples 'Customer Subscriptions' do
114
131
  expect(customer.subscriptions.data.first.plan.to_hash).to eq(plan.to_hash)
115
132
  expect(customer.subscriptions.data.first.customer).to eq(customer.id)
116
133
  end
134
+
135
+ it "overrides trial length when trial end is set" do
136
+ plan = Stripe::Plan.create(id: 'trial', amount: 999, trial_period_days: 14)
137
+ customer = Stripe::Customer.create(id: 'short_trial')
138
+ trial_end = Time.now.utc.to_i + 3600
139
+
140
+ sub = customer.subscriptions.create({ plan: 'trial', trial_end: trial_end })
141
+
142
+ expect(sub.object).to eq('subscription')
143
+ expect(sub.plan).to eq('trial')
144
+ expect(sub.current_period_end).to eq(trial_end)
145
+ expect(sub.trial_end).to eq(trial_end)
146
+ end
147
+
148
+ it "returns without a trial when trial_end is set to 'now'" do
149
+ plan = Stripe::Plan.create(id: 'trial', amount: 999, trial_period_days: 14)
150
+ customer = Stripe::Customer.create(id: 'no_trial', card: 'tk')
151
+
152
+ sub = customer.subscriptions.create({ plan: 'trial', trial_end: "now" })
153
+
154
+ expect(sub.object).to eq('subscription')
155
+ expect(sub.plan).to eq('trial')
156
+ expect(sub.status).to eq('active')
157
+ expect(sub.trial_start).to be_nil
158
+ expect(sub.trial_end).to be_nil
159
+ end
160
+
161
+ it "raises error when trial_end is not an integer or 'now'" do
162
+ plan = Stripe::Plan.create(id: 'trial', amount: 999, trial_period_days: 14)
163
+ customer = Stripe::Customer.create(id: 'cus_trial')
164
+
165
+ expect { customer.subscriptions.create({ plan: 'trial', trial_end: "gazebo" }) }.to raise_error {|e|
166
+ expect(e).to be_a Stripe::InvalidRequestError
167
+ expect(e.http_status).to eq(400)
168
+ expect(e.message).to eq("Invalid timestamp: must be an integer")
169
+ }
170
+ end
171
+
172
+ it "raises error when trial_end is set to a time in the past" do
173
+ plan = Stripe::Plan.create(id: 'trial', amount: 999, trial_period_days: 14)
174
+ customer = Stripe::Customer.create(id: 'past_trial')
175
+ trial_end = Time.now.utc.to_i - 3600
176
+
177
+ expect { customer.subscriptions.create({ plan: 'trial', trial_end: trial_end }) }.to raise_error {|e|
178
+ expect(e).to be_a Stripe::InvalidRequestError
179
+ expect(e.http_status).to eq(400)
180
+ expect(e.message).to eq("Invalid timestamp: must be an integer Unix timestamp in the future")
181
+ }
182
+ end
183
+
184
+ it "raises error when trial_end is set to a time more than five years in the future" do
185
+ plan = Stripe::Plan.create(id: 'trial', amount: 999, trial_period_days: 14)
186
+ customer = Stripe::Customer.create(id: 'long_trial')
187
+ trial_end = Time.now.utc.to_i + 31557600*5 + 3600 # 5 years + 1 hour
188
+
189
+ expect { customer.subscriptions.create({ plan: 'trial', trial_end: trial_end }) }.to raise_error {|e|
190
+ expect(e).to be_a Stripe::InvalidRequestError
191
+ expect(e.http_status).to eq(400)
192
+ expect(e.message).to eq("Invalid timestamp: can be no more than five years in the future")
193
+ }
194
+ end
117
195
  end
118
196
 
119
197
  context "updating a subscription" do
@@ -256,6 +334,69 @@ shared_examples 'Customer Subscriptions' do
256
334
  expect(customer.default_card).to_not be_nil
257
335
  expect(customer.default_card).to eq customer.cards.data.first.id
258
336
  end
337
+
338
+ it "overrides trial length when trial end is set" do
339
+ plan = Stripe::Plan.create(id: 'trial', amount: 999, trial_period_days: 14)
340
+ customer = Stripe::Customer.create(id: 'test_trial_end', plan: 'trial')
341
+
342
+ sub = customer.subscriptions.retrieve(customer.subscriptions.data.first.id)
343
+
344
+ trial_end = Time.now.utc.to_i + 3600
345
+ sub.trial_end = trial_end
346
+ sub.save
347
+
348
+ expect(sub.object).to eq('subscription')
349
+ expect(sub.trial_end).to eq(trial_end)
350
+ expect(sub.current_period_end).to eq(trial_end)
351
+ end
352
+
353
+ it "returns without a trial when trial_end is set to 'now'" do
354
+ plan = Stripe::Plan.create(id: 'trial', amount: 999, trial_period_days: 14)
355
+ customer = Stripe::Customer.create(id: 'test_trial_end', plan: 'trial')
356
+
357
+ sub = customer.subscriptions.retrieve(customer.subscriptions.data.first.id)
358
+
359
+ sub.trial_end = "now"
360
+ sub.save
361
+
362
+ expect(sub.object).to eq('subscription')
363
+ expect(sub.plan).to eq('trial')
364
+ expect(sub.status).to eq('active')
365
+ expect(sub.trial_start).to be_nil
366
+ expect(sub.trial_end).to be_nil
367
+ end
368
+
369
+ it "changes an active subscription to a trial when trial_end is set" do
370
+ plan = Stripe::Plan.create(id: 'no_trial', amount: 999)
371
+ customer = Stripe::Customer.create(id: 'test_trial_end', plan: 'no_trial', card: 'tk')
372
+
373
+ sub = customer.subscriptions.retrieve(customer.subscriptions.data.first.id)
374
+
375
+ trial_end = Time.now.utc.to_i + 3600
376
+ sub.trial_end = trial_end
377
+ sub.save
378
+
379
+ expect(sub.object).to eq('subscription')
380
+ expect(sub.plan).to eq('no_trial')
381
+ expect(sub.status).to eq('trialing')
382
+ expect(sub.trial_end).to eq(trial_end)
383
+ expect(sub.current_period_end).to eq(trial_end)
384
+ end
385
+
386
+
387
+ it "raises error when trial_end is not an integer or 'now'" do
388
+ plan = Stripe::Plan.create(id: 'no_trial', amount: 999)
389
+ customer = Stripe::Customer.create(id: 'test_trial_end', plan: 'no_trial', card: 'tk')
390
+
391
+ sub = customer.subscriptions.retrieve(customer.subscriptions.data.first.id)
392
+ sub.trial_end = "gazebo"
393
+
394
+ expect { sub.save }.to raise_error {|e|
395
+ expect(e).to be_a Stripe::InvalidRequestError
396
+ expect(e.http_status).to eq(400)
397
+ expect(e.message).to eq("Invalid timestamp: must be an integer")
398
+ }
399
+ end
259
400
  end
260
401
 
261
402
  context "cancelling a subscription" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stripe-ruby-mock
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.10.1.5
4
+ version: 1.10.1.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-03-19 00:00:00.000000000 Z
12
+ date: 2014-03-31 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: stripe