stripe-ruby-mock 2.4.0 → 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 (98) hide show
  1. checksums.yaml +4 -4
  2. data/.env +2 -0
  3. data/.travis.yml +8 -4
  4. data/README.md +12 -5
  5. data/lib/stripe_mock/api/account_balance.rb +14 -0
  6. data/lib/stripe_mock/api/client.rb +4 -4
  7. data/lib/stripe_mock/api/conversion_rate.rb +14 -0
  8. data/lib/stripe_mock/api/errors.rb +25 -14
  9. data/lib/stripe_mock/api/instance.rb +6 -6
  10. data/lib/stripe_mock/api/webhooks.rb +5 -1
  11. data/lib/stripe_mock/client.rb +18 -2
  12. data/lib/stripe_mock/data/list.rb +14 -2
  13. data/lib/stripe_mock/data.rb +217 -56
  14. data/lib/stripe_mock/instance.rb +93 -8
  15. data/lib/stripe_mock/request_handlers/accounts.rb +34 -2
  16. data/lib/stripe_mock/request_handlers/balance.rb +17 -0
  17. data/lib/stripe_mock/request_handlers/balance_transactions.rb +18 -2
  18. data/lib/stripe_mock/request_handlers/charges.rb +25 -17
  19. data/lib/stripe_mock/request_handlers/coupons.rb +5 -4
  20. data/lib/stripe_mock/request_handlers/customers.rb +26 -8
  21. data/lib/stripe_mock/request_handlers/ephemeral_key.rb +13 -0
  22. data/lib/stripe_mock/request_handlers/external_accounts.rb +55 -0
  23. data/lib/stripe_mock/request_handlers/helpers/bank_account_helpers.rb +1 -1
  24. data/lib/stripe_mock/request_handlers/helpers/card_helpers.rb +9 -7
  25. data/lib/stripe_mock/request_handlers/helpers/coupon_helpers.rb +10 -6
  26. data/lib/stripe_mock/request_handlers/helpers/external_account_helpers.rb +49 -0
  27. data/lib/stripe_mock/request_handlers/helpers/subscription_helpers.rb +55 -15
  28. data/lib/stripe_mock/request_handlers/helpers/token_helpers.rb +2 -2
  29. data/lib/stripe_mock/request_handlers/invoices.rb +93 -14
  30. data/lib/stripe_mock/request_handlers/orders.rb +5 -5
  31. data/lib/stripe_mock/request_handlers/payouts.rb +32 -0
  32. data/lib/stripe_mock/request_handlers/plans.rb +1 -0
  33. data/lib/stripe_mock/request_handlers/products.rb +43 -0
  34. data/lib/stripe_mock/request_handlers/refunds.rb +10 -7
  35. data/lib/stripe_mock/request_handlers/subscription_items.rb +36 -0
  36. data/lib/stripe_mock/request_handlers/subscriptions.rb +95 -34
  37. data/lib/stripe_mock/request_handlers/tax_rates.rb +36 -0
  38. data/lib/stripe_mock/request_handlers/tokens.rb +9 -3
  39. data/lib/stripe_mock/request_handlers/transfers.rb +11 -5
  40. data/lib/stripe_mock/request_handlers/validators/param_validators.rb +10 -1
  41. data/lib/stripe_mock/server.rb +14 -1
  42. data/lib/stripe_mock/test_strategies/base.rb +7 -5
  43. data/lib/stripe_mock/test_strategies/live.rb +5 -0
  44. data/lib/stripe_mock/test_strategies/mock.rb +8 -0
  45. data/lib/stripe_mock/util.rb +8 -2
  46. data/lib/stripe_mock/version.rb +1 -1
  47. data/lib/stripe_mock/webhook_fixtures/account.updated.json +1 -1
  48. data/lib/stripe_mock/webhook_fixtures/charge.dispute.funds_reinstated.json +88 -0
  49. data/lib/stripe_mock/webhook_fixtures/charge.dispute.funds_withdrawn.json +88 -0
  50. data/lib/stripe_mock/webhook_fixtures/charge.updated.json +58 -0
  51. data/lib/stripe_mock/webhook_fixtures/customer.subscription.created.json +40 -10
  52. data/lib/stripe_mock/webhook_fixtures/customer.subscription.deleted.json +39 -10
  53. data/lib/stripe_mock/webhook_fixtures/customer.subscription.trial_will_end.json +39 -10
  54. data/lib/stripe_mock/webhook_fixtures/customer.subscription.updated.json +40 -11
  55. data/lib/stripe_mock/webhook_fixtures/invoice.created.json +3 -2
  56. data/lib/stripe_mock/webhook_fixtures/invoice.payment_failed.json +1 -1
  57. data/lib/stripe_mock/webhook_fixtures/invoice.payment_succeeded.json +92 -85
  58. data/lib/stripe_mock/webhook_fixtures/invoice.updated.json +3 -2
  59. data/lib/stripe_mock/webhook_fixtures/plan.created.json +1 -1
  60. data/lib/stripe_mock/webhook_fixtures/plan.deleted.json +1 -1
  61. data/lib/stripe_mock/webhook_fixtures/plan.updated.json +1 -1
  62. data/lib/stripe_mock.rb +13 -0
  63. data/spec/instance_spec.rb +54 -4
  64. data/spec/integration_examples/prepare_error_examples.rb +6 -6
  65. data/spec/list_spec.rb +16 -3
  66. data/spec/readme_spec.rb +2 -0
  67. data/spec/server_spec.rb +6 -2
  68. data/spec/shared_stripe_examples/account_examples.rb +35 -0
  69. data/spec/shared_stripe_examples/balance_examples.rb +11 -0
  70. data/spec/shared_stripe_examples/balance_transaction_examples.rb +28 -0
  71. data/spec/shared_stripe_examples/bank_examples.rb +28 -1
  72. data/spec/shared_stripe_examples/card_examples.rb +10 -3
  73. data/spec/shared_stripe_examples/charge_examples.rb +73 -17
  74. data/spec/shared_stripe_examples/coupon_examples.rb +8 -2
  75. data/spec/shared_stripe_examples/customer_examples.rb +54 -2
  76. data/spec/shared_stripe_examples/dispute_examples.rb +19 -8
  77. data/spec/shared_stripe_examples/ephemeral_key_examples.rb +17 -0
  78. data/spec/shared_stripe_examples/error_mock_examples.rb +3 -3
  79. data/spec/shared_stripe_examples/external_account_examples.rb +170 -0
  80. data/spec/shared_stripe_examples/extra_features_examples.rb +2 -0
  81. data/spec/shared_stripe_examples/invoice_examples.rb +314 -51
  82. data/spec/shared_stripe_examples/payout_examples.rb +68 -0
  83. data/spec/shared_stripe_examples/plan_examples.rb +37 -5
  84. data/spec/shared_stripe_examples/product_example.rb +65 -0
  85. data/spec/shared_stripe_examples/recipient_examples.rb +7 -7
  86. data/spec/shared_stripe_examples/refund_examples.rb +17 -11
  87. data/spec/shared_stripe_examples/subscription_examples.rb +463 -33
  88. data/spec/shared_stripe_examples/subscription_items_examples.rb +75 -0
  89. data/spec/shared_stripe_examples/tax_rate_examples.rb +42 -0
  90. data/spec/shared_stripe_examples/transfer_examples.rb +72 -23
  91. data/spec/shared_stripe_examples/webhook_event_examples.rb +74 -5
  92. data/spec/spec_helper.rb +7 -6
  93. data/spec/stripe_mock_spec.rb +16 -3
  94. data/spec/support/stripe_examples.rb +7 -1
  95. data/spec/util_spec.rb +35 -1
  96. data/stripe-ruby-mock.gemspec +1 -1
  97. metadata +34 -7
  98. data/ChangeLog.rdoc +0 -4
