stripe-ruby-mock 2.5.8 → 3.1.0.rc3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (90) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +1 -1
  3. data/.travis.yml +6 -9
  4. data/CHANGELOG.md +41 -0
  5. data/Gemfile +1 -0
  6. data/README.md +17 -11
  7. data/lib/stripe_mock.rb +8 -0
  8. data/lib/stripe_mock/api/client.rb +1 -1
  9. data/lib/stripe_mock/api/errors.rb +34 -28
  10. data/lib/stripe_mock/api/instance.rb +1 -1
  11. data/lib/stripe_mock/api/webhooks.rb +5 -0
  12. data/lib/stripe_mock/client.rb +2 -1
  13. data/lib/stripe_mock/data.rb +285 -16
  14. data/lib/stripe_mock/data/list.rb +42 -9
  15. data/lib/stripe_mock/instance.rb +18 -4
  16. data/lib/stripe_mock/request_handlers/account_links.rb +15 -0
  17. data/lib/stripe_mock/request_handlers/balance_transactions.rb +2 -2
  18. data/lib/stripe_mock/request_handlers/charges.rb +6 -4
  19. data/lib/stripe_mock/request_handlers/checkout.rb +15 -0
  20. data/lib/stripe_mock/request_handlers/checkout_session.rb +22 -0
  21. data/lib/stripe_mock/request_handlers/customers.rb +35 -18
  22. data/lib/stripe_mock/request_handlers/ephemeral_key.rb +1 -1
  23. data/lib/stripe_mock/request_handlers/express_login_links.rb +15 -0
  24. data/lib/stripe_mock/request_handlers/helpers/coupon_helpers.rb +5 -0
  25. data/lib/stripe_mock/request_handlers/helpers/subscription_helpers.rb +12 -7
  26. data/lib/stripe_mock/request_handlers/helpers/token_helpers.rb +1 -1
  27. data/lib/stripe_mock/request_handlers/invoices.rb +10 -4
  28. data/lib/stripe_mock/request_handlers/payment_intents.rb +182 -0
  29. data/lib/stripe_mock/request_handlers/payment_methods.rb +120 -0
  30. data/lib/stripe_mock/request_handlers/plans.rb +1 -1
  31. data/lib/stripe_mock/request_handlers/prices.rb +44 -0
  32. data/lib/stripe_mock/request_handlers/products.rb +1 -0
  33. data/lib/stripe_mock/request_handlers/setup_intents.rb +93 -0
  34. data/lib/stripe_mock/request_handlers/sources.rb +12 -6
  35. data/lib/stripe_mock/request_handlers/subscriptions.rb +45 -22
  36. data/lib/stripe_mock/request_handlers/tokens.rb +6 -4
  37. data/lib/stripe_mock/request_handlers/validators/param_validators.rb +123 -9
  38. data/lib/stripe_mock/server.rb +2 -2
  39. data/lib/stripe_mock/test_strategies/base.rb +67 -8
  40. data/lib/stripe_mock/test_strategies/live.rb +23 -12
  41. data/lib/stripe_mock/test_strategies/mock.rb +6 -2
  42. data/lib/stripe_mock/version.rb +1 -1
  43. data/lib/stripe_mock/webhook_fixtures/balance.available.json +6 -0
  44. data/lib/stripe_mock/webhook_fixtures/charge.failed.json +166 -38
  45. data/lib/stripe_mock/webhook_fixtures/customer.created.json +1 -0
  46. data/lib/stripe_mock/webhook_fixtures/customer.updated.json +1 -0
  47. data/lib/stripe_mock/webhook_fixtures/payment_intent.payment_failed.json +186 -0
  48. data/lib/stripe_mock/webhook_fixtures/payment_intent.succeeded.json +164 -0
  49. data/lib/stripe_mock/webhook_fixtures/product.created.json +34 -0
  50. data/lib/stripe_mock/webhook_fixtures/product.deleted.json +34 -0
  51. data/lib/stripe_mock/webhook_fixtures/product.updated.json +38 -0
  52. data/spec/instance_spec.rb +10 -12
  53. data/spec/list_spec.rb +38 -0
  54. data/spec/server_spec.rb +6 -3
  55. data/spec/shared_stripe_examples/account_examples.rb +1 -1
  56. data/spec/shared_stripe_examples/account_link_examples.rb +16 -0
  57. data/spec/shared_stripe_examples/balance_examples.rb +6 -0
  58. data/spec/shared_stripe_examples/balance_transaction_examples.rb +3 -3
  59. data/spec/shared_stripe_examples/bank_examples.rb +3 -3
  60. data/spec/shared_stripe_examples/card_examples.rb +4 -4
  61. data/spec/shared_stripe_examples/card_token_examples.rb +17 -21
  62. data/spec/shared_stripe_examples/charge_examples.rb +9 -22
  63. data/spec/shared_stripe_examples/checkout_examples.rb +47 -0
  64. data/spec/shared_stripe_examples/coupon_examples.rb +1 -1
  65. data/spec/shared_stripe_examples/customer_examples.rb +93 -53
  66. data/spec/shared_stripe_examples/dispute_examples.rb +2 -2
  67. data/spec/shared_stripe_examples/error_mock_examples.rb +8 -7
  68. data/spec/shared_stripe_examples/express_login_link_examples.rb +12 -0
  69. data/spec/shared_stripe_examples/external_account_examples.rb +3 -3
  70. data/spec/shared_stripe_examples/invoice_examples.rb +41 -39
  71. data/spec/shared_stripe_examples/invoice_item_examples.rb +1 -1
  72. data/spec/shared_stripe_examples/payment_intent_examples.rb +147 -0
  73. data/spec/shared_stripe_examples/payment_method_examples.rb +449 -0
  74. data/spec/shared_stripe_examples/payout_examples.rb +2 -2
  75. data/spec/shared_stripe_examples/plan_examples.rb +135 -92
  76. data/spec/shared_stripe_examples/price_examples.rb +183 -0
  77. data/spec/shared_stripe_examples/product_examples.rb +147 -0
  78. data/spec/shared_stripe_examples/refund_examples.rb +25 -21
  79. data/spec/shared_stripe_examples/setup_intent_examples.rb +68 -0
  80. data/spec/shared_stripe_examples/subscription_examples.rb +430 -318
  81. data/spec/shared_stripe_examples/subscription_items_examples.rb +3 -2
  82. data/spec/shared_stripe_examples/transfer_examples.rb +6 -6
  83. data/spec/shared_stripe_examples/webhook_event_examples.rb +11 -11
  84. data/spec/spec_helper.rb +7 -4
  85. data/spec/stripe_mock_spec.rb +4 -4
  86. data/spec/support/shared_contexts/stripe_validator_spec.rb +8 -0
  87. data/spec/support/stripe_examples.rb +9 -1
  88. data/stripe-ruby-mock.gemspec +8 -3
  89. metadata +72 -34
  90. data/spec/shared_stripe_examples/product_example.rb +0 -65
