stripe-ruby-mock 2.5.6 → 3.1.0.rc2

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 (97) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +1 -1
  3. data/.travis.yml +6 -9
  4. data/CHANGELOG.md +35 -0
  5. data/Gemfile +1 -0
  6. data/README.md +17 -11
  7. data/lib/stripe_mock.rb +11 -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 +7 -0
  12. data/lib/stripe_mock/client.rb +2 -1
  13. data/lib/stripe_mock/data.rb +323 -13
  14. data/lib/stripe_mock/data/list.rb +42 -9
  15. data/lib/stripe_mock/instance.rb +52 -5
  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 +13 -12
  19. data/lib/stripe_mock/request_handlers/checkout.rb +15 -0
  20. data/lib/stripe_mock/request_handlers/checkout_session.rb +16 -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 +14 -10
  25. data/lib/stripe_mock/request_handlers/helpers/subscription_helpers.rb +22 -7
  26. data/lib/stripe_mock/request_handlers/helpers/token_helpers.rb +1 -1
  27. data/lib/stripe_mock/request_handlers/invoices.rb +11 -5
  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 +44 -0
  33. data/lib/stripe_mock/request_handlers/refunds.rb +6 -3
  34. data/lib/stripe_mock/request_handlers/setup_intents.rb +93 -0
  35. data/lib/stripe_mock/request_handlers/sources.rb +12 -6
  36. data/lib/stripe_mock/request_handlers/subscription_items.rb +36 -0
  37. data/lib/stripe_mock/request_handlers/subscriptions.rb +82 -41
  38. data/lib/stripe_mock/request_handlers/tax_rates.rb +36 -0
  39. data/lib/stripe_mock/request_handlers/tokens.rb +8 -6
  40. data/lib/stripe_mock/request_handlers/validators/param_validators.rb +130 -9
  41. data/lib/stripe_mock/test_strategies/base.rb +68 -8
  42. data/lib/stripe_mock/test_strategies/live.rb +23 -12
  43. data/lib/stripe_mock/test_strategies/mock.rb +6 -2
  44. data/lib/stripe_mock/version.rb +1 -1
  45. data/lib/stripe_mock/webhook_fixtures/balance.available.json +6 -0
  46. data/lib/stripe_mock/webhook_fixtures/charge.dispute.funds_reinstated.json +88 -0
  47. data/lib/stripe_mock/webhook_fixtures/charge.dispute.funds_withdrawn.json +88 -0
  48. data/lib/stripe_mock/webhook_fixtures/charge.failed.json +166 -38
  49. data/lib/stripe_mock/webhook_fixtures/customer.created.json +1 -0
  50. data/lib/stripe_mock/webhook_fixtures/customer.updated.json +1 -0
  51. data/lib/stripe_mock/webhook_fixtures/invoice.created.json +2 -1
  52. data/lib/stripe_mock/webhook_fixtures/invoice.payment_succeeded.json +1 -1
  53. data/lib/stripe_mock/webhook_fixtures/invoice.updated.json +2 -1
  54. data/lib/stripe_mock/webhook_fixtures/payment_intent.payment_failed.json +186 -0
  55. data/lib/stripe_mock/webhook_fixtures/payment_intent.succeeded.json +164 -0
  56. data/lib/stripe_mock/webhook_fixtures/product.created.json +34 -0
  57. data/lib/stripe_mock/webhook_fixtures/product.deleted.json +34 -0
  58. data/lib/stripe_mock/webhook_fixtures/product.updated.json +38 -0
  59. data/spec/instance_spec.rb +10 -12
  60. data/spec/list_spec.rb +38 -0
  61. data/spec/server_spec.rb +6 -3
  62. data/spec/shared_stripe_examples/account_examples.rb +1 -1
  63. data/spec/shared_stripe_examples/account_link_examples.rb +16 -0
  64. data/spec/shared_stripe_examples/balance_examples.rb +6 -0
  65. data/spec/shared_stripe_examples/balance_transaction_examples.rb +3 -3
  66. data/spec/shared_stripe_examples/bank_examples.rb +3 -3
  67. data/spec/shared_stripe_examples/card_examples.rb +4 -4
  68. data/spec/shared_stripe_examples/card_token_examples.rb +17 -21
  69. data/spec/shared_stripe_examples/charge_examples.rb +32 -36
  70. data/spec/shared_stripe_examples/checkout_examples.rb +38 -0
  71. data/spec/shared_stripe_examples/coupon_examples.rb +1 -1
  72. data/spec/shared_stripe_examples/customer_examples.rb +103 -53
  73. data/spec/shared_stripe_examples/dispute_examples.rb +2 -2
  74. data/spec/shared_stripe_examples/error_mock_examples.rb +8 -7
  75. data/spec/shared_stripe_examples/express_login_link_examples.rb +12 -0
  76. data/spec/shared_stripe_examples/external_account_examples.rb +3 -3
  77. data/spec/shared_stripe_examples/invoice_examples.rb +43 -41
  78. data/spec/shared_stripe_examples/invoice_item_examples.rb +1 -1
  79. data/spec/shared_stripe_examples/payment_intent_examples.rb +147 -0
  80. data/spec/shared_stripe_examples/payment_method_examples.rb +449 -0
  81. data/spec/shared_stripe_examples/payout_examples.rb +2 -2
  82. data/spec/shared_stripe_examples/plan_examples.rb +135 -77
  83. data/spec/shared_stripe_examples/price_examples.rb +183 -0
  84. data/spec/shared_stripe_examples/product_examples.rb +155 -0
  85. data/spec/shared_stripe_examples/refund_examples.rb +41 -31
  86. data/spec/shared_stripe_examples/setup_intent_examples.rb +68 -0
  87. data/spec/shared_stripe_examples/subscription_examples.rb +546 -295
  88. data/spec/shared_stripe_examples/subscription_items_examples.rb +76 -0
  89. data/spec/shared_stripe_examples/tax_rate_examples.rb +42 -0
  90. data/spec/shared_stripe_examples/transfer_examples.rb +9 -9
  91. data/spec/shared_stripe_examples/webhook_event_examples.rb +11 -11
  92. data/spec/spec_helper.rb +7 -4
  93. data/spec/stripe_mock_spec.rb +4 -4
  94. data/spec/support/shared_contexts/stripe_validator_spec.rb +8 -0
  95. data/spec/support/stripe_examples.rb +12 -2
  96. data/stripe-ruby-mock.gemspec +8 -3
  97. metadata +81 -32
