stripe-ruby-mock 2.4.0 → 2.5.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (98) hide show
  1. checksums.yaml +4 -4
  2. data/.env +2 -0
  3. data/.travis.yml +8 -4
  4. data/README.md +12 -5
  5. data/lib/stripe_mock/api/account_balance.rb +14 -0
  6. data/lib/stripe_mock/api/client.rb +4 -4
  7. data/lib/stripe_mock/api/conversion_rate.rb +14 -0
  8. data/lib/stripe_mock/api/errors.rb +25 -14
  9. data/lib/stripe_mock/api/instance.rb +6 -6
  10. data/lib/stripe_mock/api/webhooks.rb +5 -1
  11. data/lib/stripe_mock/client.rb +18 -2
  12. data/lib/stripe_mock/data/list.rb +14 -2
  13. data/lib/stripe_mock/data.rb +217 -56
  14. data/lib/stripe_mock/instance.rb +93 -8
  15. data/lib/stripe_mock/request_handlers/accounts.rb +34 -2
  16. data/lib/stripe_mock/request_handlers/balance.rb +17 -0
  17. data/lib/stripe_mock/request_handlers/balance_transactions.rb +18 -2
  18. data/lib/stripe_mock/request_handlers/charges.rb +25 -17
  19. data/lib/stripe_mock/request_handlers/coupons.rb +5 -4
  20. data/lib/stripe_mock/request_handlers/customers.rb +26 -8
  21. data/lib/stripe_mock/request_handlers/ephemeral_key.rb +13 -0
  22. data/lib/stripe_mock/request_handlers/external_accounts.rb +55 -0
  23. data/lib/stripe_mock/request_handlers/helpers/bank_account_helpers.rb +1 -1
  24. data/lib/stripe_mock/request_handlers/helpers/card_helpers.rb +9 -7
  25. data/lib/stripe_mock/request_handlers/helpers/coupon_helpers.rb +10 -6
  26. data/lib/stripe_mock/request_handlers/helpers/external_account_helpers.rb +49 -0
  27. data/lib/stripe_mock/request_handlers/helpers/subscription_helpers.rb +55 -15
  28. data/lib/stripe_mock/request_handlers/helpers/token_helpers.rb +2 -2
  29. data/lib/stripe_mock/request_handlers/invoices.rb +93 -14
  30. data/lib/stripe_mock/request_handlers/orders.rb +5 -5
  31. data/lib/stripe_mock/request_handlers/payouts.rb +32 -0
  32. data/lib/stripe_mock/request_handlers/plans.rb +1 -0
  33. data/lib/stripe_mock/request_handlers/products.rb +43 -0
  34. data/lib/stripe_mock/request_handlers/refunds.rb +10 -7
  35. data/lib/stripe_mock/request_handlers/subscription_items.rb +36 -0
  36. data/lib/stripe_mock/request_handlers/subscriptions.rb +95 -34
  37. data/lib/stripe_mock/request_handlers/tax_rates.rb +36 -0
  38. data/lib/stripe_mock/request_handlers/tokens.rb +9 -3
  39. data/lib/stripe_mock/request_handlers/transfers.rb +11 -5
  40. data/lib/stripe_mock/request_handlers/validators/param_validators.rb +10 -1
  41. data/lib/stripe_mock/server.rb +14 -1
  42. data/lib/stripe_mock/test_strategies/base.rb +7 -5
  43. data/lib/stripe_mock/test_strategies/live.rb +5 -0
  44. data/lib/stripe_mock/test_strategies/mock.rb +8 -0
  45. data/lib/stripe_mock/util.rb +8 -2
  46. data/lib/stripe_mock/version.rb +1 -1
  47. data/lib/stripe_mock/webhook_fixtures/account.updated.json +1 -1
  48. data/lib/stripe_mock/webhook_fixtures/charge.dispute.funds_reinstated.json +88 -0
  49. data/lib/stripe_mock/webhook_fixtures/charge.dispute.funds_withdrawn.json +88 -0
  50. data/lib/stripe_mock/webhook_fixtures/charge.updated.json +58 -0
  51. data/lib/stripe_mock/webhook_fixtures/customer.subscription.created.json +40 -10
  52. data/lib/stripe_mock/webhook_fixtures/customer.subscription.deleted.json +39 -10
  53. data/lib/stripe_mock/webhook_fixtures/customer.subscription.trial_will_end.json +39 -10
  54. data/lib/stripe_mock/webhook_fixtures/customer.subscription.updated.json +40 -11
  55. data/lib/stripe_mock/webhook_fixtures/invoice.created.json +3 -2
  56. data/lib/stripe_mock/webhook_fixtures/invoice.payment_failed.json +1 -1
  57. data/lib/stripe_mock/webhook_fixtures/invoice.payment_succeeded.json +92 -85
  58. data/lib/stripe_mock/webhook_fixtures/invoice.updated.json +3 -2
  59. data/lib/stripe_mock/webhook_fixtures/plan.created.json +1 -1
  60. data/lib/stripe_mock/webhook_fixtures/plan.deleted.json +1 -1
  61. data/lib/stripe_mock/webhook_fixtures/plan.updated.json +1 -1
  62. data/lib/stripe_mock.rb +13 -0
  63. data/spec/instance_spec.rb +54 -4
  64. data/spec/integration_examples/prepare_error_examples.rb +6 -6
  65. data/spec/list_spec.rb +16 -3
  66. data/spec/readme_spec.rb +2 -0
  67. data/spec/server_spec.rb +6 -2
  68. data/spec/shared_stripe_examples/account_examples.rb +35 -0
  69. data/spec/shared_stripe_examples/balance_examples.rb +11 -0
  70. data/spec/shared_stripe_examples/balance_transaction_examples.rb +28 -0
  71. data/spec/shared_stripe_examples/bank_examples.rb +28 -1
  72. data/spec/shared_stripe_examples/card_examples.rb +10 -3
  73. data/spec/shared_stripe_examples/charge_examples.rb +73 -17
  74. data/spec/shared_stripe_examples/coupon_examples.rb +8 -2
  75. data/spec/shared_stripe_examples/customer_examples.rb +54 -2
  76. data/spec/shared_stripe_examples/dispute_examples.rb +19 -8
  77. data/spec/shared_stripe_examples/ephemeral_key_examples.rb +17 -0
  78. data/spec/shared_stripe_examples/error_mock_examples.rb +3 -3
  79. data/spec/shared_stripe_examples/external_account_examples.rb +170 -0
  80. data/spec/shared_stripe_examples/extra_features_examples.rb +2 -0
  81. data/spec/shared_stripe_examples/invoice_examples.rb +314 -51
  82. data/spec/shared_stripe_examples/payout_examples.rb +68 -0
  83. data/spec/shared_stripe_examples/plan_examples.rb +37 -5
  84. data/spec/shared_stripe_examples/product_example.rb +65 -0
  85. data/spec/shared_stripe_examples/recipient_examples.rb +7 -7
  86. data/spec/shared_stripe_examples/refund_examples.rb +17 -11
  87. data/spec/shared_stripe_examples/subscription_examples.rb +463 -33
  88. data/spec/shared_stripe_examples/subscription_items_examples.rb +75 -0
  89. data/spec/shared_stripe_examples/tax_rate_examples.rb +42 -0
  90. data/spec/shared_stripe_examples/transfer_examples.rb +72 -23
  91. data/spec/shared_stripe_examples/webhook_event_examples.rb +74 -5
  92. data/spec/spec_helper.rb +7 -6
  93. data/spec/stripe_mock_spec.rb +16 -3
  94. data/spec/support/stripe_examples.rb +7 -1
  95. data/spec/util_spec.rb +35 -1
  96. data/stripe-ruby-mock.gemspec +1 -1
  97. metadata +34 -7
  98. data/ChangeLog.rdoc +0 -4
