stripe-ruby-mock 2.5.8 → 3.1.0.rc3

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 (90) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +1 -1
  3. data/.travis.yml +6 -9
  4. data/CHANGELOG.md +41 -0
  5. data/Gemfile +1 -0
  6. data/README.md +17 -11
  7. data/lib/stripe_mock.rb +8 -0
  8. data/lib/stripe_mock/api/client.rb +1 -1
  9. data/lib/stripe_mock/api/errors.rb +34 -28
  10. data/lib/stripe_mock/api/instance.rb +1 -1
  11. data/lib/stripe_mock/api/webhooks.rb +5 -0
  12. data/lib/stripe_mock/client.rb +2 -1
  13. data/lib/stripe_mock/data.rb +285 -16
  14. data/lib/stripe_mock/data/list.rb +42 -9
  15. data/lib/stripe_mock/instance.rb +18 -4
  16. data/lib/stripe_mock/request_handlers/account_links.rb +15 -0
  17. data/lib/stripe_mock/request_handlers/balance_transactions.rb +2 -2
  18. data/lib/stripe_mock/request_handlers/charges.rb +6 -4
  19. data/lib/stripe_mock/request_handlers/checkout.rb +15 -0
  20. data/lib/stripe_mock/request_handlers/checkout_session.rb +22 -0
  21. data/lib/stripe_mock/request_handlers/customers.rb +35 -18
  22. data/lib/stripe_mock/request_handlers/ephemeral_key.rb +1 -1
  23. data/lib/stripe_mock/request_handlers/express_login_links.rb +15 -0
  24. data/lib/stripe_mock/request_handlers/helpers/coupon_helpers.rb +5 -0
  25. data/lib/stripe_mock/request_handlers/helpers/subscription_helpers.rb +12 -7
  26. data/lib/stripe_mock/request_handlers/helpers/token_helpers.rb +1 -1
  27. data/lib/stripe_mock/request_handlers/invoices.rb +10 -4
  28. data/lib/stripe_mock/request_handlers/payment_intents.rb +182 -0
  29. data/lib/stripe_mock/request_handlers/payment_methods.rb +120 -0
  30. data/lib/stripe_mock/request_handlers/plans.rb +1 -1
  31. data/lib/stripe_mock/request_handlers/prices.rb +44 -0
  32. data/lib/stripe_mock/request_handlers/products.rb +1 -0
  33. data/lib/stripe_mock/request_handlers/setup_intents.rb +93 -0
  34. data/lib/stripe_mock/request_handlers/sources.rb +12 -6
  35. data/lib/stripe_mock/request_handlers/subscriptions.rb +45 -22
  36. data/lib/stripe_mock/request_handlers/tokens.rb +6 -4
  37. data/lib/stripe_mock/request_handlers/validators/param_validators.rb +123 -9
  38. data/lib/stripe_mock/server.rb +2 -2
  39. data/lib/stripe_mock/test_strategies/base.rb +67 -8
  40. data/lib/stripe_mock/test_strategies/live.rb +23 -12
  41. data/lib/stripe_mock/test_strategies/mock.rb +6 -2
  42. data/lib/stripe_mock/version.rb +1 -1
  43. data/lib/stripe_mock/webhook_fixtures/balance.available.json +6 -0
  44. data/lib/stripe_mock/webhook_fixtures/charge.failed.json +166 -38
  45. data/lib/stripe_mock/webhook_fixtures/customer.created.json +1 -0
  46. data/lib/stripe_mock/webhook_fixtures/customer.updated.json +1 -0
  47. data/lib/stripe_mock/webhook_fixtures/payment_intent.payment_failed.json +186 -0
  48. data/lib/stripe_mock/webhook_fixtures/payment_intent.succeeded.json +164 -0
  49. data/lib/stripe_mock/webhook_fixtures/product.created.json +34 -0
  50. data/lib/stripe_mock/webhook_fixtures/product.deleted.json +34 -0
  51. data/lib/stripe_mock/webhook_fixtures/product.updated.json +38 -0
  52. data/spec/instance_spec.rb +10 -12
  53. data/spec/list_spec.rb +38 -0
  54. data/spec/server_spec.rb +6 -3
  55. data/spec/shared_stripe_examples/account_examples.rb +1 -1
  56. data/spec/shared_stripe_examples/account_link_examples.rb +16 -0
  57. data/spec/shared_stripe_examples/balance_examples.rb +6 -0
  58. data/spec/shared_stripe_examples/balance_transaction_examples.rb +3 -3
  59. data/spec/shared_stripe_examples/bank_examples.rb +3 -3
  60. data/spec/shared_stripe_examples/card_examples.rb +4 -4
  61. data/spec/shared_stripe_examples/card_token_examples.rb +17 -21
  62. data/spec/shared_stripe_examples/charge_examples.rb +9 -22
  63. data/spec/shared_stripe_examples/checkout_examples.rb +47 -0
  64. data/spec/shared_stripe_examples/coupon_examples.rb +1 -1
  65. data/spec/shared_stripe_examples/customer_examples.rb +93 -53
  66. data/spec/shared_stripe_examples/dispute_examples.rb +2 -2
  67. data/spec/shared_stripe_examples/error_mock_examples.rb +8 -7
  68. data/spec/shared_stripe_examples/express_login_link_examples.rb +12 -0
  69. data/spec/shared_stripe_examples/external_account_examples.rb +3 -3
  70. data/spec/shared_stripe_examples/invoice_examples.rb +41 -39
  71. data/spec/shared_stripe_examples/invoice_item_examples.rb +1 -1
  72. data/spec/shared_stripe_examples/payment_intent_examples.rb +147 -0
  73. data/spec/shared_stripe_examples/payment_method_examples.rb +449 -0
  74. data/spec/shared_stripe_examples/payout_examples.rb +2 -2
  75. data/spec/shared_stripe_examples/plan_examples.rb +135 -92
  76. data/spec/shared_stripe_examples/price_examples.rb +183 -0
  77. data/spec/shared_stripe_examples/product_examples.rb +147 -0
  78. data/spec/shared_stripe_examples/refund_examples.rb +25 -21
  79. data/spec/shared_stripe_examples/setup_intent_examples.rb +68 -0
  80. data/spec/shared_stripe_examples/subscription_examples.rb +430 -318
  81. data/spec/shared_stripe_examples/subscription_items_examples.rb +3 -2
  82. data/spec/shared_stripe_examples/transfer_examples.rb +6 -6
  83. data/spec/shared_stripe_examples/webhook_event_examples.rb +11 -11
  84. data/spec/spec_helper.rb +7 -4
  85. data/spec/stripe_mock_spec.rb +4 -4
  86. data/spec/support/shared_contexts/stripe_validator_spec.rb +8 -0
  87. data/spec/support/stripe_examples.rb +9 -1
  88. data/stripe-ruby-mock.gemspec +8 -3
  89. metadata +72 -34
  90. data/spec/shared_stripe_examples/product_example.rb +0 -65
