stripe-ruby-mock 2.5.4 → 2.5.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +1 -1
  3. data/README.md +1 -1
  4. data/lib/stripe_mock.rb +4 -0
  5. data/lib/stripe_mock/api/webhooks.rb +2 -0
  6. data/lib/stripe_mock/data.rb +78 -17
  7. data/lib/stripe_mock/data/list.rb +7 -2
  8. data/lib/stripe_mock/instance.rb +37 -3
  9. data/lib/stripe_mock/request_handlers/accounts.rb +16 -0
  10. data/lib/stripe_mock/request_handlers/charges.rb +7 -8
  11. data/lib/stripe_mock/request_handlers/customers.rb +2 -2
  12. data/lib/stripe_mock/request_handlers/ephemeral_key.rb +13 -0
  13. data/lib/stripe_mock/request_handlers/helpers/card_helpers.rb +1 -0
  14. data/lib/stripe_mock/request_handlers/helpers/coupon_helpers.rb +10 -11
  15. data/lib/stripe_mock/request_handlers/helpers/subscription_helpers.rb +20 -2
  16. data/lib/stripe_mock/request_handlers/invoices.rb +1 -1
  17. data/lib/stripe_mock/request_handlers/products.rb +43 -0
  18. data/lib/stripe_mock/request_handlers/refunds.rb +6 -3
  19. data/lib/stripe_mock/request_handlers/subscription_items.rb +36 -0
  20. data/lib/stripe_mock/request_handlers/subscriptions.rb +40 -22
  21. data/lib/stripe_mock/request_handlers/tax_rates.rb +36 -0
  22. data/lib/stripe_mock/request_handlers/tokens.rb +2 -2
  23. data/lib/stripe_mock/request_handlers/transfers.rb +10 -4
  24. data/lib/stripe_mock/request_handlers/validators/param_validators.rb +3 -0
  25. data/lib/stripe_mock/test_strategies/base.rb +4 -2
  26. data/lib/stripe_mock/version.rb +1 -1
  27. data/lib/stripe_mock/webhook_fixtures/charge.dispute.funds_reinstated.json +88 -0
  28. data/lib/stripe_mock/webhook_fixtures/charge.dispute.funds_withdrawn.json +88 -0
  29. data/lib/stripe_mock/webhook_fixtures/customer.subscription.created.json +2 -2
  30. data/lib/stripe_mock/webhook_fixtures/customer.subscription.deleted.json +2 -2
  31. data/lib/stripe_mock/webhook_fixtures/customer.subscription.trial_will_end.json +2 -2
  32. data/lib/stripe_mock/webhook_fixtures/customer.subscription.updated.json +3 -3
  33. data/lib/stripe_mock/webhook_fixtures/invoice.created.json +3 -2
  34. data/lib/stripe_mock/webhook_fixtures/invoice.payment_failed.json +1 -1
  35. data/lib/stripe_mock/webhook_fixtures/invoice.payment_succeeded.json +1 -1
  36. data/lib/stripe_mock/webhook_fixtures/invoice.updated.json +3 -2
  37. data/lib/stripe_mock/webhook_fixtures/plan.created.json +1 -1
  38. data/lib/stripe_mock/webhook_fixtures/plan.deleted.json +1 -1
  39. data/lib/stripe_mock/webhook_fixtures/plan.updated.json +1 -1
  40. data/spec/instance_spec.rb +31 -0
  41. data/spec/shared_stripe_examples/account_examples.rb +27 -0
  42. data/spec/shared_stripe_examples/charge_examples.rb +23 -14
  43. data/spec/shared_stripe_examples/customer_examples.rb +11 -1
  44. data/spec/shared_stripe_examples/ephemeral_key_examples.rb +17 -0
  45. data/spec/shared_stripe_examples/invoice_examples.rb +5 -5
  46. data/spec/shared_stripe_examples/plan_examples.rb +19 -4
  47. data/spec/shared_stripe_examples/product_example.rb +65 -0
  48. data/spec/shared_stripe_examples/refund_examples.rb +16 -10
  49. data/spec/shared_stripe_examples/subscription_examples.rb +176 -18
  50. data/spec/shared_stripe_examples/subscription_items_examples.rb +75 -0
  51. data/spec/shared_stripe_examples/tax_rate_examples.rb +42 -0
  52. data/spec/shared_stripe_examples/transfer_examples.rb +61 -30
  53. data/spec/support/stripe_examples.rb +4 -1
  54. metadata +16 -2
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples 'EphemeralKey API' do
4
+ describe 'Create a new key' do
5
+ let(:customer) { Stripe::Customer.create email: 'test@example.com' }
6
+ let(:version) { '2016-07-06' }
7
+
8
+ it 'creates a new key' do
9
+ key = Stripe::EphemeralKey.create(
10
+ { customer: customer.id },
11
+ { stripe_version: version }
12
+ )
13
+
14
+ expect(key[:associated_objects][0][:id]).to eq customer.id
15
+ end
16
+ end
17
+ end
@@ -52,7 +52,7 @@ shared_examples 'Invoice API' do
52
52
  end