@@ -7,8 +7,39 @@ shared_examples 'Customer Subscriptions' do
7
7
  end
8
8
 
9
9
  context "creating a new subscription" do
10
+ it "adds a new subscription to customer with none using items", :live => true do
11
+ plan = stripe_helper.create_plan(id: 'silver', product: { name: 'Silver Plan' },
12
+ amount: 4999, currency: 'usd')
13
+ customer = Stripe::Customer.create(source: gen_card_tk)
14
+
15
+ expect(customer.subscriptions.data).to be_empty
16
+ expect(customer.subscriptions.count).to eq(0)
17
+
18
+ sub = Stripe::Subscription.create({ items: [{ plan: 'silver' }],
19
+ customer: customer.id, metadata: { foo: "bar", example: "yes" } })
20
+
21
+ expect(sub.object).to eq('subscription')
22
+ expect(sub.plan.to_hash).to eq(plan.to_hash)
23
+ expect(sub.metadata.foo).to eq( "bar" )
24
+ expect(sub.metadata.example).to eq( "yes" )
25
+
26
+ customer = Stripe::Customer.retrieve(customer.id)
27
+ expect(customer.subscriptions.data).to_not be_empty
28
+ expect(customer.subscriptions.count).to eq(1)
29
+ expect(customer.subscriptions.data.length).to eq(1)
30
+ expect(customer.charges.data.length).to eq(1)
31
+ expect(customer.currency).to eq( "usd" )
32
+
33
+ expect(customer.subscriptions.data.first.id).to eq(sub.id)
34
+ expect(customer.subscriptions.data.first.plan.to_hash).to eq(plan.to_hash)
35
+ expect(customer.subscriptions.data.first.customer).to eq(customer.id)
36
+ expect(customer.subscriptions.data.first.metadata.foo).to eq( "bar" )
37
+ expect(customer.subscriptions.data.first.metadata.example).to eq( "yes" )
38
+ end
39
+
10
40
  it "adds a new subscription to customer with none", :live => true do
11
- plan = stripe_helper.create_plan(id: 'silver', name: 'Silver Plan', amount: 4999, currency: 'usd')
41
+ plan = stripe_helper.create_plan(id: 'silver', product: { name: 'Silver Plan' },
42
+ amount: 4999, currency: 'usd')
12
43
  customer = Stripe::Customer.create(source: gen_card_tk)
13
44
 
14
45
  expect(customer.subscriptions.data).to be_empty
@@ -31,6 +62,38 @@ shared_examples 'Customer Subscriptions' do
31
62
  expect(customer.subscriptions.data.first.id).to eq(sub.id)
32
63
  expect(customer.subscriptions.data.first.plan.to_hash).to eq(plan.to_hash)
33
64
  expect(customer.subscriptions.data.first.customer).to eq(customer.id)