@@ -0,0 +1,147 @@
1
+ require "spec_helper"
2
+
3
+ shared_examples "Product API" do
4
+ let(:product_attributes) { {id: "prod_123", name: "My Mock Product"} }
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 "Uniqueness" do
105
+ let(:already_exists_message){ stripe_validator.already_exists_message(Stripe::Product) }
106
+
107
+ it "validates uniqueness of identifier" do
108
+ stripe_helper.delete_product(params[:id])
109
+
110
+ Stripe::Product.create(params)
111
+ expect {
112
+ Stripe::Product.create(params)
113
+ }.to raise_error(Stripe::InvalidRequestError, already_exists_message)
114
+ end
115
+ end
116
+ end
117
+
118
+ describe "Mock Data" do
119
+ let(:mock_object) { StripeMock::Data.mock_product }
120
+ let(:known_attributes) { [
121
+ :id,
122
+ :object,
123
+ :active,
124
+ :attributes,
125
+ :caption,
126
+ :created,
127
+ :deactivate_on,
128
+ :description,
129
+ :images,
130
+ :livemode,
131
+ :metadata,
132
+ :name,
133
+ :package_dimensions,
134
+ :shippable,
135
+ :statement_descriptor,
136
+ :type,
137
+ :unit_label,
138
+ :updated,
139
+ :url
140
+ ] }
141
+
142
+ it "includes all retreived attributes" do
143
+ expect(mock_object.keys).to eql(known_attributes)
144
+ end
145
+ end
146
+
147
+ 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
- shared_examples 'Customer Subscriptions' do
4
+ shared_examples 'Customer Subscriptions with plans' 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
- 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')
17
+ it "adds a new subscription to customer with none using items", live: true do
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
- 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')
52
+ it "adds a new subscription to customer with none", live: true do
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,121 +99,149 @@ 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" )
118
+ end
119
+
120
+ it "adds a new subscription with payment method provided as default" do
121
+ plan
122
+ customer = Stripe::Customer.create(source: gen_card_tk)
123
+ payment_method = Stripe::PaymentMethod.create(
124
+ type: 'card',
125
+ card: {
126
+ number: 4242_4242_4242_4242,
127
+ exp_month: 9,
128
+ exp_year: (Time.now.year + 5),
129
+ cvc: 999
130
+ }
131
+ )
132
+ sub = Stripe::Subscription.create(
133
+ plan: 'silver',
134
+ customer: customer,
135
+ metadata: { foo: "bar", example: "yes" },
136
+ collection_method: 'send_invoice',
137
+ default_payment_method: payment_method.id,
138
+ )
139
+
140
+ subscriptions = Stripe::Subscription.list(customer: customer.id)
141
+ expect(subscriptions.count).to eq(1)
142
+ expect(subscriptions.data.first.id).to eq(sub.id)
143
+ expect(subscriptions.data.first.collection_method).to eq('send_invoice')
144
+ expect(subscriptions.data.first.default_payment_method).to eq(payment_method.id)
99
145
  end
100
146
 
101
147
  it "adds a new subscription to customer (string/symbol agnostic)" do
102
148
  customer = Stripe::Customer.create(source: gen_card_tk)
103
- expect(customer.subscriptions.count).to eq(0)
149
+ subscriptions = Stripe::Subscription.list(customer: customer.id)
150
+ expect(subscriptions.count).to eq(0)
104
151
 
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)
152
+ plan
153
+ sub = Stripe::Subscription.create({plan: plan.id, customer: customer.id })
111
154
 
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
155
  customer = Stripe::Customer.retrieve(customer.id)
156
+ subscriptions = Stripe::Subscription.list(customer: customer.id)
116
157
  expect(sub.plan.to_hash).to eq(plan.to_hash)
117
- expect(customer.subscriptions.count).to eq(2)
158
+ expect(subscriptions.count).to eq(1)
118
159
  end
119
160
 
120
161
  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
162
  customer = Stripe::Customer.create(source: gen_card_tk)
125
- Stripe::Subscription.create({ plan: 'silver', customer: customer.id, metadata: { foo: "bar", example: "yes" } })
163
+ Stripe::Subscription.create({ plan: plan.id, customer: customer.id, metadata: { foo: "bar", example: "yes" } })
126
164
  customer = Stripe::Customer.retrieve(customer.id)
165
+ charges = Stripe::Charge.list(customer: customer.id)
127
166
 
128
- expect(customer.charges.data.length).to eq(1)
129
- expect(customer.charges.data.first.amount).to eq(4999)
167
+ expect(charges.data.length).to eq(1)
168
+ expect(charges.data.first.amount).to eq(4999)
130
169
  end
131
170
 
132
171
  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
172
  coupon = stripe_helper.create_coupon(id: 'free_coupon', duration: 'repeating', duration_in_months: 3)
136
173
  customer = Stripe::Customer.create(source: gen_card_tk)
137
174
  Stripe::Subscription.create(plan: plan.id, customer: customer.id, coupon: coupon.id)
138
175
  customer = Stripe::Customer.retrieve(customer.id)
139
176
 
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)
177
+ subscriptions = Stripe::Subscription.list(customer: customer.id)
178
+
179
+ expect(subscriptions.data).to be_a(Array)
180
+ expect(subscriptions.data.count).to eq(1)
181
+ expect(subscriptions.data.first.discount).not_to be_nil
182
+ expect(subscriptions.data.first.discount).to be_a(Stripe::Discount)
183
+ expect(subscriptions.data.first.discount.coupon.id).to eq(coupon.id)
145
184
  end
146
185
 
147
186
  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
187
  customer = Stripe::Customer.create(source: gen_card_tk)
151
188
 
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
- }
189
+ expect {
190
+ Stripe::Subscription.create(plan: plan.id, customer: customer.id, coupon: 'none')
191
+ }.to raise_error {|e|
192
+ expect(e).to be_a Stripe::InvalidRequestError
193
+ expect(e.http_status).to eq(400)
194
+ expect(e.message).to eq('No such coupon: none')
195
+ }
157
196
  end
158
197
 
159
198
  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
199
  customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk)
171
200
 
172
201
  subscription = Stripe::Subscription.create({
173
- plan: "silver", customer: customer.id, quantity: 2, application_fee_percent: 10, tax_percent: 20})
202
+ plan: plan.id,
203
+ customer: customer.id,
204
+ quantity: 2,
205
+ application_fee_percent: 10,
206
+ tax_percent: 20
207
+ })
174
208
  expect(subscription.quantity).to eq(2)
175
209
  expect(subscription.application_fee_percent).to eq(10)
176
210
  expect(subscription.tax_percent).to eq(20)
177
211
  end
178
212
 
213
+ it "correctly sets pending invoice item interval" do
214
+ customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk)
215
+
216
+ subscription = Stripe::Subscription.create({
217
+ plan: plan.id,
218
+ customer: customer.id,
219
+ quantity: 2,
220
+ pending_invoice_item_interval: { interval: 'month', interval_count: 1 }
221
+ })
222
+
223
+ expect(subscription.pending_invoice_item_interval.interval).to eq 'month'
224
+ expect(subscription.pending_invoice_item_interval.interval_count).to eq 1
225
+ end
226
+
179
227
  it "correctly sets created when it's not provided as a parameter", live: true do
180
228
  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 })
229
+ subscription = Stripe::Subscription.create({ plan: plan.id, customer: customer.id })
184
230
 
185
231
  expect(subscription.created).to eq(subscription.current_period_start)
186
232
  end
187
233
 