@@ -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
@@ -337,26 +335,32 @@ shared_examples 'Refund API' do
337
335
  capture: true
338
336
  )
339
337
  end
340
- let(:idempotent_refund_params) {{
341
- charge: charge.id,
338
+ let(:refund_params) {{
339
+ charge: charge.id
340
+ }}
341
+
342
+ let(:refund_headers) {{
342
343
  idempotency_key: 'onceisenough'
343
344
  }}
344
345
 
345
346
  it "returns the original refund if the same idempotency_key is passed in" do
346
- refund1 = Stripe::Refund.create(idempotent_refund_params)
347
- refund2 = Stripe::Refund.create(idempotent_refund_params)
347
+ refund1 = Stripe::Refund.create(refund_params, refund_headers)
348
+ refund2 = Stripe::Refund.create(refund_params, refund_headers)
348
349
 
349
350
  expect(refund1).to eq(refund2)
350
351
  end
351
352
 
352
- it "returns different charges if different idempotency_keys are used for each charge" do
353
- idempotent_refund_params2 = idempotent_refund_params.clone
354
- idempotent_refund_params2[:idempotency_key] = 'thisoneisdifferent'
353
+ context 'different key' do
354
+ let(:different_refund_headers) {{
355
+ idempotency_key: 'thisoneisdifferent'
356
+ }}
355
357
 
356
- refund1 = Stripe::Refund.create(idempotent_refund_params)
357
- refund2 = Stripe::Refund.create(idempotent_refund_params2)
358
+ it "returns different charges if different idempotency_keys are used for each charge" do
359
+ refund1 = Stripe::Refund.create(refund_params, refund_headers)
360
+ refund2 = Stripe::Refund.create(refund_params, different_refund_headers)
358
361
 
359
- expect(refund1).not_to eq(refund2)
362
+ expect(refund1).not_to eq(refund2)
363
+ end
360
364
  end
361
365
  end
362
366
  end
@@ -372,7 +376,8 @@ shared_examples 'Refund API' do
372
376
  description: 'card charge'
373
377
  )
374
378
 
375
- charge = charge.refund(amount: 999)
379
+ Stripe::Refund.create(charge: charge.id, amount: 999)
380
+ charge.refresh
376
381
 
377
382
  expect(charge.refunded).to eq(true)
378
383
  expect(charge.refunds.data.first.amount).to eq(999)
@@ -386,10 +391,10 @@ shared_examples 'Refund API' do
386
391
  source: stripe_helper.generate_card_token,
387
392
  description: 'card charge'
388
393
  )
389
- refund = charge.refund
394
+ refund = Stripe::Refund.create(charge: charge.id)
390
395
 
391
396
  expect(charge.id).to match(/^(test_)?ch/)
392
- expect(refund.id).to eq(charge.id)
397
+ expect(refund.charge).to eq(charge.id)
393
398
  end
394
399
 
395
400
  it "creates a stripe refund with a refund ID" do
@@ -399,10 +404,12 @@ shared_examples 'Refund API' do
399
404
  source: stripe_helper.generate_card_token,
400
405
  description: 'card charge'
401
406
  )
402
- refund = charge.refund
403
407
 
404
- expect(refund.refunds.data.count).to eq 1
405
- 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/)
406
413
  end
407
414
 
408
415
  it "creates a stripe refund with a status" do
@@ -412,10 +419,12 @@ shared_examples 'Refund API' do
412
419
  source: stripe_helper.generate_card_token,
413
420
  description: 'card charge'
414
421
  )
415
- refund = charge.refund
416
422
 
417
- expect(refund.refunds.data.count).to eq 1
418
- 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")
419
428
  end
420
429
 
421
430
  it "creates a stripe refund with a different balance transaction than the charge" do
@@ -425,9 +434,10 @@ shared_examples 'Refund API' do
425
434
  source: stripe_helper.generate_card_token,
426
435
  description: 'card charge'
427
436
  )
428
- refund = charge.refund
437
+ Stripe::Refund.create(charge: charge.id)
438
+ refunds = Stripe::Refund.list(charge: charge.id)
429
439
 
430
- 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)
431
441
  end
432
442
 
433
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::StripeObject)
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,36 +395,59 @@ 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 })
402
+
403
+ expect(sub.object).to eq('subscription')
404
+ expect(sub.plan.to_hash).to eq(plan_with_trial.to_hash)
405
+ expect(sub.current_period_end).to eq(trial_end)
406
+ expect(sub.trial_end).to eq(trial_end)
407
+ end
408
+
409
+ it "does not override trial end when trial end is not set" do
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)
413
+ customer = Stripe::Customer.create(id: 'short_trial')
414
+ trial_end = Time.now.utc.to_i + 3600
415
+ metadata = {description: 'original description'}
416
+
417
+ sub = Stripe::Subscription.create({ plan: 'trial', customer: customer.id, trial_end: trial_end, metadata: metadata })
358
418
 
