stripe-ruby-mock 2.5.8 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -1
  3. data/.travis.yml +6 -6
  4. data/README.md +8 -8
  5. data/lib/stripe_mock.rb +3 -0
  6. data/lib/stripe_mock/api/errors.rb +31 -28
  7. data/lib/stripe_mock/api/webhooks.rb +3 -0
  8. data/lib/stripe_mock/data.rb +140 -13
  9. data/lib/stripe_mock/instance.rb +10 -3
  10. data/lib/stripe_mock/request_handlers/balance_transactions.rb +2 -2
  11. data/lib/stripe_mock/request_handlers/helpers/subscription_helpers.rb +1 -1
  12. data/lib/stripe_mock/request_handlers/helpers/token_helpers.rb +1 -1
  13. data/lib/stripe_mock/request_handlers/invoices.rb +6 -1
  14. data/lib/stripe_mock/request_handlers/payment_intents.rb +175 -0
  15. data/lib/stripe_mock/request_handlers/payment_methods.rb +138 -0
  16. data/lib/stripe_mock/request_handlers/products.rb +1 -0
  17. data/lib/stripe_mock/request_handlers/setup_intents.rb +93 -0
  18. data/lib/stripe_mock/request_handlers/subscriptions.rb +17 -4
  19. data/lib/stripe_mock/request_handlers/validators/param_validators.rb +95 -9
  20. data/lib/stripe_mock/test_strategies/base.rb +42 -8
  21. data/lib/stripe_mock/test_strategies/live.rb +23 -12
  22. data/lib/stripe_mock/test_strategies/mock.rb +6 -2
  23. data/lib/stripe_mock/version.rb +1 -1
  24. data/lib/stripe_mock/webhook_fixtures/charge.failed.json +166 -38
  25. data/lib/stripe_mock/webhook_fixtures/customer.created.json +1 -0
  26. data/lib/stripe_mock/webhook_fixtures/customer.updated.json +1 -0
  27. data/lib/stripe_mock/webhook_fixtures/product.created.json +34 -0
  28. data/lib/stripe_mock/webhook_fixtures/product.deleted.json +34 -0
  29. data/lib/stripe_mock/webhook_fixtures/product.updated.json +38 -0
  30. data/spec/instance_spec.rb +6 -6
  31. data/spec/server_spec.rb +2 -1
  32. data/spec/shared_stripe_examples/account_examples.rb +1 -1
  33. data/spec/shared_stripe_examples/balance_transaction_examples.rb +3 -3
  34. data/spec/shared_stripe_examples/bank_examples.rb +3 -3
  35. data/spec/shared_stripe_examples/card_examples.rb +4 -4
  36. data/spec/shared_stripe_examples/charge_examples.rb +9 -22
  37. data/spec/shared_stripe_examples/coupon_examples.rb +1 -1
  38. data/spec/shared_stripe_examples/customer_examples.rb +44 -30
  39. data/spec/shared_stripe_examples/dispute_examples.rb +2 -2
  40. data/spec/shared_stripe_examples/error_mock_examples.rb +8 -7
  41. data/spec/shared_stripe_examples/external_account_examples.rb +3 -3
  42. data/spec/shared_stripe_examples/invoice_examples.rb +41 -39
  43. data/spec/shared_stripe_examples/invoice_item_examples.rb +1 -1
  44. data/spec/shared_stripe_examples/payment_intent_examples.rb +137 -0
  45. data/spec/shared_stripe_examples/payment_method_examples.rb +185 -0
  46. data/spec/shared_stripe_examples/payout_examples.rb +2 -2
  47. data/spec/shared_stripe_examples/plan_examples.rb +135 -92
  48. data/spec/shared_stripe_examples/product_examples.rb +155 -0
  49. data/spec/shared_stripe_examples/refund_examples.rb +25 -21
  50. data/spec/shared_stripe_examples/setup_intent_examples.rb +68 -0
  51. data/spec/shared_stripe_examples/subscription_examples.rb +281 -310
  52. data/spec/shared_stripe_examples/subscription_items_examples.rb +3 -2
  53. data/spec/shared_stripe_examples/transfer_examples.rb +6 -6
  54. data/spec/shared_stripe_examples/webhook_event_examples.rb +11 -11
  55. data/spec/spec_helper.rb +3 -4
  56. data/spec/stripe_mock_spec.rb +2 -2
  57. data/spec/support/shared_contexts/stripe_validator_spec.rb +8 -0
  58. data/spec/support/stripe_examples.rb +3 -0
  59. data/stripe-ruby-mock.gemspec +2 -2
  60. metadata +47 -27
  61. data/spec/shared_stripe_examples/product_example.rb +0 -65
@@ -0,0 +1,155 @@
1
+ require "spec_helper"
2
+
3
+ shared_examples "Product API" do
4
+ let(:product_attributes) { {id: "prod_123", name: "My Mock Product", type: "service"} }
5
+ let(:product) { Stripe::Product.create(product_attributes) }
6
+
7
+ it "creates a stripe product" do
8
+ expect(product.id).to eq("prod_123")
9
+ expect(product.name).to eq("My Mock Product")
10
+ expect(product.type).to eq("service")
11
+ end
12
+
13
+ it "stores a created stripe product in memory" do
14
+ product = Stripe::Product.create(product_attributes)
15
+ product2 = Stripe::Product.create(product_attributes.merge({id: "prod_456", name: "My Other Product"}))
16
+
17
+ data = test_data_source(:products)
18
+ expect(data[product.id]).to_not be_nil
19
+ expect(data[product.id][:id]).to eq("prod_123")
20
+ expect(data[product.id][:name]).to eq("My Mock Product")
21
+ expect(data[product2.id]).to_not be_nil
22
+ expect(data[product2.id][:id]).to eq("prod_456")
23
+ expect(data[product2.id][:name]).to eq("My Other Product")
24
+ end
25
+
26
+ it "retrieves a stripe product" do
27
+ original = stripe_helper.create_product(product_attributes)
28
+ product = Stripe::Product.retrieve(original.id)
29
+
30
+ expect(product.id).to eq(original.id)
31
+ expect(product.name).to eq(original.name)
32
+ end
33
+
34
+ it "updates a stripe product" do
35
+ stripe_helper.create_product(id: "prod_XYZ", name: "Future Product")
36
+
37
+ product = Stripe::Product.retrieve("prod_XYZ")
38
+ expect(product.name).to eq("Future Product")
39
+
40
+ product.name = "Updated Product"
41
+ product.save
42
+ product = Stripe::Product.retrieve("prod_XYZ")
43
+ expect(product.name).to eq("Updated Product")
44
+ end
45
+
46
+ it "cannot retrieve a stripe product that doesn't exist" do
47
+ expect { Stripe::Product.retrieve('nope') }.to raise_error {|e|
48
+ expect(e).to be_a Stripe::InvalidRequestError
49
+ expect(e.param).to eq("product")
50
+ expect(e.http_status).to eq(404)
51
+ }
52
+ end
53
+
54
+ it "deletes a stripe product" do
55
+ stripe_helper.create_product(id: "prod_DEL", name: "Aging Product")
56
+
57
+ product = Stripe::Product.retrieve("prod_DEL")
58
+ expect(product).to_not be_nil
59
+
60
+ product.delete
61
+
62
+ expect { Stripe::Product.retrieve("prod_DEL") }.to raise_error {|e|
63
+ expect(e).to be_a Stripe::InvalidRequestError
64
+ expect(e.param).to eq("product")
65
+ expect(e.http_status).to eq(404)
66
+ }
67
+ end
68
+
69
+ it "retrieves all products" do
70
+ stripe_helper.create_product(id: "prod_123", name: "First Product")
71
+ stripe_helper.create_product(id: "prod_456", name: "Second Product")
72
+
73
+ all = Stripe::Product.list
74
+ expect(all.count).to eq(2)
75
+ expect(all.map &:id).to include("prod_123", "prod_456")
76
+ expect(all.map &:name).to include("First Product", "Second Product")
77
+ end
78
+
79
+ it 'retrieves products with limit' do
80
+ 101.times do |i|
81
+ stripe_helper.create_product(id: "Product #{i}", name: "My Product ##{i}")
82
+ end
83
+ all = Stripe::Product.list(limit: 100)
84
+
85
+ expect(all.count).to eq(100)
86
+ end
87
+
88
+ describe "Validation", :live => true do
89
+ include_context "stripe validator"
90
+ let(:params) { stripe_helper.create_product_params }
91
+ let(:subject) { Stripe::Product.create(params) }
92
+ before { stripe_helper.delete_product(params[:id]) }
93
+
94
+ describe "Required Parameters" do
95
+ after do
96
+ params.delete(@attribute_name)
97
+ message = stripe_validator.missing_param_message(@attribute_name)
98
+ expect { subject }.to raise_error(Stripe::InvalidRequestError, message)
99
+ end
100
+
101
+ it("requires a name") { @attribute_name = :name }
102
+ end
103
+
104
+ describe "Inclusion" do
105
+ it "validates inclusion of type in 'good' or 'service'" do
106
+ expect {
107
+ Stripe::Product.create(params.merge({type: "OOPS"}))
108
+ }.to raise_error(Stripe::InvalidRequestError, "Invalid type: must be one of good or service")
109
+ end
110
+ end
111
+
112
+ describe "Uniqueness" do
113
+ let(:already_exists_message){ stripe_validator.already_exists_message(Stripe::Product) }
114
+
115
+ it "validates uniqueness of identifier" do
116
+ stripe_helper.delete_product(params[:id])
117
+
118
+ Stripe::Product.create(params)
119
+ expect {
120
+ Stripe::Product.create(params)
121
+ }.to raise_error(Stripe::InvalidRequestError, already_exists_message)
122
+ end
123
+ end
124
+ end
125
+
126
+ describe "Mock Data" do
127
+ let(:mock_object) { StripeMock::Data.mock_product }
128
+ let(:known_attributes) { [
129
+ :id,
130
+ :object,
131
+ :active,
132
+ :attributes,
133
+ :caption,
134
+ :created,
135
+ :deactivate_on,
136
+ :description,
137
+ :images,
138
+ :livemode,
139
+ :metadata,
140
+ :name,
141
+ :package_dimensions,
142
+ :shippable,
143
+ :statement_descriptor,
144
+ :type,
145
+ :unit_label,
146
+ :updated,
147
+ :url
148
+ ] }
149
+
150
+ it "includes all retreived attributes" do
151
+ expect(mock_object.keys).to eql(known_attributes)
152
+ end
153
+ end
154
+
155
+ end
@@ -30,9 +30,7 @@ shared_examples 'Refund API' do
30
30
  description: 'card charge'