188
234
  it "correctly sets created when it's provided as a parameter" do
189
235
  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 })
236
+ subscription = Stripe::Subscription.create({ plan: plan.id, customer: customer.id, created: 1473576318 })
193
237
 
194
238
  expect(subscription.created).to eq(1473576318)
195
239
  end
196
240
 
197
241
  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')
242
+ silver = stripe_helper.create_plan(id: 'silver', product: product.id)
243
+ gold = stripe_helper.create_plan(id: 'gold', product: product.id)
244
+ customer = Stripe::Customer.create(id: 'test_customer_sub', product: product.id, source: gen_card_tk, plan: 'gold')
201
245
 
202
246
  sub = Stripe::Subscription.create({ plan: 'silver', customer: customer.id })
203
247
 
@@ -218,7 +262,7 @@ shared_examples 'Customer Subscriptions' do
218
262
  end
219
263
 
220
264
  it "subscribes a cardless customer when specifing a card token" do
221
- plan = stripe_helper.create_plan(id: 'enterprise', amount: 499)
265
+ plan = stripe_helper.create_plan(id: 'enterprise', product: product.id, amount: 499)
222
266
  customer = Stripe::Customer.create(id: 'cardless')
223
267
 
224
268
  sub = Stripe::Subscription.create(plan: 'enterprise', customer: customer.id, source: gen_card_tk)
@@ -247,10 +291,10 @@ shared_examples 'Customer Subscriptions' do
247
291
  end
248
292
 
249
293
  it "throws an error when subscribing a customer with no card" do
250
- plan = stripe_helper.create_plan(id: 'enterprise', amount: 499)
294
+ plan = stripe_helper.create_plan(id: 'enterprise', product: product.id, amount: 499)
251
295
  customer = Stripe::Customer.create(id: 'cardless')
252
296
 
253
- expect { Stripe::Subscription.create({ plan: 'enterprise', customer: customer.id }) }.to raise_error {|e|
297
+ expect { Stripe::Subscription.create({ plan: plan.id, customer: customer.id }) }.to raise_error {|e|
254
298
  expect(e).to be_a Stripe::InvalidRequestError
255
299
  expect(e.http_status).to eq(400)
256
300
  expect(e.message).to_not be_nil
@@ -261,12 +305,14 @@ shared_examples 'Customer Subscriptions' do
261
305
  end
262
306
 
263
307
  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
308
  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
309
 
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|
310
+ expect(plan.currency).to eql("usd")
311
+ usd_subscription = Stripe::Subscription.create({ plan: plan.id, customer: customer.id })
312
+
313
+ eur_plan = stripe_helper.create_plan(plan_attrs.merge(id: "plan_EURO", currency: 'eur'))
314
+ expect(eur_plan.currency).to eql("eur")
315
+ expect { Stripe::Subscription.create({ plan: eur_plan.id, customer: customer.id }) }.to raise_error {|e|
270
316
  expect(e).to be_a Stripe::InvalidRequestError
271
317
  expect(e.http_status).to eq(400)
272
318
  expect(e.message).to_not be_nil
@@ -274,7 +320,7 @@ shared_examples 'Customer Subscriptions' do
274
320
  end
275
321
 
276
322
  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)
323
+ plan = stripe_helper.create_plan(id: 'trial', product: product.id, amount: 999)
278
324
  customer = Stripe::Customer.create(source: gen_card_tk)
279
325
 
280
326
  expect{ Stripe::Subscription.create(plan: plan.id, customer: customer.id, trial: 10) }.to raise_error {|e|
@@ -286,35 +332,34 @@ shared_examples 'Customer Subscriptions' do
286
332
  end
287
333
 
288
334
  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
335
  customer = Stripe::Customer.create(id: 'cardless')
291
-
292
- sub = Stripe::Subscription.create({ plan: 'trial', customer: customer.id })
336
+ sub = Stripe::Subscription.create({ plan: plan_with_trial.id, customer: customer.id })
293
337
 
294
338
  expect(sub.object).to eq('subscription')
295
- expect(sub.plan.to_hash).to eq(plan.to_hash)
339
+ expect(sub.plan.to_hash).to eq(plan_with_trial.to_hash)
296
340
  expect(sub.trial_end - sub.trial_start).to eq(14 * 86400)
297
341
  expect(sub.billing_cycle_anchor).to be_nil
298
342
 
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)
343
+ customer = Stripe::Customer.retrieve(customer.id)
344
+ subscriptions = Stripe::Subscription.list(customer: customer.id)
345
+ charges = Stripe::Charge.list(customer: customer.id)
303
346
 
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)
347
+ expect(subscriptions.data).to_not be_empty
348
+ expect(subscriptions.count).to eq(1)
349
+ expect(subscriptions.data.length).to eq(1)
350
+
351
+ expect(subscriptions.data.first.id).to eq(sub.id)
352
+ expect(subscriptions.data.first.plan.to_hash).to eq(plan_with_trial.to_hash)
353
+ expect(subscriptions.data.first.customer).to eq(customer.id)
354
+ expect(charges.count).to eq(0)
308
355
  end
309
356
 
310
357
  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
358
  customer = Stripe::Customer.create(id: 'cardless')
313
-
314
- sub = Stripe::Subscription.create({ items: [ { plan: 'trial' } ], customer: customer.id })
359
+ sub = Stripe::Subscription.create({ items: [ { plan: plan_with_trial.id } ], customer: customer.id })
315
360
 
316
361
  expect(sub.object).to eq('subscription')
317
- expect(sub.items.data[0].plan.to_hash).to eq(plan.to_hash)
362
+ expect(sub.items.data[0].plan.to_hash).to eq(plan_with_trial.to_hash)
318
363
  # no idea how to fix this one
319
364
  # expect(sub.trial_end - sub.trial_start).to eq(14 * 86400)
320
365
 
@@ -324,17 +369,17 @@ shared_examples 'Customer Subscriptions' do
324
369
  expect(customer.subscriptions.data.length).to eq(1)
325
370
 
326
371
  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)
372
+ expect(customer.subscriptions.data.first.items.data.first.plan.to_hash).to eq(plan_with_trial.to_hash)
328
373
  expect(customer.subscriptions.data.first.customer).to eq(customer.id)
329
374
  # No idea on this one
330
375
  # expect(customer.charges.count).to eq(0)
331
376
  end
332
377
 
333
378
  it "subscribes a customer with no card to a free plan" do
334
- plan = stripe_helper.create_plan(id: 'free_tier', amount: 0)
379
+ plan = stripe_helper.create_plan(id: 'free_tier', product: product.id, amount: 0)
335
380
  customer = Stripe::Customer.create(id: 'cardless')
336
381
 
337
- sub = Stripe::Subscription.create({ plan: 'free_tier', customer: customer.id })
382
+ sub = Stripe::Subscription.create({ plan: plan.id, customer: customer.id })
338
383
 
339
384
  expect(sub.object).to eq('subscription')
340
385
  expect(sub.plan.to_hash).to eq(plan.to_hash)
@@ -350,20 +395,21 @@ shared_examples 'Customer Subscriptions' do
350
395
  end
351
396
 
352
397
  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
398
  customer = Stripe::Customer.create(id: 'short_trial')
355
399
  trial_end = Time.now.utc.to_i + 3600