359
419
  expect(sub.object).to eq('subscription')
360
420
  expect(sub.plan.to_hash).to eq(plan.to_hash)
361
421
  expect(sub.current_period_end).to eq(trial_end)
362
422
  expect(sub.trial_end).to eq(trial_end)
423
+ expect(sub.metadata.description).to eq(metadata[:description])
424
+
425
+ metadata = {description: 'updated description'}
426
+ sub = Stripe::Subscription.update(sub.id, { metadata: metadata })
427
+
428
+ expect(sub.object).to eq('subscription')
429
+ expect(sub.plan.to_hash).to eq(plan.to_hash)
430
+ expect(sub.current_period_end).to eq(trial_end)
431
+ expect(sub.trial_end).to eq(trial_end) # check that the trial_end has NOT changed
432
+ expect(sub.metadata.description).to eq(metadata[:description]) # check that the description has changed
363
433
  end
364
434
 
365
435
  it "returns without a trial when trial_end is set to 'now'" do
366
- plan = stripe_helper.create_plan(id: 'trial', amount: 999, trial_period_days: 14)
367
436
  customer = Stripe::Customer.create(id: 'no_trial', source: gen_card_tk)
368
437
 
369
- 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" })
370
439
 
371
440
  expect(sub.object).to eq('subscription')
372
- expect(sub.plan.to_hash).to eq(plan.to_hash)
441
+ expect(sub.plan.to_hash).to eq(plan_with_trial.to_hash)
373
442
  expect(sub.status).to eq('active')
374
443
  expect(sub.trial_start).to be_nil
375
444
  expect(sub.trial_end).to be_nil
376
445
  end
377
446
 
378
447
  it "raises error when trial_end is not an integer or 'now'" do
379
- plan = stripe_helper.create_plan(id: 'trial', amount: 999, trial_period_days: 14)
380
448
  customer = Stripe::Customer.create(id: 'cus_trial')
381
449
 
382
- 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|
383
451
  expect(e).to be_a Stripe::InvalidRequestError
384
452
  expect(e.http_status).to eq(400)
385
453
  expect(e.message).to eq("Invalid timestamp: must be an integer")
@@ -387,11 +455,10 @@ shared_examples 'Customer Subscriptions' do
387
455
  end
388
456
 
389
457
  it "raises error when trial_end is set to a time in the past" do
390
- plan = stripe_helper.create_plan(id: 'trial', amount: 999, trial_period_days: 14)
391
458
  customer = Stripe::Customer.create(id: 'past_trial')
392
459
  trial_end = Time.now.utc.to_i - 3600
393
460
 
394
- 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|
395
462
  expect(e).to be_a Stripe::InvalidRequestError
396
463
  expect(e.http_status).to eq(400)
397
464
  expect(e.message).to eq("Invalid timestamp: must be an integer Unix timestamp in the future")
@@ -399,11 +466,10 @@ shared_examples 'Customer Subscriptions' do
399
466
  end
400
467
 
401
468
  it "raises error when trial_end is set to a time more than five years in the future" do
402
- plan = stripe_helper.create_plan(id: 'trial', amount: 999, trial_period_days: 14)
403
469
  customer = Stripe::Customer.create(id: 'long_trial')
404
470
  trial_end = Time.now.utc.to_i + 31557600*5 + 3600 # 5 years + 1 hour
405
471
 
406
- 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|
407
473
  expect(e).to be_a Stripe::InvalidRequestError
408
474
  expect(e.http_status).to eq(400)
409
475
  expect(e.message).to eq("Invalid timestamp: can be no more than five years in the future")
@@ -411,11 +477,10 @@ shared_examples 'Customer Subscriptions' do
411
477
  end
412
478
 
413
479
  it 'overrides current period end when billing cycle anchor is set' do
414
- plan = stripe_helper.create_plan(id: 'plan', amount: 999)
415
480
  customer = Stripe::Customer.create(source: gen_card_tk)
416
481
  billing_cycle_anchor = Time.now.utc.to_i + 3600
417
482
 
418
- 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 })
419
484
 
420
485
  expect(sub.status).to eq('active')
421
486
  expect(sub.current_period_end).to eq(billing_cycle_anchor)
@@ -423,17 +488,11 @@ shared_examples 'Customer Subscriptions' do
423
488
  end
424
489
 
425
490
  it 'when plan defined inside items', live: true do
426
- plan = stripe_helper.create_plan(id: 'BASE_PRICE_PLAN1')
491
+ plan = stripe_helper.create_plan(id: 'BASE_PRICE_PLAN1', product: product.id)
427
492
 
428
- plan2 = stripe_helper.create_plan(id: 'PER_USER_PLAN1')
493
+ plan2 = stripe_helper.create_plan(id: 'PER_USER_PLAN1', product: product.id)
429
494
  customer = Stripe::Customer.create(
430
- source: {
431
- object: 'card',
432
- exp_month: 11,
433
- exp_year: 2019,
434
- number: '4242424242424242',
435
- cvc: '123'
436
- }
495
+ source: stripe_helper.generate_card_token
437
496
  )