31
31
  )
32
32
 
33
- Stripe::Refund.create(
34
- charge: charge.id
35
- )
33
+ Stripe::Refund.create(charge: charge.id)
36
34
 
37
35
  charge = Stripe::Charge.retrieve(charge.id)
38
36
 
@@ -262,7 +260,7 @@ shared_examples 'Refund API' do
262
260
  end
263
261
 
264
262
  it "stores all charges in memory" do
265
- expect(Stripe::Refund.all.data.map(&:id)).to eq([@refund2.id, @refund.id])
263
+ expect(Stripe::Refund.list.data.map(&:id)).to eq([@refund2.id, @refund.id])
266
264
  end
267
265
 
268
266
  it "defaults count to 10 charges" do
@@ -275,7 +273,7 @@ shared_examples 'Refund API' do
275
273
  Stripe::Refund.create(charge: charge.id)
276
274
  end
277
275
 
278
- expect(Stripe::Refund.all.data.count).to eq(10)
276
+ expect(Stripe::Refund.list.data.count).to eq(10)
279
277
  end
280
278
 
281
279
  it "is marked as having more when more objects exist" do
@@ -288,12 +286,12 @@ shared_examples 'Refund API' do
288
286
  Stripe::Refund.create(charge: charge.id)
289
287
  end
290
288
 
291
- expect(Stripe::Refund.all.has_more).to eq(true)
289
+ expect(Stripe::Refund.list.has_more).to eq(true)
292
290
  end
293
291
 
294
292
  context "when passing limit" do
295
293
  it "gets that many charges" do
296
- expect(Stripe::Refund.all(limit: 1).count).to eq(1)
294
+ expect(Stripe::Refund.list(limit: 1).count).to eq(1)
297
295
  end
298
296
  end
299
297
  end
@@ -318,13 +316,13 @@ shared_examples 'Refund API' do
318
316
  Stripe::Refund.create(charge: charge.id)
319
317
  end
320
318
 
321
- all = Stripe::Refund.all
319
+ all_refunds = Stripe::Refund.list
322
320
  default_limit = 10
323
- half = Stripe::Refund.all(starting_after: all.data.at(1).id)
321
+ half = Stripe::Refund.list(starting_after: all_refunds.data.at(1).id)
324
322
 
325
323
  expect(half).to be_a(Stripe::ListObject)
326
324
  expect(half.data.count).to eq(default_limit)
327
- expect(half.data.first.id).to eq(all.data.at(2).id)
325
+ expect(half.data.first.id).to eq(all_refunds.data.at(2).id)
328
326
  end
329
327
 
330
328
  describe "idempotency" do
@@ -378,7 +376,8 @@ shared_examples 'Refund API' do
378
376
  description: 'card charge'
379
377
  )
380
378
 
381
- charge = charge.refund(amount: 999)
379
+ Stripe::Refund.create(charge: charge.id, amount: 999)
380
+ charge.refresh
382
381
 
383
382
  expect(charge.refunded).to eq(true)
384
383
  expect(charge.refunds.data.first.amount).to eq(999)
@@ -392,10 +391,10 @@ shared_examples 'Refund API' do
392
391
  source: stripe_helper.generate_card_token,
393
392
  description: 'card charge'
394
393
  )
395
- refund = charge.refund
394
+ refund = Stripe::Refund.create(charge: charge.id)
396
395
 
397
396
  expect(charge.id).to match(/^(test_)?ch/)
398
- expect(refund.id).to eq(charge.id)
397
+ expect(refund.charge).to eq(charge.id)
399
398
  end
400
399
 
401
400
  it "creates a stripe refund with a refund ID" do
@@ -405,10 +404,12 @@ shared_examples 'Refund API' do
405
404
  source: stripe_helper.generate_card_token,
406
405
  description: 'card charge'
407
406
  )
408
- refund = charge.refund
409
407
 
410
- expect(refund.refunds.data.count).to eq 1
411
- expect(refund.refunds.data.first.id).to match(/^test_re/)
408
+ Stripe::Refund.create(charge: charge.id)
409
+ refunds = Stripe::Refund.list(charge: charge.id)
410
+
411
+ expect(refunds.data.count).to eq 1
412
+ expect(refunds.data.first.id).to match(/^test_re/)
412
413
  end
413
414
 
414
415
  it "creates a stripe refund with a status" do
@@ -418,10 +419,12 @@ shared_examples 'Refund API' do
418
419
  source: stripe_helper.generate_card_token,
419
420
  description: 'card charge'
420
421
  )
421
- refund = charge.refund
422
422
 
423
- expect(refund.refunds.data.count).to eq 1
424
- expect(refund.refunds.data.first.status).to eq("succeeded")
423
+ Stripe::Refund.create(charge: charge.id)
424
+ refunds = Stripe::Refund.list(charge: charge.id)
425
+
426
+ expect(refunds.data.count).to eq 1
427
+ expect(refunds.data.first.status).to eq("succeeded")
425
428
  end
426
429
 
427
430
  it "creates a stripe refund with a different balance transaction than the charge" do
@@ -431,9 +434,10 @@ shared_examples 'Refund API' do
431
434
  source: stripe_helper.generate_card_token,
432
435
  description: 'card charge'
433
436
  )
434
- refund = charge.refund
437
+ Stripe::Refund.create(charge: charge.id)
438
+ refunds = Stripe::Refund.list(charge: charge.id)
435
439
 
436
- expect(charge.balance_transaction).not_to eq(refund.refunds.data.first.balance_transaction)
440
+ expect(charge.balance_transaction).not_to eq(refunds.data.first.balance_transaction)
437
441
  end
438
442
 
439
443
  it "creates a refund off a charge", :live => true do
@@ -0,0 +1,68 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples 'SetupIntent API' do
4
+
5
+ it "creates a stripe setup_intent" do
6
+ setup_intent = Stripe::SetupIntent.create()
7
+
8
+ expect(setup_intent.id).to match(/^test_si/)
9
+ expect(setup_intent.metadata.to_hash).to eq({})
10
+ expect(setup_intent.status).to eq('requires_payment_method')
11
+ end
12
+
13
+ describe "listing setup_intent" do
14
+ before do
15
+ 3.times do
16
+ Stripe::SetupIntent.create()
17
+ end
18
+ end
19
+
20
+ it "without params retrieves all stripe setup_intent" do
21
+ expect(Stripe::SetupIntent.list.count).to eq(3)
22
+ end
23
+
24
+ it "accepts a limit param" do
25
+ expect(Stripe::SetupIntent.list(limit: 2).count).to eq(2)
26
+ end
27
+ end
28
+
29
+ it "retrieves a stripe setup_intent" do
30
+ original = Stripe::SetupIntent.create()
31
+ setup_intent = Stripe::SetupIntent.retrieve(original.id)
32
+
33
+ expect(setup_intent.id).to eq(original.id)
34
+ expect(setup_intent.metadata.to_hash).to eq(original.metadata.to_hash)
35
+ end
36
+
37
+ it "cannot retrieve a setup_intent that doesn't exist" do
38
+ expect { Stripe::SetupIntent.retrieve('nope') }.to raise_error {|e|
39
+ expect(e).to be_a Stripe::InvalidRequestError
40
+ expect(e.param).to eq('setup_intent')
41
+ expect(e.http_status).to eq(404)
42
+ }
43
+ end
44
+
45
+ it "confirms a stripe setup_intent" do
46
+ setup_intent = Stripe::SetupIntent.create()
47
+ confirmed_setup_intent = setup_intent.confirm()
48
+ expect(confirmed_setup_intent.status).to eq("succeeded")
49
+ end
50
+
51
+ it "cancels a stripe setup_intent" do
52
+ setup_intent = Stripe::SetupIntent.create()
53
+ confirmed_setup_intent = setup_intent.cancel()
54
+ expect(confirmed_setup_intent.status).to eq("canceled")
55
+ end
56
+
57
+ it "updates a stripe setup_intent" do
58
+ original = Stripe::SetupIntent.create()
59
+ setup_intent = Stripe::SetupIntent.retrieve(original.id)
60
+
61
+ setup_intent.metadata[:foo] = :bar
62
+ setup_intent.save
63
+
64
+ updated = Stripe::SetupIntent.retrieve(original.id)
65
+
66
+ expect(updated.metadata[:foo]).to eq(:bar)
67
+ end
68
+ end
@@ -1,49 +1,61 @@
1
1
  require 'spec_helper'
2
+ require 'securerandom'
2
3
 
3
4
  shared_examples 'Customer Subscriptions' do
5
+ let(:gen_card_tk) { stripe_helper.generate_card_token }
4
6
 