356
400
 
357
- sub = Stripe::Subscription.create({ plan: 'trial', customer: customer.id, trial_end: trial_end })
401
+ sub = Stripe::Subscription.create({ plan: plan_with_trial.id, customer: customer.id, trial_end: trial_end })
358
402
 
359
403
  expect(sub.object).to eq('subscription')
360
- expect(sub.plan.to_hash).to eq(plan.to_hash)
404
+ expect(sub.plan.to_hash).to eq(plan_with_trial.to_hash)
361
405
  expect(sub.current_period_end).to eq(trial_end)
362
406
  expect(sub.trial_end).to eq(trial_end)
363
407
  end
364
408
 
365
409
  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)
410
+ product = stripe_helper.create_product(name: 'Trial')
411
+ plan = stripe_helper.create_plan(id: 'trial', amount: 999,
412
+ trial_period_days: 14, product: product.id)
367
413
  customer = Stripe::Customer.create(id: 'short_trial')
368
414
  trial_end = Time.now.utc.to_i + 3600
369
415
  metadata = {description: 'original description'}
@@ -387,23 +433,21 @@ shared_examples 'Customer Subscriptions' do
387
433
  end
388
434
 
389
435
  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
436
  customer = Stripe::Customer.create(id: 'no_trial', source: gen_card_tk)
392
437
 
393
- sub = Stripe::Subscription.create({ plan: 'trial', customer: customer.id, trial_end: "now" })
438
+ sub = Stripe::Subscription.create({ plan: plan_with_trial.id, customer: customer.id, trial_end: "now" })
394
439
 
395
440
  expect(sub.object).to eq('subscription')
396
- expect(sub.plan.to_hash).to eq(plan.to_hash)
441
+ expect(sub.plan.to_hash).to eq(plan_with_trial.to_hash)
397
442
  expect(sub.status).to eq('active')
398
443
  expect(sub.trial_start).to be_nil
399
444
  expect(sub.trial_end).to be_nil
400
445
  end
401
446
 
402
447
  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
448
  customer = Stripe::Customer.create(id: 'cus_trial')
405
449
 