438
497
  subscription = Stripe::Subscription.create(
439
498
  customer: customer.id,
@@ -452,9 +511,9 @@ shared_examples 'Customer Subscriptions' do
452
511
  end
453
512
 
454
513
  it 'when plan defined inside items for trials with no card', live: true do
455
- plan = stripe_helper.create_plan(id: 'BASE_PRICE_PLAN1')
514
+ plan = stripe_helper.create_plan(id: 'BASE_PRICE_PLAN1', product: product.id)
456
515
 
457
- plan2 = stripe_helper.create_plan(id: 'PER_USER_PLAN1')
516
+ plan2 = stripe_helper.create_plan(id: 'PER_USER_PLAN1', product: product.id)
458
517
  customer = Stripe::Customer.create
459
518
  trial_end = Time.now.utc.to_i + 3600
460
519
 
@@ -472,12 +531,113 @@ shared_examples 'Customer Subscriptions' do
472
531
  expect(subscription.items.data[0].plan.id).to eq plan.id
473
532
  expect(subscription.items.data[1].plan.id).to eq plan2.id
474
533
  end
534
+
535
+ it 'add a new subscription to bill via an invoice' do
536
+ product = stripe_helper.create_product(id: 'invoice_billing')
537
+ plan = stripe_helper.create_plan(id: 'silver', product: product.id,
538
+ amount: 4999, currency: 'usd')
539
+ customer = Stripe::Customer.create(id: 'cardless')
540
+
541
+ expect(customer.subscriptions.data).to be_empty
542
+ expect(customer.subscriptions.count).to eq(0)
543
+
544
+ sub = Stripe::Subscription.create({
545
+ plan: 'silver',
546
+ customer: customer.id,
547
+ metadata: { foo: 'bar', example: 'yes' },
548
+ billing: 'send_invoice',
549
+ days_until_due: 30,
550
+ })
551
+
552
+ expect(sub.object).to eq('subscription')
553
+ expect(sub.plan.to_hash).to eq(plan.to_hash)
554
+ expect(sub.billing).to eq 'send_invoice'
555
+ expect(sub.days_until_due).to eq 30
556
+ end
557
+
558
+ let(:subscription_header) {{
559
+ :idempotency_key => SecureRandom.hex
560
+ }}
561
+
562
+ it "adds a new subscription to customer with identical idempotency key" do
563
+ product = stripe_helper.create_product(name: 'Silver Product')
564
+ plan = stripe_helper.create_plan(id: 'silver', product: product.id,
565
+ amount: 4999, currency: 'usd')
566
+ customer = Stripe::Customer.create(source: gen_card_tk)
567
+
568
+ expect(customer.subscriptions.data).to be_empty
569
+ expect(customer.subscriptions.count).to eq(0)
570
+
571
+ subscription_header = {
572
+ :idempotency_key => "uniq_idempotency_key_#{customer.id}"
573
+ }
574
+
575
+ sub1 = Stripe::Subscription.create({ items: [{ plan: 'silver' }], customer: customer.id }, subscription_header)
576
+ sub2 = Stripe::Subscription.create({ items: [{ plan: 'silver' }], customer: customer.id }, subscription_header)
577
+ expect(sub1).to eq(sub2)
578
+ end
579
+
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,
583
+ amount: 4999, currency: 'usd')
584
+ customer = Stripe::Customer.create(source: gen_card_tk)
585
+
586
+ expect(customer.subscriptions.data).to be_empty
587
+ expect(customer.subscriptions.count).to eq(0)
588
+
589
+ sub_header_1 = {
590
+ :idempotency_key => SecureRandom.hex
591
+ }
592
+ sub_header_2 = {
593
+ :idempotency_key => SecureRandom.hex
594
+ }
595
+
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)
598
+ expect(sub1).not_to eq(sub2)
599
+ end
600
+
601
+ it "accepts a hash of items" do
602
+ product = stripe_helper.create_product(name: 'Silver Product')
603
+ silver = stripe_helper.create_plan(id: 'silver', product: product.id)
604
+ customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk)
605
+
606
+ sub = Stripe::Subscription.create({ items: { '0' => { plan: 'silver' } }, customer: customer.id })
607
+ sub.delete(at_period_end: true)
608
+
609
+ expect(sub.cancel_at_period_end).to be_truthy
610
+ expect(sub.save).to be_truthy
611
+ expect(sub.cancel_at_period_end).to be_truthy
612
+ end
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
475
636
  end
476
637
 
477
638
  context "updating a subscription" do
478
639
  it 'raises invalid request exception when subscription is cancelled' do
479
- stripe_helper.create_plan(id: 'the truth')
480
- customer = Stripe::Customer.create(source: gen_card_tk, plan: 'the truth')
640
+ customer = Stripe::Customer.create(source: gen_card_tk, plan: plan.id)
481
641
 
482
642
  subscription = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
483
643
  subscription.delete
@@ -490,24 +650,22 @@ shared_examples 'Customer Subscriptions' do
490
650
  end
491
651
 
492
652
  it "updates a stripe customer's existing subscription with one plan inside items" do
493
- silver = stripe_helper.create_plan(id: 'silver')
494
653
  customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk)
495
654
 
496
- sub = Stripe::Subscription.create({ items: [ { plan: 'silver' } ], customer: customer.id })
655
+ sub = Stripe::Subscription.create({ items: [ { plan: plan.id } ], customer: customer.id })
497
656
  sub.delete(at_period_end: true)
498
657
 
499
658
  expect(sub.cancel_at_period_end).to be_truthy
500
659
  expect(sub.save).to be_truthy
501
- expect(sub.cancel_at_period_end).to be_falsey
660
+ expect(sub.cancel_at_period_end).to be_truthy
502
661
  end
503
662
 
504
663
  it "updates a stripe customer's existing subscription when plan inside of items" do
505
- silver = stripe_helper.create_plan(id: 'silver')
506
- gold = stripe_helper.create_plan(id: 'gold')
507
- 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)
508
665
 
666
+ gold_plan = stripe_helper.create_plan(id: 'gold', product: product.id)
509
667
  sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