53
53
 
54
54
  it "stores all invoices in memory" do
55
- expect(Stripe::Invoice.all.map(&:id)).to eq([@invoice.id, @invoice2.id])
55
+ expect(Stripe::Invoice.all.map(&:id).sort).to eq([@invoice.id, @invoice2.id].sort)
56
56
  end
57
57
 
58
58
  it "defaults count to 10 invoices" do
@@ -79,7 +79,7 @@ shared_examples 'Invoice API' do
79
79
  end
80
80
 
81
81
  it 'updates attempted and paid flags' do
82
- @invoice.pay
82
+ @invoice = @invoice.pay
83
83
  expect(@invoice.attempted).to eq(true)
84
84
  expect(@invoice.paid).to eq(true)
85
85
  end
@@ -89,7 +89,7 @@ shared_examples 'Invoice API' do
89
89
  end
90
90
 
91
91
  it 'sets the charge attribute' do
92
- @invoice.pay
92
+ @invoice = @invoice.pay
93
93
  expect(@invoice.charge).to be_a String
94
94
  expect(@invoice.charge.length).to be > 0
95
95
  end
@@ -351,7 +351,7 @@ shared_examples 'Invoice API' do
351
351
  it 'generates a preview without performing an actual proration', live: true do
352
352
  expect(preview.subtotal).to eq 150_00
353
353
  # this is a future invoice (generted at the end of the current subscription cycle), rather than a proration invoice
354
- expect(preview.date).to be_within(1).of subscription.current_period_end
354
+ expect(preview.created).to be_within(1).of subscription.current_period_end
355
355
  expect(preview.period_start).to eq subscription.current_period_start
356
356
  expect(preview.period_end).to eq subscription.current_period_end
357
357
  expect(preview.lines.count).to eq 1
@@ -360,7 +360,7 @@ shared_examples 'Invoice API' do
360
360
  expect(line.amount).to eq 150_00
361
361
  # line period is for the NEXT subscription cycle
362
362
  expect(line.period.start).to be_within(1).of subscription.current_period_end
363
- expect(line.period.end).to be_within(1).of (Time.at(subscription.current_period_end).to_datetime >> 1).to_time.to_i # +1 month
363
+ expect(Time.at(line.period.end).month).to be_within(1).of (Time.at(subscription.current_period_end).to_datetime >> 1).month # +1 month
364
364
  end
365
365
  end
366
366
 
@@ -9,6 +9,9 @@ shared_examples 'Plan API' do
9
9
  :amount => 9900,
10
10
  :currency => 'USD',
11
11
  :interval => 1,
12
+ :product => {
13
+ :name => 'A product'
14
+ },
12
15
  :metadata => {
13
16
  :description => "desc text",
14
17
  :info => "info text"
@@ -36,6 +39,9 @@ shared_examples 'Plan API' do
36
39
  :amount => 9900,
37
40
  :currency => 'USD',
38
41
  :interval => 1,
42
+ :product => {
43
+ :name => 'A product'
44
+ }
39
45
  )
40
46
 
41
47
  expect(plan.id).to match(/^test_plan/)
@@ -47,14 +53,20 @@ shared_examples 'Plan API' do
47
53
  :name => 'The Memory Plan',