5
- def gen_card_tk
6
- stripe_helper.generate_card_token
7
- end
7
+ let(:product) { stripe_helper.create_product }
8
+ let(:plan_attrs) { {id: 'silver', product: product.id, amount: 4999, currency: 'usd'} }
9
+ let(:plan) { stripe_helper.create_plan(plan_attrs) }
10
+
11
+ let(:plan_with_trial_attrs) { {id: 'trial', product: product.id, amount: 999, trial_period_days: 14 } }
12
+ let(:plan_with_trial) { stripe_helper.create_plan(plan_with_trial_attrs) }
13
+
14
+ let(:free_plan) { stripe_helper.create_plan(id: 'free', product: product.id, amount: 0) }
8
15
 
9
16
  context "creating a new subscription" do
10
17
  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')
18
+ plan
13
19
  customer = Stripe::Customer.create(source: gen_card_tk)
14
20
 
15
21
  expect(customer.subscriptions.data).to be_empty
16
22
  expect(customer.subscriptions.count).to eq(0)
17
23
 
18
- sub = Stripe::Subscription.create({ items: [{ plan: 'silver' }],
19
- customer: customer.id, metadata: { foo: "bar", example: "yes" } })
24
+ subscription = Stripe::Subscription.create({
25
+ customer: customer.id,
26
+ items: [{ plan: 'silver' }],
27
+ metadata: { foo: "bar", example: "yes" }
28
+ })
20
29
 
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" )
30
+ expect(subscription.object).to eq('subscription')
31
+ expect(subscription.plan.to_hash).to eq(plan.to_hash)
32
+ expect(subscription.metadata.foo).to eq("bar")
33
+ expect(subscription.metadata.example).to eq("yes")
25
34
 
26
35
  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" )
36
+ subscriptions = Stripe::Subscription.list(customer: customer.id)
37
+ charges = Stripe::Charge.list(customer: customer.id)
38
+
39
+ expect(subscriptions.data).to_not be_empty
40
+ expect(subscriptions.count).to eq(1)
41
+ expect(subscriptions.data.length).to eq(1)
42
+ expect(charges.data.length).to eq(1)
43
+ expect(customer.currency).to eq("usd")
44
+
45
+ expect(subscriptions.data.first.id).to eq(subscription.id)
46
+ expect(subscriptions.data.first.plan.to_hash).to eq(plan.to_hash)
47
+ expect(subscriptions.data.first.customer).to eq(customer.id)
48
+ expect(subscriptions.data.first.metadata.foo).to eq( "bar" )
49
+ expect(subscriptions.data.first.metadata.example).to eq( "yes" )
38
50
  end
39
51
 
40
52
  it "adds a new subscription to customer with none", :live => true do
41
- plan = stripe_helper.create_plan(id: 'silver', product: { name: 'Silver Plan' },
42
- amount: 4999, currency: 'usd')
53
+ plan
43
54
  customer = Stripe::Customer.create(source: gen_card_tk)
55
+ subscriptions = Stripe::Subscription.list(customer: customer.id)
44
56
 
45
- expect(customer.subscriptions.data).to be_empty
46
- expect(customer.subscriptions.count).to eq(0)
57
+ expect(subscriptions.data).to be_empty
58
+ expect(subscriptions.count).to eq(0)
47
59
 
48
60
  sub = Stripe::Subscription.create({ plan: 'silver', customer: customer.id, metadata: { foo: "bar", example: "yes" } })
49
61
 
@@ -53,26 +65,30 @@ shared_examples 'Customer Subscriptions' do
53
65
  expect(sub.metadata.example).to eq( "yes" )
54
66
 
55
67
  customer = Stripe::Customer.retrieve(customer.id)
56
- expect(customer.subscriptions.data).to_not be_empty
57
- expect(customer.subscriptions.count).to eq(1)
58
- expect(customer.subscriptions.data.length).to eq(1)
59
- expect(customer.charges.data.length).to eq(1)
68
+ subscriptions = Stripe::Subscription.list(customer: customer.id)
69
+ charges = Stripe::Charge.list(customer: customer.id)
70
+
71
+ expect(subscriptions.data).to_not be_empty
72
+ expect(subscriptions.count).to eq(1)
73
+ expect(subscriptions.data.length).to eq(1)
74
+ expect(charges.data.length).to eq(1)
60
75
  expect(customer.currency).to eq( "usd" )
61
76
 
62
- expect(customer.subscriptions.data.first.id).to eq(sub.id)
63
- expect(customer.subscriptions.data.first.plan.to_hash).to eq(plan.to_hash)
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" )
77
+ expect(subscriptions.data.first.id).to eq(sub.id)
78
+ expect(subscriptions.data.first.plan.to_hash).to eq(plan.to_hash)
79
+ expect(subscriptions.data.first.customer).to eq(customer.id)
80
+ expect(subscriptions.data.first.billing).to eq('charge_automatically')
81
+ expect(subscriptions.data.first.metadata.foo).to eq( "bar" )
82
+ expect(subscriptions.data.first.metadata.example).to eq( "yes" )
68
83
  end
69
84
 
70
85
  it 'when customer object provided' do
71
- plan = stripe_helper.create_plan(id: 'silver', product: { name: 'Silver Plan' }, amount: 4999, currency: 'usd')
86
+ plan
72
87
  customer = Stripe::Customer.create(source: gen_card_tk)
88
+ subscriptions = Stripe::Subscription.list(customer: customer.id)
73
89
 
74
- expect(customer.subscriptions.data).to be_empty
75
- expect(customer.subscriptions.count).to eq(0)
90
+ expect(subscriptions.data).to be_empty
91
+ expect(subscriptions.count).to eq(0)
76
92
 
77
93
  sub = Stripe::Subscription.create({ plan: 'silver', customer: customer, metadata: { foo: "bar", example: "yes" } })
78
94
 
@@ -83,94 +99,85 @@ shared_examples 'Customer Subscriptions' do
83
99
  expect(sub.metadata.example).to eq( "yes" )
84
100
 
85
101
  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)
102
+ subscriptions = Stripe::Subscription.list(customer: customer.id)
103
+ charges = Stripe::Charge.list(customer: customer.id)
104
+
105
+ expect(subscriptions.data).to_not be_empty
106
+ expect(subscriptions.count).to eq(1)
107
+ expect(subscriptions.data.length).to eq(1)
108
+ expect(charges.data.length).to eq(1)
90
109
  expect(customer.currency).to eq( "usd" )
91
110
 
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)
111
+ expect(subscriptions.data.first.id).to eq(sub.id)
112
+ expect(subscriptions.data.first.plan.to_hash).to eq(plan.to_hash)
94
113
 
95
- expect(customer.subscriptions.data.first.customer).to eq(customer.id)
96
- expect(customer.subscriptions.data.first.billing).to eq('charge_automatically')
97
- expect(customer.subscriptions.data.first.metadata.foo).to eq( "bar" )
98
- expect(customer.subscriptions.data.first.metadata.example).to eq( "yes" )
114
+ expect(subscriptions.data.first.customer).to eq(customer.id)
115
+ expect(subscriptions.data.first.billing).to eq('charge_automatically')
116
+ expect(subscriptions.data.first.metadata.foo).to eq( "bar" )
117
+ expect(subscriptions.data.first.metadata.example).to eq( "yes" )
99
118
  end
100
119
 
101
120
  it "adds a new subscription to customer (string/symbol agnostic)" do
102
121
  customer = Stripe::Customer.create(source: gen_card_tk)
103
- expect(customer.subscriptions.count).to eq(0)
122
+ subscriptions = Stripe::Subscription.list(customer: customer.id)
123
+ expect(subscriptions.count).to eq(0)
104
124
 
105
- plan = stripe_helper.create_plan(id: :silver, product: { name: 'Silver Plan' },
106
- amount: 4999, currency: 'usd')
107
- sub = Stripe::Subscription.create({ plan: 'silver', customer: customer.id })
108
- customer = Stripe::Customer.retrieve(customer.id)
109
- expect(sub.plan.to_hash).to eq(plan.to_hash)
110
- expect(customer.subscriptions.count).to eq(1)
125
+ plan
126
+ sub = Stripe::Subscription.create({plan: plan.id, customer: customer.id })
111
127
 
112
- plan = stripe_helper.create_plan(id: 'gold', product: { name: 'Gold Plan' },
113
- amount: 14999, currency: 'usd')
114
- sub = Stripe::Subscription.create({ plan: 'gold', customer: customer.id })
115
128
  customer = Stripe::Customer.retrieve(customer.id)
129
+ subscriptions = Stripe::Subscription.list(customer: customer.id)
116
130
  expect(sub.plan.to_hash).to eq(plan.to_hash)
117
- expect(customer.subscriptions.count).to eq(2)
131
+ expect(subscriptions.count).to eq(1)
118
132
  end
119
133
 
120
134
  it 'creates a charge for the customer', live: true do
121
- stripe_helper.create_plan(id: 'silver', product: { name: 'Silver Plan' },
122
- amount: 4999)
123
-
124
135
  customer = Stripe::Customer.create(source: gen_card_tk)
125
- Stripe::Subscription.create({ plan: 'silver', customer: customer.id, metadata: { foo: "bar", example: "yes" } })
136
+ Stripe::Subscription.create({ plan: plan.id, customer: customer.id, metadata: { foo: "bar", example: "yes" } })
126
137
  customer = Stripe::Customer.retrieve(customer.id)
138
+ charges = Stripe::Charge.list(customer: customer.id)
127
139
 
128
- expect(customer.charges.data.length).to eq(1)
129
- expect(customer.charges.data.first.amount).to eq(4999)
140
+ expect(charges.data.length).to eq(1)
141
+ expect(charges.data.first.amount).to eq(4999)
130
142
  end
131
143
 
132
144
  it 'contains coupon object', live: true do
133
- plan = stripe_helper.create_plan(id: 'plan_with_coupon', product: { name: 'One More Test Plan' },
134
- amount: 777)
135
145
  coupon = stripe_helper.create_coupon(id: 'free_coupon', duration: 'repeating', duration_in_months: 3)