@@ -21,7 +21,7 @@ shared_examples 'Card API' do
21
21
  expect(card.exp_year).to eq(2099)
22
22
  end
23
23
 
24
- it 'creates/returns a card when using recipient.cards.create given a card token' do
24
+ it 'creates/returns a card when using recipient.cards.create given a card token', skip: 'Stripe has deprecated Recipients' do
25
25
  params = {
26
26
  id: 'test_recipient_sub',
27
27
  name: 'MyRec',
@@ -69,7 +69,7 @@ shared_examples 'Card API' do
69
69
  expect(card.exp_year).to eq(3031)
70
70
  end
71
71
 
72
- it 'creates/returns a card when using recipient.cards.create given card params' do
72
+ it 'creates/returns a card when using recipient.cards.create given card params', skip: 'Stripe has deprecated Recipients' do
73
73
  params = {
74
74
  id: 'test_recipient_sub',
75
75
  name: 'MyRec',
@@ -166,6 +166,13 @@ shared_examples 'Card API' do
166
166
  expect(retrieved_cus.default_source).to be_nil
167
167
  end
168
168
 
169
+ it 'updates total_count if deleted' do
170
+ card.delete
171
+ sources = Stripe::Customer.retrieve(customer.id).sources
172
+
173
+ expect(sources.total_count).to eq 0
174
+ end
175
+
169
176
  context "deletion when the user has two cards" do
170
177
  let!(:card_token_2) { stripe_helper.generate_card_token(last4: "1123", exp_month: 11, exp_year: 2099) }
171
178
  let!(:card_2) { customer.sources.create(source: card_token_2) }
@@ -185,7 +192,7 @@ shared_examples 'Card API' do
185
192
  end
186
193
  end
187
194
 
188
- describe "retrieval and deletion with recipients", :live => true do
195
+ describe "retrieval and deletion with recipients", :live => true, skip: 'Stripe has deprecated Recipients' do
189
196
  let!(:recipient) { Stripe::Recipient.create(name: 'Test Recipient', type: 'individual') }
190
197
  let!(:card_token) { stripe_helper.generate_card_token(number: "4000056655665556") }
191
198
  let!(:card) { recipient.cards.create(card: card_token) }
@@ -163,18 +163,38 @@ shared_examples 'Charge API' do
163
163
  end
164
164
 
165
165
  it "creates a balance transaction" do
166
+ amount = 300
167
+ fee = 10
166
168
  charge = Stripe::Charge.create({
167
- amount: 300,
169
+ amount: amount,
168
170
  currency: 'USD',
169
- source: stripe_helper.generate_card_token
171
+ source: stripe_helper.generate_card_token,
172
+ application_fee: fee,
170
173
  })
171
174
  bal_trans = Stripe::BalanceTransaction.retrieve(charge.balance_transaction)
172
- expect(bal_trans.amount).to eq(charge.amount)
173
- expect(bal_trans.fee).to eq(39)
174
- expect(bal_trans.source).to eq(charge.source)
175
+ expect(bal_trans.amount).to eq(amount)
176
+ expect(bal_trans.fee).to eq(39 + fee)
177
+ expect(bal_trans.source).to eq(charge.id)
178
+ expect(bal_trans.net).to eq(amount - bal_trans.fee)
175
179
  end
176
180
 
177
- it "can expand balance transaction" do
181
+ context 'when conversion rate is set' do
182
+ it "balance transaction stores amount converted from charge currency to USD" do
183
+ StripeMock.set_conversion_rate(1.2)
184
+
185
+ charge = Stripe::Charge.create({
186
+ amount: 300,
187
+ currency: 'CAD',
188
+ source: stripe_helper.generate_card_token
189
+ })
190
+ bal_trans = Stripe::BalanceTransaction.retrieve(charge.balance_transaction)
191
+ expect(bal_trans.amount).to eq(charge.amount * 1.2)
192
+ expect(bal_trans.fee).to eq(39)
193
+ expect(bal_trans.currency).to eq('usd')
194
+ end
195
+ end
196
+
197
+ it "can expand balance transaction when creating a charge" do
178
198
  charge = Stripe::Charge.create({
179
199
  amount: 300,
180
200
  currency: 'USD',
@@ -196,6 +216,20 @@ shared_examples 'Charge API' do
196
216
  expect(charge.amount).to eq(original.amount)
197
217
  end
198
218
 
219
+ it "can expand balance transaction when retrieving a charge" do
220
+ original = Stripe::Charge.create({
221
+ amount: 300,
222
+ currency: 'USD',
223
+ source: stripe_helper.generate_card_token
224
+ })
225
+ charge = Stripe::Charge.retrieve(
226
+ id: original.id,
227
+ expand: ['balance_transaction']
228
+ )
229
+
230
+ expect(charge.balance_transaction).to be_a(Stripe::BalanceTransaction)
231
+ end
232
+
199
233
  it "cannot retrieve a charge that doesn't exist" do
200
234
  expect { Stripe::Charge.retrieve('nope') }.to raise_error {|e|
201
235
  expect(e).to be_a Stripe::InvalidRequestError
@@ -227,6 +261,23 @@ shared_examples 'Charge API' do
227
261
  expect(updated.fraud_details.to_hash).to eq(charge.fraud_details.to_hash)
228
262
  end
229
263
 
264
+ it "updates a stripe charge with no changes" do
265
+ original = Stripe::Charge.create({
266
+ amount: 777,
267
+ currency: 'USD',
268
+ source: stripe_helper.generate_card_token,
269
+ description: 'Original description',
270
+ destination: {
271
+ account: "acct_SOMEBOGUSID",
272
+ amount: 150
273
+ }
274
+ })
275
+
276
+ expect {
277
+ updated = original.save
278
+ }.not_to raise_error
279
+ end
280
+
230
281
  it "marks a charge as safe" do
231
282
  original = Stripe::Charge.create({
232
283
  amount: 777,
@@ -310,7 +361,7 @@ shared_examples 'Charge API' do
310
361
  end
311
362
 
312
363
  it "stores all charges in memory" do
313
- expect(Stripe::Charge.all.data.map(&:id)).to eq([@charge.id, @charge2.id])
364
+ expect(Stripe::Charge.all.data.map(&:id).reverse).to eq([@charge.id, @charge2.id])
314
365
  end
315
366
 
316
367
  it "defaults count to 10 charges" do
@@ -425,29 +476,34 @@ shared_examples 'Charge API' do
425
476
 
426
477
  describe "idempotency" do
427
478
  let(:customer) { Stripe::Customer.create(email: 'johnny@appleseed.com') }
428
- let(:idempotent_charge_params) {{
479
+ let(:charge_params) {{
429
480
  amount: 777,
430
481
  currency: 'USD',
431
482
  customer: customer.id,
432
- capture: true,
483
+ capture: true
484
+ }}
485
+ let(:charge_headers) {{
433
486
  idempotency_key: 'onceisenough'
434
487
  }}
435
488
 
436
489
  it "returns the original charge if the same idempotency_key is passed in" do
437
- charge1 = Stripe::Charge.create(idempotent_charge_params)
438
- charge2 = Stripe::Charge.create(idempotent_charge_params)
490
+ charge1 = Stripe::Charge.create(charge_params, charge_headers)
491
+ charge2 = Stripe::Charge.create(charge_params, charge_headers)
439
492
 
440
493
  expect(charge1).to eq(charge2)
441
494
  end
442
495
 
443
- it "returns different charges if different idempotency_keys are used for each charge" do
444
- idempotent_charge_params2 = idempotent_charge_params.clone
445
- idempotent_charge_params2[:idempotency_key] = 'thisoneisdifferent'
496
+ context 'different key' do
497
+ let(:different_charge_headers) {{
498
+ idempotency_key: 'thisoneisdifferent'
499
+ }}
446
500
 
447
- charge1 = Stripe::Charge.create(idempotent_charge_params)
448
- charge2 = Stripe::Charge.create(idempotent_charge_params2)
501
+ it "returns different charges if different idempotency_keys are used for each charge" do
502
+ charge1 = Stripe::Charge.create(charge_params, charge_headers)
503
+ charge2 = Stripe::Charge.create(charge_params, different_charge_headers)
449
504
 
450
- expect(charge1).not_to eq(charge2)
505
+ expect(charge1).not_to eq(charge2)
506
+ end
451
507
  end
452
508
  end
453
509
 
@@ -27,6 +27,12 @@ shared_examples 'Coupon API' do
27
27
  expect(e.message).to match /duration/
28
28
  }
29
29
  end
30
+ it 'fails when a coupon is created without a currency when amount_off is specified' do
31
+ expect { Stripe::Coupon.create(id: '10OFF', duration: 'once', amount_off: 1000) }.to raise_error {|e|
32
+ expect(e).to be_a(Stripe::InvalidRequestError)
33
+ expect(e.message).to match /You must pass currency when passing amount_off/
34
+ }
35
+ end
30
36
  end
31
37
 
32
38
  context 'retrieve coupon', live: true do
@@ -56,7 +62,7 @@ shared_examples 'Coupon API' do
56
62
  it "cannot retrieve a stripe coupon that doesn't exist" do
57
63
  expect { Stripe::Coupon.retrieve('nope') }.to raise_error {|e|
58
64
  expect(e).to be_a Stripe::InvalidRequestError
59
- expect(e.param).to eq('id')
65
+ expect(e.param).to eq('coupon')
60
66
  expect(e.http_status).to eq(404)
61
67
  }
62
68
  end
@@ -71,7 +77,7 @@ shared_examples 'Coupon API' do
71
77
 
72
78
  expect { Stripe::Coupon.retrieve(coupon.id) }.to raise_error {|e|
73
79
  expect(e).to be_a Stripe::InvalidRequestError
74
- expect(e.param).to eq('id')
80
+ expect(e.param).to eq('coupon')
75
81
  expect(e.http_status).to eq(404)
76
82
  }
77
83
  end
@@ -78,6 +78,15 @@ shared_examples 'Customer API' do
78
78
  expect(customer.sources.data.first.exp_year).to eq 2024
79
79
  end
80
80
 
81
+ it 'creates a customer with name' do
82
+ customer = Stripe::Customer.create(
83
+ source: gen_card_tk,
84
+ name: 'John Appleseed'
85
+ )
86
+ expect(customer.id).to match(/^test_cus/)
87
+ expect(customer.name).to eq('John Appleseed')
88
+ end
89
+
81
90
  it 'creates a customer with a plan' do
82
91
  plan = stripe_helper.create_plan(id: 'silver')
83
92
  customer = Stripe::Customer.create(id: 'test_cus_plan', source: gen_card_tk, :plan => 'silver')
@@ -215,6 +224,21 @@ shared_examples 'Customer API' do
215
224
  customer = Stripe::Customer.retrieve('test_cus_coupon')
216
225
  expect(customer.discount).to_not be_nil
217
226
  expect(customer.discount.coupon).to_not be_nil
227
+ expect(customer.discount.customer).to eq customer.id
228
+ expect(customer.discount.start).to be_within(1).of Time.now.to_i
229
+ end
230
+
231
+ describe 'repeating coupon with duration limit', live: true do
232
+ let!(:coupon) { stripe_helper.create_coupon(id: '10OFF', amount_off: 1000, currency: 'usd', duration: 'repeating', duration_in_months: 12) }
233
+ let!(:customer) { Stripe::Customer.create(coupon: coupon.id) }
234
+ it 'creates the discount with the end date', live: true do
235
+ discount = Stripe::Customer.retrieve(customer.id).discount
236
+ expect(discount).to_not be_nil
237
+ 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
239
+ end
240
+ after { Stripe::Coupon.retrieve(coupon.id).delete }
241
+ after { Stripe::Customer.retrieve(customer.id).delete }
218
242
  end
219
243
 
220
244
  it 'cannot create a customer with a coupon that does not exist' do
@@ -252,11 +276,25 @@ shared_examples 'Customer API' do
252
276
 
253
277
  expect(customer.id).to eq(original.id)
254
278
  expect(customer.email).to eq(original.email)
279
+ expect(customer.name).to eq(nil)
255
280
  expect(customer.default_source).to eq(original.default_source)
281
+ expect(customer.default_source).not_to be_a(Stripe::Card)
256
282
  expect(customer.subscriptions.count).to eq(0)
257
283
  expect(customer.subscriptions.data).to be_empty
258
284
  end
259
285
 
286
+ it "can expand default_source" do
287
+ original = Stripe::Customer.create({
288
+ email: 'johnny@appleseed.com',
289
+ source: gen_card_tk
290
+ })
291
+ customer = Stripe::Customer.retrieve(
292
+ id: original.id,
293
+ expand: ['default_source']
294
+ )
295
+ expect(customer.default_source).to be_a(Stripe::Card)
296
+ end
297
+
260
298
  it "cannot retrieve a customer that doesn't exist" do
261
299
  expect { Stripe::Customer.retrieve('nope') }.to raise_error {|e|
262
300
  expect(e).to be_a Stripe::InvalidRequestError
@@ -378,7 +416,7 @@ shared_examples 'Customer API' do
378
416
  customer = customer.delete
379
417
  expect(customer.deleted).to eq(true)
380
418
  end
381
-
419
+
382
420
  it 'works with the update_subscription method' do
383
421
  stripe_helper.create_plan(id: 'silver')
384
422
  cus = Stripe::Customer.create(source: gen_card_tk)
@@ -386,5 +424,19 @@ shared_examples 'Customer API' do
386
424
  cus.update_subscription(plan: 'silver')
387
425
  }.not_to raise_error
388
426
  end
389
-
427
+
428
+ it "deletes a stripe customer discount" do
429
+ original = Stripe::Customer.create(id: 'test_customer_update')
430
+
431
+ coupon = Stripe::Coupon.create(id: "10PERCENT", duration: 'once')
432
+ original.coupon = coupon.id
433
+ original.save
434
+
435
+ expect(original.discount.coupon).to be_a Stripe::Coupon
436
+
437
+ original.delete_discount
438
+
439
+ customer = Stripe::Customer.retrieve("test_customer_update")
440
+ expect(customer.discount).to be nil
441
+ end
390
442
  end
@@ -3,6 +3,8 @@ require 'pp'
3
3
 
4
4
  shared_examples 'Dispute API' do
5
5
 
6
+ let(:stripe_helper) { StripeMock.create_test_helper }
7
+
6
8
  it "returns an error if dispute does not exist" do
7
9
  dispute_id = 'dp_xxxxxxxxxxxxxxxxxxxxxxxx'
8
10
 
@@ -18,7 +20,7 @@ shared_examples 'Dispute API' do
18
20
  dispute_id = 'dp_05RsQX2eZvKYlo2C0FRTGSSA'
19
21
  dispute = Stripe::Dispute.retrieve(dispute_id)
20
22
 
21
- expect(dispute).to be_a(Stripe::Dispute)
23
+ expect(dispute).to be_a(Stripe::Dispute)
22
24
  expect(dispute.id).to eq(dispute_id)
23
25
  end
24
26
 
@@ -36,7 +38,7 @@ shared_examples 'Dispute API' do
36
38
  :customer_name => 'Rebel Idealist',
37
39
  :product_description => 'Lorem ipsum dolor sit amet.',
38
40
  :shipping_documentation => 'fil_15BZxW2eZvKYlo2CvQbrn9dc',
39
- }
41
+ }
40
42
  dispute.save
41
43
 
42
44
  dispute = Stripe::Dispute.retrieve(dispute_id)
@@ -50,13 +52,13 @@ shared_examples 'Dispute API' do
50
52
 
51
53
  it "closes a dispute" do
52
54
  dispute_id = 'dp_75RsQX2eZvKYlo2C0EDCXSWQ'
53
-
55
+
54
56
  dispute = Stripe::Dispute.retrieve(dispute_id)
55
-
57
+
56
58
  expect(dispute).to be_a(Stripe::Dispute)
57
59
  expect(dispute.id).to eq(dispute_id)
58
60
  expect(dispute.status).to eq('under_review')
59
-
61
+
60
62
  dispute.close
61
63
 
62
64
  dispute = Stripe::Dispute.retrieve(dispute_id)
@@ -67,7 +69,7 @@ shared_examples 'Dispute API' do
67
69
  end
68
70
 
69
71
  describe "listing disputes" do
70
-
72
+
71
73
  it "retrieves all disputes" do
72
74
  disputes = Stripe::Dispute.all
73
75
 
@@ -79,9 +81,18 @@ shared_examples 'Dispute API' do
79
81
  disputes = Stripe::Dispute.all(limit: 3)
80
82
 
81
83
  expect(disputes.count).to eq(3)
82
- expect(disputes.map &:id).to include('dp_05RsQX2eZvKYlo2C0FRTGSSA','dp_15RsQX2eZvKYlo2C0ERTYUIA', 'dp_25RsQX2eZvKYlo2C0ZXCVBNM')
84
+ expected = ['dp_95RsQX2eZvKYlo2C0EDFRYUI','dp_85RsQX2eZvKYlo2C0UJMCDET', 'dp_75RsQX2eZvKYlo2C0EDCXSWQ']
85
+ expect(disputes.map &:id).to include(*expected)
83
86
  end
84
87
 
85
88
  end
86
-
89
+
90
+ it "creates a dispute" do
91
+ card_token = stripe_helper.generate_card_token(last4: "1123", exp_month: 11, exp_year: 2099)
92
+ charge = Stripe::Charge.create(amount: 1000, currency: "usd", source: card_token)
93
+ stripe_dispute_id = stripe_helper.upsert_stripe_object(:dispute, {amount: charge.amount, charge: charge.id})
94
+ stripe_dispute = Stripe::Dispute.retrieve(stripe_dispute_id)
95
+ expect(stripe_dispute.charge).to eq(charge.id)
96
+ end
97
+
87
98
  end
@@ -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
@@ -12,7 +12,7 @@ end
12
12
  shared_examples 'Stripe Error Mocking' do
13
13
 
14
14
  it "mocks a manually given stripe card error" do
15
- error = Stripe::CardError.new('Test Msg', 'param_name', 'bad_code', 444, 'body', 'json body')
15
+ error = Stripe::CardError.new('Test Msg', 'param_name', 'bad_code', http_status: 444, http_body: 'body', json_body: 'json body')
16
16
  StripeMock.prepare_error(error)
17
17
 
18
18
  expect { Stripe::Customer.create() }.to raise_error {|e|
@@ -30,7 +30,7 @@ shared_examples 'Stripe Error Mocking' do
30
30
 
31
31
  it "mocks a manually gives stripe invalid request error" do
32
32
 
33
- error = Stripe::InvalidRequestError.new('Test Invalid', 'param', 987, 'ibody', 'json ibody')
33
+ error = Stripe::InvalidRequestError.new('Test Invalid', 'param', http_status: 987, http_body: 'ibody', json_body: 'json ibody')
34
34
  StripeMock.prepare_error(error)
35
35
 
36
36
  expect { Stripe::Charge.create(amount: 1, currency: 'usd') }.to raise_error {|e|
@@ -46,7 +46,7 @@ shared_examples 'Stripe Error Mocking' do
46
46
 
47
47
 
48
48
  it "mocks a manually gives stripe invalid auth error" do
49
- error = Stripe::AuthenticationError.new('Bad Auth', 499, 'abody', 'json abody')
49
+ error = Stripe::AuthenticationError.new('Bad Auth', http_status: 499, http_body: 'abody', json_body: 'json abody')
50
50
  StripeMock.prepare_error(error)
51
51
 
52
52
  expect { stripe_helper.create_plan() }.to raise_error {|e|
@@ -0,0 +1,170 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples 'External Account API' do
4
+
5
+ it 'creates/returns a bank when using account.external_accounts.create given a bank token' do
6
+ account = Stripe::Account.create(id: 'test_account', type: 'custom', country: "US")
7
+ bank_token = stripe_helper.generate_bank_token(last4: "1123", exp_month: 11, exp_year: 2099)
8
+ bank = account.external_accounts.create(external_account: bank_token)
9
+
10
+ expect(bank.account).to eq('test_account')
11
+ expect(bank.last4).to eq("1123")
12
+ expect(bank.exp_month).to eq(11)
13
+ expect(bank.exp_year).to eq(2099)
14
+
15
+ account = Stripe::Account.retrieve('test_account')
16
+ expect(account.external_accounts.count).to eq(1)
17
+ bank = account.external_accounts.first
18
+ expect(bank.account).to eq('test_account')
19
+ expect(bank.last4).to eq("1123")
20
+ expect(bank.exp_month).to eq(11)
21
+ expect(bank.exp_year).to eq(2099)
22
+ end
23
+
24
+ it 'creates/returns a bank when using account.external_accounts.create given bank params' do
25
+ account = Stripe::Account.create(id: 'test_account', type: 'custom', country: "US")
26
+ bank = account.external_accounts.create(external_account: {
27
+ object: 'bank_account',
28
+ account_number: '000123456789',
29
+ routing_number: '110000000',
30
+ country: 'US',
31
+ currency: 'usd'
32
+ })
33
+
34
+ expect(bank.account).to eq('test_account')
35
+ expect(bank.routing_number).to eq('110000000')
36
+ expect(bank.country).to eq('US')
37
+ expect(bank.currency).to eq('usd')
38
+
39
+ account = Stripe::Account.retrieve('test_account')
40
+ expect(account.external_accounts.count).to eq(1)
41
+ bank = account.external_accounts.first
42
+ expect(bank.account).to eq('test_account')
43
+ expect(bank.routing_number).to eq('110000000')
44
+ expect(bank.country).to eq('US')
45
+ expect(bank.currency).to eq('usd')
46
+ end
47
+
48
+ it "creates a single bank with a generated bank token" do
49
+ account = Stripe::Account.create(type: 'custom', country: "US")
50
+ expect(account.external_accounts.count).to eq 0
51
+
52
+ account.external_accounts.create external_account: stripe_helper.generate_bank_token
53
+ # Yes, stripe-ruby does not actually add the new bank to the account instance
54
+ expect(account.external_accounts.count).to eq 0
55
+
56
+ account2 = Stripe::Account.retrieve(account.id)
57
+ expect(account2.external_accounts.count).to eq 1
58
+ end
59
+
60
+ describe "retrieval and deletion with accounts" do
61
+ let!(:account) { Stripe::Account.create(id: 'test_account', type: 'custom', country: "US") }
62
+ let!(:bank_token) { stripe_helper.generate_bank_token(last4: "1123", exp_month: 11, exp_year: 2099) }
63
+ let!(:bank) { account.external_accounts.create(external_account: bank_token) }
64
+
65
+ it "can retrieve all account's banks" do
66
+ retrieved = account.external_accounts.all
67
+ expect(retrieved.count).to eq(1)
68
+ end
69
+
70
+ it "retrieves an account bank" do
71
+ retrieved = account.external_accounts.retrieve(bank.id)
72
+ expect(retrieved.to_s).to eq(bank.to_s)
73
+ end
74
+
75
+ it "retrieves an account's bank after re-fetching the account" do
76
+ retrieved = Stripe::Account.retrieve(account.id).external_accounts.retrieve(bank.id)
77
+ expect(retrieved.id).to eq bank.id
78
+ end
79
+
80
+ it "deletes an accounts bank" do
81
+ bank.delete
82
+ retrieved_acct = Stripe::Account.retrieve(account.id)
83
+ expect(retrieved_acct.external_accounts.data).to be_empty
84
+ end
85
+
86
+ context "deletion when the user has two external accounts" do
87
+ let!(:bank_token_2) { stripe_helper.generate_bank_token(last4: "1123", exp_month: 11, exp_year: 2099) }
88
+ let!(:bank_2) { account.external_accounts.create(external_account: bank_token_2) }
89
+
90
+ it "has just one bank anymore" do
91
+ bank.delete
92
+ retrieved_acct = Stripe::Account.retrieve(account.id)
93
+ expect(retrieved_acct.external_accounts.data.count).to eq 1
94
+ expect(retrieved_acct.external_accounts.data.first.id).to eq bank_2.id
95
+ end
96
+ end
97
+ end
98
+
99
+ describe "Errors" do
100
+ it "throws an error when the account does not have the retrieving bank id" do
101
+ account = Stripe::Account.create(type: 'custom', country: "US")
102
+ bank_id = "bank_123"
103
+ expect { account.external_accounts.retrieve(bank_id) }.to raise_error {|e|
104
+ expect(e).to be_a Stripe::InvalidRequestError
105
+ expect(e.message).to match /no.*source/i
106
+ expect(e.message).to include bank_id
107
+ expect(e.param).to eq 'id'
108
+ expect(e.http_status).to eq 404
109
+ }
110
+ end
111
+ end
112
+
113
+ context "update bank" do
114
+ let!(:account) { Stripe::Account.create(id: 'test_account', type: 'custom', country: "US") }
115
+ let!(:bank_token) { stripe_helper.generate_bank_token(last4: "1123", exp_month: 11, exp_year: 2099) }
116
+ let!(:bank) { account.external_accounts.create(external_account: bank_token) }
117
+
118
+ it "updates the bank" do
119
+ exp_month = 10
120
+ exp_year = 2098
121
+
122
+ bank.exp_month = exp_month
123
+ bank.exp_year = exp_year
124
+ bank.save
125
+
126
+ retrieved = account.external_accounts.retrieve(bank.id)
127
+
128
+ expect(retrieved.exp_month).to eq(exp_month)
129
+ expect(retrieved.exp_year).to eq(exp_year)
130
+ end
131
+ end
132
+
133
+ context "retrieve multiple banks" do
134
+
135
+ it "retrieves a list of multiple banks" do
136
+ account = Stripe::Account.create(id: 'test_account', type: 'custom', country: "US")
137
+
138
+ bank_token = stripe_helper.generate_bank_token(last4: "1123", exp_month: 11, exp_year: 2099)
139
+ bank1 = account.external_accounts.create(external_accout: bank_token)
140
+ bank_token = stripe_helper.generate_bank_token(last4: "1124", exp_month: 12, exp_year: 2098)
141
+ bank2 = account.external_accounts.create(external_account: bank_token)
142
+
143
+ account = Stripe::Account.retrieve('test_account')
144
+
145
+ list = account.external_accounts.all
146
+
147
+ expect(list.object).to eq("list")
148
+ expect(list.count).to eq(2)
149
+ expect(list.data.length).to eq(2)
150
+
151
+ expect(list.data.first.object).to eq("bank_account")
152
+ expect(list.data.first.to_hash).to eq(bank1.to_hash)
153
+
154
+ expect(list.data.last.object).to eq("bank_account")
155
+ expect(list.data.last.to_hash).to eq(bank2.to_hash)
156
+ end
157
+
158
+ it "retrieves an empty list if there's no subscriptions" do
159
+ Stripe::Account.create(id: 'no_banks', type: 'custom', country: "US")
160
+ account = Stripe::Account.retrieve('no_banks')
161
+
162
+ list = account.external_accounts.all
163
+
164
+ expect(list.object).to eq("list")
165
+ expect(list.count).to eq(0)
166
+ expect(list.data.length).to eq(0)
167
+ end
168
+ end
169
+
170
+ end
@@ -8,6 +8,7 @@ shared_examples 'Extra Features' do
8
8
 
9
9
  customer = Stripe::Customer.create
10
10
  expect(customer.id).to match /^custom_prefix_cus/
11
+ StripeMock.global_id_prefix = nil
11
12
  end
12
13
 
13
14
  it "can set the global id prefix to nothing" do
@@ -23,6 +24,7 @@ shared_examples 'Extra Features' do
23
24
 
24
25
  customer = Stripe::Customer.create
25
26
  expect(customer.id).to match /^cus/
27
+ StripeMock.global_id_prefix = nil
26
28
  end
27
29
 
28
30
  it "has a default global id prefix" do