48
54
  :amount => 1100,
49
55
  :currency => 'USD',
50
- :interval => 1
56
+ :interval => 1,
57
+ :product => {
58
+ :name => 'A product'
59
+ }
51
60
  )
52
61
  plan2 = Stripe::Plan.create(
53
62
  :id => 'pid_3',
54
63
  :name => 'The Bonk Plan',
55
64
  :amount => 7777,
56
65
  :currency => 'USD',
57
- :interval => 1
66
+ :interval => 1,
67
+ :product => {
68
+ :name => 'A product'
69
+ }
58
70
  )
59
71
  data = test_data_source(:plans)
60
72
  expect(data[plan.id]).to_not be_nil
@@ -136,7 +148,10 @@ shared_examples 'Plan API' do
136
148
  :name => 'The Mock Plan',
137
149
  :amount => 99.99,
138
150
  :currency => 'USD',
139
- :interval => 'month'
151
+ :interval => 'month',
152
+ :product => {
153
+ :name => 'A product'
154
+ }
140
155
  )
141
156
  }.to raise_error(Stripe::InvalidRequestError, "Invalid integer: 99.99")
142
157
  end
@@ -157,7 +172,7 @@ shared_examples 'Plan API' do
157
172
  expect { subject }.to raise_error(Stripe::InvalidRequestError, message)
158
173
  end
159
174
 
160
- it("requires a name") { @name = :name }
175
+ it("requires a product") { @name = :product }
161
176
  it("requires an amount") { @name = :amount }
162
177
  it("requires a currency") { @name = :currency }
163
178
  it("requires an interval") { @name = :interval }
@@ -0,0 +1,65 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples 'Product API' do
4
+ it 'creates a product' do
5
+ product = Stripe::Product.create(
6
+ name: 'my awesome product',
7
+ type: 'service'
8
+ )
9
+
10
+ expect(product.name).to eq 'my awesome product'
11
+ expect(product.type).to eq 'service'
12
+ end
13
+
14
+ it 'retrieves a product' do
15
+ Stripe::Product.create(
16
+ id: 'test_prod_1',
17
+ name: 'my awesome product',
18
+ type: 'service'
19
+ )
20
+
21
+ product = Stripe::Product.retrieve('test_prod_1')
22
+
23
+ expect(product.name).to eq 'my awesome product'
24
+ expect(product.type).to eq 'service'
25
+ end
26
+
27
+ it 'updates a product' do
28
+ Stripe::Product.create(
29
+ id: 'test_prod_1',
30
+ name: 'my awesome product',
31
+ type: 'service'
32
+ )
33
+
34
+ Stripe::Product.update('test_prod_1', name: 'my lame product')
35
+
36
+ product = Stripe::Product.retrieve('test_prod_1')
37
+
38
+ expect(product.name).to eq 'my lame product'
39
+ end
40
+
41
+ it 'lists all products' do
42
+ 2.times do |n|
43
+ Stripe::Product.create(
44
+ name: "product #{n}",
45
+ type: 'service'
46
+ )
47
+ end
48
+
49
+ products = Stripe::Product.list
50
+
51
+ expect(products.map(&:name)).to match_array ['product 0', 'product 1']
52
+ end
53
+
54
+ it 'destroys a product', live: true do
55
+ Stripe::Product.create(
56
+ id: 'test_prod_1',
57
+ name: 'my awesome product',
58
+ type: 'service'
59
+ )
60
+
61
+ Stripe::Product.delete('test_prod_1')
62
+
63
+ expect { Stripe::Product.retrieve('test_prod_1') }. to raise_error(Stripe::InvalidRequestError)
64
+ end
65
+ end
@@ -337,26 +337,32 @@ shared_examples 'Refund API' do
337
337
  capture: true
338
338
  )
339
339
  end