510
- sub.plan = 'gold'
668
+ sub.plan = gold_plan.id
511
669
  sub.quantity = 5
512
670
  sub.metadata.foo = "bar"
513
671
  sub.metadata.example = "yes"
@@ -515,7 +673,7 @@ shared_examples 'Customer Subscriptions' do
515
673
  expect(sub.save).to be_truthy
516
674
 
517
675
  expect(sub.object).to eq('subscription')
518
- expect(sub.plan.to_hash).to eq(gold.to_hash)
676
+ expect(sub.plan.to_hash).to eq(gold_plan.to_hash)
519
677
  expect(sub.quantity).to eq(5)
520
678
  expect(sub.metadata.foo).to eq( "bar" )
521
679
  expect(sub.metadata.example).to eq( "yes" )
@@ -526,16 +684,15 @@ shared_examples 'Customer Subscriptions' do
526
684
  expect(customer.subscriptions.data.length).to eq(1)
527
685
 
528
686
  expect(customer.subscriptions.data.first.id).to eq(sub.id)
529
- 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)
530
688
  expect(customer.subscriptions.data.first.customer).to eq(customer.id)
531
689
  end
532
690
 
533
691
  it "updates a stripe customer's existing subscription with single plan when multiple plans inside of items" do
534
- silver_plan = stripe_helper.create_plan(id: 'silver')
535
- gold_plan = stripe_helper.create_plan(id: 'gold')
536
- addon_plan = stripe_helper.create_plan(id: 'addon_plan')
537
- 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)
538
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)
539
696
  sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
540
697
  sub.items = [{ plan: gold_plan.id, quantity: 2 }, { plan: addon_plan.id, quantity: 2 }]
541
698
  expect(sub.save).to be_truthy
@@ -556,12 +713,12 @@ shared_examples 'Customer Subscriptions' do
556
713
  end
557
714
 
558
715
  it "updates a stripe customer's existing subscription with multple plans when multiple plans inside of items" do
559
- silver_plan = stripe_helper.create_plan(id: 'silver')
560
- gold_plan = stripe_helper.create_plan(id: 'gold')
561
- addon1_plan = stripe_helper.create_plan(id: 'addon1')
562
- addon2_plan = stripe_helper.create_plan(id: 'addon2')
563
716
  customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk)
564
- 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)
565
722
 
566
723
  sub.items = [{ plan: gold_plan.id, quantity: 2 }, { plan: addon2_plan.id, quantity: 2 }]
567
724
  expect(sub.save).to be_truthy
@@ -581,9 +738,87 @@ shared_examples 'Customer Subscriptions' do
581
738
  expect(customer.subscriptions.data.first.items.data[1].plan.to_hash).to eq(addon2_plan.to_hash)
582
739
  end
583
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
+
584
821
  it 'when adds coupon', live: true do
585
- plan = stripe_helper.create_plan(id: 'plan_with_coupon2', product: { name: 'One More Test Plan' },
586
- amount: 777)
587
822
  coupon = stripe_helper.create_coupon
588
823
  customer = Stripe::Customer.create(source: gen_card_tk, plan: plan.id)
589
824
  subscription = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
@@ -592,13 +827,11 @@ shared_examples 'Customer Subscriptions' do
592
827
  subscription.save
593
828
 
594
829
  expect(subscription.discount).not_to be_nil
595
- expect(subscription.discount).to be_an_instance_of(Stripe::StripeObject)
830
+ expect(subscription.discount).to be_a(Stripe::Discount)
596
831
  expect(subscription.discount.coupon.id).to eq(coupon.id)
597
832
  end
598
833
 
599
834
  it 'when add not exist coupon' do
600
- plan = stripe_helper.create_plan(id: 'plan_with_coupon3', product: { name: 'One More Test Plan' },
601
- amount: 777)
602
835
  customer = Stripe::Customer.create(source: gen_card_tk, plan: plan.id)
603
836
  subscription = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
604
837
 
@@ -613,8 +846,6 @@ shared_examples 'Customer Subscriptions' do
613
846
  end
614
847
 
615
848
  it 'when coupon is removed' do
616
- plan = stripe_helper.create_plan(id: 'plan_with_coupon3', product: { name: 'One More Test Plan' },
617
- amount: 777)
618
849
  customer = Stripe::Customer.create(source: gen_card_tk, plan: plan.id)
619
850
  coupon = stripe_helper.create_coupon
620
851
  subscription = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
@@ -628,8 +859,7 @@ shared_examples 'Customer Subscriptions' do
628
859
  end
629
860
 
630
861
  it "throws an error when plan does not exist" do
631
- free = stripe_helper.create_plan(id: 'free', amount: 0)
632
- customer = Stripe::Customer.create(id: 'cardless', plan: 'free')
862
+ customer = Stripe::Customer.create(id: 'cardless', plan: free_plan.id)
633
863
 
634
864
  sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
635
865
  sub.plan = 'gazebo'
@@ -643,75 +873,59 @@ shared_examples 'Customer Subscriptions' do
643
873
  customer = Stripe::Customer.retrieve('cardless')
644
874
  expect(customer.subscriptions.count).to eq(1)
645
875
  expect(customer.subscriptions.data.length).to eq(1)
646
- 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)
647
877
  end
648
878
 
649
879
  it "throws an error when subscription does not exist" do
650
- free = stripe_helper.create_plan(id: 'free', amount: 0)
651
- customer = Stripe::Customer.create(id: 'cardless', plan: 'free')
652
-
653
- 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|
654
882
  expect(e).to be_a Stripe::InvalidRequestError
655
883
  expect(e.http_status).to eq(404)