@@ -77,7 +77,7 @@ shared_examples 'Bank API' do
77
77
  let!(:bank) { customer.sources.create(source: bank_token) }
78
78
 
79
79
  it "can retrieve all customer's banks" do
80
- retrieved = customer.sources.all
80
+ retrieved = customer.sources.list
81
81
  expect(retrieved.count).to eq(1)
82
82
  end
83
83
 
@@ -174,7 +174,7 @@ shared_examples 'Bank API' do
174
174
 
175
175
  customer = Stripe::Customer.retrieve('test_customer_bank')
176
176
 
177
- list = customer.sources.all
177
+ list = customer.sources.list
178
178
 
179
179
  expect(list.object).to eq("list")
180
180
  expect(list.count).to eq(2)
@@ -191,7 +191,7 @@ shared_examples 'Bank API' do
191
191
  Stripe::Customer.create(id: 'no_banks')
192
192
  customer = Stripe::Customer.retrieve('no_banks')
193
193
 
194
- list = customer.sources.all
194
+ list = customer.sources.list
195
195
 
196
196
  expect(list.object).to eq("list")
197
197
  expect(list.count).to eq(0)
@@ -134,7 +134,7 @@ shared_examples 'Card API' do
134
134
  let!(:card) { customer.sources.create(source: card_token) }
135
135
 
136
136
  it "can retrieve all customer's cards" do
137
- retrieved = customer.sources.all
137
+ retrieved = customer.sources.list
138
138
  expect(retrieved.count).to eq(1)
139
139
  end
140
140
 
@@ -198,7 +198,7 @@ shared_examples 'Card API' do
198
198
  let!(:card) { recipient.cards.create(card: card_token) }
199
199
 
200
200
  it "can retrieve all recipient's cards" do