136
146
  customer = Stripe::Customer.create(source: gen_card_tk)
137
147
  Stripe::Subscription.create(plan: plan.id, customer: customer.id, coupon: coupon.id)
138
148
  customer = Stripe::Customer.retrieve(customer.id)
139
149
 
140
- expect(customer.subscriptions.data).to be_a(Array)
141
- expect(customer.subscriptions.data.count).to eq(1)
142
- expect(customer.subscriptions.data.first.discount).not_to be_nil
143
- expect(customer.subscriptions.data.first.discount).to be_a(Stripe::Discount)
144
- expect(customer.subscriptions.data.first.discount.coupon.id).to eq(coupon.id)
150
+ subscriptions = Stripe::Subscription.list(customer: customer.id)
151
+
152
+ expect(subscriptions.data).to be_a(Array)
153
+ expect(subscriptions.data.count).to eq(1)
154
+ expect(subscriptions.data.first.discount).not_to be_nil
155
+ expect(subscriptions.data.first.discount).to be_a(Stripe::Discount)
156
+ expect(subscriptions.data.first.discount.coupon.id).to eq(coupon.id)
145
157
  end
146
158
 
147
159
  it 'when coupon is not exist', live: true do
148
- plan = stripe_helper.create_plan(id: 'plan_with_coupon', product: { name: 'One More Test Plan' },
149
- amount: 777)
150
160
  customer = Stripe::Customer.create(source: gen_card_tk)
151
161
 
152
- expect { Stripe::Subscription.create(plan: plan.id, customer: customer.id, coupon: 'none') }.to raise_error {|e|
153
- expect(e).to be_a Stripe::InvalidRequestError
154
- expect(e.http_status).to eq(400)
155
- expect(e.message).to eq('No such coupon: none')
156
- }
162
+ expect {
163
+ Stripe::Subscription.create(plan: plan.id, customer: customer.id, coupon: 'none')
164
+ }.to raise_error {|e|
165
+ expect(e).to be_a Stripe::InvalidRequestError
166
+ expect(e.http_status).to eq(400)
167
+ expect(e.message).to eq('No such coupon: none')
168
+ }
157
169
  end
158
170
 
159
171
  it "correctly sets quantity, application_fee_percent and tax_percent" do
160
- Stripe::Plan.create(
161
- :amount => 2500,
162
- :interval => 'month',
163
- :product => {
164
- :name => 'Test plan'
165
- },
166
- :currency => 'usd',
167
- :id => 'silver',
168
- :statement_description => "testPlan"
169
- )
170
172
  customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk)
171
173
 
172
174
  subscription = Stripe::Subscription.create({
173
- plan: "silver", customer: customer.id, quantity: 2, application_fee_percent: 10, tax_percent: 20})
175
+ plan: plan.id,
176
+ customer: customer.id,
177
+ quantity: 2,
178
+ application_fee_percent: 10,
179
+ tax_percent: 20
180
+ })
174
181
  expect(subscription.quantity).to eq(2)
175
182
  expect(subscription.application_fee_percent).to eq(10)
176
183
  expect(subscription.tax_percent).to eq(20)
@@ -178,26 +185,22 @@ shared_examples 'Customer Subscriptions' do
178
185
 
179
186
  it "correctly sets created when it's not provided as a parameter", live: true do
180
187
  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 })
188
+ subscription = Stripe::Subscription.create({ plan: plan.id, customer: customer.id })
184
189
 
185
190
  expect(subscription.created).to eq(subscription.current_period_start)
186
191
  end
187
192
 
188
193
  it "correctly sets created when it's provided as a parameter" do
189
194
  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 })
195
+ subscription = Stripe::Subscription.create({ plan: plan.id, customer: customer.id, created: 1473576318 })
193
196
 
194
197
  expect(subscription.created).to eq(1473576318)
195
198
  end
196
199
 
197
200
  it "adds additional subscription to customer with existing subscription" do
198
- silver = stripe_helper.create_plan(id: 'silver')
199
- gold = stripe_helper.create_plan(id: 'gold')
200
- customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk, plan: 'gold')
201
+ silver = stripe_helper.create_plan(id: 'silver', product: product.id)
202
+ gold = stripe_helper.create_plan(id: 'gold', product: product.id)
203
+ customer = Stripe::Customer.create(id: 'test_customer_sub', product: product.id, source: gen_card_tk, plan: 'gold')
201
204
 
202
205
  sub = Stripe::Subscription.create({ plan: 'silver', customer: customer.id })
203
206
 
@@ -218,7 +221,7 @@ shared_examples 'Customer Subscriptions' do
218
221
  end
219
222
 
220
223
  it "subscribes a cardless customer when specifing a card token" do
221
- plan = stripe_helper.create_plan(id: 'enterprise', amount: 499)
224
+ plan = stripe_helper.create_plan(id: 'enterprise', product: product.id, amount: 499)
222
225
  customer = Stripe::Customer.create(id: 'cardless')
223
226
 
224
227
  sub = Stripe::Subscription.create(plan: 'enterprise', customer: customer.id, source: gen_card_tk)
@@ -247,10 +250,10 @@ shared_examples 'Customer Subscriptions' do
247
250
  end
248
251
 
249
252
  it "throws an error when subscribing a customer with no card" do
250
- plan = stripe_helper.create_plan(id: 'enterprise', amount: 499)
253
+ plan = stripe_helper.create_plan(id: 'enterprise', product: product.id, amount: 499)
251
254
  customer = Stripe::Customer.create(id: 'cardless')
252
255
 