656
884
  expect(e.message).to_not be_nil
657
885
  }
658
-
659
- customer = Stripe::Customer.retrieve('cardless')
660
- expect(customer.subscriptions.count).to eq(1)
661
- expect(customer.subscriptions.data.length).to eq(1)
662
- expect(customer.subscriptions.data.first.plan.to_hash).to eq(free.to_hash)
663
886
  end
664
887
 
665
888
  [nil, 0].each do |trial_period_days|
666
- 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
667
890
  begin
668
- free = stripe_helper.create_plan(id: 'free', amount: 0)
669
- paid = stripe_helper.create_plan(id: 'enterprise', amount: 499, trial_period_days: trial_period_days)
670
- 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)
671
894
 
672
895
  sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
673
- sub.plan = 'enterprise'
674
-
675
- expect { sub.save }.to raise_error {|e|
676
- expect(e).to be_a Stripe::InvalidRequestError
677
- expect(e.http_status).to eq(400)
678
- expect(e.message).to_not be_nil
679
- }
680
-
681
- customer = Stripe::Customer.retrieve(customer.id)
682
- expect(customer.subscriptions.count).to eq(1)
683
- expect(customer.subscriptions.data.length).to eq(1)
684
- 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")
685
901
  ensure
686
902
  customer.delete if customer
687
- paid.delete if paid
688
- free.delete if free
903
+ paid_plan.delete if paid_plan
904
+ free_plan.delete if free_plan
689
905
  end
690
906
  end
691
907
  end
692
908
 
693
909
  it 'updates a subscription if the customer has a free trial', live: true do
694
- stripe_helper.create_plan(id: 'enterprise', amount: 499)
695
910
  trial_end = Time.now.utc.to_i + 3600
696
- customer = Stripe::Customer.create(plan: 'enterprise',
697
- trial_end: trial_end)
698
- subscription = customer.subscriptions.first
911
+ customer = Stripe::Customer.create(plan: plan.id, trial_end: trial_end)
912
+ subscription = customer.subscriptions.first
699
913
  subscription.quantity = 2
700
914
  subscription.save
701
915
  expect(subscription.quantity).to eq(2)
702
916
  end
703
917
 
704
918
  it "updates a customer with no card to a plan with a free trial" do
705
- free = stripe_helper.create_plan(id: 'free', amount: 0)
706
- trial = stripe_helper.create_plan(id: 'trial', amount: 999, trial_period_days: 14)
707
- 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)
708
922
 
709
923
  sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
710
- sub.plan = 'trial'
924
+ sub.plan = trial_plan.id
711
925
  sub.save
712
926
 
713
927
  expect(sub.object).to eq('subscription')
714
- expect(sub.plan.to_hash).to eq(trial.to_hash)
928
+ expect(sub.plan.to_hash).to eq(trial_plan.to_hash)
715
929
 
716
930
  customer = Stripe::Customer.retrieve('cardless')
717
931
  expect(customer.subscriptions.data).to_not be_empty
@@ -719,21 +933,21 @@ shared_examples 'Customer Subscriptions' do
719
933
  expect(customer.subscriptions.data.length).to eq(1)
720
934
 
721
935
  expect(customer.subscriptions.data.first.id).to eq(sub.id)
722
- 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)
723
937
  expect(customer.subscriptions.data.first.customer).to eq(customer.id)
724
938
  end
725
939
 
726
940
  it "updates a customer with no card to a free plan" do
727
- free = stripe_helper.create_plan(id: 'free', amount: 0)
728
- gratis = stripe_helper.create_plan(id: 'gratis', amount: 0)
729
- 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)
730
943
 
731
944
  sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
732
- sub.plan = 'gratis'
945
+ gratis_plan = stripe_helper.create_plan(id: 'gratis', product: product.id, amount: 0)
946
+ sub.plan = gratis_plan.id
733
947
  sub.save
734
948
 
735
949
  expect(sub.object).to eq('subscription')
736
- expect(sub.plan.to_hash).to eq(gratis.to_hash)
950
+ expect(sub.plan.to_hash).to eq(gratis_plan.to_hash)
737
951
 
738
952
  customer = Stripe::Customer.retrieve('cardless')
739
953
  expect(customer.subscriptions.data).to_not be_empty
@@ -741,17 +955,15 @@ shared_examples 'Customer Subscriptions' do
741
955
  expect(customer.subscriptions.data.length).to eq(1)
742
956
 
743
957
  expect(customer.subscriptions.data.first.id).to eq(sub.id)
744
- 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)
745
959
  expect(customer.subscriptions.data.first.customer).to eq(customer.id)
746
960
  end
747
961
 
748
962
  it "sets a card when updating a customer's subscription" do
749
- free = stripe_helper.create_plan(id: 'free', amount: 0)
750
- paid = stripe_helper.create_plan(id: 'paid', amount: 499)
751
- customer = Stripe::Customer.create(id: 'test_customer_sub', plan: 'free')
963
+ customer = Stripe::Customer.create(id: 'test_customer_sub', plan: free_plan.id)
752
964
 
753
965
  sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
754
- sub.plan = 'paid'
966
+ sub.plan = plan.id
755
967
  sub.source = gen_card_tk
756
968
  sub.save
757
969
 
@@ -764,11 +976,8 @@ shared_examples 'Customer Subscriptions' do
764
976
  end
765
977
 
766
978
  it "overrides trial length when trial end is set" do
767
- plan = stripe_helper.create_plan(id: 'trial', amount: 999, trial_period_days: 14)
768
- customer = Stripe::Customer.create(id: 'test_trial_end', plan: 'trial')
769
-
979
+ customer = Stripe::Customer.create(id: 'test_trial_end', plan: plan_with_trial.id)
770
980
  sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