340
- let(:idempotent_refund_params) {{
341
- charge: charge.id,
340
+ let(:refund_params) {{
341
+ charge: charge.id
342
+ }}
343
+
344
+ let(:refund_headers) {{
342
345
  idempotency_key: 'onceisenough'
343
346
  }}
344
347
 
345
348
  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)
349
+ refund1 = Stripe::Refund.create(refund_params, refund_headers)
350
+ refund2 = Stripe::Refund.create(refund_params, refund_headers)
348
351
 
349
352
  expect(refund1).to eq(refund2)
350
353
  end
351
354
 
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'
355
+ context 'different key' do
356
+ let(:different_refund_headers) {{
357
+ idempotency_key: 'thisoneisdifferent'
358
+ }}
355
359
 
356
- refund1 = Stripe::Refund.create(idempotent_refund_params)
357
- refund2 = Stripe::Refund.create(idempotent_refund_params2)
360
+ it "returns different charges if different idempotency_keys are used for each charge" do
361
+ refund1 = Stripe::Refund.create(refund_params, refund_headers)
362
+ refund2 = Stripe::Refund.create(refund_params, different_refund_headers)
358
363
 
359
- expect(refund1).not_to eq(refund2)
364
+ expect(refund1).not_to eq(refund2)
365
+ end
360
366
  end
361
367
  end
362
368
  end
@@ -8,7 +8,8 @@ shared_examples 'Customer Subscriptions' do
8
8
 
9
9
  context "creating a new subscription" do
10
10
  it "adds a new subscription to customer with none using items", :live => true do
11
- plan = stripe_helper.create_plan(id: 'silver', name: 'Silver Plan', amount: 4999, currency: 'usd')
11
+ plan = stripe_helper.create_plan(id: 'silver', product: { name: 'Silver Plan' },
12
+ amount: 4999, currency: 'usd')
12
13
  customer = Stripe::Customer.create(source: gen_card_tk)
13
14
 
14
15
  expect(customer.subscriptions.data).to be_empty
@@ -37,7 +38,8 @@ shared_examples 'Customer Subscriptions' do
37
38
  end
38
39
 
39
40
  it "adds a new subscription to customer with none", :live => true do
40
- plan = stripe_helper.create_plan(id: 'silver', name: 'Silver Plan', amount: 4999, currency: 'usd')
41
+ plan = stripe_helper.create_plan(id: 'silver', product: { name: 'Silver Plan' },
42
+ amount: 4999, currency: 'usd')
41
43
  customer = Stripe::Customer.create(source: gen_card_tk)
42
44
 
43
45
  expect(customer.subscriptions.data).to be_empty
@@ -66,7 +68,7 @@ shared_examples 'Customer Subscriptions' do
66
68
  end
67
69
 
68
70
  it 'when customer object provided' do
69
- plan = stripe_helper.create_plan(id: 'silver', name: 'Silver Plan', amount: 4999, currency: 'usd')
71
+ plan = stripe_helper.create_plan(id: 'silver', product: { name: 'Silver Plan' }, amount: 4999, currency: 'usd')
70
72
  customer = Stripe::Customer.create(source: gen_card_tk)
71
73
 
72
74
  expect(customer.subscriptions.data).to be_empty
@@ -100,13 +102,15 @@ shared_examples 'Customer Subscriptions' do
100
102
  customer = Stripe::Customer.create(source: gen_card_tk)
101
103
  expect(customer.subscriptions.count).to eq(0)
102
104
 
103
- plan = stripe_helper.create_plan(id: :silver, name: 'Silver Plan', amount: 4999, currency: 'usd')
105
+ plan = stripe_helper.create_plan(id: :silver, product: { name: 'Silver Plan' },
106
+ amount: 4999, currency: 'usd')
104
107
  sub = Stripe::Subscription.create({ plan: 'silver', customer: customer.id })
105
108
  customer = Stripe::Customer.retrieve(customer.id)
106
109
  expect(sub.plan.to_hash).to eq(plan.to_hash)
107
110
  expect(customer.subscriptions.count).to eq(1)
108
111
 
109
- plan = stripe_helper.create_plan(id: 'gold', name: 'Gold Plan', amount: 14999, currency: 'usd')
112
+ plan = stripe_helper.create_plan(id: 'gold', product: { name: 'Gold Plan' },
113
+ amount: 14999, currency: 'usd')
110
114
  sub = Stripe::Subscription.create({ plan: 'gold', customer: customer.id })
111
115
  customer = Stripe::Customer.retrieve(customer.id)
112
116
  expect(sub.plan.to_hash).to eq(plan.to_hash)
@@ -114,7 +118,8 @@ shared_examples 'Customer Subscriptions' do
114
118
  end
115
119
 
116
120
  it 'creates a charge for the customer', live: true do
117
- stripe_helper.create_plan(id: 'silver', name: 'Silver Plan', amount: 4999)
121
+ stripe_helper.create_plan(id: 'silver', product: { name: 'Silver Plan' },
122
+ amount: 4999)
118
123
 
119
124
  customer = Stripe::Customer.create(source: gen_card_tk)
120
125
  Stripe::Subscription.create({ plan: 'silver', customer: customer.id, metadata: { foo: "bar", example: "yes" } })
@@ -125,7 +130,8 @@ shared_examples 'Customer Subscriptions' do
125
130
  end
126
131
 
127
132
  it 'contains coupon object', live: true do
128
- plan = stripe_helper.create_plan(id: 'plan_with_coupon', name: 'One More Test Plan', amount: 777)
133
+ plan = stripe_helper.create_plan(id: 'plan_with_coupon', product: { name: 'One More Test Plan' },
134
+ amount: 777)
129
135
  coupon = stripe_helper.create_coupon(id: 'free_coupon', duration: 'repeating', duration_in_months: 3)
130
136
  customer = Stripe::Customer.create(source: gen_card_tk)
131
137
  Stripe::Subscription.create(plan: plan.id, customer: customer.id, coupon: coupon.id)
@@ -134,12 +140,13 @@ shared_examples 'Customer Subscriptions' do
134
140
  expect(customer.subscriptions.data).to be_a(Array)
135
141
  expect(customer.subscriptions.data.count).to eq(1)
136
142
  expect(customer.subscriptions.data.first.discount).not_to be_nil
137
- expect(customer.subscriptions.data.first.discount).to be_a(Stripe::StripeObject)
143
+ expect(customer.subscriptions.data.first.discount).to be_a(Stripe::Discount)
138
144
  expect(customer.subscriptions.data.first.discount.coupon.id).to eq(coupon.id)
139
145
  end
140
146
 
141
147
  it 'when coupon is not exist', live: true do
142
- plan = stripe_helper.create_plan(id: 'plan_with_coupon', name: 'One More Test Plan', amount: 777)
148
+ plan = stripe_helper.create_plan(id: 'plan_with_coupon', product: { name: 'One More Test Plan' },
149
+ amount: 777)
143
150
  customer = Stripe::Customer.create(source: gen_card_tk)
144
151
 
145
152
  expect { Stripe::Subscription.create(plan: plan.id, customer: customer.id, coupon: 'none') }.to raise_error {|e|
@@ -153,7 +160,9 @@ shared_examples 'Customer Subscriptions' do
153
160
  Stripe::Plan.create(
154
161
  :amount => 2500,
155
162
  :interval => 'month',
156
- :name => 'Test plan',
163
+ :product => {
164
+ :name => 'Test plan'
165
+ },
157
166
  :currency => 'usd',
158
167
  :id => 'silver',
159
168
  :statement_description => "testPlan"
@@ -169,7 +178,8 @@ shared_examples 'Customer Subscriptions' do
169
178
 
170
179
  it "correctly sets created when it's not provided as a parameter", live: true do
171
180
  customer = Stripe::Customer.create(source: gen_card_tk)
172
- plan = stripe_helper.create_plan(id: 'silver', name: 'Silver Plan', amount: 4999, currency: 'usd')
181
+ plan = stripe_helper.create_plan(id: 'silver', product: { name: 'Silver Plan' },
182
+ amount: 4999, currency: 'usd')
173
183
  subscription = Stripe::Subscription.create({ plan: 'silver', customer: customer.id })
174
184
 
175
185
  expect(subscription.created).to eq(subscription.current_period_start)
@@ -177,7 +187,8 @@ shared_examples 'Customer Subscriptions' do
177
187
 
178
188
  it "correctly sets created when it's provided as a parameter" do
179
189
  customer = Stripe::Customer.create(source: gen_card_tk)
180
- plan = stripe_helper.create_plan(id: 'silver', name: 'Silver Plan', amount: 4999, currency: 'usd')
190
+ plan = stripe_helper.create_plan(id: 'silver', product: { name: 'Silver Plan' },
191
+ amount: 4999, currency: 'usd')
181
192
  subscription = Stripe::Subscription.create({ plan: 'silver', customer: customer.id, created: 1473576318 })
182
193
 
183
194
  expect(subscription.created).to eq(1473576318)
@@ -351,6 +362,30 @@ shared_examples 'Customer Subscriptions' do
351
362
  expect(sub.trial_end).to eq(trial_end)
352
363
  end
353
364
 
365
+ it "does not override trial end when trial end is not set" do
366
+ plan = stripe_helper.create_plan(id: 'trial', amount: 999, trial_period_days: 14)
367
+ customer = Stripe::Customer.create(id: 'short_trial')
368
+ trial_end = Time.now.utc.to_i + 3600
369
+ metadata = {description: 'original description'}
370
+
371
+ sub = Stripe::Subscription.create({ plan: 'trial', customer: customer.id, trial_end: trial_end, metadata: metadata })
372
+
373
+ expect(sub.object).to eq('subscription')
374
+ expect(sub.plan.to_hash).to eq(plan.to_hash)
375
+ expect(sub.current_period_end).to eq(trial_end)
376
+ expect(sub.trial_end).to eq(trial_end)
377
+ expect(sub.metadata.description).to eq(metadata[:description])
378
+
379
+ metadata = {description: 'updated description'}
380
+ sub = Stripe::Subscription.update(sub.id, { metadata: metadata })
381
+
382
+ expect(sub.object).to eq('subscription')
383
+ expect(sub.plan.to_hash).to eq(plan.to_hash)
384
+ expect(sub.current_period_end).to eq(trial_end)
385
+ expect(sub.trial_end).to eq(trial_end) # check that the trial_end has NOT changed
386
+ expect(sub.metadata.description).to eq(metadata[:description]) # check that the description has changed
387
+ end
388
+
354
389
  it "returns without a trial when trial_end is set to 'now'" do
355
390
  plan = stripe_helper.create_plan(id: 'trial', amount: 999, trial_period_days: 14)
356
391
  customer = Stripe::Customer.create(id: 'no_trial', source: gen_card_tk)
@@ -436,6 +471,8 @@ shared_examples 'Customer Subscriptions' do
436
471
  expect(subscription.plan).to eq nil
437
472
  expect(subscription.items.data[0].plan.id).to eq plan.id
438
473
  expect(subscription.items.data[1].plan.id).to eq plan2.id
474
+ expect(subscription.items.data[0].quantity).to eq 1
475
+ expect(subscription.items.data[1].quantity).to eq 2
439
476
  end
440
477
 
441
478
  it 'when plan defined inside items for trials with no card', live: true do
@@ -459,6 +496,75 @@ shared_examples 'Customer Subscriptions' do
459
496
  expect(subscription.items.data[0].plan.id).to eq plan.id
460
497
  expect(subscription.items.data[1].plan.id).to eq plan2.id
461
498
  end
499
+
500
+ it 'add a new subscription to bill via an invoice' do
501
+ plan = stripe_helper.create_plan(id: 'silver', product: { name: 'Silver Plan' },
502
+ amount: 4999, currency: 'usd')
503
+ customer = Stripe::Customer.create(id: 'cardless')
504
+
505
+ expect(customer.subscriptions.data).to be_empty
506
+ expect(customer.subscriptions.count).to eq(0)
507
+
508
+ sub = Stripe::Subscription.create({
509
+ plan: 'silver',
510
+ customer: customer.id,
511
+ metadata: { foo: 'bar', example: 'yes' },
512
+ billing: 'send_invoice',
513
+ days_until_due: 30,
514
+ })
515
+
516
+ expect(sub.object).to eq('subscription')
517
+ expect(sub.plan.to_hash).to eq(plan.to_hash)
518
+ expect(sub.billing).to eq 'send_invoice'
519
+ expect(sub.days_until_due).to eq 30
520
+ end
521
+
522
+ let(:subscription_header) {{
523
+ :idempotency_key => 'a_idempotency_key'
524
+ }}
525
+
526
+ it "adds a new subscription to customer with identical idempotency key" do
527
+ plan = stripe_helper.create_plan(id: 'silver', product: { name: 'Silver Plan' },
528
+ amount: 4999, currency: 'usd')
529
+ customer = Stripe::Customer.create(source: gen_card_tk)
530
+
531
+ expect(customer.subscriptions.data).to be_empty
532
+ expect(customer.subscriptions.count).to eq(0)
533
+
534
+ sub1 = Stripe::Subscription.create({ items: [{ plan: 'silver' }], customer: customer.id }, subscription_header)
535
+ sub2 = Stripe::Subscription.create({ items: [{ plan: 'silver' }], customer: customer.id }, subscription_header)
536
+ expect(sub1).to eq(sub2)
537
+ end
538
+
539
+ it "adds a new subscription to customer with different idempotency key", :live => true do
540
+ plan = stripe_helper.create_plan(id: 'silver', product: { name: 'Silver Plan' },
541
+ amount: 4999, currency: 'usd')
542
+ customer = Stripe::Customer.create(source: gen_card_tk)
543
+
544
+ expect(customer.subscriptions.data).to be_empty
545
+ expect(customer.subscriptions.count).to eq(0)
546
+
547
+ another_subscription_header = {
548
+ :idempotency_key => 'another_idempotency_key'
549
+ }
550
+
551
+ sub1 = Stripe::Subscription.create({ items: [{ plan: 'silver' }], customer: customer.id }, subscription_header)
552
+ sub2 = Stripe::Subscription.create({ items: [{ plan: 'silver' }], customer: customer.id }, another_subscription_header)
553
+ expect(sub1).not_to eq(sub2)
554
+ end
555
+
556
+ it "accepts a hash of items" do
557
+ silver = stripe_helper.create_plan(id: 'silver')
558
+ customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk)
559
+
560
+ sub = Stripe::Subscription.create({ items: { '0' => { plan: 'silver' } }, customer: customer.id })
561
+ sub.delete(at_period_end: true)
562
+
563
+ expect(sub.cancel_at_period_end).to be_truthy
564
+ expect(sub.save).to be_truthy
565
+ expect(sub.cancel_at_period_end).to be_falsey
566
+ end
567
+
462
568
  end
463
569
 
464
570
  context "updating a subscription" do
@@ -569,7 +675,8 @@ shared_examples 'Customer Subscriptions' do
569
675
  end
570
676
 
571
677
  it 'when adds coupon', live: true do
572
- plan = stripe_helper.create_plan(id: 'plan_with_coupon2', name: 'One More Test Plan', amount: 777)
678
+ plan = stripe_helper.create_plan(id: 'plan_with_coupon2', product: { name: 'One More Test Plan' },
679
+ amount: 777)
573
680
  coupon = stripe_helper.create_coupon
574
681
  customer = Stripe::Customer.create(source: gen_card_tk, plan: plan.id)
575
682
  subscription = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
@@ -578,12 +685,13 @@ shared_examples 'Customer Subscriptions' do
578
685
  subscription.save
579
686
 
580
687
  expect(subscription.discount).not_to be_nil
581
- expect(subscription.discount).to be_an_instance_of(Stripe::StripeObject)
688
+ expect(subscription.discount).to be_a(Stripe::Discount)
582
689
  expect(subscription.discount.coupon.id).to eq(coupon.id)
583
690
  end
584
691
 
585
692
  it 'when add not exist coupon' do
586
- plan = stripe_helper.create_plan(id: 'plan_with_coupon3', name: 'One More Test Plan', amount: 777)
693
+ plan = stripe_helper.create_plan(id: 'plan_with_coupon3', product: { name: 'One More Test Plan' },
694
+ amount: 777)
587
695
  customer = Stripe::Customer.create(source: gen_card_tk, plan: plan.id)
588
696
  subscription = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
589
697
 
@@ -598,7 +706,8 @@ shared_examples 'Customer Subscriptions' do
598
706
  end
599
707
 
600
708
  it 'when coupon is removed' do
601
- plan = stripe_helper.create_plan(id: 'plan_with_coupon3', name: 'One More Test Plan', amount: 777)
709
+ plan = stripe_helper.create_plan(id: 'plan_with_coupon3', product: { name: 'One More Test Plan' },
710
+ amount: 777)
602
711
  customer = Stripe::Customer.create(source: gen_card_tk, plan: plan.id)
603
712
  coupon = stripe_helper.create_coupon
604
713
  subscription = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
@@ -875,6 +984,52 @@ shared_examples 'Customer Subscriptions' do
875
984
  end
876
985
  end
877
986
 
987
+ it "supports 'cancelling' by updating cancel_at_period_end" do
988
+ truth = stripe_helper.create_plan(id: 'the_truth')
989
+ customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk, plan: "the_truth")
990
+
991
+ sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
992
+ result = Stripe::Subscription.update(sub.id, cancel_at_period_end: true)
993
+
994
+ expect(result.status).to eq('active')
995
+ expect(result.cancel_at_period_end).to eq(true)
996
+ expect(result.id).to eq(sub.id)
997
+
998
+ customer = Stripe::Customer.retrieve('test_customer_sub')
999
+ expect(customer.subscriptions.data).to_not be_empty
1000
+ expect(customer.subscriptions.count).to eq(1)
1001
+ expect(customer.subscriptions.data.length).to eq(1)
1002
+
1003
+ expect(customer.subscriptions.data.first.status).to eq('active')
1004
+ expect(customer.subscriptions.data.first.cancel_at_period_end).to eq(true)
1005
+ expect(customer.subscriptions.data.first.ended_at).to be_nil
1006
+ expect(customer.subscriptions.data.first.canceled_at).to_not be_nil
1007
+ end
1008
+
1009
+ it "resumes a subscription cancelled by updating cancel_at_period_end" do
1010
+ truth = stripe_helper.create_plan(id: 'the_truth')
1011
+ customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk, plan: "the_truth")
1012
+
1013
+ sub = Stripe::Subscription.retrieve(customer.subscriptions.data.first.id)
1014
+ Stripe::Subscription.update(sub.id, cancel_at_period_end: true)
1015
+
1016
+ result = Stripe::Subscription.update(sub.id, cancel_at_period_end: false)
1017
+
1018
+ expect(result.status).to eq('active')
1019
+ expect(result.cancel_at_period_end).to eq(false)
1020
+ expect(result.id).to eq(sub.id)
1021
+
1022
+ customer = Stripe::Customer.retrieve('test_customer_sub')
1023
+ expect(customer.subscriptions.data).to_not be_empty
1024
+ expect(customer.subscriptions.count).to eq(1)
1025
+ expect(customer.subscriptions.data.length).to eq(1)
1026
+
1027
+ expect(customer.subscriptions.data.first.status).to eq('active')
1028
+ expect(customer.subscriptions.data.first.cancel_at_period_end).to eq(false)
1029
+ expect(customer.subscriptions.data.first.ended_at).to be_nil
1030
+ expect(customer.subscriptions.data.first.canceled_at).to be_nil
1031
+ end
1032
+
878
1033
  it "doesn't change status of subscription when cancelling at period end" do
879
1034
  trial = stripe_helper.create_plan(id: 'trial', trial_period_days: 14)
880
1035
  customer = Stripe::Customer.create(id: 'test_customer_sub', source: gen_card_tk, plan: "trial")
@@ -962,10 +1117,13 @@ shared_examples 'Customer Subscriptions' do
962
1117
 
963
1118
  it "creates a stripe customer and subscribes them to a plan with meta data", :live => true do
964
1119
 
965
- stripe_helper.create_plan(
1120
+ stripe_helper.
1121
+ create_plan(
966
1122
  :amount => 500,
967
1123
  :interval => 'month',
968
- :name => 'Sample Plan',
1124
+ :product => {
1125
+ :name => 'Sample Plan'
1126
+ },
969
1127
  :currency => 'usd',
970
1128
  :id => 'Sample5'
971
1129
  )