253
- expect { Stripe::Subscription.create({ plan: 'enterprise', customer: customer.id }) }.to raise_error {|e|
256
+ expect { Stripe::Subscription.create({ plan: plan.id, customer: customer.id }) }.to raise_error {|e|
254
257
  expect(e).to be_a Stripe::InvalidRequestError
255
258
  expect(e.http_status).to eq(400)
256
259
  expect(e.message).to_not be_nil
@@ -261,12 +264,14 @@ shared_examples 'Customer Subscriptions' do
261
264
  end
262
265
 
263
266
  it "throws an error when subscribing the customer to a second plan in a different currency" do
264
- usd_plan = stripe_helper.create_plan(id: 'enterprise_usd', amount: 499, currency: 'usd')
265
267
  customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk)
266
- usd_subscription = Stripe::Subscription.create({ plan: 'enterprise_usd', customer: customer.id })
267
268
 
268
- eur_plan = stripe_helper.create_plan(id: 'enterprise_eur', amount: 499, currency: 'eur')
269
- expect { Stripe::Subscription.create({ plan: 'enterprise_eur', customer: customer.id }) }.to raise_error {|e|
269
+ expect(plan.currency).to eql("usd")
270
+ usd_subscription = Stripe::Subscription.create({ plan: plan.id, customer: customer.id })
271
+
272
+ eur_plan = stripe_helper.create_plan(plan_attrs.merge(id: "plan_EURO", currency: 'eur'))
273
+ expect(eur_plan.currency).to eql("eur")
274
+ expect { Stripe::Subscription.create({ plan: eur_plan.id, customer: customer.id }) }.to raise_error {|e|
270
275
  expect(e).to be_a Stripe::InvalidRequestError
271
276
  expect(e.http_status).to eq(400)
272
277
  expect(e.message).to_not be_nil
@@ -274,7 +279,7 @@ shared_examples 'Customer Subscriptions' do
274
279
  end
275
280
 
276
281
  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)
282
+ plan = stripe_helper.create_plan(id: 'trial', product: product.id, amount: 999)
278
283
  customer = Stripe::Customer.create(source: gen_card_tk)
279
284
 
280
285
  expect{ Stripe::Subscription.create(plan: plan.id, customer: customer.id, trial: 10) }.to raise_error {|e|
@@ -286,35 +291,34 @@ shared_examples 'Customer Subscriptions' do
286
291
  end
287
292
 
288
293
  it "subscribes a customer with no card to a plan with a free trial" do
289
- plan = stripe_helper.create_plan(id: 'trial', amount: 999, trial_period_days: 14)
290
294
  customer = Stripe::Customer.create(id: 'cardless')
291
-
292
- sub = Stripe::Subscription.create({ plan: 'trial', customer: customer.id })
295
+ sub = Stripe::Subscription.create({ plan: plan_with_trial.id, customer: customer.id })
293
296
 
294
297
  expect(sub.object).to eq('subscription')
295
- expect(sub.plan.to_hash).to eq(plan.to_hash)
298
+ expect(sub.plan.to_hash).to eq(plan_with_trial.to_hash)
296
299
  expect(sub.trial_end - sub.trial_start).to eq(14 * 86400)
297
300
  expect(sub.billing_cycle_anchor).to be_nil
298
301
 
299
- customer = Stripe::Customer.retrieve('cardless')
300
- expect(customer.subscriptions.data).to_not be_empty
301
- expect(customer.subscriptions.count).to eq(1)
302
- expect(customer.subscriptions.data.length).to eq(1)
302
+ customer = Stripe::Customer.retrieve(customer.id)
303
+ subscriptions = Stripe::Subscription.list(customer: customer.id)
304
+ charges = Stripe::Charge.list(customer: customer.id)
303
305
 
304
- expect(customer.subscriptions.data.first.id).to eq(sub.id)
305
- expect(customer.subscriptions.data.first.plan.to_hash).to eq(plan.to_hash)
306
- expect(customer.subscriptions.data.first.customer).to eq(customer.id)
307
- expect(customer.charges.count).to eq(0)
306
+ expect(subscriptions.data).to_not be_empty
307
+ expect(subscriptions.count).to eq(1)
308
+ expect(subscriptions.data.length).to eq(1)
309
+
310
+ expect(subscriptions.data.first.id).to eq(sub.id)
311
+ expect(subscriptions.data.first.plan.to_hash).to eq(plan_with_trial.to_hash)
312
+ expect(subscriptions.data.first.customer).to eq(customer.id)
313
+ expect(charges.count).to eq(0)
308
314
  end
309
315
 
310
316
  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
317
  customer = Stripe::Customer.create(id: 'cardless')
313
-
314
- sub = Stripe::Subscription.create({ items: [ { plan: 'trial' } ], customer: customer.id })
318
+ sub = Stripe::Subscription.create({ items: [ { plan: plan_with_trial.id } ], customer: customer.id })
315
319
 
316
320
  expect(sub.object).to eq('subscription')
317
- expect(sub.items.data[0].plan.to_hash).to eq(plan.to_hash)
321
+ expect(sub.items.data[0].plan.to_hash).to eq(plan_with_trial.to_hash)
318
322
  # no idea how to fix this one
319
323
  # expect(sub.trial_end - sub.trial_start).to eq(14 * 86400)
320
324
 
@@ -324,17 +328,17 @@ shared_examples 'Customer Subscriptions' do
324
328
  expect(customer.subscriptions.data.length).to eq(1)
325
329
 
326
330
  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)
331
+ expect(customer.subscriptions.data.first.items.data.first.plan.to_hash).to eq(plan_with_trial.to_hash)
328
332
  expect(customer.subscriptions.data.first.customer).to eq(customer.id)
329
333
  # No idea on this one
330
334
  # expect(customer.charges.count).to eq(0)
331
335
  end
332
336
 
333
337
  it "subscribes a customer with no card to a free plan" do
334
- plan = stripe_helper.create_plan(id: 'free_tier', amount: 0)
338
+ plan = stripe_helper.create_plan(id: 'free_tier', product: product.id, amount: 0)
335
339
  customer = Stripe::Customer.create(id: 'cardless')
336
340
 
337
- sub = Stripe::Subscription.create({ plan: 'free_tier', customer: customer.id })
341
+ sub = Stripe::Subscription.create({ plan: plan.id, customer: customer.id })
338
342
 
339
343
  expect(sub.object).to eq('subscription')
340
344
  expect(sub.plan.to_hash).to eq(plan.to_hash)
@@ -350,20 +354,21 @@ shared_examples 'Customer Subscriptions' do
350
354
  end
351
355
 
352
356
  it "overrides trial length when trial end is set" do
353
- plan = stripe_helper.create_plan(id: 'trial', amount: 999, trial_period_days: 14)
354
357
  customer = Stripe::Customer.create(id: 'short_trial')
355
358
  trial_end = Time.now.utc.to_i + 3600
356
359
 
357
- sub = Stripe::Subscription.create({ plan: 'trial', customer: customer.id, trial_end: trial_end })
360
+ sub = Stripe::Subscription.create({ plan: plan_with_trial.id, customer: customer.id, trial_end: trial_end })
358
361
 
359
362
  expect(sub.object).to eq('subscription')
360
- expect(sub.plan.to_hash).to eq(plan.to_hash)
363
+ expect(sub.plan.to_hash).to eq(plan_with_trial.to_hash)
361
364
  expect(sub.current_period_end).to eq(trial_end)
362
365
  expect(sub.trial_end).to eq(trial_end)
363
366
  end
364
367
 
365
368
  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)
369
+ product = stripe_helper.create_product(name: 'Trial')
370
+ plan = stripe_helper.create_plan(id: 'trial', amount: 999,
371
+ trial_period_days: 14, product: product.id)
367
372
  customer = Stripe::Customer.create(id: 'short_trial')
368
373
  trial_end = Time.now.utc.to_i + 3600
369
374
  metadata = {description: 'original description'}
@@ -387,23 +392,21 @@ shared_examples 'Customer Subscriptions' do
387
392
  end
388
393
 
389
394
  it "returns without a trial when trial_end is set to 'now'" do
390
- plan = stripe_helper.create_plan(id: 'trial', amount: 999, trial_period_days: 14)
391
395
  customer = Stripe::Customer.create(id: 'no_trial', source: gen_card_tk)
392
396
 
393
- sub = Stripe::Subscription.create({ plan: 'trial', customer: customer.id, trial_end: "now" })
397
+ sub = Stripe::Subscription.create({ plan: plan_with_trial.id, customer: customer.id, trial_end: "now" })
394
398
 
395
399
  expect(sub.object).to eq('subscription')
396
- expect(sub.plan.to_hash).to eq(plan.to_hash)
400
+ expect(sub.plan.to_hash).to eq(plan_with_trial.to_hash)
397
401
  expect(sub.status).to eq('active')
398
402
  expect(sub.trial_start).to be_nil
399
403
  expect(sub.trial_end).to be_nil
400
404
  end
401
405
 
402
406
  it "raises error when trial_end is not an integer or 'now'" do
403
- plan = stripe_helper.create_plan(id: 'trial', amount: 999, trial_period_days: 14)
404
407
  customer = Stripe::Customer.create(id: 'cus_trial')
405
408
 
406
- expect { Stripe::Subscription.create({ plan: 'trial', customer: customer.id, trial_end: "gazebo" }) }.to raise_error {|e|
409
+ expect { Stripe::Subscription.create({ plan: plan_with_trial.id, customer: customer.id, trial_end: "gazebo" }) }.to raise_error {|e|
407
410
  expect(e).to be_a Stripe::InvalidRequestError
408
411
  expect(e.http_status).to eq(400)
409
412
  expect(e.message).to eq("Invalid timestamp: must be an integer")
@@ -411,11 +414,10 @@ shared_examples 'Customer Subscriptions' do
411
414
  end
412
415
 
413
416
  it "raises error when trial_end is set to a time in the past" do
414
- plan = stripe_helper.create_plan(id: 'trial', amount: 999, trial_period_days: 14)
415
417
  customer = Stripe::Customer.create(id: 'past_trial')
416
418
  trial_end = Time.now.utc.to_i - 3600
417
419
 
418
- expect { Stripe::Subscription.create({ plan: 'trial', customer: customer.id, trial_end: trial_end }) }.to raise_error {|e|
420
+ expect { Stripe::Subscription.create({ plan: plan_with_trial.id, customer: customer.id, trial_end: trial_end }) }.to raise_error {|e|
419
421
  expect(e).to be_a Stripe::InvalidRequestError
420
422
  expect(e.http_status).to eq(400)
421
423
  expect(e.message).to eq("Invalid timestamp: must be an integer Unix timestamp in the future")
@@ -423,11 +425,10 @@ shared_examples 'Customer Subscriptions' do
423
425
  end
424
426
 
425
427
  it "raises error when trial_end is set to a time more than five years in the future" do
426
- plan = stripe_helper.create_plan(id: 'trial', amount: 999, trial_period_days: 14)
427
428
  customer = Stripe::Customer.create(id: 'long_trial')
428
429
  trial_end = Time.now.utc.to_i + 31557600*5 + 3600 # 5 years + 1 hour
429
430
 
430
- expect { Stripe::Subscription.create({ plan: 'trial', customer: customer.id, trial_end: trial_end }) }.to raise_error {|e|
431
+ expect { Stripe::Subscription.create({ plan: plan_with_trial.id, customer: customer.id, trial_end: trial_end }) }.to raise_error {|e|
431
432
  expect(e).to be_a Stripe::InvalidRequestError
432
433
  expect(e.http_status).to eq(400)
433
434
  expect(e.message).to eq("Invalid timestamp: can be no more than five years in the future")
@@ -435,11 +436,10 @@ shared_examples 'Customer Subscriptions' do
435
436
  end
436
437
 
437
438
  it 'overrides current period end when billing cycle anchor is set' do
438
- plan = stripe_helper.create_plan(id: 'plan', amount: 999)
439
439
  customer = Stripe::Customer.create(source: gen_card_tk)
440
440
  billing_cycle_anchor = Time.now.utc.to_i + 3600
441
441
 
442
- sub = Stripe::Subscription.create({ plan: 'plan', customer: customer.id, billing_cycle_anchor: billing_cycle_anchor })
442
+ sub = Stripe::Subscription.create({ plan: plan.id, customer: customer.id, billing_cycle_anchor: billing_cycle_anchor })
443
443
 
444
444
  expect(sub.status).to eq('active')
445
445
  expect(sub.current_period_end).to eq(billing_cycle_anchor)
@@ -447,17 +447,11 @@ shared_examples 'Customer Subscriptions' do
447
447
  end
448
448
 
449
449
  it 'when plan defined inside items', live: true do
450
- plan = stripe_helper.create_plan(id: 'BASE_PRICE_PLAN1')
450
+ plan = stripe_helper.create_plan(id: 'BASE_PRICE_PLAN1', product: product.id)
451
451
 
452
- plan2 = stripe_helper.create_plan(id: 'PER_USER_PLAN1')
452
+ plan2 = stripe_helper.create_plan(id: 'PER_USER_PLAN1', product: product.id)
453
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
- }
454
+ source: stripe_helper.generate_card_token
461
455
  )
462
456
  subscription = Stripe::Subscription.create(
463
457
  customer: customer.id,
@@ -476,9 +470,9 @@ shared_examples 'Customer Subscriptions' do
476
470
  end
477
471
 
478
472
  it 'when plan defined inside items for trials with no card', live: true do
479
- plan = stripe_helper.create_plan(id: 'BASE_PRICE_PLAN1')
473
+ plan = stripe_helper.create_plan(id: 'BASE_PRICE_PLAN1', product: product.id)
480
474
 
481
- plan2 = stripe_helper.create_plan(id: 'PER_USER_PLAN1')
475
+ plan2 = stripe_helper.create_plan(id: 'PER_USER_PLAN1', product: product.id)
482
476
  customer = Stripe::Customer.create
483
477
  trial_end = Time.now.utc.to_i + 3600
484
478
 
@@ -498,7 +492,8 @@ shared_examples 'Customer Subscriptions' do
498
492
  end
499
493
 
500
494
  it 'add a new subscription to bill via an invoice' do
501
- plan = stripe_helper.create_plan(id: 'silver', product: { name: 'Silver Plan' },
495
+ product = stripe_helper.create_product(id: 'invoice_billing')
496
+ plan = stripe_helper.create_plan(id: 'silver', product: product.id,
502
497
  amount: 4999, currency: 'usd')
503
498
  customer = Stripe::Customer.create(id: 'cardless')
504
499
 
@@ -520,41 +515,51 @@ shared_examples 'Customer Subscriptions' do
520
515
  end
521
516
 
522
517
  let(:subscription_header) {{
523
- :idempotency_key => 'a_idempotency_key'
518
+ :idempotency_key => SecureRandom.hex
524
519
  }}
525
520
 
526
521
  it "adds a new subscription to customer with identical idempotency key" do
527
- plan = stripe_helper.create_plan(id: 'silver', product: { name: 'Silver Plan' },
522
+ product = stripe_helper.create_product(name: 'Silver Product')
523
+ plan = stripe_helper.create_plan(id: 'silver', product: product.id,
528
524
  amount: 4999, currency: 'usd')
529
525
  customer = Stripe::Customer.create(source: gen_card_tk)
530
526
 
531
527
  expect(customer.subscriptions.data).to be_empty
532
528
  expect(customer.subscriptions.count).to eq(0)
533
529
 
530
+ subscription_header = {
531
+ :idempotency_key => "uniq_idempotency_key_#{customer.id}"
532
+ }
533
+
534
534
  sub1 = Stripe::Subscription.create({ items: [{ plan: 'silver' }], customer: customer.id }, subscription_header)
535
535
  sub2 = Stripe::Subscription.create({ items: [{ plan: 'silver' }], customer: customer.id }, subscription_header)
536
536
  expect(sub1).to eq(sub2)
537
537
  end
538
538
 
539
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' },
540
+ product = stripe_helper.create_product(name: 'Silver Product')
541
+ plan = stripe_helper.create_plan(id: 'silver', product: product.id,
541
542
  amount: 4999, currency: 'usd')
542
543
  customer = Stripe::Customer.create(source: gen_card_tk)
543
544
 
544
545
  expect(customer.subscriptions.data).to be_empty
545
546
  expect(customer.subscriptions.count).to eq(0)
546
547
 
547
- another_subscription_header = {
548
- :idempotency_key => 'another_idempotency_key'
548
+ sub_header_1 = {
549
+ :idempotency_key => SecureRandom.hex
550
+ }
551
+ sub_header_2 = {
552
+ :idempotency_key => SecureRandom.hex
549
553
  }
550
554
 
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)
555
+ sub1 = Stripe::Subscription.create({ items: [{ plan: 'silver' }], customer: customer.id }, sub_header_1)
556
+ sub2 = Stripe::Subscription.create({ items: [{ plan: 'silver' }], customer: customer.id }, sub_header_2)
553
557
  expect(sub1).not_to eq(sub2)
554
558
  end
555
559
 
556
560
  it "accepts a hash of items" do
557
- silver = stripe_helper.create_plan(id: 'silver')
561
+ product = stripe_helper.create_product(name: 'Silver Product')
562
+ silver = stripe_helper.create_plan(id: 'silver', product: product.id)
558
563
  customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk)
559
564
 
560
565
  sub = Stripe::Subscription.create({ items: { '0' => { plan: 'silver' } }, customer: customer.id })
@@ -562,15 +567,36 @@ shared_examples 'Customer Subscriptions' do
562
567
 
563
568
  expect(sub.cancel_at_period_end).to be_truthy
564
569
  expect(sub.save).to be_truthy
565
- expect(sub.cancel_at_period_end).to be_falsey
570
+ expect(sub.cancel_at_period_end).to be_truthy
566
571
  end
567
572
 
573
+ it 'accepts default_tax_rates param' do
574
+ tax_rate = Stripe::TaxRate.create(
575
+ display_name: 'VAT',
576
+ description: 'VAT Germany',
577
+ jurisdiction: 'DE',
578
+ percentage: 19.0,
579
+ inclusive: false,
580
+ )
581
+ product = stripe_helper.create_product(name: 'Silver Product')
582
+ silver = stripe_helper.create_plan(id: 'silver', product: product.id)
583
+ customer = Stripe::Customer.create(source: gen_card_tk)
584
+ subscription = Stripe::Subscription.create(
585
+ customer: customer.id,
586
+ items: [{ plan: silver.id }],
587
+ default_tax_rates: [ tax_rate ]
588
+ )
589
+
590
+ aggregate_failures do
591
+ expect(subscription.default_tax_rates.length).to eq(1)
592
+ expect(subscription.default_tax_rates.first.id).to eq(tax_rate.id)
593
+ end
594
+ end
568
595
  end
569
596
 
570
597
  context "updating a subscription" do
571
598
  it 'raises invalid request exception when subscription is cancelled' do
572
- stripe_helper.create_plan(id: 'the truth')
573
- customer = Stripe::Customer.create(source: gen_card_tk, plan: 'the truth')
599
+ customer = Stripe::Customer.create(source: gen_card_tk, plan: plan.id)
574
600
 
575
601
  subscription = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
576
602
  subscription.delete
@@ -583,24 +609,22 @@ shared_examples 'Customer Subscriptions' do
583
609
  end
584
610
 
585
611
  it "updates a stripe customer's existing subscription with one plan inside items" do
586
- silver = stripe_helper.create_plan(id: 'silver')
587
612
  customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk)
588
613
 
589
- sub = Stripe::Subscription.create({ items: [ { plan: 'silver' } ], customer: customer.id })
614
+ sub = Stripe::Subscription.create({ items: [ { plan: plan.id } ], customer: customer.id })
590
615
  sub.delete(at_period_end: true)
591
616
 
592
617
  expect(sub.cancel_at_period_end).to be_truthy
593
618
  expect(sub.save).to be_truthy
594
- expect(sub.cancel_at_period_end).to be_falsey
619
+ expect(sub.cancel_at_period_end).to be_truthy
595
620
  end
596
621
 
597
622
  it "updates a stripe customer's existing subscription when plan inside of items" do
598
- silver = stripe_helper.create_plan(id: 'silver')
599
- gold = stripe_helper.create_plan(id: 'gold')
600
- customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk, plan: 'silver')
623
+ customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk, plan: plan.id)
601
624
 
625
+ gold_plan = stripe_helper.create_plan(id: 'gold', product: product.id)
602
626
  sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
603
- sub.plan = 'gold'
627
+ sub.plan = gold_plan.id
604
628
  sub.quantity = 5
605
629
  sub.metadata.foo = "bar"
606
630
  sub.metadata.example = "yes"
@@ -608,7 +632,7 @@ shared_examples 'Customer Subscriptions' do
608
632
  expect(sub.save).to be_truthy
609
633
 
610
634
  expect(sub.object).to eq('subscription')
611
- expect(sub.plan.to_hash).to eq(gold.to_hash)
635
+ expect(sub.plan.to_hash).to eq(gold_plan.to_hash)
612
636
  expect(sub.quantity).to eq(5)
613
637
  expect(sub.metadata.foo).to eq( "bar" )
614
638
  expect(sub.metadata.example).to eq( "yes" )
@@ -619,16 +643,15 @@ shared_examples 'Customer Subscriptions' do
619
643
  expect(customer.subscriptions.data.length).to eq(1)
620
644
 
621
645
  expect(customer.subscriptions.data.first.id).to eq(sub.id)
622
- expect(customer.subscriptions.data.first.plan.to_hash).to eq(gold.to_hash)
646
+ expect(customer.subscriptions.data.first.plan.to_hash).to eq(gold_plan.to_hash)
623
647
  expect(customer.subscriptions.data.first.customer).to eq(customer.id)
624
648
  end
625
649
 
626
650
  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)
651
+ customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk, plan: plan.id)
631
652
 
653
+ gold_plan = stripe_helper.create_plan(id: 'gold', product: product.id)
654
+ addon_plan = stripe_helper.create_plan(id: 'addon_plan', product: product.id)
632
655
  sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
633
656
  sub.items = [{ plan: gold_plan.id, quantity: 2 }, { plan: addon_plan.id, quantity: 2 }]
634
657
  expect(sub.save).to be_truthy
@@ -649,12 +672,12 @@ shared_examples 'Customer Subscriptions' do
649
672
  end
650
673
 
651
674
  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
675
  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 }])