771
-
772
981
  trial_end = Time.now.utc.to_i + 3600
773
982
  sub.trial_end = trial_end
774
983
  sub.save
@@ -779,8 +988,7 @@ shared_examples 'Customer Subscriptions' do
779
988
  end
780
989
 
781
990
  it "returns without a trial when trial_end is set to 'now'" do
782
- plan = stripe_helper.create_plan(id: 'trial', amount: 999, trial_period_days: 14)
783
- 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')
784
992
 
785
993
  sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
786
994
 
@@ -788,18 +996,15 @@ shared_examples 'Customer Subscriptions' do
788
996
  sub.save
789
997
 
790
998
  expect(sub.object).to eq('subscription')
791
- expect(sub.plan.to_hash).to eq(plan.to_hash)
999
+ expect(sub.plan.to_hash).to eq(plan_with_trial.to_hash)
792
1000
  expect(sub.status).to eq('active')
793
1001
  expect(sub.trial_start).to be_nil
794
1002
  expect(sub.trial_end).to be_nil
795
1003
  end
796
1004
 
797
1005
  it "changes an active subscription to a trial when trial_end is set" do
798
- plan = stripe_helper.create_plan(id: 'no_trial', amount: 999)
799
- customer = Stripe::Customer.create(id: 'test_trial_end', plan: 'no_trial', source: gen_card_tk)
800
-
1006
+ customer = Stripe::Customer.create(id: 'test_trial_end', plan: plan.id, source: gen_card_tk)
801
1007
  sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
802
-
803
1008
  trial_end = Time.now.utc.to_i + 3600
804
1009
  sub.trial_end = trial_end
805
1010
  sub.save
@@ -813,8 +1018,8 @@ shared_examples 'Customer Subscriptions' do
813
1018
 
814
1019
 
815
1020
  it "raises error when trial_end is not an integer or 'now'" do
816
- plan = stripe_helper.create_plan(id: 'no_trial', amount: 999)
817
- 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)
818
1023
 
819
1024
  sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
820
1025
  sub.trial_end = "gazebo"
@@ -828,10 +1033,10 @@ shared_examples 'Customer Subscriptions' do
828
1033
  end
829
1034
 
830
1035
  context "cancelling a subscription" do
1036
+ let(:customer) { Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk, plan: plan.id) }
831
1037
 
832
- it "cancels a stripe customer's subscription", :live => true do
833
- truth = stripe_helper.create_plan(id: 'the truth')
834
- 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)
835
1040
 
836
1041
  sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
837
1042
  result = sub.delete
@@ -846,53 +1051,58 @@ shared_examples 'Customer Subscriptions' do
846
1051
  expect(customer.subscriptions.count).to eq(0)
847
1052
  expect(customer.subscriptions.data.length).to eq(0)
848
1053
  end
1054
+ end
849
1055
 
850
- it "cancels a stripe customer's subscription at period end" do
851
- truth = stripe_helper.create_plan(id: 'the_truth')
852
- customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk, plan: "the_truth")
853
-
854
- sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
855
- result = sub.delete(at_period_end: true)
1056
+ it "supports 'cancelling' by updating cancel_at_period_end" do
1057
+ product = stripe_helper.create_product(name: 'Truth Product')
1058
+ truth = stripe_helper.create_plan(id: 'the_truth', product: product.id)
1059
+ customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk, plan: "the_truth")
856
1060
 
857
- expect(result.status).to eq('active')
858
- expect(result.cancel_at_period_end).to eq(true)
859
- expect(result.id).to eq(sub.id)
1061
+ sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
1062
+ result = Stripe::Subscription.update(sub.id, cancel_at_period_end: true)
860
1063
 
861
- customer = Stripe::Customer.retrieve('test_customer_sub')
862
- expect(customer.subscriptions.data).to_not be_empty
863
- expect(customer.subscriptions.count).to eq(1)
864
- expect(customer.subscriptions.data.length).to eq(1)
1064
+ expect(result.status).to eq('active')
1065
+ expect(result.cancel_at_period_end).to eq(true)
1066
+ expect(result.id).to eq(sub.id)
865
1067
 
866
- expect(customer.subscriptions.data.first.status).to eq('active')
867
- expect(customer.subscriptions.data.first.cancel_at_period_end).to eq(true)
868
- expect(customer.subscriptions.data.first.ended_at).to be_nil
869
- expect(customer.subscriptions.data.first.canceled_at).to_not be_nil
870
- end
1068
+ customer = Stripe::Customer.retrieve('test_customer_sub')
1069
+ expect(customer.subscriptions.data).to_not be_empty
1070
+ expect(customer.subscriptions.count).to eq(1)
1071
+ expect(customer.subscriptions.data.length).to eq(1)
1072
+
1073
+ expect(customer.subscriptions.data.first.status).to eq('active')
1074
+ expect(customer.subscriptions.data.first.cancel_at_period_end).to eq(true)
1075
+ expect(customer.subscriptions.data.first.ended_at).to be_nil
1076
+ expect(customer.subscriptions.data.first.canceled_at).to_not be_nil
1077
+ end
871
1078
 
872
- it "resumes an at period end cancelled subscription" do
873
- truth = stripe_helper.create_plan(id: 'the_truth')
874
- customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk, plan: "the_truth")
1079
+ it "resumes a subscription cancelled by updating cancel_at_period_end" do
1080
+ product = stripe_helper.create_product(name: 'Truth Product')
1081
+ truth = stripe_helper.create_plan(id: 'the_truth', product: product.id)
1082
+ customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk, plan: "the_truth")
875
1083
 