406
- expect { Stripe::Subscription.create({ plan: 'trial', customer: customer.id, trial_end: "gazebo" }) }.to raise_error {|e|
450
+ expect { Stripe::Subscription.create({ plan: plan_with_trial.id, customer: customer.id, trial_end: "gazebo" }) }.to raise_error {|e|
407
451
  expect(e).to be_a Stripe::InvalidRequestError
408
452
  expect(e.http_status).to eq(400)
409
453
  expect(e.message).to eq("Invalid timestamp: must be an integer")
@@ -411,11 +455,10 @@ shared_examples 'Customer Subscriptions' do
411
455
  end
412
456
 
413
457
  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
458
  customer = Stripe::Customer.create(id: 'past_trial')
416
459
  trial_end = Time.now.utc.to_i - 3600
417
460
 
418
- expect { Stripe::Subscription.create({ plan: 'trial', customer: customer.id, trial_end: trial_end }) }.to raise_error {|e|
461
+ expect { Stripe::Subscription.create({ plan: plan_with_trial.id, customer: customer.id, trial_end: trial_end }) }.to raise_error {|e|
419
462
  expect(e).to be_a Stripe::InvalidRequestError
420
463
  expect(e.http_status).to eq(400)
421
464
  expect(e.message).to eq("Invalid timestamp: must be an integer Unix timestamp in the future")
@@ -423,11 +466,10 @@ shared_examples 'Customer Subscriptions' do
423
466
  end
424
467
 
425
468
  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
469
  customer = Stripe::Customer.create(id: 'long_trial')
428
470
  trial_end = Time.now.utc.to_i + 31557600*5 + 3600 # 5 years + 1 hour
429
471
 
430
- expect { Stripe::Subscription.create({ plan: 'trial', customer: customer.id, trial_end: trial_end }) }.to raise_error {|e|
472
+ expect { Stripe::Subscription.create({ plan: plan_with_trial.id, customer: customer.id, trial_end: trial_end }) }.to raise_error {|e|
431
473
  expect(e).to be_a Stripe::InvalidRequestError
432
474
  expect(e.http_status).to eq(400)
433
475
  expect(e.message).to eq("Invalid timestamp: can be no more than five years in the future")
@@ -435,11 +477,10 @@ shared_examples 'Customer Subscriptions' do
435
477
  end
436
478
 
437
479
  it 'overrides current period end when billing cycle anchor is set' do
438
- plan = stripe_helper.create_plan(id: 'plan', amount: 999)
439
480
  customer = Stripe::Customer.create(source: gen_card_tk)
440
481
  billing_cycle_anchor = Time.now.utc.to_i + 3600
441
482
 
442
- sub = Stripe::Subscription.create({ plan: 'plan', customer: customer.id, billing_cycle_anchor: billing_cycle_anchor })
483
+ sub = Stripe::Subscription.create({ plan: plan.id, customer: customer.id, billing_cycle_anchor: billing_cycle_anchor })
443
484
 
444
485
  expect(sub.status).to eq('active')
445
486
  expect(sub.current_period_end).to eq(billing_cycle_anchor)
@@ -447,17 +488,11 @@ shared_examples 'Customer Subscriptions' do
447
488
  end
448
489
 
449
490
  it 'when plan defined inside items', live: true do
450
- plan = stripe_helper.create_plan(id: 'BASE_PRICE_PLAN1')
491
+ plan = stripe_helper.create_plan(id: 'BASE_PRICE_PLAN1', product: product.id)
451
492
 
452
- plan2 = stripe_helper.create_plan(id: 'PER_USER_PLAN1')
493
+ plan2 = stripe_helper.create_plan(id: 'PER_USER_PLAN1', product: product.id)
453
494
  customer = Stripe::Customer.create(
454
- source: {
455
- object: 'card',
456
- exp_month: 11,
457
- exp_year: 2019,
458
- number: '4242424242424242',
459
- cvc: '123'
460
- }
495
+ source: stripe_helper.generate_card_token
461
496
  )
462
497
  subscription = Stripe::Subscription.create(
463
498
  customer: customer.id,
@@ -476,9 +511,9 @@ shared_examples 'Customer Subscriptions' do
476
511
  end
477
512
 
478
513
  it 'when plan defined inside items for trials with no card', live: true do
479
- plan = stripe_helper.create_plan(id: 'BASE_PRICE_PLAN1')
514
+ plan = stripe_helper.create_plan(id: 'BASE_PRICE_PLAN1', product: product.id)
480
515
 
481
- plan2 = stripe_helper.create_plan(id: 'PER_USER_PLAN1')
516
+ plan2 = stripe_helper.create_plan(id: 'PER_USER_PLAN1', product: product.id)
482
517
  customer = Stripe::Customer.create
483
518
  trial_end = Time.now.utc.to_i + 3600
484
519
 
@@ -498,7 +533,8 @@ shared_examples 'Customer Subscriptions' do
498
533
  end
499
534
 
500
535
  it 'add a new subscription to bill via an invoice' do
501
- plan = stripe_helper.create_plan(id: 'silver', product: { name: 'Silver Plan' },
536
+ product = stripe_helper.create_product(id: 'invoice_billing')
537
+ plan = stripe_helper.create_plan(id: 'silver', product: product.id,
502
538
  amount: 4999, currency: 'usd')
503
539
  customer = Stripe::Customer.create(id: 'cardless')
504
540
 
@@ -520,41 +556,51 @@ shared_examples 'Customer Subscriptions' do
520
556
  end
521
557
 
522
558
  let(:subscription_header) {{
523
- :idempotency_key => 'a_idempotency_key'
559
+ :idempotency_key => SecureRandom.hex
524
560
  }}
525
561
 
526
562
  it "adds a new subscription to customer with identical idempotency key" do
527
- plan = stripe_helper.create_plan(id: 'silver', product: { name: 'Silver Plan' },
563
+ product = stripe_helper.create_product(name: 'Silver Product')
564
+ plan = stripe_helper.create_plan(id: 'silver', product: product.id,
528
565
  amount: 4999, currency: 'usd')
529
566
  customer = Stripe::Customer.create(source: gen_card_tk)
530
567
 
531
568
  expect(customer.subscriptions.data).to be_empty
532
569
  expect(customer.subscriptions.count).to eq(0)
533
570
 
571
+ subscription_header = {
572
+ :idempotency_key => "uniq_idempotency_key_#{customer.id}"
573
+ }
574
+
534
575
  sub1 = Stripe::Subscription.create({ items: [{ plan: 'silver' }], customer: customer.id }, subscription_header)
535
576
  sub2 = Stripe::Subscription.create({ items: [{ plan: 'silver' }], customer: customer.id }, subscription_header)
536
577
  expect(sub1).to eq(sub2)
537
578
  end
538
579
 
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' },
580
+ it "adds a new subscription to customer with different idempotency key", live: true do
581
+ product = stripe_helper.create_product(name: 'Silver Product')
582
+ plan = stripe_helper.create_plan(id: 'silver', product: product.id,
541
583
  amount: 4999, currency: 'usd')
542
584
  customer = Stripe::Customer.create(source: gen_card_tk)
543
585
 
544
586
  expect(customer.subscriptions.data).to be_empty
545
587
  expect(customer.subscriptions.count).to eq(0)
546
588
 
547
- another_subscription_header = {
548
- :idempotency_key => 'another_idempotency_key'
589
+ sub_header_1 = {
590
+ :idempotency_key => SecureRandom.hex
591
+ }
592
+ sub_header_2 = {
593
+ :idempotency_key => SecureRandom.hex
549
594
  }
550
595
 
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)
596
+ sub1 = Stripe::Subscription.create({ items: [{ plan: 'silver' }], customer: customer.id }, sub_header_1)
597
+ sub2 = Stripe::Subscription.create({ items: [{ plan: 'silver' }], customer: customer.id }, sub_header_2)
553
598
  expect(sub1).not_to eq(sub2)
554
599
  end
555
600
 
556
601
  it "accepts a hash of items" do
557
- silver = stripe_helper.create_plan(id: 'silver')
602
+ product = stripe_helper.create_product(name: 'Silver Product')
603
+ silver = stripe_helper.create_plan(id: 'silver', product: product.id)
558
604
  customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk)
559
605
 
560
606
  sub = Stripe::Subscription.create({ items: { '0' => { plan: 'silver' } }, customer: customer.id })
@@ -562,15 +608,36 @@ shared_examples 'Customer Subscriptions' do
562
608
 
563
609
  expect(sub.cancel_at_period_end).to be_truthy
564
610
  expect(sub.save).to be_truthy
565
- expect(sub.cancel_at_period_end).to be_falsey
611
+ expect(sub.cancel_at_period_end).to be_truthy
566
612
  end
567
613
 
614
+ it 'accepts default_tax_rates param' do
615
+ tax_rate = Stripe::TaxRate.create(
616
+ display_name: 'VAT',
617
+ description: 'VAT Germany',
618
+ jurisdiction: 'DE',
619
+ percentage: 19.0,
620
+ inclusive: false,
621
+ )
622
+ product = stripe_helper.create_product(name: 'Silver Product')
623
+ silver = stripe_helper.create_plan(id: 'silver', product: product.id)
624
+ customer = Stripe::Customer.create(source: gen_card_tk)
625
+ subscription = Stripe::Subscription.create(
626
+ customer: customer.id,
627
+ items: [{ plan: silver.id }],
628
+ default_tax_rates: [ tax_rate ]
629
+ )
630
+
631
+ aggregate_failures do
632
+ expect(subscription.default_tax_rates.length).to eq(1)
633
+ expect(subscription.default_tax_rates.first.id).to eq(tax_rate.id)
634
+ end
635
+ end
568
636
  end
569
637
 
570
638
  context "updating a subscription" do
571
639
  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')
640
+ customer = Stripe::Customer.create(source: gen_card_tk, plan: plan.id)
574
641
 
575
642
  subscription = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
576
643
  subscription.delete
@@ -583,24 +650,22 @@ shared_examples 'Customer Subscriptions' do
583
650
  end
584
651
 
585
652
  it "updates a stripe customer's existing subscription with one plan inside items" do
586
- silver = stripe_helper.create_plan(id: 'silver')
587
653
  customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk)
588
654
 
589
- sub = Stripe::Subscription.create({ items: [ { plan: 'silver' } ], customer: customer.id })
655
+ sub = Stripe::Subscription.create({ items: [ { plan: plan.id } ], customer: customer.id })
590
656
  sub.delete(at_period_end: true)
591
657
 
592
658
  expect(sub.cancel_at_period_end).to be_truthy
593
659
  expect(sub.save).to be_truthy
594
- expect(sub.cancel_at_period_end).to be_falsey
660
+ expect(sub.cancel_at_period_end).to be_truthy
595
661
  end
596
662
 
597
663
  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')
664
+ customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk, plan: plan.id)
601
665
 
666
+ gold_plan = stripe_helper.create_plan(id: 'gold', product: product.id)
602
667
  sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
603
- sub.plan = 'gold'
668
+ sub.plan = gold_plan.id
604
669
  sub.quantity = 5
605
670
  sub.metadata.foo = "bar"
606
671
  sub.metadata.example = "yes"
@@ -608,7 +673,7 @@ shared_examples 'Customer Subscriptions' do
608
673
  expect(sub.save).to be_truthy
609
674
 
610
675
  expect(sub.object).to eq('subscription')
611
- expect(sub.plan.to_hash).to eq(gold.to_hash)
676
+ expect(sub.plan.to_hash).to eq(gold_plan.to_hash)
612
677
  expect(sub.quantity).to eq(5)
613
678
  expect(sub.metadata.foo).to eq( "bar" )
614
679
  expect(sub.metadata.example).to eq( "yes" )
@@ -619,16 +684,15 @@ shared_examples 'Customer Subscriptions' do
619
684
  expect(customer.subscriptions.data.length).to eq(1)
620
685
 
621
686
  expect(customer.subscriptions.data.first.id).to eq(sub.id)
622
- expect(customer.subscriptions.data.first.plan.to_hash).to eq(gold.to_hash)
687
+ expect(customer.subscriptions.data.first.plan.to_hash).to eq(gold_plan.to_hash)
623
688
  expect(customer.subscriptions.data.first.customer).to eq(customer.id)
624
689
  end
625
690
 
626
691
  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)
692
+ customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk, plan: plan.id)
631
693
 
694
+ gold_plan = stripe_helper.create_plan(id: 'gold', product: product.id)
695
+ addon_plan = stripe_helper.create_plan(id: 'addon_plan', product: product.id)
632
696
  sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
633
697
  sub.items = [{ plan: gold_plan.id, quantity: 2 }, { plan: addon_plan.id, quantity: 2 }]
634
698
  expect(sub.save).to be_truthy
@@ -649,12 +713,12 @@ shared_examples 'Customer Subscriptions' do
649
713
  end
650
714
 
651
715
  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
716
  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 }])
717
+ addon1_plan = stripe_helper.create_plan(id: 'addon1', product: product.id)
718
+ sub = Stripe::Subscription.create(customer: customer.id, items: [{ plan: plan.id }, { plan: addon1_plan.id }])
719
+
720
+ gold_plan = stripe_helper.create_plan(id: 'gold', product: product.id)
721
+ addon2_plan = stripe_helper.create_plan(id: 'addon2', product: product.id)
658
722
 
659
723
  sub.items = [{ plan: gold_plan.id, quantity: 2 }, { plan: addon2_plan.id, quantity: 2 }]
660
724
  expect(sub.save).to be_truthy
@@ -674,9 +738,87 @@ shared_examples 'Customer Subscriptions' do
674
738
  expect(customer.subscriptions.data.first.items.data[1].plan.to_hash).to eq(addon2_plan.to_hash)
675
739
  end
676
740
 
741
+ it "updates a subscription's cancel_at_period_end" do
742
+ silver = stripe_helper.create_plan(id: 'silver', product: product.id)
743
+ customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk)
744
+
745
+ sub = Stripe::Subscription.create({ items: [ { plan: 'silver' } ], customer: customer.id })
746
+
747
+ expect(sub.cancel_at_period_end).to be_falsey
748
+
749
+ sub.cancel_at_period_end = true
750
+ sub.save
751
+
752
+ expect(sub.save).to be_truthy
753
+ expect(sub.cancel_at_period_end).to be_truthy
754
+ expect(sub.canceled_at).to be_truthy
755
+
756
+ sub.cancel_at_period_end = false
757
+ sub.save
758
+
759
+ expect(sub.save).to be_truthy
760
+ expect(sub.cancel_at_period_end).to be_falsey
761
+ expect(sub.canceled_at).to be_falsey
762
+ end
763
+
764
+ it "updates a subscription's pending invoice item interval" do
765
+ customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk)
766
+
767
+ subscription = Stripe::Subscription.create({
768
+ plan: plan.id,
769
+ customer: customer.id,
770
+ quantity: 2,
771
+ pending_invoice_item_interval: { interval: 'month', interval_count: 1 }
772
+ })
773
+
774
+ expect(subscription.pending_invoice_item_interval.interval).to eq 'month'
775
+ expect(subscription.pending_invoice_item_interval.interval_count).to eq 1
776
+
777
+ subscription.pending_invoice_item_interval = { interval: 'week', interval_count: 3 }
778
+ subscription.save
779
+
780
+ expect(subscription.pending_invoice_item_interval.interval).to eq 'week'
781
+ expect(subscription.pending_invoice_item_interval.interval_count).to eq 3
782
+ end
783
+
784
+ it "updates a subscription's default payment method" do
785
+ plan
786
+ customer = Stripe::Customer.create(source: gen_card_tk)
787
+ payment_method_card = Stripe::PaymentMethod.create(
788
+ type: 'card',
789
+ card: {
790
+ number: 4242_4242_4242_4242,
791
+ exp_month: 9,
792
+ exp_year: (Time.now.year + 5),
793
+ cvc: 999
794
+ }
795
+ )
796
+ payment_method_sepa = Stripe::PaymentMethod.create(
797
+ type: 'sepa_debit',
798
+ sepa_debit: {iban: 'DE89370400440532013000'},
799
+ )
800
+ subscription = Stripe::Subscription.create(
801
+ plan: 'silver',
802
+ customer: customer,
803
+ metadata: { foo: "bar", example: "yes" },
804
+ default_payment_method: payment_method_card.id,
805
+ )
806
+
807
+ subscriptions = Stripe::Subscription.list(customer: customer)
808
+ expect(subscriptions.data.first.default_payment_method).to eq(payment_method_card.id)
809
+
810
+ Stripe::Subscription.update(
811
+ subscription.id,
812
+ default_payment_method: payment_method_sepa.id,
813
+ collection_method: 'send_invoice',
814
+ )
815
+
816
+ subscriptions = Stripe::Subscription.list(customer: customer)
817
+ expect(subscriptions.data.first.collection_method).to eq('send_invoice')
818
+ expect(subscriptions.data.first.default_payment_method).to eq(payment_method_sepa.id)
819
+ end
820
+
677
821
  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