201
- retrieved = recipient.cards.all
201
+ retrieved = recipient.cards.list
202
202
  expect(retrieved.count).to eq(1)
203
203
  end
204
204
 
@@ -279,7 +279,7 @@ shared_examples 'Card API' do
279
279
 
280
280
  customer = Stripe::Customer.retrieve('test_customer_card')
281
281
 
282
- list = customer.sources.all
282
+ list = customer.sources.list
283
283
 
284
284
  expect(list.object).to eq("list")
285
285
  expect(list.count).to eq(2)
@@ -296,7 +296,7 @@ shared_examples 'Card API' do
296
296
  Stripe::Customer.create(id: 'no_cards')
297
297
  customer = Stripe::Customer.retrieve('no_cards')
298
298
 
299
- list = customer.sources.all
299
+ list = customer.sources.list
300
300
 
301
301
  expect(list.object).to eq("list")
302
302
  expect(list.count).to eq(0)
@@ -145,34 +145,30 @@ shared_examples 'Card Token Mocking' do
145
145
  end
146
146
 
147
147
  it 'generates a card token from another card', oauth: true do
148
- token = Stripe::Token.create(
149
- card: {
150
- exp_month: 10,
151
- exp_year: 2016,
152
- number: '4242424242424242'
153
- }
154
- )
148
+ token = StripeMock.generate_card_token(last4: "2244", exp_month: 33, exp_year: 2255)
155
149
 
156
- cus1 = Stripe::Customer.create(source: token.id)
150
+ cus1 = Stripe::Customer.create()
151
+ cus1.source = token
152
+ cus1.save
157
153
 
158
154
  card1 = cus1.sources.data.first
159
- expect(card1.last4).to eq('4242')
160
- expect(card1.exp_month).to eq(10)
161
- expect(card1.exp_year).to eq(2016)
155
+ expect(card1.last4).to eq("2244")
156
+ expect(card1.exp_month).to eq(33)
157
+ expect(card1.exp_year).to eq(2255)
162
158
 
163
- card_token = Stripe::Token.create(
164
- {
165
- customer: cus1.id,
166
- card: card1.id
167
- },
168
- ENV['STRIPE_TEST_OAUTH_ACCESS_TOKEN'])
159
+ card_token = Stripe::Token.create({
160
+ customer: cus1.id,
161
+ card: card1.id
162
+ })
169
163
 
170
- cus2 = Stripe::Customer.create({ source: card_token.id }, ENV['STRIPE_TEST_OAUTH_ACCESS_TOKEN'])
164
+ cus2 = Stripe::Customer.create({}, ENV['STRIPE_TEST_OAUTH_ACCESS_TOKEN'])
165
+ cus2.source = card_token.id
166
+ cus2.save
171
167
 
172
168
  card2 = cus2.sources.data.first
173
- expect(card2.last4).to eq('4242')
174
- expect(card2.exp_month).to eq(10)
175
- expect(card2.exp_year).to eq(2016)
169
+ expect(card2.last4).to eq("2244")
170
+ expect(card2.exp_month).to eq(33)
171
+ expect(card2.exp_year).to eq(2255)
176
172
  end
177
173
 
178
174
  it "throws an error if neither card nor customer are provided", :live => true do
@@ -278,20 +278,6 @@ shared_examples 'Charge API' do
278
278
  }.not_to raise_error
279
279
  end
280
280
 
281
- it "marks a charge as safe" do
282
- original = Stripe::Charge.create({
283
- amount: 777,
284
- currency: 'USD',
285
- source: stripe_helper.generate_card_token
286
- })
287
- charge = Stripe::Charge.retrieve(original.id)
288
-
289
- charge.mark_as_safe
290
-
291
- updated = Stripe::Charge.retrieve(original.id)
292
- expect(updated.fraud_details[:user_report]).to eq "safe"
293
- end
294
-
295
281
  it "does not lose data when updating a charge" do