65
+ expect(customer.subscriptions.data.first.billing).to eq('charge_automatically')
66
+ expect(customer.subscriptions.data.first.metadata.foo).to eq( "bar" )
67
+ expect(customer.subscriptions.data.first.metadata.example).to eq( "yes" )
68
+ end
69
+
70
+ it 'when customer object provided' do
71
+ plan = stripe_helper.create_plan(id: 'silver', product: { name: 'Silver Plan' }, amount: 4999, currency: 'usd')
72
+ customer = Stripe::Customer.create(source: gen_card_tk)
73
+
74
+ expect(customer.subscriptions.data).to be_empty
75
+ expect(customer.subscriptions.count).to eq(0)
76
+
77
+ sub = Stripe::Subscription.create({ plan: 'silver', customer: customer, metadata: { foo: "bar", example: "yes" } })
78
+
79
+ expect(sub.object).to eq('subscription')
80
+ expect(sub.plan.to_hash).to eq(plan.to_hash)
81
+ expect(sub.billing).to eq('charge_automatically')
82
+ expect(sub.metadata.foo).to eq( "bar" )
83
+ expect(sub.metadata.example).to eq( "yes" )
84
+
85
+ customer = Stripe::Customer.retrieve(customer.id)
86
+ expect(customer.subscriptions.data).to_not be_empty
87
+ expect(customer.subscriptions.count).to eq(1)
88
+ expect(customer.subscriptions.data.length).to eq(1)
89
+ expect(customer.charges.data.length).to eq(1)
90
+ expect(customer.currency).to eq( "usd" )
91
+
92
+ expect(customer.subscriptions.data.first.id).to eq(sub.id)
93
+ expect(customer.subscriptions.data.first.plan.to_hash).to eq(plan.to_hash)
94
+
95
+ expect(customer.subscriptions.data.first.customer).to eq(customer.id)
96
+ expect(customer.subscriptions.data.first.billing).to eq('charge_automatically')
34
97
  expect(customer.subscriptions.data.first.metadata.foo).to eq( "bar" )
35
98
  expect(customer.subscriptions.data.first.metadata.example).to eq( "yes" )
36
99
  end
@@ -39,13 +102,15 @@ shared_examples 'Customer Subscriptions' do
39
102
  customer = Stripe::Customer.create(source: gen_card_tk)
40
103
  expect(customer.subscriptions.count).to eq(0)
41
104
 
42
- plan = stripe_helper.create_plan(id: :silver, name: 'Silver Plan', amount: 4999, currency: 'usd')
105
+ plan = stripe_helper.create_plan(id: :silver, product: { name: 'Silver Plan' },
106
+ amount: 4999, currency: 'usd')
43
107
  sub = Stripe::Subscription.create({ plan: 'silver', customer: customer.id })
44
108
  customer = Stripe::Customer.retrieve(customer.id)
45
109
  expect(sub.plan.to_hash).to eq(plan.to_hash)
46
110
  expect(customer.subscriptions.count).to eq(1)
47
111
 
48
- plan = stripe_helper.create_plan(id: 'gold', name: 'Gold Plan', amount: 14999, currency: 'usd')
112
+ plan = stripe_helper.create_plan(id: 'gold', product: { name: 'Gold Plan' },
113
+ amount: 14999, currency: 'usd')
49
114
  sub = Stripe::Subscription.create({ plan: 'gold', customer: customer.id })
50
115
  customer = Stripe::Customer.retrieve(customer.id)
51
116
  expect(sub.plan.to_hash).to eq(plan.to_hash)
@@ -53,7 +118,8 @@ shared_examples 'Customer Subscriptions' do
53
118
  end
54
119
 
55
120
  it 'creates a charge for the customer', live: true do
56
- stripe_helper.create_plan(id: 'silver', name: 'Silver Plan', amount: 4999)
121
+ stripe_helper.create_plan(id: 'silver', product: { name: 'Silver Plan' },
122
+ amount: 4999)
57
123
 
58
124
  customer = Stripe::Customer.create(source: gen_card_tk)
59
125
  Stripe::Subscription.create({ plan: 'silver', customer: customer.id, metadata: { foo: "bar", example: "yes" } })
@@ -64,7 +130,8 @@ shared_examples 'Customer Subscriptions' do
64
130
  end
65
131
 
66
132
  it 'contains coupon object', live: true do
67
- plan = stripe_helper.create_plan(id: 'plan_with_coupon', name: 'One More Test Plan', amount: 777)
133
+ plan = stripe_helper.create_plan(id: 'plan_with_coupon', product: { name: 'One More Test Plan' },
134
+ amount: 777)
68
135
  coupon = stripe_helper.create_coupon(id: 'free_coupon', duration: 'repeating', duration_in_months: 3)
69
136
  customer = Stripe::Customer.create(source: gen_card_tk)
70
137
  Stripe::Subscription.create(plan: plan.id, customer: customer.id, coupon: coupon.id)
@@ -73,12 +140,13 @@ shared_examples 'Customer Subscriptions' do
73
140
  expect(customer.subscriptions.data).to be_a(Array)
74
141
  expect(customer.subscriptions.data.count).to eq(1)
75
142
  expect(customer.subscriptions.data.first.discount).not_to be_nil
76
- expect(customer.subscriptions.data.first.discount).to be_a(Stripe::StripeObject)
143
+ expect(customer.subscriptions.data.first.discount).to be_a(Stripe::Discount)
77
144
  expect(customer.subscriptions.data.first.discount.coupon.id).to eq(coupon.id)
78
145
  end
79
146
 
80
147
  it 'when coupon is not exist', live: true do
81
- plan = stripe_helper.create_plan(id: 'plan_with_coupon', name: 'One More Test Plan', amount: 777)
148
+ plan = stripe_helper.create_plan(id: 'plan_with_coupon', product: { name: 'One More Test Plan' },
149
+ amount: 777)
82
150
  customer = Stripe::Customer.create(source: gen_card_tk)