822
  coupon = stripe_helper.create_coupon
681
823
  customer = Stripe::Customer.create(source: gen_card_tk, plan: plan.id)
682
824
  subscription = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
@@ -690,8 +832,6 @@ shared_examples 'Customer Subscriptions' do
690
832
  end
691
833
 
692
834
  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
835
  customer = Stripe::Customer.create(source: gen_card_tk, plan: plan.id)
696
836
  subscription = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
697
837
 
@@ -706,8 +846,6 @@ shared_examples 'Customer Subscriptions' do
706
846
  end
707
847
 
708
848
  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
849
  customer = Stripe::Customer.create(source: gen_card_tk, plan: plan.id)
712
850
  coupon = stripe_helper.create_coupon
713
851
  subscription = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
@@ -721,8 +859,7 @@ shared_examples 'Customer Subscriptions' do
721
859
  end
722
860
 
723
861
  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')
862
+ customer = Stripe::Customer.create(id: 'cardless', plan: free_plan.id)
726
863
 
727
864
  sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
728
865
  sub.plan = 'gazebo'
@@ -736,75 +873,59 @@ shared_examples 'Customer Subscriptions' do
736
873
  customer = Stripe::Customer.retrieve('cardless')
737
874
  expect(customer.subscriptions.count).to eq(1)
738
875
  expect(customer.subscriptions.data.length).to eq(1)
739
- expect(customer.subscriptions.data.first.plan.to_hash).to eq(free.to_hash)
876
+ expect(customer.subscriptions.data.first.plan.to_hash).to eq(free_plan.to_hash)
740
877
  end
741
878
 
742
879
  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|
880
+ expect(stripe_helper.list_subscriptions(50).keys).to_not include("sub_NONEXIST")
881
+ expect { Stripe::Subscription.retrieve("sub_NONEXIST") }.to raise_error {|e|
747
882
  expect(e).to be_a Stripe::InvalidRequestError
748
883
  expect(e.http_status).to eq(404)
749
884
  expect(e.message).to_not be_nil
750
885
  }
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
886
  end
757
887
 
758
888
  [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
889
+ it "raises an error when updating a customer with no card, and plan trial_period_days = #{trial_period_days}", live: true do
760
890
  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')
891
+ free_plan
892
+ paid_plan = stripe_helper.create_plan(id: 'enterprise', product: product.id, amount: 499, trial_period_days: trial_period_days)
893
+ customer = Stripe::Customer.create(description: 'cardless', plan: free_plan.id)
764
894
 
765
895
  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)
