stripe-ruby-mock 2.3.1 → 2.5.8

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.
Files changed (106) 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 -13
  9. data/lib/stripe_mock/api/instance.rb +16 -6
  10. data/lib/stripe_mock/api/webhooks.rb +8 -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 +398 -58
  14. data/lib/stripe_mock/instance.rb +105 -9
  15. data/lib/stripe_mock/request_handlers/accounts.rb +41 -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 +44 -33
  19. data/lib/stripe_mock/request_handlers/country_spec.rb +22 -0
  20. data/lib/stripe_mock/request_handlers/coupons.rb +5 -4
  21. data/lib/stripe_mock/request_handlers/customers.rb +29 -11
  22. data/lib/stripe_mock/request_handlers/ephemeral_key.rb +13 -0
  23. data/lib/stripe_mock/request_handlers/external_accounts.rb +55 -0
  24. data/lib/stripe_mock/request_handlers/helpers/bank_account_helpers.rb +1 -1
  25. data/lib/stripe_mock/request_handlers/helpers/card_helpers.rb +9 -7
  26. data/lib/stripe_mock/request_handlers/helpers/coupon_helpers.rb +10 -6
  27. data/lib/stripe_mock/request_handlers/helpers/external_account_helpers.rb +49 -0
  28. data/lib/stripe_mock/request_handlers/helpers/subscription_helpers.rb +59 -16
  29. data/lib/stripe_mock/request_handlers/helpers/token_helpers.rb +3 -2
  30. data/lib/stripe_mock/request_handlers/invoices.rb +93 -14
  31. data/lib/stripe_mock/request_handlers/orders.rb +5 -5
  32. data/lib/stripe_mock/request_handlers/payouts.rb +32 -0
  33. data/lib/stripe_mock/request_handlers/plans.rb +1 -0
  34. data/lib/stripe_mock/request_handlers/products.rb +43 -0
  35. data/lib/stripe_mock/request_handlers/recipients.rb +12 -0
  36. data/lib/stripe_mock/request_handlers/refunds.rb +91 -0
  37. data/lib/stripe_mock/request_handlers/subscription_items.rb +36 -0
  38. data/lib/stripe_mock/request_handlers/subscriptions.rb +101 -39
  39. data/lib/stripe_mock/request_handlers/tax_rates.rb +36 -0
  40. data/lib/stripe_mock/request_handlers/tokens.rb +9 -3
  41. data/lib/stripe_mock/request_handlers/transfers.rb +11 -5
  42. data/lib/stripe_mock/request_handlers/validators/param_validators.rb +15 -1
  43. data/lib/stripe_mock/server.rb +14 -1
  44. data/lib/stripe_mock/test_strategies/base.rb +10 -5
  45. data/lib/stripe_mock/test_strategies/live.rb +5 -0
  46. data/lib/stripe_mock/test_strategies/mock.rb +8 -0
  47. data/lib/stripe_mock/util.rb +8 -2
  48. data/lib/stripe_mock/version.rb +1 -1
  49. data/lib/stripe_mock/webhook_fixtures/account.external_account.created.json +27 -0
  50. data/lib/stripe_mock/webhook_fixtures/account.external_account.deleted.json +27 -0
  51. data/lib/stripe_mock/webhook_fixtures/account.external_account.updated.json +27 -0
  52. data/lib/stripe_mock/webhook_fixtures/account.updated.json +1 -1
  53. data/lib/stripe_mock/webhook_fixtures/charge.dispute.funds_reinstated.json +88 -0
  54. data/lib/stripe_mock/webhook_fixtures/charge.dispute.funds_withdrawn.json +88 -0
  55. data/lib/stripe_mock/webhook_fixtures/charge.updated.json +58 -0
  56. data/lib/stripe_mock/webhook_fixtures/customer.subscription.created.json +40 -10
  57. data/lib/stripe_mock/webhook_fixtures/customer.subscription.deleted.json +39 -10
  58. data/lib/stripe_mock/webhook_fixtures/customer.subscription.trial_will_end.json +39 -10
  59. data/lib/stripe_mock/webhook_fixtures/customer.subscription.updated.json +40 -11
  60. data/lib/stripe_mock/webhook_fixtures/invoice.created.json +3 -2
  61. data/lib/stripe_mock/webhook_fixtures/invoice.payment_failed.json +1 -1
  62. data/lib/stripe_mock/webhook_fixtures/invoice.payment_succeeded.json +92 -85
  63. data/lib/stripe_mock/webhook_fixtures/invoice.updated.json +3 -2
  64. data/lib/stripe_mock/webhook_fixtures/plan.created.json +1 -1
  65. data/lib/stripe_mock/webhook_fixtures/plan.deleted.json +1 -1
  66. data/lib/stripe_mock/webhook_fixtures/plan.updated.json +1 -1
  67. data/lib/stripe_mock.rb +15 -0
  68. data/spec/api/instance_spec.rb +30 -0
  69. data/spec/instance_spec.rb +54 -4
  70. data/spec/integration_examples/prepare_error_examples.rb +6 -6
  71. data/spec/list_spec.rb +27 -10
  72. data/spec/readme_spec.rb +2 -0
  73. data/spec/server_spec.rb +7 -3
  74. data/spec/shared_stripe_examples/account_examples.rb +46 -0
  75. data/spec/shared_stripe_examples/balance_examples.rb +11 -0
  76. data/spec/shared_stripe_examples/balance_transaction_examples.rb +28 -0
  77. data/spec/shared_stripe_examples/bank_examples.rb +28 -1
  78. data/spec/shared_stripe_examples/card_examples.rb +23 -5
  79. data/spec/shared_stripe_examples/card_token_examples.rb +1 -0
  80. data/spec/shared_stripe_examples/charge_examples.rb +131 -26
  81. data/spec/shared_stripe_examples/country_specs_examples.rb +18 -0
  82. data/spec/shared_stripe_examples/coupon_examples.rb +8 -2
  83. data/spec/shared_stripe_examples/customer_examples.rb +90 -0
  84. data/spec/shared_stripe_examples/dispute_examples.rb +19 -8
  85. data/spec/shared_stripe_examples/ephemeral_key_examples.rb +17 -0
  86. data/spec/shared_stripe_examples/error_mock_examples.rb +15 -5
  87. data/spec/shared_stripe_examples/external_account_examples.rb +170 -0
  88. data/spec/shared_stripe_examples/extra_features_examples.rb +2 -0
  89. data/spec/shared_stripe_examples/invoice_examples.rb +314 -51
  90. data/spec/shared_stripe_examples/payout_examples.rb +68 -0
  91. data/spec/shared_stripe_examples/plan_examples.rb +47 -4
  92. data/spec/shared_stripe_examples/product_example.rb +65 -0
  93. data/spec/shared_stripe_examples/recipient_examples.rb +13 -7
  94. data/spec/shared_stripe_examples/refund_examples.rb +453 -84
  95. data/spec/shared_stripe_examples/subscription_examples.rb +477 -32
  96. data/spec/shared_stripe_examples/subscription_items_examples.rb +75 -0
  97. data/spec/shared_stripe_examples/tax_rate_examples.rb +42 -0
  98. data/spec/shared_stripe_examples/transfer_examples.rb +72 -23
  99. data/spec/shared_stripe_examples/webhook_event_examples.rb +74 -5
  100. data/spec/spec_helper.rb +7 -6
  101. data/spec/stripe_mock_spec.rb +16 -3
  102. data/spec/support/stripe_examples.rb +8 -1
  103. data/spec/util_spec.rb +35 -1
  104. data/stripe-ruby-mock.gemspec +1 -1
  105. metadata +44 -8
  106. 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