876
- sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
877
- result = sub.delete(at_period_end: true)
1084
+ sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
1085
+ Stripe::Subscription.update(sub.id, cancel_at_period_end: true)
878
1086
 
879
- sub.plan = 'the_truth'
880
- sub.save
1087
+ result = Stripe::Subscription.update(sub.id, cancel_at_period_end: false)
881
1088
 
882
- customer = Stripe::Customer.retrieve('test_customer_sub')
883
- expect(customer.subscriptions.data).to_not be_empty
884
- expect(customer.subscriptions.count).to eq(1)
885
- expect(customer.subscriptions.data.length).to eq(1)
1089
+ expect(result.status).to eq('active')
1090
+ expect(result.cancel_at_period_end).to eq(false)
1091
+ expect(result.id).to eq(sub.id)
886
1092
 
887
- expect(customer.subscriptions.data.first.status).to eq('active')
888
- expect(customer.subscriptions.data.first.cancel_at_period_end).to eq(false)
889
- expect(customer.subscriptions.data.first.ended_at).to be_nil
890
- expect(customer.subscriptions.data.first.canceled_at).to be_nil
891
- end
1093
+ customer = Stripe::Customer.retrieve('test_customer_sub')
1094
+ expect(customer.subscriptions.data).to_not be_empty
1095
+ expect(customer.subscriptions.count).to eq(1)
1096
+ expect(customer.subscriptions.data.length).to eq(1)
1097
+
1098
+ expect(customer.subscriptions.data.first.status).to eq('active')
1099
+ expect(customer.subscriptions.data.first.cancel_at_period_end).to eq(false)
1100
+ expect(customer.subscriptions.data.first.ended_at).to be_nil
1101
+ expect(customer.subscriptions.data.first.canceled_at).to be_nil
892
1102
  end
893
1103
 
894
1104
  it "doesn't change status of subscription when cancelling at period end" do
895
- 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)
896
1106
  customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk, plan: "trial")
897
1107
 
898
1108
  sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
@@ -905,11 +1115,11 @@ shared_examples 'Customer Subscriptions' do
905
1115
  expect(customer.subscriptions.data.first.status).to eq('trialing')
906
1116
  end
907
1117
 
908
- 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
909
1119
  plan = stripe_helper.create_plan(
910
1120
  :amount => 2000,
1121
+ :product => product.id,
911
1122
  :interval => 'month',
912
- :name => 'Amazing Gold Plan',
913
1123
  :currency => 'usd',
914
1124
  :id => 'gold'
915
1125
  )
@@ -924,7 +1134,7 @@ shared_examples 'Customer Subscriptions' do
924
1134
  let(:subscription) { Stripe::Subscription.retrieve(customer.subscriptions.data.first.id) }
925
1135
 
926
1136
  before do
927
- stripe_helper.create_plan(id: 'free', amount: 0)
1137
+ free_plan
928
1138
  Stripe::Subscription.create({ plan: 'free', customer: customer.id })
929
1139
  end
930
1140
 
@@ -937,7 +1147,7 @@ shared_examples 'Customer Subscriptions' do
937
1147
  expect(subscription.items.object).to eq('list')
938
1148
  expect(subscription.items.data.class).to eq(Array)
939
1149
  expect(subscription.items.data.count).to eq(1)
940
- expect(subscription.items.data.first.id).to eq('test_txn_default')
1150
+ expect(subscription.items.data.first.id).to include('test_si_')
941
1151
  expect(subscription.items.data.first.created).to eq(1504716183)
942
1152
  expect(subscription.items.data.first.object).to eq('subscription_item')
943
1153
  expect(subscription.items.data.first.plan.amount).to eq(0)
@@ -950,12 +1160,12 @@ shared_examples 'Customer Subscriptions' do
950
1160
  context "retrieve multiple subscriptions" do
951
1161
 
952
1162
  it "retrieves a list of multiple subscriptions" do
953
- free = stripe_helper.create_plan(id: 'free', amount: 0)
954
- paid = stripe_helper.create_plan(id: 'paid', amount: 499)
955
- 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)
956
1166
  Stripe::Subscription.create({ plan: 'paid', customer: customer.id })
957
1167
 
958
- subs = Stripe::Subscription.all({ customer: customer.id })
1168
+ subs = Stripe::Subscription.list({ customer: customer.id })
959
1169
 
960
1170
  expect(subs.object).to eq("list")
961
1171
  expect(subs.count).to eq(2)
@@ -966,7 +1176,7 @@ shared_examples 'Customer Subscriptions' do
966
1176
  Stripe::Customer.create(id: 'no_subs')
967
1177
  customer = Stripe::Customer.retrieve('no_subs')
968
1178
 
969
- list = Stripe::Subscription.all({ customer: customer.id })
1179
+ list = Stripe::Subscription.list({ customer: customer.id })
970
1180
 
971
1181
  expect(list.object).to eq("list")
972
1182
  expect(list.count).to eq(0)
@@ -976,15 +1186,13 @@ shared_examples 'Customer Subscriptions' do
976
1186
 
977
1187
  describe "metadata" do
978
1188
 
979
- 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
980
1190
 
981
1191
  stripe_helper.
982
1192
  create_plan(
983
1193
  :amount => 500,
984
1194
  :interval => 'month',
985
- :product => {
986
- :name => 'Sample Plan'
987
- },
1195
+ :product => product.id,
988
1196
  :currency => 'usd',
989
1197
  :id => 'Sample5'
990
1198
  )
@@ -1007,3 +1215,46 @@ shared_examples 'Customer Subscriptions' do
1007
1215
  end
1008
1216
 
1009
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