676
+ addon1_plan = stripe_helper.create_plan(id: 'addon1', product: product.id)
677
+ sub = Stripe::Subscription.create(customer: customer.id, items: [{ plan: plan.id }, { plan: addon1_plan.id }])
678
+
679
+ gold_plan = stripe_helper.create_plan(id: 'gold', product: product.id)
680
+ addon2_plan = stripe_helper.create_plan(id: 'addon2', product: product.id)
658
681
 
659
682
  sub.items = [{ plan: gold_plan.id, quantity: 2 }, { plan: addon2_plan.id, quantity: 2 }]
660
683
  expect(sub.save).to be_truthy
@@ -674,9 +697,30 @@ shared_examples 'Customer Subscriptions' do
674
697
  expect(customer.subscriptions.data.first.items.data[1].plan.to_hash).to eq(addon2_plan.to_hash)
675
698
  end
676
699
 
700
+ it "updates a subscription's cancel_at_period_end" do
701
+ silver = stripe_helper.create_plan(id: 'silver', product: product.id)
702
+ customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk)
703
+
704
+ sub = Stripe::Subscription.create({ items: [ { plan: 'silver' } ], customer: customer.id })
705
+
706
+ expect(sub.cancel_at_period_end).to be_falsey
707
+
708
+ sub.cancel_at_period_end = true
709
+ sub.save
710
+
711
+ expect(sub.save).to be_truthy
712
+ expect(sub.cancel_at_period_end).to be_truthy
713
+ expect(sub.canceled_at).to be_truthy
714
+
715
+ sub.cancel_at_period_end = false
716
+ sub.save
717
+
718
+ expect(sub.save).to be_truthy
719
+ expect(sub.cancel_at_period_end).to be_falsey
720
+ expect(sub.canceled_at).to be_falsey
721
+ end
722
+
677
723
  it 'when adds coupon', live: true do