@@ -203,6 +304,30 @@ shared_examples 'Customer Subscriptions' do
203
304
  expect(customer.subscriptions.data.first.id).to eq(sub.id)
204
305
  expect(customer.subscriptions.data.first.plan.to_hash).to eq(plan.to_hash)
205
306
  expect(customer.subscriptions.data.first.customer).to eq(customer.id)
307
+ expect(customer.charges.count).to eq(0)
308
+ end
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)
206
331
  end
207
332
 
208
333
  it "subscribes a customer with no card to a free plan" do
@@ -237,6 +362,30 @@ shared_examples 'Customer Subscriptions' do
237
362
  expect(sub.trial_end).to eq(trial_end)
238
363
  end
239
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
+
240
389
  it "returns without a trial when trial_end is set to 'now'" do
241
390
  plan = stripe_helper.create_plan(id: 'trial', amount: 999, trial_period_days: 14)
242
391
  customer = Stripe::Customer.create(id: 'no_trial', source: gen_card_tk)
@@ -285,6 +434,137 @@ shared_examples 'Customer Subscriptions' do
285
434
  }
286
435
  end
287
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
+
288
568
  end
289
569
 
290
570
  context "updating a subscription" do
@@ -302,7 +582,19 @@ shared_examples 'Customer Subscriptions' do
302
582
  }
303
583
  end
304
584
 
305
- 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
306
598
  silver = stripe_helper.create_plan(id: 'silver')