896
+ sub.plan = paid_plan.id
897
+
898
+ expect {
899
+ sub.save
900
+ }.to raise_error(Stripe::InvalidRequestError, "This customer has no attached payment source")
778
901
  ensure
779
902
  customer.delete if customer
780
- paid.delete if paid
781
- free.delete if free
903
+ paid_plan.delete if paid_plan
904
+ free_plan.delete if free_plan
782
905
  end
783
906
  end
784
907
  end
785
908
 
786
909
  it 'updates a subscription if the customer has a free trial', live: true do
787
- stripe_helper.create_plan(id: 'enterprise', amount: 499)
788
910
  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
911
+ customer = Stripe::Customer.create(plan: plan.id, trial_end: trial_end)
912
+ subscription = customer.subscriptions.first
792
913
  subscription.quantity = 2
793
914
  subscription.save
794
915
  expect(subscription.quantity).to eq(2)
795
916
  end
796
917
 
797
918
  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')
919
+ free_plan
920
+ trial_plan = stripe_helper.create_plan(id: 'trial', product: product.id, amount: 999, trial_period_days: 14)
921
+ customer = Stripe::Customer.create(id: 'cardless', plan: free_plan.id)
801
922
 
802
923
  sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
803
- sub.plan = 'trial'
924
+ sub.plan = trial_plan.id
804
925
  sub.save
805
926
 
806
927
  expect(sub.object).to eq('subscription')
807
- expect(sub.plan.to_hash).to eq(trial.to_hash)
928
+ expect(sub.plan.to_hash).to eq(trial_plan.to_hash)
808
929
 
809
930
  customer = Stripe::Customer.retrieve('cardless')
810
931
  expect(customer.subscriptions.data).to_not be_empty
@@ -812,21 +933,21 @@ shared_examples 'Customer Subscriptions' do
812
933
  expect(customer.subscriptions.data.length).to eq(1)
813
934
 
814
935
  expect(customer.subscriptions.data.first.id).to eq(sub.id)
815
- expect(customer.subscriptions.data.first.plan.to_hash).to eq(trial.to_hash)
936
+ expect(customer.subscriptions.data.first.plan.to_hash).to eq(trial_plan.to_hash)
816
937
  expect(customer.subscriptions.data.first.customer).to eq(customer.id)
817
938
  end
818
939
 
819
940
  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')
941
+ free_plan
942
+ customer = Stripe::Customer.create(id: 'cardless', product: product.id, plan: free_plan.id)
823
943
 
824
944
  sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
825
- sub.plan = 'gratis'
945
+ gratis_plan = stripe_helper.create_plan(id: 'gratis', product: product.id, amount: 0)
946
+ sub.plan = gratis_plan.id
826
947
  sub.save
827
948
 
828
949
  expect(sub.object).to eq('subscription')
829
- expect(sub.plan.to_hash).to eq(gratis.to_hash)
950
+ expect(sub.plan.to_hash).to eq(gratis_plan.to_hash)
830
951
 
831
952
  customer = Stripe::Customer.retrieve('cardless')
832
953
  expect(customer.subscriptions.data).to_not be_empty
@@ -834,17 +955,15 @@ shared_examples 'Customer Subscriptions' do
834
955
  expect(customer.subscriptions.data.length).to eq(1)
835
956
 
836
957
  expect(customer.subscriptions.data.first.id).to eq(sub.id)
837
- expect(customer.subscriptions.data.first.plan.to_hash).to eq(gratis.to_hash)
958
+ expect(customer.subscriptions.data.first.plan.to_hash).to eq(gratis_plan.to_hash)
838
959
  expect(customer.subscriptions.data.first.customer).to eq(customer.id)
839
960
  end
840
961
 
841
962
  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')
963
+ customer = Stripe::Customer.create(id: 'test_customer_sub', plan: free_plan.id)
845
964
 
846
965
  sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
847
- sub.plan = 'paid'
966
+ sub.plan = plan.id
848
967
  sub.source = gen_card_tk
849
968
  sub.save
850
969
 
@@ -857,11 +976,8 @@ shared_examples 'Customer Subscriptions' do
857
976
  end
858
977
 
859
978
  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
-
979
+ customer = Stripe::Customer.create(id: 'test_trial_end', plan: plan_with_trial.id)
863
980
  sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
864
-
865
981
  trial_end = Time.now.utc.to_i + 3600
866
982
  sub.trial_end = trial_end
867
983
  sub.save
@@ -872,8 +988,7 @@ shared_examples 'Customer Subscriptions' do
872
988
  end
873
989
 
874
990
  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')
991
+ customer = Stripe::Customer.create(id: 'test_trial_end', plan: plan_with_trial.id, default_source: 'tok_visa')
877
992
 
878
993
  sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
879
994
 
@@ -881,18 +996,15 @@ shared_examples 'Customer Subscriptions' do
881
996
  sub.save
882
997
 
883
998
  expect(sub.object).to eq('subscription')
884
- expect(sub.plan.to_hash).to eq(plan.to_hash)
999
+ expect(sub.plan.to_hash).to eq(plan_with_trial.to_hash)
885
1000
  expect(sub.status).to eq('active')
886
1001
  expect(sub.trial_start).to be_nil
887
1002
  expect(sub.trial_end).to be_nil
888
1003
  end
889
1004
 
890
1005
  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
-
1006
+ customer = Stripe::Customer.create(id: 'test_trial_end', plan: plan.id, source: gen_card_tk)
894
1007
  sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
895
-
896
1008
  trial_end = Time.now.utc.to_i + 3600
897
1009
  sub.trial_end = trial_end
898
1010
  sub.save
@@ -906,8 +1018,8 @@ shared_examples 'Customer Subscriptions' do
906
1018
 
907
1019
 
908
1020
  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)
1021
+ expect(plan.trial_period_days).to be_nil
1022
+ customer = Stripe::Customer.create(id: 'test_trial_end', plan: plan.id, source: gen_card_tk)
911
1023
 
912
1024
  sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
913
1025
  sub.trial_end = "gazebo"
@@ -921,10 +1033,10 @@ shared_examples 'Customer Subscriptions' do
921
1033
  end
922
1034
 
923
1035
  context "cancelling a subscription" do
1036
+ let(:customer) { Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk, plan: plan.id) }
924
1037
 
925
- 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")
1038
+ it "cancels a stripe customer's subscription", live: true do
1039
+ customer = Stripe::Customer.create(source: gen_card_tk, plan: plan.id)
928
1040
 
929
1041
  sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
930
1042
  result = sub.delete
@@ -939,53 +1051,11 @@ shared_examples 'Customer Subscriptions' do
939
1051
  expect(customer.subscriptions.count).to eq(0)
940
1052
  expect(customer.subscriptions.data.length).to eq(0)
941
1053
  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
1054
  end
986
1055
 
987
1056
  it "supports 'cancelling' by updating cancel_at_period_end" do
988
- truth = stripe_helper.create_plan(id: 'the_truth')
1057
+ product = stripe_helper.create_product(name: 'Truth Product')
1058
+ truth = stripe_helper.create_plan(id: 'the_truth', product: product.id)
989
1059
  customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk, plan: "the_truth")