678
- plan = stripe_helper.create_plan(id: 'plan_with_coupon2', product: { name: 'One More Test Plan' },
679
- amount: 777)
680
724
  coupon = stripe_helper.create_coupon
681
725
  customer = Stripe::Customer.create(source: gen_card_tk, plan: plan.id)
682
726
  subscription = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
@@ -690,8 +734,6 @@ shared_examples 'Customer Subscriptions' do
690
734
  end
691
735
 
692
736
  it 'when add not exist coupon' do
693
- plan = stripe_helper.create_plan(id: 'plan_with_coupon3', product: { name: 'One More Test Plan' },
694
- amount: 777)
695
737
  customer = Stripe::Customer.create(source: gen_card_tk, plan: plan.id)
696
738
  subscription = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
697
739
 
@@ -706,8 +748,6 @@ shared_examples 'Customer Subscriptions' do
706
748
  end
707
749
 
708
750
  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
751
  customer = Stripe::Customer.create(source: gen_card_tk, plan: plan.id)
712
752
  coupon = stripe_helper.create_coupon
713
753
  subscription = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
@@ -721,8 +761,7 @@ shared_examples 'Customer Subscriptions' do
721
761
  end
722
762
 
723
763
  it "throws an error when plan does not exist" do
724
- free = stripe_helper.create_plan(id: 'free', amount: 0)
725
- customer = Stripe::Customer.create(id: 'cardless', plan: 'free')
764
+ customer = Stripe::Customer.create(id: 'cardless', plan: free_plan.id)
726
765
 
727
766
  sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
728
767
  sub.plan = 'gazebo'
@@ -736,75 +775,59 @@ shared_examples 'Customer Subscriptions' do
736
775
  customer = Stripe::Customer.retrieve('cardless')
737
776
  expect(customer.subscriptions.count).to eq(1)
738
777
  expect(customer.subscriptions.data.length).to eq(1)
739
- expect(customer.subscriptions.data.first.plan.to_hash).to eq(free.to_hash)
778
+ expect(customer.subscriptions.data.first.plan.to_hash).to eq(free_plan.to_hash)
740
779
  end
741
780
 
742
781
  it "throws an error when subscription does not exist" do
743
- free = stripe_helper.create_plan(id: 'free', amount: 0)
744
- customer = Stripe::Customer.create(id: 'cardless', plan: 'free')
745
-
746
- expect { Stripe::Subscription.retrieve("gazebo") }.to raise_error {|e|
782
+ expect(stripe_helper.list_subscriptions(50).keys).to_not include("sub_NONEXIST")
783
+ expect { Stripe::Subscription.retrieve("sub_NONEXIST") }.to raise_error {|e|
747
784
  expect(e).to be_a Stripe::InvalidRequestError
748
785
  expect(e.http_status).to eq(404)
749
786
  expect(e.message).to_not be_nil
750
787
  }
751
-
752
- customer = Stripe::Customer.retrieve('cardless')
753
- expect(customer.subscriptions.count).to eq(1)
754
- expect(customer.subscriptions.data.length).to eq(1)
755
- expect(customer.subscriptions.data.first.plan.to_hash).to eq(free.to_hash)
756
788
  end
757
789
 
758
790
  [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
791
+ it "raises an error when updating a customer with no card, and plan trial_period_days = #{trial_period_days}", live: true do
760
792
  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')
793
+ free_plan
794
+ paid_plan = stripe_helper.create_plan(id: 'enterprise', product: product.id, amount: 499, trial_period_days: trial_period_days)
795
+ customer = Stripe::Customer.create(description: 'cardless', plan: free_plan.id)
764
796
 
765
797
  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)
798
+ sub.plan = paid_plan.id
799
+
800
+ expect {
801
+ sub.save
802
+ }.to raise_error(Stripe::InvalidRequestError, "This customer has no attached payment source")
778
803
  ensure
779
804
  customer.delete if customer
780
- paid.delete if paid
781
- free.delete if free
805
+ paid_plan.delete if paid_plan
806
+ free_plan.delete if free_plan
782
807
  end
783
808
  end
784
809
  end
785
810
 
786
811
  it 'updates a subscription if the customer has a free trial', live: true do
787
- stripe_helper.create_plan(id: 'enterprise', amount: 499)
788
812
  trial_end = Time.now.utc.to_i + 3600
789
- customer = Stripe::Customer.create(plan: 'enterprise',
790
- trial_end: trial_end)
791
- subscription = customer.subscriptions.first
813
+ customer = Stripe::Customer.create(plan: plan.id, trial_end: trial_end)
814
+ subscription = customer.subscriptions.first
792
815
  subscription.quantity = 2
793
816
  subscription.save
794
817
  expect(subscription.quantity).to eq(2)
795
818
  end
796
819
 
797
820
  it "updates a customer with no card to a plan with a free trial" do
798
- free = stripe_helper.create_plan(id: 'free', amount: 0)
799
- trial = stripe_helper.create_plan(id: 'trial', amount: 999, trial_period_days: 14)
800
- customer = Stripe::Customer.create(id: 'cardless', plan: 'free')
821
+ free_plan
822
+ trial_plan = stripe_helper.create_plan(id: 'trial', product: product.id, amount: 999, trial_period_days: 14)
823
+ customer = Stripe::Customer.create(id: 'cardless', plan: free_plan.id)
801
824
 
802
825
  sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
803
- sub.plan = 'trial'
826
+ sub.plan = trial_plan.id
804
827
  sub.save
805
828
 
806
829
  expect(sub.object).to eq('subscription')
807
- expect(sub.plan.to_hash).to eq(trial.to_hash)
830
+ expect(sub.plan.to_hash).to eq(trial_plan.to_hash)
808
831
 
809
832
  customer = Stripe::Customer.retrieve('cardless')
810
833
  expect(customer.subscriptions.data).to_not be_empty
@@ -812,21 +835,21 @@ shared_examples 'Customer Subscriptions' do
812
835
  expect(customer.subscriptions.data.length).to eq(1)
813
836
 
814
837
  expect(customer.subscriptions.data.first.id).to eq(sub.id)
815
- expect(customer.subscriptions.data.first.plan.to_hash).to eq(trial.to_hash)
838
+ expect(customer.subscriptions.data.first.plan.to_hash).to eq(trial_plan.to_hash)
816
839
  expect(customer.subscriptions.data.first.customer).to eq(customer.id)
817
840
  end
818
841
 
819
842
  it "updates a customer with no card to a free plan" do
820
- free = stripe_helper.create_plan(id: 'free', amount: 0)
821
- gratis = stripe_helper.create_plan(id: 'gratis', amount: 0)
822
- customer = Stripe::Customer.create(id: 'cardless', plan: 'free')
843
+ free_plan
844
+ customer = Stripe::Customer.create(id: 'cardless', product: product.id, plan: free_plan.id)
823
845
 
824
846
  sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
825
- sub.plan = 'gratis'
847
+ gratis_plan = stripe_helper.create_plan(id: 'gratis', product: product.id, amount: 0)
848
+ sub.plan = gratis_plan.id
826
849
  sub.save
827
850
 
828
851
  expect(sub.object).to eq('subscription')
829
- expect(sub.plan.to_hash).to eq(gratis.to_hash)
852
+ expect(sub.plan.to_hash).to eq(gratis_plan.to_hash)
830
853
 
831
854
  customer = Stripe::Customer.retrieve('cardless')
832
855
  expect(customer.subscriptions.data).to_not be_empty
@@ -834,17 +857,15 @@ shared_examples 'Customer Subscriptions' do
834
857
  expect(customer.subscriptions.data.length).to eq(1)
835
858
 
836
859
  expect(customer.subscriptions.data.first.id).to eq(sub.id)
837
- expect(customer.subscriptions.data.first.plan.to_hash).to eq(gratis.to_hash)
860
+ expect(customer.subscriptions.data.first.plan.to_hash).to eq(gratis_plan.to_hash)
838
861
  expect(customer.subscriptions.data.first.customer).to eq(customer.id)
839
862
  end
840
863
 
841
864
  it "sets a card when updating a customer's subscription" do
842
- free = stripe_helper.create_plan(id: 'free', amount: 0)
843
- paid = stripe_helper.create_plan(id: 'paid', amount: 499)
844
- customer = Stripe::Customer.create(id: 'test_customer_sub', plan: 'free')
865
+ customer = Stripe::Customer.create(id: 'test_customer_sub', plan: free_plan.id)
845
866
 
846
867
  sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
847
- sub.plan = 'paid'
868
+ sub.plan = plan.id
848
869
  sub.source = gen_card_tk
849
870
  sub.save
850
871
 
@@ -857,11 +878,8 @@ shared_examples 'Customer Subscriptions' do
857
878
  end
858
879
 
859
880
  it "overrides trial length when trial end is set" do
860
- plan = stripe_helper.create_plan(id: 'trial', amount: 999, trial_period_days: 14)
861
- customer = Stripe::Customer.create(id: 'test_trial_end', plan: 'trial')
862
-
881
+ customer = Stripe::Customer.create(id: 'test_trial_end', plan: plan_with_trial.id)
863
882
  sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
864
-
865
883
  trial_end = Time.now.utc.to_i + 3600
866
884
  sub.trial_end = trial_end
867
885
  sub.save
@@ -872,8 +890,7 @@ shared_examples 'Customer Subscriptions' do
872
890
  end
873
891
 
874
892
  it "returns without a trial when trial_end is set to 'now'" do
875
- plan = stripe_helper.create_plan(id: 'trial', amount: 999, trial_period_days: 14)
876
- customer = Stripe::Customer.create(id: 'test_trial_end', plan: 'trial')
893
+ customer = Stripe::Customer.create(id: 'test_trial_end', plan: plan_with_trial.id, default_source: 'tok_visa')
877
894
 
878
895
  sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
879
896
 
@@ -881,18 +898,15 @@ shared_examples 'Customer Subscriptions' do
881
898
  sub.save
882
899
 
883
900
  expect(sub.object).to eq('subscription')
884
- expect(sub.plan.to_hash).to eq(plan.to_hash)
901
+ expect(sub.plan.to_hash).to eq(plan_with_trial.to_hash)
885
902
  expect(sub.status).to eq('active')
886
903
  expect(sub.trial_start).to be_nil
887
904
  expect(sub.trial_end).to be_nil
888
905
  end
889
906
 
890
907
  it "changes an active subscription to a trial when trial_end is set" do
891
- plan = stripe_helper.create_plan(id: 'no_trial', amount: 999)
892
- customer = Stripe::Customer.create(id: 'test_trial_end', plan: 'no_trial', source: gen_card_tk)
893
-
908
+ customer = Stripe::Customer.create(id: 'test_trial_end', plan: plan.id, source: gen_card_tk)
894
909
  sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
895
-
896
910
  trial_end = Time.now.utc.to_i + 3600
897
911
  sub.trial_end = trial_end
898
912
  sub.save
@@ -906,8 +920,8 @@ shared_examples 'Customer Subscriptions' do
906
920
 
907
921
 
908
922
  it "raises error when trial_end is not an integer or 'now'" do
909
- plan = stripe_helper.create_plan(id: 'no_trial', amount: 999)
910
- customer = Stripe::Customer.create(id: 'test_trial_end', plan: 'no_trial', source: gen_card_tk)
923
+ expect(plan.trial_period_days).to be_nil
924
+ customer = Stripe::Customer.create(id: 'test_trial_end', plan: plan.id, source: gen_card_tk)
911
925
 
912
926
  sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
913
927
  sub.trial_end = "gazebo"
@@ -921,10 +935,10 @@ shared_examples 'Customer Subscriptions' do
921
935
  end
922
936
 
923
937
  context "cancelling a subscription" do
938
+ let(:customer) { Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk, plan: plan.id) }
924
939
 