83
151
 
84
152
  expect { Stripe::Subscription.create(plan: plan.id, customer: customer.id, coupon: 'none') }.to raise_error {|e|
@@ -92,7 +160,9 @@ shared_examples 'Customer Subscriptions' do
92
160
  Stripe::Plan.create(
93
161
  :amount => 2500,
94
162
  :interval => 'month',
95
- :name => 'Test plan',
163
+ :product => {
164
+ :name => 'Test plan'
165
+ },
96
166
  :currency => 'usd',
97
167
  :id => 'silver',
98
168
  :statement_description => "testPlan"
@@ -106,6 +176,24 @@ shared_examples 'Customer Subscriptions' do
106
176
  expect(subscription.tax_percent).to eq(20)
107
177
  end
108
178
 
179
+ it "correctly sets created when it's not provided as a parameter", live: true do
180
+ customer = Stripe::Customer.create(source: gen_card_tk)
181
+ plan = stripe_helper.create_plan(id: 'silver', product: { name: 'Silver Plan' },
182
+ amount: 4999, currency: 'usd')
183
+ subscription = Stripe::Subscription.create({ plan: 'silver', customer: customer.id })
184
+
185
+ expect(subscription.created).to eq(subscription.current_period_start)
186
+ end
187
+
188
+ it "correctly sets created when it's provided as a parameter" do
189
+ customer = Stripe::Customer.create(source: gen_card_tk)
190
+ plan = stripe_helper.create_plan(id: 'silver', product: { name: 'Silver Plan' },
191
+ amount: 4999, currency: 'usd')
192
+ subscription = Stripe::Subscription.create({ plan: 'silver', customer: customer.id, created: 1473576318 })
193
+
194
+ expect(subscription.created).to eq(1473576318)
195
+ end
196
+
109
197
  it "adds additional subscription to customer with existing subscription" do
110
198
  silver = stripe_helper.create_plan(id: 'silver')
111
199
  gold = stripe_helper.create_plan(id: 'gold')
@@ -185,6 +273,18 @@ shared_examples 'Customer Subscriptions' do
185
273
  }
186
274
  end
187
275
 
276
+ it 'when attempting to create a new subscription with the params trial', live: true do
277
+ plan = stripe_helper.create_plan(id: 'trial', amount: 999)
278
+ customer = Stripe::Customer.create(source: gen_card_tk)
279
+
280
+ expect{ Stripe::Subscription.create(plan: plan.id, customer: customer.id, trial: 10) }.to raise_error {|e|
281
+ expect(e).to be_a Stripe::InvalidRequestError
282
+ expect(e.http_status).to eq(400)
283
+ expect(e.param).to eq('trial')
284
+ expect(e.message).to match /Received unknown parameter/
285
+ }
286
+ end
287
+
188
288
  it "subscribes a customer with no card to a plan with a free trial" do
189
289
  plan = stripe_helper.create_plan(id: 'trial', amount: 999, trial_period_days: 14)
190
290
  customer = Stripe::Customer.create(id: 'cardless')
@@ -194,6 +294,7 @@ shared_examples 'Customer Subscriptions' do
194
294
  expect(sub.object).to eq('subscription')
195
295
  expect(sub.plan.to_hash).to eq(plan.to_hash)
196
296
  expect(sub.trial_end - sub.trial_start).to eq(14 * 86400)
297
+ expect(sub.billing_cycle_anchor).to be_nil
197
298
 
198
299
  customer = Stripe::Customer.retrieve('cardless')
199
300
  expect(customer.subscriptions.data).to_not be_empty
@@ -206,6 +307,29 @@ shared_examples 'Customer Subscriptions' do
206
307
  expect(customer.charges.count).to eq(0)
207
308
  end
208
309
 
310
+ it "subscribes a customer with no card to a plan with a free trial with plan as item" do
311
+ plan = stripe_helper.create_plan(id: 'trial', amount: 999, trial_period_days: 14)
312
+ customer = Stripe::Customer.create(id: 'cardless')
313
+
314
+ sub = Stripe::Subscription.create({ items: [ { plan: 'trial' } ], customer: customer.id })
315
+
316
+ expect(sub.object).to eq('subscription')
317
+ expect(sub.items.data[0].plan.to_hash).to eq(plan.to_hash)
318
+ # no idea how to fix this one
319
+ # expect(sub.trial_end - sub.trial_start).to eq(14 * 86400)
320
+
321
+ customer = Stripe::Customer.retrieve('cardless')
322
+ expect(customer.subscriptions.data).to_not be_empty
323
+ expect(customer.subscriptions.count).to eq(1)
324
+ expect(customer.subscriptions.data.length).to eq(1)
325
+
326
+ expect(customer.subscriptions.data.first.id).to eq(sub.id)
327
+ expect(customer.subscriptions.data.first.items.data.first.plan.to_hash).to eq(plan.to_hash)
328
+ expect(customer.subscriptions.data.first.customer).to eq(customer.id)
329
+ # No idea on this one
330
+ # expect(customer.charges.count).to eq(0)
331
+ end
332
+
209
333
  it "subscribes a customer with no card to a free plan" do
210
334
  plan = stripe_helper.create_plan(id: 'free_tier', amount: 0)
211
335
  customer = Stripe::Customer.create(id: 'cardless')
@@ -238,6 +362,30 @@ shared_examples 'Customer Subscriptions' do
238
362
  expect(sub.trial_end).to eq(trial_end)
239
363
  end
240
364
 
365
+ it "does not override trial end when trial end is not set" do
366
+ plan = stripe_helper.create_plan(id: 'trial', amount: 999, trial_period_days: 14)
367
+ customer = Stripe::Customer.create(id: 'short_trial')
368
+ trial_end = Time.now.utc.to_i + 3600
369
+ metadata = {description: 'original description'}
370
+
371
+ sub = Stripe::Subscription.create({ plan: 'trial', customer: customer.id, trial_end: trial_end, metadata: metadata })
372
+
373
+ expect(sub.object).to eq('subscription')
374
+ expect(sub.plan.to_hash).to eq(plan.to_hash)
375
+ expect(sub.current_period_end).to eq(trial_end)
376
+ expect(sub.trial_end).to eq(trial_end)
377
+ expect(sub.metadata.description).to eq(metadata[:description])
378
+
379
+ metadata = {description: 'updated description'}
380
+ sub = Stripe::Subscription.update(sub.id, { metadata: metadata })
381
+
382
+ expect(sub.object).to eq('subscription')
383
+ expect(sub.plan.to_hash).to eq(plan.to_hash)
384
+ expect(sub.current_period_end).to eq(trial_end)
385
+ expect(sub.trial_end).to eq(trial_end) # check that the trial_end has NOT changed
386
+ expect(sub.metadata.description).to eq(metadata[:description]) # check that the description has changed
387
+ end
388
+
241
389
  it "returns without a trial when trial_end is set to 'now'" do
242
390
  plan = stripe_helper.create_plan(id: 'trial', amount: 999, trial_period_days: 14)
243
391
  customer = Stripe::Customer.create(id: 'no_trial', source: gen_card_tk)
@@ -286,6 +434,137 @@ shared_examples 'Customer Subscriptions' do
286
434
  }