307
599
  gold = stripe_helper.create_plan(id: 'gold')
308
600
  customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk, plan: 'silver')
@@ -331,8 +623,60 @@ shared_examples 'Customer Subscriptions' do
331
623
  expect(customer.subscriptions.data.first.customer).to eq(customer.id)
332
624
  end
333
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
+
334
677
  it 'when adds coupon', live: true do
335
- 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)
336
680
  coupon = stripe_helper.create_coupon
337
681
  customer = Stripe::Customer.create(source: gen_card_tk, plan: plan.id)
338
682
  subscription = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
@@ -341,12 +685,13 @@ shared_examples 'Customer Subscriptions' do
341
685
  subscription.save
342
686
 
343
687
  expect(subscription.discount).not_to be_nil
344
- expect(subscription.discount).to be_an_instance_of(Stripe::StripeObject)
688
+ expect(subscription.discount).to be_a(Stripe::Discount)
345
689
  expect(subscription.discount.coupon.id).to eq(coupon.id)
346
690
  end
347
691
 
348
692
  it 'when add not exist coupon' do
349
- 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)
350
695
  customer = Stripe::Customer.create(source: gen_card_tk, plan: plan.id)
351
696
  subscription = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
352
697
 
@@ -360,6 +705,21 @@ shared_examples 'Customer Subscriptions' do
360
705
 
361
706
  end
362
707
 
708
+ it 'when coupon is removed' do
709
+ plan = stripe_helper.create_plan(id: 'plan_with_coupon3', product: { name: 'One More Test Plan' },
710
+ amount: 777)
711
+ customer = Stripe::Customer.create(source: gen_card_tk, plan: plan.id)
712
+ coupon = stripe_helper.create_coupon
713
+ subscription = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
714
+
715
+ subscription.coupon = coupon.id
716
+ subscription.save
717
+ subscription.coupon = nil
718
+ subscription.save
719
+
720
+ expect(subscription.discount).to be_nil
721
+ end
722
+
363
723
  it "throws an error when plan does not exist" do
364
724
  free = stripe_helper.create_plan(id: 'free', amount: 0)
365
725
  customer = Stripe::Customer.create(id: 'cardless', plan: 'free')
@@ -395,24 +755,32 @@ shared_examples 'Customer Subscriptions' do
395
755
  expect(customer.subscriptions.data.first.plan.to_hash).to eq(free.to_hash)
396
756
  end
397
757
 
398
- it "throws an error when updating a customer with no card" do
399
- free = stripe_helper.create_plan(id: 'free', amount: 0)
400
- paid = stripe_helper.create_plan(id: 'enterprise', amount: 499)
401
- customer = Stripe::Customer.create(id: 'cardless', plan: 'free')
402
-
403
- sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
404
- sub.plan = 'enterprise'
405
-
406
- expect { sub.save }.to raise_error {|e|
407
- expect(e).to be_a Stripe::InvalidRequestError
408
- expect(e.http_status).to eq(400)
409
- expect(e.message).to_not be_nil
410
- }
411
-
412
- customer = Stripe::Customer.retrieve('cardless')
413
- expect(customer.subscriptions.count).to eq(1)
414
- expect(customer.subscriptions.data.length).to eq(1)
415
- 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
416
784
  end
417
785
 
418
786
  it 'updates a subscription if the customer has a free trial', live: true do
@@ -616,6 +984,52 @@ shared_examples 'Customer Subscriptions' do
616
984
  end
617
985
  end
618
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
+
619
1033
  it "doesn't change status of subscription when cancelling at period end" do
620
1034
  trial = stripe_helper.create_plan(id: 'trial', trial_period_days: 14)
621
1035
  customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk, plan: "trial")
@@ -644,6 +1058,34 @@ shared_examples 'Customer Subscriptions' do
644
1058
  Stripe::Subscription.create options
645
1059
  end
646
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
+
647
1089
  context "retrieve multiple subscriptions" do
648
1090
 
649
1091
  it "retrieves a list of multiple subscriptions" do
@@ -675,10 +1117,13 @@ shared_examples 'Customer Subscriptions' do
675
1117
 
676
1118
  it "creates a stripe customer and subscribes them to a plan with meta data", :live => true do
677
1119
 
678
- stripe_helper.create_plan(
1120
+ stripe_helper.
1121
+ create_plan(
679
1122
  :amount => 500,
680
1123
  :interval => 'month',
681
- :name => 'Sample Plan',
1124
+ :product => {
1125
+ :name => 'Sample Plan'
1126
+ },
682
1127
  :currency => 'usd',
683
1128
  :id => 'Sample5'
684
1129
  )