925
940
  it "cancels a stripe customer's subscription", :live => true do
926
- truth = stripe_helper.create_plan(id: 'the truth')
927
- customer = Stripe::Customer.create(source: gen_card_tk, plan: "the truth")
941
+ customer = Stripe::Customer.create(source: gen_card_tk, plan: plan.id)
928
942
 
929
943
  sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
930
944
  result = sub.delete
@@ -939,53 +953,11 @@ shared_examples 'Customer Subscriptions' do
939
953
  expect(customer.subscriptions.count).to eq(0)
940
954
  expect(customer.subscriptions.data.length).to eq(0)
941
955
  end
942
-
943
- it "cancels a stripe customer's subscription at period end" do
944
- truth = stripe_helper.create_plan(id: 'the_truth')
945
- customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk, plan: "the_truth")
946
-
947
- sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
948
- result = sub.delete(at_period_end: true)
949
-
950
- expect(result.status).to eq('active')
951
- expect(result.cancel_at_period_end).to eq(true)
952
- expect(result.id).to eq(sub.id)
953
-
954
- customer = Stripe::Customer.retrieve('test_customer_sub')
955
- expect(customer.subscriptions.data).to_not be_empty
956
- expect(customer.subscriptions.count).to eq(1)
957
- expect(customer.subscriptions.data.length).to eq(1)
958
-
959
- expect(customer.subscriptions.data.first.status).to eq('active')
960
- expect(customer.subscriptions.data.first.cancel_at_period_end).to eq(true)
961
- expect(customer.subscriptions.data.first.ended_at).to be_nil
962
- expect(customer.subscriptions.data.first.canceled_at).to_not be_nil
963
- end
964
-
965
- it "resumes an at period end cancelled subscription" do
966
- truth = stripe_helper.create_plan(id: 'the_truth')
967
- customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk, plan: "the_truth")
968
-
969
- sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
970
- result = sub.delete(at_period_end: true)
971
-
972
- sub.plan = 'the_truth'
973
- sub.save
974
-
975
- customer = Stripe::Customer.retrieve('test_customer_sub')
976
- expect(customer.subscriptions.data).to_not be_empty
977
- expect(customer.subscriptions.count).to eq(1)
978
- expect(customer.subscriptions.data.length).to eq(1)
979
-
980
- expect(customer.subscriptions.data.first.status).to eq('active')
981
- expect(customer.subscriptions.data.first.cancel_at_period_end).to eq(false)
982
- expect(customer.subscriptions.data.first.ended_at).to be_nil
983
- expect(customer.subscriptions.data.first.canceled_at).to be_nil
984
- end
985
956
  end
986
957
 
987
958
  it "supports 'cancelling' by updating cancel_at_period_end" do
988
- truth = stripe_helper.create_plan(id: 'the_truth')
959
+ product = stripe_helper.create_product(name: 'Truth Product')
960
+ truth = stripe_helper.create_plan(id: 'the_truth', product: product.id)
989
961
  customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk, plan: "the_truth")
990
962
 
991
963
  sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
@@ -1007,7 +979,8 @@ shared_examples 'Customer Subscriptions' do
1007
979
  end
1008
980
 
1009
981
  it "resumes a subscription cancelled by updating cancel_at_period_end" do
1010
- truth = stripe_helper.create_plan(id: 'the_truth')
982
+ product = stripe_helper.create_product(name: 'Truth Product')
983
+ truth = stripe_helper.create_plan(id: 'the_truth', product: product.id)
1011
984
  customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk, plan: "the_truth")
1012
985
 
1013
986
  sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
@@ -1031,7 +1004,7 @@ shared_examples 'Customer Subscriptions' do
1031
1004
  end
1032
1005
 
1033
1006
  it "doesn't change status of subscription when cancelling at period end" do
1034
- trial = stripe_helper.create_plan(id: 'trial', trial_period_days: 14)
1007
+ trial = stripe_helper.create_plan(id: 'trial', product: product.id, trial_period_days: 14)
1035
1008
  customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk, plan: "trial")
1036
1009
 
1037
1010
  sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
@@ -1047,8 +1020,8 @@ shared_examples 'Customer Subscriptions' do
1047
1020
  it "doesn't require a card when trial_end is present", :live => true do
1048
1021
  plan = stripe_helper.create_plan(
1049
1022
  :amount => 2000,
1023
+ :product => product.id,
1050
1024
  :interval => 'month',
1051
- :name => 'Amazing Gold Plan',
1052
1025
  :currency => 'usd',
1053
1026
  :id => 'gold'
1054
1027
  )
@@ -1063,7 +1036,7 @@ shared_examples 'Customer Subscriptions' do
1063
1036
  let(:subscription) { Stripe::Subscription.retrieve(customer.subscriptions.data.first.id) }
1064
1037
 
1065
1038
  before do
1066
- stripe_helper.create_plan(id: 'free', amount: 0)
1039
+ free_plan
1067
1040
  Stripe::Subscription.create({ plan: 'free', customer: customer.id })
1068
1041
  end
1069
1042
 
@@ -1089,12 +1062,12 @@ shared_examples 'Customer Subscriptions' do
1089
1062
  context "retrieve multiple subscriptions" do
1090
1063
 
1091
1064
  it "retrieves a list of multiple subscriptions" do
1092
- free = stripe_helper.create_plan(id: 'free', amount: 0)
1093
- paid = stripe_helper.create_plan(id: 'paid', amount: 499)
1094
- customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk, plan: "free")
1065
+ free_plan
1066
+ paid = stripe_helper.create_plan(id: 'paid', product: product.id, amount: 499)
1067
+ customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk, plan: free_plan.id)
1095
1068
  Stripe::Subscription.create({ plan: 'paid', customer: customer.id })
1096
1069
 
1097
- subs = Stripe::Subscription.all({ customer: customer.id })
1070
+ subs = Stripe::Subscription.list({ customer: customer.id })
1098
1071
 
1099
1072
  expect(subs.object).to eq("list")
1100
1073
  expect(subs.count).to eq(2)
@@ -1105,7 +1078,7 @@ shared_examples 'Customer Subscriptions' do
1105
1078
  Stripe::Customer.create(id: 'no_subs')
1106
1079
  customer = Stripe::Customer.retrieve('no_subs')
1107
1080
 
1108
- list = Stripe::Subscription.all({ customer: customer.id })
1081
+ list = Stripe::Subscription.list({ customer: customer.id })
1109
1082
 
1110
1083
  expect(list.object).to eq("list")
1111
1084
  expect(list.count).to eq(0)
@@ -1121,9 +1094,7 @@ shared_examples 'Customer Subscriptions' do
1121
1094
  create_plan(
1122
1095
  :amount => 500,
1123
1096
  :interval => 'month',
1124
- :product => {
1125
- :name => 'Sample Plan'
1126
- },
1097
+ :product => product.id,
1127
1098
  :currency => 'usd',
1128
1099
  :id => 'Sample5'
1129
1100
  )