287
435
  end
288
436
 
437
+ it 'overrides current period end when billing cycle anchor is set' do
438
+ plan = stripe_helper.create_plan(id: 'plan', amount: 999)
439
+ customer = Stripe::Customer.create(source: gen_card_tk)
440
+ billing_cycle_anchor = Time.now.utc.to_i + 3600
441
+
442
+ sub = Stripe::Subscription.create({ plan: 'plan', customer: customer.id, billing_cycle_anchor: billing_cycle_anchor })
443
+
444
+ expect(sub.status).to eq('active')
445
+ expect(sub.current_period_end).to eq(billing_cycle_anchor)
446
+ expect(sub.billing_cycle_anchor).to eq(billing_cycle_anchor)
447
+ end
448
+
449
+ it 'when plan defined inside items', live: true do
450
+ plan = stripe_helper.create_plan(id: 'BASE_PRICE_PLAN1')
451
+
452
+ plan2 = stripe_helper.create_plan(id: 'PER_USER_PLAN1')
453
+ customer = Stripe::Customer.create(
454
+ source: {
455
+ object: 'card',
456
+ exp_month: 11,
457
+ exp_year: 2019,
458
+ number: '4242424242424242',
459
+ cvc: '123'
460
+ }
461
+ )
462
+ subscription = Stripe::Subscription.create(
463
+ customer: customer.id,
464
+ items: [
465
+ { plan: plan.id, quantity: 1 },
466
+ { plan: plan2.id, quantity: 2 }
467
+ ]
468
+ )
469
+
470
+ expect(subscription.id).to match /(test_su_|sub_).+/
471
+ expect(subscription.plan).to eq nil
472
+ expect(subscription.items.data[0].plan.id).to eq plan.id
473
+ expect(subscription.items.data[1].plan.id).to eq plan2.id
474
+ expect(subscription.items.data[0].quantity).to eq 1
475
+ expect(subscription.items.data[1].quantity).to eq 2
476
+ end
477
+
478
+ it 'when plan defined inside items for trials with no card', live: true do
479
+ plan = stripe_helper.create_plan(id: 'BASE_PRICE_PLAN1')
480
+
481
+ plan2 = stripe_helper.create_plan(id: 'PER_USER_PLAN1')
482
+ customer = Stripe::Customer.create
483
+ trial_end = Time.now.utc.to_i + 3600
484
+
485
+ subscription = Stripe::Subscription.create(
486
+ customer: customer.id,
487
+ items: [
488
+ { plan: plan.id, quantity: 1 },
489
+ { plan: plan2.id, quantity: 2 }
490
+ ],
491
+ trial_end: trial_end
492
+ )
493
+
494
+ expect(subscription.id).to match /(test_su_|sub_).+/
495
+ expect(subscription.plan).to eq nil
496
+ expect(subscription.items.data[0].plan.id).to eq plan.id
497
+ expect(subscription.items.data[1].plan.id).to eq plan2.id
498
+ end
499
+
500
+ it 'add a new subscription to bill via an invoice' do
501
+ plan = stripe_helper.create_plan(id: 'silver', product: { name: 'Silver Plan' },
502
+ amount: 4999, currency: 'usd')
503
+ customer = Stripe::Customer.create(id: 'cardless')
504
+
505
+ expect(customer.subscriptions.data).to be_empty
506
+ expect(customer.subscriptions.count).to eq(0)
507
+
508
+ sub = Stripe::Subscription.create({
509
+ plan: 'silver',
510
+ customer: customer.id,
511
+ metadata: { foo: 'bar', example: 'yes' },
512
+ billing: 'send_invoice',
513
+ days_until_due: 30,
514
+ })
515
+
516
+ expect(sub.object).to eq('subscription')
517
+ expect(sub.plan.to_hash).to eq(plan.to_hash)
518
+ expect(sub.billing).to eq 'send_invoice'
519
+ expect(sub.days_until_due).to eq 30
520
+ end
521
+
522
+ let(:subscription_header) {{
523
+ :idempotency_key => 'a_idempotency_key'
524
+ }}
525
+
526
+ it "adds a new subscription to customer with identical idempotency key" do
527
+ plan = stripe_helper.create_plan(id: 'silver', product: { name: 'Silver Plan' },
528
+ amount: 4999, currency: 'usd')
529
+ customer = Stripe::Customer.create(source: gen_card_tk)
530
+
531
+ expect(customer.subscriptions.data).to be_empty
532
+ expect(customer.subscriptions.count).to eq(0)
533
+
534
+ sub1 = Stripe::Subscription.create({ items: [{ plan: 'silver' }], customer: customer.id }, subscription_header)
535
+ sub2 = Stripe::Subscription.create({ items: [{ plan: 'silver' }], customer: customer.id }, subscription_header)
536
+ expect(sub1).to eq(sub2)
537
+ end
538
+
539
+ it "adds a new subscription to customer with different idempotency key", :live => true do
540
+ plan = stripe_helper.create_plan(id: 'silver', product: { name: 'Silver Plan' },
541
+ amount: 4999, currency: 'usd')
542
+ customer = Stripe::Customer.create(source: gen_card_tk)
543
+
544
+ expect(customer.subscriptions.data).to be_empty
545
+ expect(customer.subscriptions.count).to eq(0)
546
+
547
+ another_subscription_header = {
548
+ :idempotency_key => 'another_idempotency_key'
549
+ }
550
+
551
+ sub1 = Stripe::Subscription.create({ items: [{ plan: 'silver' }], customer: customer.id }, subscription_header)
552
+ sub2 = Stripe::Subscription.create({ items: [{ plan: 'silver' }], customer: customer.id }, another_subscription_header)
553
+ expect(sub1).not_to eq(sub2)
554
+ end
555
+
556
+ it "accepts a hash of items" do
557
+ silver = stripe_helper.create_plan(id: 'silver')
558
+ customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk)
559
+
560
+ sub = Stripe::Subscription.create({ items: { '0' => { plan: 'silver' } }, customer: customer.id })
561
+ sub.delete(at_period_end: true)
562
+
563
+ expect(sub.cancel_at_period_end).to be_truthy
564
+ expect(sub.save).to be_truthy
565
+ expect(sub.cancel_at_period_end).to be_falsey
566
+ end
567
+
289
568
  end