990
1060
 
991
1061
  sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
@@ -1007,7 +1077,8 @@ shared_examples 'Customer Subscriptions' do
1007
1077
  end
1008
1078
 
1009
1079
  it "resumes a subscription cancelled by updating cancel_at_period_end" do
1010
- truth = stripe_helper.create_plan(id: 'the_truth')
1080
+ product = stripe_helper.create_product(name: 'Truth Product')
1081
+ truth = stripe_helper.create_plan(id: 'the_truth', product: product.id)
1011
1082
  customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk, plan: "the_truth")
1012
1083
 
1013
1084
  sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
@@ -1031,7 +1102,7 @@ shared_examples 'Customer Subscriptions' do
1031
1102
  end
1032
1103
 
1033
1104
  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)
1105
+ trial = stripe_helper.create_plan(id: 'trial', product: product.id, trial_period_days: 14)
1035
1106
  customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk, plan: "trial")
1036
1107
 
1037
1108
  sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
@@ -1044,11 +1115,11 @@ shared_examples 'Customer Subscriptions' do
1044
1115
  expect(customer.subscriptions.data.first.status).to eq('trialing')
1045
1116
  end
1046
1117
 
1047
- it "doesn't require a card when trial_end is present", :live => true do
1118
+ it "doesn't require a card when trial_end is present", live: true do
1048
1119
  plan = stripe_helper.create_plan(
1049
1120
  :amount => 2000,
1121
+ :product => product.id,
1050
1122
  :interval => 'month',
1051
- :name => 'Amazing Gold Plan',
1052
1123
  :currency => 'usd',
1053
1124
  :id => 'gold'
1054
1125
  )
@@ -1063,7 +1134,7 @@ shared_examples 'Customer Subscriptions' do
1063
1134
  let(:subscription) { Stripe::Subscription.retrieve(customer.subscriptions.data.first.id) }
1064
1135
 
1065
1136
  before do
1066
- stripe_helper.create_plan(id: 'free', amount: 0)
1137
+ free_plan
1067
1138
  Stripe::Subscription.create({ plan: 'free', customer: customer.id })
1068
1139
  end
1069
1140
 
@@ -1076,7 +1147,7 @@ shared_examples 'Customer Subscriptions' do
1076
1147
  expect(subscription.items.object).to eq('list')
1077
1148
  expect(subscription.items.data.class).to eq(Array)
1078
1149
  expect(subscription.items.data.count).to eq(1)
1079
- expect(subscription.items.data.first.id).to eq('test_txn_default')
1150
+ expect(subscription.items.data.first.id).to include('test_si_')
1080
1151
  expect(subscription.items.data.first.created).to eq(1504716183)
1081
1152
  expect(subscription.items.data.first.object).to eq('subscription_item')
1082
1153
  expect(subscription.items.data.first.plan.amount).to eq(0)
@@ -1089,12 +1160,12 @@ shared_examples 'Customer Subscriptions' do
1089
1160
  context "retrieve multiple subscriptions" do
1090
1161
 
1091
1162
  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")
1163
+ free_plan
1164
+ paid = stripe_helper.create_plan(id: 'paid', product: product.id, amount: 499)
1165
+ customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk, plan: free_plan.id)
1095
1166
  Stripe::Subscription.create({ plan: 'paid', customer: customer.id })
1096
1167
 
1097
- subs = Stripe::Subscription.all({ customer: customer.id })
1168
+ subs = Stripe::Subscription.list({ customer: customer.id })
1098
1169
 
1099
1170
  expect(subs.object).to eq("list")
1100
1171
  expect(subs.count).to eq(2)
@@ -1105,7 +1176,7 @@ shared_examples 'Customer Subscriptions' do
1105
1176
  Stripe::Customer.create(id: 'no_subs')
1106
1177
  customer = Stripe::Customer.retrieve('no_subs')
1107
1178
 
1108
- list = Stripe::Subscription.all({ customer: customer.id })
1179
+ list = Stripe::Subscription.list({ customer: customer.id })
1109
1180
 
1110
1181
  expect(list.object).to eq("list")
1111
1182
  expect(list.count).to eq(0)
@@ -1115,15 +1186,13 @@ shared_examples 'Customer Subscriptions' do
1115
1186
 
1116
1187
  describe "metadata" do
1117
1188
 
1118
- it "creates a stripe customer and subscribes them to a plan with meta data", :live => true do
1189
+ it "creates a stripe customer and subscribes them to a plan with meta data", live: true do
1119
1190
 
1120
1191
  stripe_helper.
1121
1192
  create_plan(
1122
1193
  :amount => 500,
1123
1194
  :interval => 'month',
1124
- :product => {
1125
- :name => 'Sample Plan'
1126
- },
1195
+ :product => product.id,
1127
1196
  :currency => 'usd',
1128
1197
  :id => 'Sample5'
1129
1198
  )
@@ -1146,3 +1215,46 @@ shared_examples 'Customer Subscriptions' do
1146
1215
  end
1147
1216
 
1148
1217
  end
1218
+
1219
+ shared_examples 'Customer Subscriptions with prices' do
1220
+ let(:gen_card_tk) { stripe_helper.generate_card_token }
1221
+
1222
+ let(:product) { stripe_helper.create_product }
1223
+ let(:price) { stripe_helper.create_price(product: product.id, amount: 4999, currency: 'usd') }
1224
+
1225
+ context "creating a new subscription" do
1226
+ it "adds a new subscription to customer with none using items", live: true do
1227
+ customer = Stripe::Customer.create(source: gen_card_tk)
1228
+
1229
+ expect(customer.subscriptions.data).to be_empty
1230
+ expect(customer.subscriptions.count).to eq(0)
1231
+
1232
+ subscription = Stripe::Subscription.create({
1233
+ customer: customer.id,
1234
+ items: [{ price: price.id }],
1235
+ metadata: { foo: "bar", example: "yes" }
1236
+ })
1237
+
1238
+ expect(subscription.object).to eq('subscription')
1239
+ expect(subscription.plan.to_hash).to eq(price.to_hash)
1240
+ expect(subscription.metadata.foo).to eq("bar")
1241
+ expect(subscription.metadata.example).to eq("yes")
1242
+
1243
+ customer = Stripe::Customer.retrieve(customer.id)
1244
+ subscriptions = Stripe::Subscription.list(customer: customer.id)
1245
+ charges = Stripe::Charge.list(customer: customer.id)
1246
+
1247
+ expect(subscriptions.data).to_not be_empty
1248
+ expect(subscriptions.count).to eq(1)
1249
+ expect(subscriptions.data.length).to eq(1)
1250
+ expect(charges.data.length).to eq(1)
1251
+ expect(customer.currency).to eq("usd")
1252
+
1253
+ expect(subscriptions.data.first.id).to eq(subscription.id)
1254
+ expect(subscriptions.data.first.plan.to_hash).to eq(price.to_hash)
1255
+ expect(subscriptions.data.first.customer).to eq(customer.id)
1256
+ expect(subscriptions.data.first.metadata.foo).to eq( "bar" )
1257
+ expect(subscriptions.data.first.metadata.example).to eq( "yes" )
1258
+ end
1259
+ end
1260
+ end