296
282
  original = Stripe::Charge.create({
297
283
  amount: 777,
@@ -357,28 +343,29 @@ shared_examples 'Charge API' do
357
343
  end
358
344
 
359
345
  it "stores charges for a customer in memory" do
360
- expect(@customer.charges.data.map(&:id)).to eq([@charge.id])
346
+ charges = Stripe::Charge.list(customer: @customer.id)
347
+ expect(charges.map(&:id)).to eq([@charge.id])
361
348
  end
362
349
 
363
350
  it "stores all charges in memory" do
364
- expect(Stripe::Charge.all.data.map(&:id).reverse).to eq([@charge.id, @charge2.id])
351
+ expect(Stripe::Charge.list.data.map(&:id).reverse).to eq([@charge.id, @charge2.id])
365
352
  end
366
353
 
367
354
  it "defaults count to 10 charges" do
368
355
  11.times { Stripe::Charge.create(amount: 1, currency: 'usd', source: stripe_helper.generate_card_token) }
369
356
 
370
- expect(Stripe::Charge.all.data.count).to eq(10)
357
+ expect(Stripe::Charge.list.data.count).to eq(10)
371
358
  end
372
359
 
373
360
  it "is marked as having more when more objects exist" do
374
361
  11.times { Stripe::Charge.create(amount: 1, currency: 'usd', source: stripe_helper.generate_card_token) }
375
362
 
376
- expect(Stripe::Charge.all.has_more).to eq(true)
363
+ expect(Stripe::Charge.list.has_more).to eq(true)
377
364
  end
378
365
 
379
366
  context "when passing limit" do
380
367
  it "gets that many charges" do
381
- expect(Stripe::Charge.all(limit: 1).count).to eq(1)
368
+ expect(Stripe::Charge.list(limit: 1).count).to eq(1)
382
369
  end
383
370
  end
384
371
  end
@@ -398,13 +385,13 @@ shared_examples 'Charge API' do
398
385
  Stripe::Charge.create(customer: cus.id, amount: 100, currency: "usd")
399
386
  end
400
387
 
401
- all = Stripe::Charge.all
388
+ all_charges = Stripe::Charge.list
402
389
  default_limit = 10
403
- half = Stripe::Charge.all(starting_after: all.data.at(1).id)
390
+ half = Stripe::Charge.list(starting_after: all_charges.data.at(1).id)
404
391
 
405
392
  expect(half).to be_a(Stripe::ListObject)
406
393
  expect(half.data.count).to eq(default_limit)
407
- expect(half.data.first.id).to eq(all.data.at(2).id)
394
+ expect(half.data.first.id).to eq(all_charges.data.at(2).id)
408
395
  end
409
396
 
410
397
 
@@ -0,0 +1,47 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples 'Checkout API' do
4
+
5
+ it "creates a stripe checkout session" do
6
+ session = Stripe::Checkout::Session.create({
7
+ payment_method_types: ['card'],
8
+ line_items: [{
9
+ name: 'T-shirt',
10
+ quantity: 1,
11
+ amount: 500,
12
+ currency: 'usd',
13
+ }],
14
+ })
15
+ expect(session.id).to match(/^test_cs/)
16
+ expect(session.line_items.count).to eq(1)
17
+ end
18
+
19
+ context 'retrieve a checkout session' do
20
+ let(:checkout_session1) { stripe_helper.create_checkout_session }
21
+
22
+ it 'ca be retrieved by id' do
23
+ checkout_session1
24
+
25
+ checkout_session = Stripe::Checkout::Session.retrieve(checkout_session1.id)
26
+
27
+ expect(checkout_session.id).to eq(checkout_session1.id)
28
+ end
29
+
30
+ it "cannot retrieve a checkout session that doesn't exist" do
31
+ expect { Stripe::Checkout::Session.retrieve('nope') }.to raise_error {|e|
32
+ expect(e).to be_a Stripe::InvalidRequestError
33
+ expect(e.param).to eq('checkout_session')
34
+ expect(e.http_status).to eq(404)
35
+ }
36
+ end
37
+
38
+ it 'can expand setup_intent' do
39
+ setup_intent = Stripe::SetupIntent.create
40
+ initial_session = Stripe::Checkout::Session.create(setup_intent: setup_intent.id)
41
+
42
+ checkout_session = Stripe::Checkout::Session.retrieve(id: initial_session.id, expand: ['setup_intent'])
43
+
44
+ expect(checkout_session.setup_intent).to eq(setup_intent)
45
+ end
46
+ end
47
+ end
@@ -53,7 +53,7 @@ shared_examples 'Coupon API' do
53
53
  coupon1
54
54
  coupon2
55
55
 
56
- all = Stripe::Coupon.all
56
+ all = Stripe::Coupon.list
57
57
 
58
58
  expect(all.count).to eq(2)
59
59
  expect(all.map &:id).to include('10BUCKS', '11BUCKS')
@@ -1,6 +1,8 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  shared_examples 'Customer API' do
4
+ let(:product_params) { {id: "prod_CCC", name: "My Product", type: "service"} }
5
+ let(:product) { stripe_helper.create_product(product_params) }
4
6
 
5
7
  def gen_card_tk
6
8
  stripe_helper.generate_card_token
@@ -15,6 +17,7 @@ shared_examples 'Customer API' do
15
17
  expect(customer.id).to match(/^test_cus/)
16
18
  expect(customer.email).to eq('johnny@appleseed.com')
17
19
  expect(customer.description).to eq('a description')
20
+ expect(customer.preferred_locales).to eq([])
18
21
 
19
22
  expect(customer.sources.count).to eq(1)
20
23
  expect(customer.sources.data.length).to eq(1)
@@ -24,6 +27,17 @@ shared_examples 'Customer API' do
24
27
  expect { customer.source }.to raise_error
25
28
  end
26
29
 
30
+ it "creates a stripe customer with a default payment method" do
31
+ customer = Stripe::Customer.create({
32
+ email: 'johnny@appleseed.com',
33
+ invoice_settings: {
34
+ default_payment_method: "pm_1ExEuFL2DI6wht39WNJgbybl"
35
+ },
36
+ description: "a description"
37
+ })
38
+ expect(customer.invoice_settings.default_payment_method).to eq("pm_1ExEuFL2DI6wht39WNJgbybl")
39
+ end
40
+
27
41
  it "creates a stripe customer with multiple cards and updates the default card" do
28
42
  card_a = gen_card_tk
29
43
  card_b = gen_card_tk
@@ -88,7 +102,7 @@ shared_examples 'Customer API' do
88
102
  end
89
103
 
90
104
  it 'creates a customer with a plan' do
91
- plan = stripe_helper.create_plan(id: 'silver')
105
+ plan = stripe_helper.create_plan(id: 'silver', product: product.id)
92
106
  customer = Stripe::Customer.create(id: 'test_cus_plan', source: gen_card_tk, :plan => 'silver')
93
107
 
94
108
  customer = Stripe::Customer.retrieve('test_cus_plan')
@@ -100,24 +114,29 @@ shared_examples 'Customer API' do
100
114
  expect(customer.subscriptions.first.customer).to eq(customer.id)
101
115
  end
102
116
 
103
- it "creates a customer with a plan (string/symbol agnostic)" do
104
- plan = stripe_helper.create_plan(id: 'string_id')
105
- customer = Stripe::Customer.create(id: 'test_cus_plan', source: gen_card_tk, :plan => :string_id)
117
+ it 'creates a customer with a plan (string/symbol agnostic)' do
118
+ stripe_helper.create_plan(id: 'silver', product: product.id)
106
119
 
107
- customer = Stripe::Customer.retrieve('test_cus_plan')
108
- expect(customer.subscriptions.first.plan.id).to eq('string_id')
109
-
110
- plan = stripe_helper.create_plan(:id => :sym_id)
111
- customer = Stripe::Customer.create(id: 'test_cus_plan', source: gen_card_tk, :plan => 'sym_id')
120
+ Stripe::Customer.create(id: 'cust_SLV1', source: gen_card_tk, :plan => 'silver')
121
+ customer = Stripe::Customer.retrieve('cust_SLV1')
122
+ expect(customer.subscriptions.count).to eq(1)
123
+ expect(customer.subscriptions.data.length).to eq(1)
124
+ expect(customer.subscriptions).to_not be_nil
125
+ expect(customer.subscriptions.first.plan.id).to eq('silver')
126
+ expect(customer.subscriptions.first.customer).to eq(customer.id)
112
127
 
113
- customer = Stripe::Customer.retrieve('test_cus_plan')
114
- expect(customer.subscriptions.first.plan.id).to eq('sym_id')
128
+ Stripe::Customer.create(id: 'cust_SLV2', source: gen_card_tk, :plan => :silver)
129
+ customer = Stripe::Customer.retrieve('cust_SLV2')
130
+ expect(customer.subscriptions.count).to eq(1)
131
+ expect(customer.subscriptions.data.length).to eq(1)
132
+ expect(customer.subscriptions).to_not be_nil
133
+ expect(customer.subscriptions.first.plan.id).to eq('silver')
134
+ expect(customer.subscriptions.first.customer).to eq(customer.id)
115
135
  end
116
136
 
117
137
  context "create customer" do
118
-
119
138
  it "with a trial when trial_end is set" do
120
- plan = stripe_helper.create_plan(id: 'no_trial', amount: 999)
139
+ plan = stripe_helper.create_plan(id: 'no_trial', product: product.id, amount: 999)
121
140
  trial_end = Time.now.utc.to_i + 3600
122
141
  customer = Stripe::Customer.create(id: 'test_cus_trial_end', source: gen_card_tk, plan: 'no_trial', trial_end: trial_end)
123
142
 
@@ -133,7 +152,7 @@ shared_examples 'Customer API' do
133
152
  end
134
153
 
135
154
  it 'overrides trial period length when trial_end is set' do
136
- plan = stripe_helper.create_plan(id: 'silver', amount: 999, trial_period_days: 14)
155
+ plan = stripe_helper.create_plan(id: 'silver', product: product.id, amount: 999, trial_period_days: 14)
137
156
  trial_end = Time.now.utc.to_i + 3600
138
157
  customer = Stripe::Customer.create(id: 'test_cus_trial_end', source: gen_card_tk, plan: 'silver', trial_end: trial_end)
139
158
 
@@ -148,7 +167,7 @@ shared_examples 'Customer API' do
148
167
  end
149
168
 
150
169
  it 'creates a customer when trial_end is set and no source', live: true do
151
- plan = stripe_helper.create_plan(id: 'silver', amount: 999)
170
+ plan = stripe_helper.create_plan(id: 'silver', product: product.id, amount: 999)
152
171
  trial_end = Time.now.utc.to_i + 3600
153
172
  customer = Stripe::Customer.create(plan: 'silver', trial_end: trial_end)
154
173
  expect(customer.subscriptions.count).to eq(1)
@@ -161,7 +180,7 @@ shared_examples 'Customer API' do
161
180
  end
162
181
 
163
182
  it "returns no trial when trial_end is set to 'now'" do
164
- plan = stripe_helper.create_plan(id: 'silver', amount: 999, trial_period_days: 14)
183
+ plan = stripe_helper.create_plan(id: 'silver', product: product.id, amount: 999, trial_period_days: 14)
165
184
  customer = Stripe::Customer.create(id: 'test_cus_trial_end', source: gen_card_tk, plan: 'silver', trial_end: "now")
166
185
 
167
186
  customer = Stripe::Customer.retrieve('test_cus_trial_end')
@@ -175,8 +194,8 @@ shared_examples 'Customer API' do
175
194
  expect(customer.subscriptions.first.trial_end).to be_nil
176
195
  end
177
196
 
178
- it "returns an error if trial_end is set to a past time" do
179
- plan = stripe_helper.create_plan(id: 'silver', amount: 999)
197
+ it 'returns an error if trial_end is set to a past time' do
198
+ plan = stripe_helper.create_plan(id: 'silver', product: product.id, amount: 999)
180
199
  expect {
181
200
  Stripe::Customer.create(id: 'test_cus_trial_end', source: gen_card_tk, plan: 'silver', trial_end: Time.now.utc.to_i - 3600)
182
201
  }.to raise_error {|e|
@@ -185,7 +204,7 @@ shared_examples 'Customer API' do
185
204
  }
186
205
  end
187
206
 
188
- it "returns an error if trial_end is set without a plan" do
207
+ it 'returns an error if trial_end is set without a plan' do
189
208
  expect {
190
209
  Stripe::Customer.create(id: 'test_cus_trial_end', source: gen_card_tk, trial_end: "now")
191
210
  }.to raise_error {|e|
@@ -198,7 +217,7 @@ shared_examples 'Customer API' do
198
217
 
199
218
  it 'cannot create a customer with a plan that does not exist' do
200
219
  expect {
201
- customer = Stripe::Customer.create(id: 'test_cus_no_plan', source: gen_card_tk, :plan => 'non-existant')
220
+ Stripe::Customer.create(id: 'test_cus_no_plan', source: gen_card_tk, :plan => 'non-existant')
202
221
  }.to raise_error {|e|
203
222
  expect(e).to be_a(Stripe::InvalidRequestError)
204
223
  expect(e.message).to eq('No such plan: non-existant')
@@ -206,9 +225,9 @@ shared_examples 'Customer API' do
206
225
  end
207
226
 
208
227
  it 'cannot create a customer with an existing plan, but no card token' do
209
- plan = stripe_helper.create_plan(id: 'p')
228
+ plan = stripe_helper.create_plan(id: 'p', product: product.id)
210
229
  expect {
211
- customer = Stripe::Customer.create(id: 'test_cus_no_plan', :plan => 'p')
230
+ Stripe::Customer.create(id: 'test_cus_no_plan', :plan => 'p')
212
231
  }.to raise_error {|e|
213
232
  expect(e).to be_a(Stripe::InvalidRequestError)
214
233
  expect(e.message).to eq('You must supply a valid card')
@@ -216,10 +235,9 @@ shared_examples 'Customer API' do
216
235
  end
217
236
 
218
237
  it 'creates a customer with a coupon discount' do
219
- coupon = Stripe::Coupon.create(id: "10PERCENT", duration: 'once')
238
+ coupon = Stripe::Coupon.create(id: '10PERCENT', duration: 'once')
220
239
 
221
- customer =
222
- Stripe::Customer.create(id: 'test_cus_coupon', coupon: '10PERCENT')
240
+ Stripe::Customer.create(id: 'test_cus_coupon', coupon: '10PERCENT')
223
241
 
224
242
  customer = Stripe::Customer.retrieve('test_cus_coupon')
225
243
  expect(customer.discount).to_not be_nil
@@ -231,40 +249,59 @@ shared_examples 'Customer API' do
231
249
  describe 'repeating coupon with duration limit', live: true do
232
250
  let!(:coupon) { stripe_helper.create_coupon(id: '10OFF', amount_off: 1000, currency: 'usd', duration: 'repeating', duration_in_months: 12) }
233
251
  let!(:customer) { Stripe::Customer.create(coupon: coupon.id) }
252
+
234
253
  it 'creates the discount with the end date', live: true do
235
254
  discount = Stripe::Customer.retrieve(customer.id).discount
236
255
  expect(discount).to_not be_nil
237
256
  expect(discount.coupon).to_not be_nil
238
- expect(discount.end).to be_within(1).of (Time.now.to_datetime >> 12).to_time.to_i
257
+ expect(discount.end).to be_within(10).of (DateTime.now >> 12).to_time.to_i
239
258
  end
259
+
240
260
  after { Stripe::Coupon.retrieve(coupon.id).delete }
241
261
  after { Stripe::Customer.retrieve(customer.id).delete }
242
262
  end
243
263
 
244
264
  it 'cannot create a customer with a coupon that does not exist' do
245
265
  expect{
246
- customer = Stripe::Customer.create(id: 'test_cus_no_coupon', coupon: '5OFF')
266
+ Stripe::Customer.create(id: 'test_cus_no_coupon', coupon: '5OFF')
247
267
  }.to raise_error {|e|
248
268
  expect(e).to be_a(Stripe::InvalidRequestError)
249
269
  expect(e.message).to eq('No such coupon: 5OFF')
250
270
  }
251
271
  end
252
272
 
273
+ context 'with coupon on customer' do
274
+ before do
275
+ Stripe::Coupon.create(id: '10PERCENT', duration: 'once')
276
+ Stripe::Customer.create(id: 'test_cus_coupon', coupon: '10PERCENT')
277
+ end
278
+
279
+ it 'remove the coupon from customer' do
280
+ customer = Stripe::Customer.retrieve('test_cus_coupon')
281
+ expect(customer.discount).to_not be_nil
282
+ expect(customer.discount.coupon).to_not be_nil
283
+ expect(customer.discount.customer).to eq customer.id
284
+ expect(customer.discount.start).to be_within(1).of Time.now.to_i
285
+
286
+ Stripe::Customer.update('test_cus_coupon', coupon: '')
287
+ customer = Stripe::Customer.retrieve('test_cus_coupon')
288
+ expect(customer.discount).to be_nil
289
+ end
290
+ end
291
+
253
292
  it "stores a created stripe customer in memory" do
254
- customer = Stripe::Customer.create({
255
- email: 'johnny@appleseed.com',
256
- source: gen_card_tk
257
- })
258
- customer2 = Stripe::Customer.create({
259
- email: 'bob@bobbers.com',
260
- source: gen_card_tk
261
- })
293
+ customer = Stripe::Customer.create(email: 'johnny@appleseed.com')
294
+ customer2 = Stripe::Customer.create(email: 'bob@bobbers.com')
262
295
  data = test_data_source(:customers)
263
- expect(data[customer.id]).to_not be_nil
264
- expect(data[customer.id][:email]).to eq('johnny@appleseed.com')
296
+ list = data[data.keys.first]
297
+
298
+ customer_hash = list[customer.id.to_sym] || list[customer.id]
299
+ expect(customer_hash).to_not be_nil
300
+ expect(customer_hash[:email]).to eq('johnny@appleseed.com')
265
301
 
266
- expect(data[customer2.id]).to_not be_nil
267
- expect(data[customer2.id][:email]).to eq('bob@bobbers.com')
302
+ customer2_hash = list[customer2.id.to_sym] || list[customer2.id]
303
+ expect(customer2_hash).to_not be_nil
304
+ expect(customer2_hash[:email]).to eq('bob@bobbers.com')
268
305
  end
269
306
 
270
307
  it "retrieves a stripe customer" do
@@ -307,9 +344,9 @@ shared_examples 'Customer API' do
307
344
  Stripe::Customer.create({ email: 'one@one.com' })
308
345
  Stripe::Customer.create({ email: 'two@two.com' })
309
346
 
310
- all = Stripe::Customer.all
347
+ all = Stripe::Customer.list
311
348
  expect(all.count).to eq(2)
312
- expect(all.map &:email).to include('one@one.com', 'two@two.com')
349
+ expect(all.data.map &:email).to include('one@one.com', 'two@two.com')
313
350
  end
314
351
 
315
352
  it "updates a stripe customer" do
@@ -317,8 +354,9 @@ shared_examples 'Customer API' do
317
354
  email = original.email
318
355
 
319
356
  coupon = Stripe::Coupon.create(id: "10PERCENT", duration: 'once')
320
- original.description = 'new desc'
321
- original.coupon = coupon.id
357
+ original.description = 'new desc'
358
+ original.preferred_locales = %w(fr en)
359
+ original.coupon = coupon.id
322
360
  original.save
323
361
 
324
362
  expect(original.email).to eq(email)
@@ -328,9 +366,19 @@ shared_examples 'Customer API' do
328
366
  customer = Stripe::Customer.retrieve("test_customer_update")
329
367
  expect(customer.email).to eq(original.email)
330
368
  expect(customer.description).to eq('new desc')
369
+ expect(customer.preferred_locales).to eq(%w(fr en))
331
370
  expect(customer.discount.coupon).to be_a Stripe::Coupon
332
371
  end
333
372
 
373
+ it "preserves stripe customer metadata" do
374
+ metadata = {user_id: "38"}
375
+ customer = Stripe::Customer.create(metadata: metadata)
376
+ expect(customer.metadata.to_h).to eq(metadata)
377
+
378
+ updated = Stripe::Customer.update(customer.id, metadata: {fruit: "apples"})
379
+ expect(updated.metadata.to_h).to eq(metadata.merge(fruit: "apples"))
380
+ end
381
+
334
382
  it "retrieves the customer's default source after it was updated" do
335
383
  customer = Stripe::Customer.create()
336
384
  customer.source = gen_card_tk
@@ -391,7 +439,7 @@ shared_examples 'Customer API' do
391
439
  end
392
440
 
393
441
  it "still has subscriptions after save when subscriptions unchanged" do
394
- plan = stripe_helper.create_plan(id: 'silver')
442
+ plan = stripe_helper.create_plan(id: 'silver', product: product.id)
395
443
  original = Stripe::Customer.create(source: gen_card_tk, plan: 'silver')
396
444
  subscription = original.subscriptions.data.first
397
445
  subscription_id = subscription.id
@@ -404,7 +452,7 @@ shared_examples 'Customer API' do
404
452
  end
405
453
 
406
454
  it "should add a customer to a subscription" do
407
- plan = stripe_helper.create_plan(id: 'silver')
455
+ plan = stripe_helper.create_plan(id: 'silver', product: product.id)
408
456
  customer = Stripe::Customer.create(source: gen_card_tk)
409
457
  customer.subscriptions.create(plan: plan.id)
410
458
 
@@ -417,14 +465,6 @@ shared_examples 'Customer API' do
417
465
  expect(customer.deleted).to eq(true)
418
466
  end
419
467
 
420
- it 'works with the update_subscription method' do
421
- stripe_helper.create_plan(id: 'silver')
422
- cus = Stripe::Customer.create(source: gen_card_tk)
423
- expect {
424
- cus.update_subscription(plan: 'silver')
425
- }.not_to raise_error
426
- end
427
-
428
468
  it "deletes a stripe customer discount" do
429
469
  original = Stripe::Customer.create(id: 'test_customer_update')
430
470