290
569
 
291
570
  context "updating a subscription" do
@@ -303,7 +582,19 @@ shared_examples 'Customer Subscriptions' do
303
582
  }
304
583
  end
305
584
 
306
- it "updates a stripe customer's existing subscription" do
585
+ it "updates a stripe customer's existing subscription with one plan inside items" do
586
+ silver = stripe_helper.create_plan(id: 'silver')
587
+ customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk)
588
+
589
+ sub = Stripe::Subscription.create({ items: [ { plan: 'silver' } ], customer: customer.id })
590
+ sub.delete(at_period_end: true)
591
+
592
+ expect(sub.cancel_at_period_end).to be_truthy
593
+ expect(sub.save).to be_truthy
594
+ expect(sub.cancel_at_period_end).to be_falsey
595
+ end
596
+
597
+ it "updates a stripe customer's existing subscription when plan inside of items" do
307
598
  silver = stripe_helper.create_plan(id: 'silver')
308
599
  gold = stripe_helper.create_plan(id: 'gold')
309
600
  customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk, plan: 'silver')
@@ -332,8 +623,60 @@ shared_examples 'Customer Subscriptions' do
332
623
  expect(customer.subscriptions.data.first.customer).to eq(customer.id)
333
624
  end
334
625
 
626
+ it "updates a stripe customer's existing subscription with single plan when multiple plans inside of items" do
627
+ silver_plan = stripe_helper.create_plan(id: 'silver')
628
+ gold_plan = stripe_helper.create_plan(id: 'gold')
629
+ addon_plan = stripe_helper.create_plan(id: 'addon_plan')
630
+ customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk, plan: silver_plan.id)
631
+
632
+ sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
633
+ sub.items = [{ plan: gold_plan.id, quantity: 2 }, { plan: addon_plan.id, quantity: 2 }]
634
+ expect(sub.save).to be_truthy
635
+
636
+ expect(sub.object).to eq('subscription')
637
+ expect(sub.plan).to be_nil
638
+
639
+ customer = Stripe::Customer.retrieve('test_customer_sub')
640
+ expect(customer.subscriptions.data).to_not be_empty
641
+ expect(customer.subscriptions.count).to eq(1)
642
+ expect(customer.subscriptions.data.length).to eq(1)
643
+
644
+ expect(customer.subscriptions.data.first.id).to eq(sub.id)
645
+ expect(customer.subscriptions.data.first.plan).to be_nil
646
+ expect(customer.subscriptions.data.first.customer).to eq(customer.id)
647
+ expect(customer.subscriptions.data.first.items.data[0].plan.to_hash).to eq(gold_plan.to_hash)
648
+ expect(customer.subscriptions.data.first.items.data[1].plan.to_hash).to eq(addon_plan.to_hash)
649
+ end
650
+
651
+ it "updates a stripe customer's existing subscription with multple plans when multiple plans inside of items" do
652
+ silver_plan = stripe_helper.create_plan(id: 'silver')
653
+ gold_plan = stripe_helper.create_plan(id: 'gold')
654
+ addon1_plan = stripe_helper.create_plan(id: 'addon1')
655
+ addon2_plan = stripe_helper.create_plan(id: 'addon2')
656
+ customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk)
657
+ sub = Stripe::Subscription.create(customer: customer.id, items: [{ plan: silver_plan.id }, { plan: addon1_plan.id }])
658
+
659
+ sub.items = [{ plan: gold_plan.id, quantity: 2 }, { plan: addon2_plan.id, quantity: 2 }]
660
+ expect(sub.save).to be_truthy
661
+
662
+ expect(sub.object).to eq('subscription')
663
+ expect(sub.plan).to be_nil
664
+
665
+ customer = Stripe::Customer.retrieve('test_customer_sub')
666
+ expect(customer.subscriptions.data).to_not be_empty
667
+ expect(customer.subscriptions.count).to eq(1)
668
+ expect(customer.subscriptions.data.length).to eq(1)
669
+
670
+ expect(customer.subscriptions.data.first.id).to eq(sub.id)
671
+ expect(customer.subscriptions.data.first.plan).to be_nil
672
+ expect(customer.subscriptions.data.first.customer).to eq(customer.id)
673
+ expect(customer.subscriptions.data.first.items.data[0].plan.to_hash).to eq(gold_plan.to_hash)
674
+ expect(customer.subscriptions.data.first.items.data[1].plan.to_hash).to eq(addon2_plan.to_hash)
675
+ end
676
+
335
677
  it 'when adds coupon', live: true do
336
- plan = stripe_helper.create_plan(id: 'plan_with_coupon2', name: 'One More Test Plan', amount: 777)
678
+ plan = stripe_helper.create_plan(id: 'plan_with_coupon2', product: { name: 'One More Test Plan' },
679
+ amount: 777)
337
680
  coupon = stripe_helper.create_coupon
338
681
  customer = Stripe::Customer.create(source: gen_card_tk, plan: plan.id)
339
682
  subscription = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
@@ -342,12 +685,13 @@ shared_examples 'Customer Subscriptions' do
342
685
  subscription.save
343
686
 
344
687
  expect(subscription.discount).not_to be_nil
345
- expect(subscription.discount).to be_an_instance_of(Stripe::StripeObject)
688
+ expect(subscription.discount).to be_a(Stripe::Discount)
346
689
  expect(subscription.discount.coupon.id).to eq(coupon.id)
347
690
  end
348
691
 
349
692
  it 'when add not exist coupon' do
350
- plan = stripe_helper.create_plan(id: 'plan_with_coupon3', name: 'One More Test Plan', amount: 777)
693
+ plan = stripe_helper.create_plan(id: 'plan_with_coupon3', product: { name: 'One More Test Plan' },
694
+ amount: 777)
351
695
  customer = Stripe::Customer.create(source: gen_card_tk, plan: plan.id)
352
696
  subscription = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
353
697
 
@@ -362,7 +706,8 @@ shared_examples 'Customer Subscriptions' do
362
706
  end
363
707
 
364
708
  it 'when coupon is removed' do
365
- plan = stripe_helper.create_plan(id: 'plan_with_coupon3', name: 'One More Test Plan', amount: 777)
709
+ plan = stripe_helper.create_plan(id: 'plan_with_coupon3', product: { name: 'One More Test Plan' },
710
+ amount: 777)
366
711
  customer = Stripe::Customer.create(source: gen_card_tk, plan: plan.id)
367
712
  coupon = stripe_helper.create_coupon
368
713
  subscription = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
@@ -410,24 +755,32 @@ shared_examples 'Customer Subscriptions' do
410
755
  expect(customer.subscriptions.data.first.plan.to_hash).to eq(free.to_hash)
411
756
  end
412
757
 
413
- it "throws an error when updating a customer with no card" do
414
- free = stripe_helper.create_plan(id: 'free', amount: 0)
415
- paid = stripe_helper.create_plan(id: 'enterprise', amount: 499)
416
- customer = Stripe::Customer.create(id: 'cardless', plan: 'free')
417
-
418
- sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
419
- sub.plan = 'enterprise'
420
-
421
- expect { sub.save }.to raise_error {|e|
422
- expect(e).to be_a Stripe::InvalidRequestError
423
- expect(e.http_status).to eq(400)
424
- expect(e.message).to_not be_nil
425
- }
426
-
427
- customer = Stripe::Customer.retrieve('cardless')
428
- expect(customer.subscriptions.count).to eq(1)
429
- expect(customer.subscriptions.data.length).to eq(1)
430
- expect(customer.subscriptions.data.first.plan.to_hash).to eq(free.to_hash)
758
+ [nil, 0].each do |trial_period_days|
759
+ it "throws an error when updating a customer with no card, and plan trail_period_days = #{trial_period_days}", live: true do
760
+ begin
761
+ free = stripe_helper.create_plan(id: 'free', amount: 0)
762
+ paid = stripe_helper.create_plan(id: 'enterprise', amount: 499, trial_period_days: trial_period_days)
763
+ customer = Stripe::Customer.create(description: 'cardless', plan: 'free')
764
+
765
+ sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
766
+ sub.plan = 'enterprise'
767
+
768
+ expect { sub.save }.to raise_error {|e|
769
+ expect(e).to be_a Stripe::InvalidRequestError
770
+ expect(e.http_status).to eq(400)
771
+ expect(e.message).to_not be_nil
772
+ }
773
+
774
+ customer = Stripe::Customer.retrieve(customer.id)
775
+ expect(customer.subscriptions.count).to eq(1)
776
+ expect(customer.subscriptions.data.length).to eq(1)
777
+ expect(customer.subscriptions.data.first.plan.to_hash).to eq(free.to_hash)
778
+ ensure
779
+ customer.delete if customer
780
+ paid.delete if paid
781
+ free.delete if free
782
+ end
783
+ end
431
784
  end
432
785
 
433
786
  it 'updates a subscription if the customer has a free trial', live: true do
@@ -631,6 +984,52 @@ shared_examples 'Customer Subscriptions' do
631
984
  end
632
985
  end
633
986
 
987
+ it "supports 'cancelling' by updating cancel_at_period_end" do
988
+ truth = stripe_helper.create_plan(id: 'the_truth')
989
+ customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk, plan: "the_truth")
990
+
991
+ sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
992
+ result = Stripe::Subscription.update(sub.id, cancel_at_period_end: true)
993
+
994
+ expect(result.status).to eq('active')
995
+ expect(result.cancel_at_period_end).to eq(true)
996
+ expect(result.id).to eq(sub.id)
997
+
998
+ customer = Stripe::Customer.retrieve('test_customer_sub')
999
+ expect(customer.subscriptions.data).to_not be_empty
1000
+ expect(customer.subscriptions.count).to eq(1)
1001
+ expect(customer.subscriptions.data.length).to eq(1)
1002
+
1003
+ expect(customer.subscriptions.data.first.status).to eq('active')
1004
+ expect(customer.subscriptions.data.first.cancel_at_period_end).to eq(true)
1005
+ expect(customer.subscriptions.data.first.ended_at).to be_nil
1006
+ expect(customer.subscriptions.data.first.canceled_at).to_not be_nil
1007
+ end
1008
+
1009
+ it "resumes a subscription cancelled by updating cancel_at_period_end" do
1010
+ truth = stripe_helper.create_plan(id: 'the_truth')
1011
+ customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk, plan: "the_truth")
1012
+
1013
+ sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
1014
+ Stripe::Subscription.update(sub.id, cancel_at_period_end: true)
1015
+
1016
+ result = Stripe::Subscription.update(sub.id, cancel_at_period_end: false)
1017
+
1018
+ expect(result.status).to eq('active')
1019
+ expect(result.cancel_at_period_end).to eq(false)
1020
+ expect(result.id).to eq(sub.id)
1021
+
1022
+ customer = Stripe::Customer.retrieve('test_customer_sub')
1023
+ expect(customer.subscriptions.data).to_not be_empty
1024
+ expect(customer.subscriptions.count).to eq(1)
1025
+ expect(customer.subscriptions.data.length).to eq(1)
1026
+
1027
+ expect(customer.subscriptions.data.first.status).to eq('active')
1028
+ expect(customer.subscriptions.data.first.cancel_at_period_end).to eq(false)
1029
+ expect(customer.subscriptions.data.first.ended_at).to be_nil
1030
+ expect(customer.subscriptions.data.first.canceled_at).to be_nil
1031
+ end
1032
+
634
1033
  it "doesn't change status of subscription when cancelling at period end" do
635
1034
  trial = stripe_helper.create_plan(id: 'trial', trial_period_days: 14)
636
1035
  customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk, plan: "trial")
@@ -659,6 +1058,34 @@ shared_examples 'Customer Subscriptions' do
659
1058
  Stripe::Subscription.create options
660
1059
  end
661
1060
 
1061
+ context 'retrieving a single subscription' do
1062
+ let(:customer) { Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk, plan: 'free') }
1063
+ let(:subscription) { Stripe::Subscription.retrieve(customer.subscriptions.data.first.id) }
1064
+
1065
+ before do
1066
+ stripe_helper.create_plan(id: 'free', amount: 0)
1067
+ Stripe::Subscription.create({ plan: 'free', customer: customer.id })
1068
+ end
1069
+
1070
+ it 'retrieves a single subscription' do
1071
+ expect(subscription).to be_truthy
1072
+ end
1073
+
1074
+ it "includes 'items' object on retrieved subscription" do
1075
+ expect(subscription.items).to be_truthy
1076
+ expect(subscription.items.object).to eq('list')
1077
+ expect(subscription.items.data.class).to eq(Array)
1078
+ expect(subscription.items.data.count).to eq(1)
1079
+ expect(subscription.items.data.first.id).to eq('test_txn_default')
1080
+ expect(subscription.items.data.first.created).to eq(1504716183)
1081
+ expect(subscription.items.data.first.object).to eq('subscription_item')
1082
+ expect(subscription.items.data.first.plan.amount).to eq(0)
1083
+ expect(subscription.items.data.first.plan.created).to eq(1466698898)
1084
+ expect(subscription.items.data.first.plan.currency).to eq('usd')
1085
+ expect(subscription.items.data.first.quantity).to eq(2)
1086
+ end
1087
+ end
1088
+
662
1089
  context "retrieve multiple subscriptions" do
663
1090
 
664
1091
  it "retrieves a list of multiple subscriptions" do
@@ -690,10 +1117,13 @@ shared_examples 'Customer Subscriptions' do
690
1117
 
691
1118
  it "creates a stripe customer and subscribes them to a plan with meta data", :live => true do
692
1119
 
693
- stripe_helper.create_plan(
1120
+ stripe_helper.
1121
+ create_plan(
694
1122
  :amount => 500,
695
1123
  :interval => 'month',
696
- :name => 'Sample Plan',
1124
+ :product => {
1125
+ :name => 'Sample Plan'
1126
+ },
697
1127
  :currency => 'usd',
698
1128
  :id => 'Sample5'
699
1129
  )