stripe-ruby-mock 2.3.1 → 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 (106) 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 -13
  9. data/lib/stripe_mock/api/instance.rb +16 -6
  10. data/lib/stripe_mock/api/webhooks.rb +8 -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 +398 -58
  14. data/lib/stripe_mock/instance.rb +105 -9
  15. data/lib/stripe_mock/request_handlers/accounts.rb +41 -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 +44 -33
  19. data/lib/stripe_mock/request_handlers/country_spec.rb +22 -0
  20. data/lib/stripe_mock/request_handlers/coupons.rb +5 -4
  21. data/lib/stripe_mock/request_handlers/customers.rb +29 -11
  22. data/lib/stripe_mock/request_handlers/ephemeral_key.rb +13 -0
  23. data/lib/stripe_mock/request_handlers/external_accounts.rb +55 -0
  24. data/lib/stripe_mock/request_handlers/helpers/bank_account_helpers.rb +1 -1
  25. data/lib/stripe_mock/request_handlers/helpers/card_helpers.rb +9 -7
  26. data/lib/stripe_mock/request_handlers/helpers/coupon_helpers.rb +10 -6
  27. data/lib/stripe_mock/request_handlers/helpers/external_account_helpers.rb +49 -0
  28. data/lib/stripe_mock/request_handlers/helpers/subscription_helpers.rb +59 -16
  29. data/lib/stripe_mock/request_handlers/helpers/token_helpers.rb +3 -2
  30. data/lib/stripe_mock/request_handlers/invoices.rb +93 -14
  31. data/lib/stripe_mock/request_handlers/orders.rb +5 -5
  32. data/lib/stripe_mock/request_handlers/payouts.rb +32 -0
  33. data/lib/stripe_mock/request_handlers/plans.rb +1 -0
  34. data/lib/stripe_mock/request_handlers/products.rb +43 -0
  35. data/lib/stripe_mock/request_handlers/recipients.rb +12 -0
  36. data/lib/stripe_mock/request_handlers/refunds.rb +91 -0
  37. data/lib/stripe_mock/request_handlers/subscription_items.rb +36 -0
  38. data/lib/stripe_mock/request_handlers/subscriptions.rb +101 -39
  39. data/lib/stripe_mock/request_handlers/tax_rates.rb +36 -0
  40. data/lib/stripe_mock/request_handlers/tokens.rb +9 -3
  41. data/lib/stripe_mock/request_handlers/transfers.rb +11 -5
  42. data/lib/stripe_mock/request_handlers/validators/param_validators.rb +15 -1
  43. data/lib/stripe_mock/server.rb +14 -1
  44. data/lib/stripe_mock/test_strategies/base.rb +10 -5
  45. data/lib/stripe_mock/test_strategies/live.rb +5 -0
  46. data/lib/stripe_mock/test_strategies/mock.rb +8 -0
  47. data/lib/stripe_mock/util.rb +8 -2
  48. data/lib/stripe_mock/version.rb +1 -1
  49. data/lib/stripe_mock/webhook_fixtures/account.external_account.created.json +27 -0
  50. data/lib/stripe_mock/webhook_fixtures/account.external_account.deleted.json +27 -0
  51. data/lib/stripe_mock/webhook_fixtures/account.external_account.updated.json +27 -0
  52. data/lib/stripe_mock/webhook_fixtures/account.updated.json +1 -1
  53. data/lib/stripe_mock/webhook_fixtures/charge.dispute.funds_reinstated.json +88 -0
  54. data/lib/stripe_mock/webhook_fixtures/charge.dispute.funds_withdrawn.json +88 -0
  55. data/lib/stripe_mock/webhook_fixtures/charge.updated.json +58 -0
  56. data/lib/stripe_mock/webhook_fixtures/customer.subscription.created.json +40 -10
  57. data/lib/stripe_mock/webhook_fixtures/customer.subscription.deleted.json +39 -10
  58. data/lib/stripe_mock/webhook_fixtures/customer.subscription.trial_will_end.json +39 -10
  59. data/lib/stripe_mock/webhook_fixtures/customer.subscription.updated.json +40 -11
  60. data/lib/stripe_mock/webhook_fixtures/invoice.created.json +3 -2
  61. data/lib/stripe_mock/webhook_fixtures/invoice.payment_failed.json +1 -1
  62. data/lib/stripe_mock/webhook_fixtures/invoice.payment_succeeded.json +92 -85
  63. data/lib/stripe_mock/webhook_fixtures/invoice.updated.json +3 -2
  64. data/lib/stripe_mock/webhook_fixtures/plan.created.json +1 -1
  65. data/lib/stripe_mock/webhook_fixtures/plan.deleted.json +1 -1
  66. data/lib/stripe_mock/webhook_fixtures/plan.updated.json +1 -1
  67. data/lib/stripe_mock.rb +15 -0
  68. data/spec/api/instance_spec.rb +30 -0
  69. data/spec/instance_spec.rb +54 -4
  70. data/spec/integration_examples/prepare_error_examples.rb +6 -6
  71. data/spec/list_spec.rb +27 -10
  72. data/spec/readme_spec.rb +2 -0
  73. data/spec/server_spec.rb +7 -3
  74. data/spec/shared_stripe_examples/account_examples.rb +46 -0
  75. data/spec/shared_stripe_examples/balance_examples.rb +11 -0
  76. data/spec/shared_stripe_examples/balance_transaction_examples.rb +28 -0
  77. data/spec/shared_stripe_examples/bank_examples.rb +28 -1
  78. data/spec/shared_stripe_examples/card_examples.rb +23 -5
  79. data/spec/shared_stripe_examples/card_token_examples.rb +1 -0
  80. data/spec/shared_stripe_examples/charge_examples.rb +131 -26
  81. data/spec/shared_stripe_examples/country_specs_examples.rb +18 -0
  82. data/spec/shared_stripe_examples/coupon_examples.rb +8 -2
  83. data/spec/shared_stripe_examples/customer_examples.rb +90 -0
  84. data/spec/shared_stripe_examples/dispute_examples.rb +19 -8
  85. data/spec/shared_stripe_examples/ephemeral_key_examples.rb +17 -0
  86. data/spec/shared_stripe_examples/error_mock_examples.rb +15 -5
  87. data/spec/shared_stripe_examples/external_account_examples.rb +170 -0
  88. data/spec/shared_stripe_examples/extra_features_examples.rb +2 -0
  89. data/spec/shared_stripe_examples/invoice_examples.rb +314 -51
  90. data/spec/shared_stripe_examples/payout_examples.rb +68 -0
  91. data/spec/shared_stripe_examples/plan_examples.rb +47 -4
  92. data/spec/shared_stripe_examples/product_example.rb +65 -0
  93. data/spec/shared_stripe_examples/recipient_examples.rb +13 -7
  94. data/spec/shared_stripe_examples/refund_examples.rb +453 -84
  95. data/spec/shared_stripe_examples/subscription_examples.rb +477 -32
  96. data/spec/shared_stripe_examples/subscription_items_examples.rb +75 -0
  97. data/spec/shared_stripe_examples/tax_rate_examples.rb +42 -0
  98. data/spec/shared_stripe_examples/transfer_examples.rb +72 -23
  99. data/spec/shared_stripe_examples/webhook_event_examples.rb +74 -5
  100. data/spec/spec_helper.rb +7 -6
  101. data/spec/stripe_mock_spec.rb +16 -3
  102. data/spec/support/stripe_examples.rb +8 -1
  103. data/spec/util_spec.rb +35 -1
  104. data/stripe-ruby-mock.gemspec +1 -1
  105. metadata +44 -8
  106. data/ChangeLog.rdoc +0 -4
@@ -2,6 +2,8 @@ require 'spec_helper'
2
2
 
3
3
  shared_examples 'Balance Transaction API' do
4
4
 
5
+ let(:stripe_helper) { StripeMock.create_test_helper }
6
+
5
7
  it "returns an error if balance transaction does not exist" do
6
8
  txn_id = 'txn_xxxxxxxxxxxxxxxxxxxxxxxx'
7
9
 
@@ -32,4 +34,30 @@ shared_examples 'Balance Transaction API' do
32
34
 
33
35
  end
34
36
 
37
+ it 'retrieves balance transactions for an automated transfer' do
38
+ transfer_id = Stripe::Transfer.create({ amount: 2730, currency: "usd" })
39
+
40
+ # verify transfer currently has no balance transactions
41
+ transfer_transactions = Stripe::BalanceTransaction.all({transfer: transfer_id})
42
+ expect(transfer_transactions.count).to eq(0)
43
+
44
+ # verify we can create a new balance transaction associated with the transfer
45
+ new_txn_id = stripe_helper.upsert_stripe_object(:balance_transaction, {amount: 12300, transfer: transfer_id})
46
+ new_txn = Stripe::BalanceTransaction.retrieve(new_txn_id)
47
+ expect(new_txn).to be_a(Stripe::BalanceTransaction)
48
+ expect(new_txn.amount).to eq(12300)
49
+ # although transfer was specified as an attribute on the balance_transaction, it should not be returned in the object
50
+ expect{new_txn.transfer}.to raise_error(NoMethodError)
51
+
52
+ # verify we can update an existing balance transaction to associate with the transfer
53
+ existing_txn_id = 'txn_05RsQX2eZvKYlo2C0FRTGSSA'
54
+ existing_txn = Stripe::BalanceTransaction.retrieve(existing_txn_id)
55
+ stripe_helper.upsert_stripe_object(:balance_transaction, {id: existing_txn_id, transfer: transfer_id})
56
+
57
+ # now verify that only these balance transactions are retrieved with the transfer
58
+ transfer_transactions = Stripe::BalanceTransaction.all({transfer: transfer_id})
59
+ expect(transfer_transactions.count).to eq(2)
60
+ expect(transfer_transactions.map &:id).to include(new_txn_id, existing_txn_id)
61
+ end
62
+
35
63
  end
@@ -40,7 +40,7 @@ shared_examples 'Bank API' do
40
40
  expect(bank.last4).to eq("6789")
41
41
  end
42
42
 
43
- it "creates a single bank with a generated bank token", :live => true do
43
+ it "creates a single bank with a generated bank token" do
44
44
  customer = Stripe::Customer.create
45
45
  expect(customer.sources.count).to eq 0
46
46
 
@@ -199,4 +199,31 @@ shared_examples 'Bank API' do
199
199
  end
200
200
  end
201
201
 
202
+ describe 'Stripe::Token creation from bank account' do
203
+ it 'generates token from bank account informations' do
204
+ token = Stripe::Token.create({
205
+ bank_account: {
206
+ account_number: "4222222222222222",
207
+ routing_number: "123456",
208
+ bank_name: "Fake bank"
209
+ }
210
+ })
211
+
212
+ cus = Stripe::Customer.create(source: token.id)
213
+ bank_account = cus.sources.data.first
214
+ expect(bank_account.bank_name).to eq('Fake bank')
215
+ end
216
+
217
+ it 'generates token from existing bank account token' do
218
+ bank_token = StripeMock.generate_bank_token(bank_name: 'Fake bank')
219
+ cus = Stripe::Customer.create(source: bank_token)
220
+ token = Stripe::Token.create({ customer: cus.id, bank_account: cus.sources.first.id })
221
+ cus.sources.create(source: token.id)
222
+ cus = Stripe::Customer.retrieve(cus.id)
223
+ expect(cus.sources.data.count).to eq 2
224
+ cus.sources.data.each do |source|
225
+ expect(source.bank_name).to eq('Fake bank')
226
+ end
227
+ end
228
+ end
202
229
  end
@@ -21,8 +21,14 @@ 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
25
- recipient = Stripe::Recipient.create(id: 'test_recipient_sub')
24
+ it 'creates/returns a card when using recipient.cards.create given a card token', skip: 'Stripe has deprecated Recipients' do
25
+ params = {
26
+ id: 'test_recipient_sub',
27
+ name: 'MyRec',
28
+ type: 'individual'
29
+ }
30
+
31
+ recipient = Stripe::Recipient.create(params)
26
32
  card_token = stripe_helper.generate_card_token(last4: "1123", exp_month: 11, exp_year: 2099)
27
33
  card = recipient.cards.create(card: card_token)
28
34
 
@@ -63,8 +69,13 @@ shared_examples 'Card API' do
63
69
  expect(card.exp_year).to eq(3031)
64
70
  end
65
71
 
66
- it 'creates/returns a card when using recipient.cards.create given card params' do
67
- recipient = Stripe::Recipient.create(id: 'test_recipient_sub')
72
+ it 'creates/returns a card when using recipient.cards.create given card params', skip: 'Stripe has deprecated Recipients' do
73
+ params = {
74
+ id: 'test_recipient_sub',
75
+ name: 'MyRec',
76
+ type: 'individual'
77
+ }
78
+ recipient = Stripe::Recipient.create(params)
68
79
  card = recipient.cards.create(card: {
69
80
  number: '4000056655665556',
70
81
  exp_month: '11',
@@ -155,6 +166,13 @@ shared_examples 'Card API' do
155
166
  expect(retrieved_cus.default_source).to be_nil
156
167
  end
157
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
+
158
176
  context "deletion when the user has two cards" do
159
177
  let!(:card_token_2) { stripe_helper.generate_card_token(last4: "1123", exp_month: 11, exp_year: 2099) }
160
178
  let!(:card_2) { customer.sources.create(source: card_token_2) }
@@ -174,7 +192,7 @@ shared_examples 'Card API' do
174
192
  end
175
193
  end
176
194
 
177
- describe "retrieval and deletion with recipients", :live => true do
195
+ describe "retrieval and deletion with recipients", :live => true, skip: 'Stripe has deprecated Recipients' do
178
196
  let!(:recipient) { Stripe::Recipient.create(name: 'Test Recipient', type: 'individual') }
179
197
  let!(:card_token) { stripe_helper.generate_card_token(number: "4000056655665556") }
180
198
  let!(:card) { recipient.cards.create(card: card_token) }
@@ -12,6 +12,7 @@ shared_examples 'Card Token Mocking' do
12
12
  expect(card.last4).to eq("4242")
13
13
  expect(card.exp_month).to eq(4)
14
14
  expect(card.exp_year).to eq(2016)
15
+ expect(card.tokenization_method).to eq(nil)
15
16
  end
16
17
 
17
18
  it "generates and reads a card token for create charge" do
@@ -12,11 +12,20 @@ shared_examples 'Charge API' do
12
12
  }.to raise_error(Stripe::InvalidRequestError, /token/i)
13
13
  end
14
14
 
15
+ it "requires a valid customer or source", :live => true do
16
+ expect {
17
+ charge = Stripe::Charge.create(
18
+ amount: 99,
19
+ currency: 'usd',
20
+ )
21
+ }.to raise_error(Stripe::InvalidRequestError, /Must provide source or customer/i)
22
+ end
23
+
15
24
  it "requires presence of amount", :live => true do
16
25
  expect {
17
26
  charge = Stripe::Charge.create(
18
27
  currency: 'usd',
19
- card: stripe_helper.generate_card_token
28
+ source: stripe_helper.generate_card_token
20
29
  )
21
30
  }.to raise_error(Stripe::InvalidRequestError, /missing required param: amount/i)
22
31
  end
@@ -25,7 +34,7 @@ shared_examples 'Charge API' do
25
34
  expect {
26
35
  charge = Stripe::Charge.create(
27
36
  amount: 99,
28
- card: stripe_helper.generate_card_token
37
+ source: stripe_helper.generate_card_token
29
38
  )
30
39
  }.to raise_error(Stripe::InvalidRequestError, /missing required param: currency/i)
31
40
  end
@@ -35,7 +44,7 @@ shared_examples 'Charge API' do
35
44
  charge = Stripe::Charge.create(
36
45
  amount: -99,
37
46
  currency: 'usd',
38
- card: stripe_helper.generate_card_token
47
+ source: stripe_helper.generate_card_token
39
48
  )
40
49
  }.to raise_error(Stripe::InvalidRequestError, /invalid positive integer/i)
41
50
  end
@@ -45,7 +54,7 @@ shared_examples 'Charge API' do
45
54
  charge = Stripe::Charge.create(
46
55
  amount: 99.0,
47
56
  currency: 'usd',
48
- card: stripe_helper.generate_card_token
57
+ source: stripe_helper.generate_card_token
49
58
  )
50
59
  }.to raise_error(Stripe::InvalidRequestError, /invalid integer/i)
51
60
  end
@@ -65,6 +74,21 @@ shared_examples 'Charge API' do
65
74
  expect(charge.status).to eq('succeeded')
66
75
  end
67
76
 
77
+ it "creates a stripe charge item with a bank token" do
78
+ charge = Stripe::Charge.create(
79
+ amount: 999,
80
+ currency: 'USD',
81
+ source: stripe_helper.generate_bank_token,
82
+ description: 'bank charge'
83
+ )
84
+
85
+ expect(charge.id).to match(/^test_ch/)
86
+ expect(charge.amount).to eq(999)
87
+ expect(charge.description).to eq('bank charge')
88
+ expect(charge.captured).to eq(true)
89
+ expect(charge.status).to eq('succeeded')
90
+ end
91
+
68
92
  it 'creates a stripe charge item with a customer', :live => true do
69
93
  customer = Stripe::Customer.create({
70
94
  email: 'johnny@appleseed.com',
@@ -138,6 +162,48 @@ shared_examples 'Charge API' do
138
162
  expect(data[charge2.id][:amount]).to eq(777)
139
163
  end
140
164
 
165
+ it "creates a balance transaction" do
166
+ amount = 300
167
+ fee = 10
168
+ charge = Stripe::Charge.create({
169
+ amount: amount,
170
+ currency: 'USD',
171
+ source: stripe_helper.generate_card_token,
172
+ application_fee: fee,
173
+ })
174
+ bal_trans = Stripe::BalanceTransaction.retrieve(charge.balance_transaction)
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)
179
+ end
180
+
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
198
+ charge = Stripe::Charge.create({
199
+ amount: 300,
200
+ currency: 'USD',
201
+ source: stripe_helper.generate_card_token,
202
+ expand: ['balance_transaction']
203
+ })
204
+ expect(charge.balance_transaction).to be_a(Stripe::BalanceTransaction)
205
+ end
206
+
141
207
  it "retrieves a stripe charge" do
142
208
  original = Stripe::Charge.create({
143
209
  amount: 777,
@@ -150,6 +216,20 @@ shared_examples 'Charge API' do
150
216
  expect(charge.amount).to eq(original.amount)
151
217
  end
152
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
+
153
233
  it "cannot retrieve a charge that doesn't exist" do
154
234
  expect { Stripe::Charge.retrieve('nope') }.to raise_error {|e|
155
235
  expect(e).to be_a Stripe::InvalidRequestError
@@ -181,6 +261,23 @@ shared_examples 'Charge API' do
181
261
  expect(updated.fraud_details.to_hash).to eq(charge.fraud_details.to_hash)
182
262
  end
183
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
+
184
281
  it "marks a charge as safe" do
185
282
  original = Stripe::Charge.create({
186
283
  amount: 777,
@@ -237,14 +334,14 @@ shared_examples 'Charge API' do
237
334
  charge1 = Stripe::Charge.create(
238
335
  amount: 999,
239
336
  currency: 'USD',
240
- card: stripe_helper.generate_card_token,
337
+ source: stripe_helper.generate_card_token,
241
338
  description: 'card charge'
242
339
  )
243
340
 
244
341
  charge2 = Stripe::Charge.create(
245
342
  amount: 999,
246
343
  currency: 'USD',
247
- card: stripe_helper.generate_card_token,
344
+ source: stripe_helper.generate_card_token,
248
345
  description: 'card charge'
249
346
  )
250
347
 
@@ -254,8 +351,9 @@ shared_examples 'Charge API' do
254
351
  context "retrieving a list of charges" do
255
352
  before do
256
353
  @customer = Stripe::Customer.create(email: 'johnny@appleseed.com')
354
+ @customer2 = Stripe::Customer.create(email: 'johnny2@appleseed.com')
257
355
  @charge = Stripe::Charge.create(amount: 1, currency: 'usd', customer: @customer.id)
258
- @charge2 = Stripe::Charge.create(amount: 1, currency: 'usd')
356
+ @charge2 = Stripe::Charge.create(amount: 1, currency: 'usd', customer: @customer2.id)
259
357
  end
260
358
 
261
359
  it "stores charges for a customer in memory" do
@@ -263,16 +361,17 @@ shared_examples 'Charge API' do
263
361
  end
264
362
 
265
363
  it "stores all charges in memory" do
266
- 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])
267
365
  end
268
366
 
269
367
  it "defaults count to 10 charges" do
270
- 11.times { Stripe::Charge.create(amount: 1, currency: 'usd') }
368
+ 11.times { Stripe::Charge.create(amount: 1, currency: 'usd', source: stripe_helper.generate_card_token) }
369
+
271
370
  expect(Stripe::Charge.all.data.count).to eq(10)
272
371
  end
273
372
 
274
373
  it "is marked as having more when more objects exist" do
275
- 11.times { Stripe::Charge.create(amount: 1, currency: 'usd') }
374
+ 11.times { Stripe::Charge.create(amount: 1, currency: 'usd', source: stripe_helper.generate_card_token) }
276
375
 
277
376
  expect(Stripe::Charge.all.has_more).to eq(true)
278
377
  end
@@ -314,7 +413,7 @@ shared_examples 'Charge API' do
314
413
  charge = Stripe::Charge.create({
315
414
  amount: 777,
316
415
  currency: 'USD',
317
- card: stripe_helper.generate_card_token
416
+ source: stripe_helper.generate_card_token
318
417
  })
319
418
 
320
419
  expect(charge.captured).to eq(true)
@@ -324,7 +423,7 @@ shared_examples 'Charge API' do
324
423
  charge = Stripe::Charge.create({
325
424
  amount: 777,
326
425
  currency: 'USD',
327
- card: stripe_helper.generate_card_token,
426
+ source: stripe_helper.generate_card_token,
328
427
  capture: true
329
428
  })
330
429
 
@@ -335,7 +434,7 @@ shared_examples 'Charge API' do
335
434
  charge = Stripe::Charge.create({
336
435
  amount: 777,
337
436
  currency: 'USD',
338
- card: stripe_helper.generate_card_token,
437
+ source: stripe_helper.generate_card_token,
339
438
  capture: false
340
439
  })
341
440
 
@@ -348,7 +447,7 @@ shared_examples 'Charge API' do
348
447
  charge = Stripe::Charge.create({
349
448
  amount: 777,
350
449
  currency: 'USD',
351
- card: stripe_helper.generate_card_token,
450
+ source: stripe_helper.generate_card_token,
352
451
  capture: false
353
452
  })
354
453
 
@@ -362,7 +461,7 @@ shared_examples 'Charge API' do
362
461
  charge = Stripe::Charge.create({
363
462
  amount: 777,
364
463
  currency: 'USD',
365
- card: stripe_helper.generate_card_token,
464
+ source: stripe_helper.generate_card_token,
366
465
  capture: false
367
466
  })
368
467
 
@@ -376,29 +475,35 @@ shared_examples 'Charge API' do
376
475
  end
377
476
 
378
477
  describe "idempotency" do
379
- let(:idempotent_charge_params) {{
478
+ let(:customer) { Stripe::Customer.create(email: 'johnny@appleseed.com') }
479
+ let(:charge_params) {{
380
480
  amount: 777,
381
481
  currency: 'USD',
382
- card: stripe_helper.generate_card_token,
383
- capture: true,
482
+ customer: customer.id,
483
+ capture: true
484
+ }}
485
+ let(:charge_headers) {{
384
486
  idempotency_key: 'onceisenough'
385
487
  }}
386
488
 
387
489
  it "returns the original charge if the same idempotency_key is passed in" do
388
- charge1 = Stripe::Charge.create(idempotent_charge_params)
389
- 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)
390
492
 
391
493
  expect(charge1).to eq(charge2)
392
494
  end
393
495
 
394
- it "returns different charges if different idempotency_keys are used for each charge" do
395
- idempotent_charge_params2 = idempotent_charge_params.clone
396
- idempotent_charge_params2[:idempotency_key] = 'thisoneisdifferent'
496
+ context 'different key' do
497
+ let(:different_charge_headers) {{
498
+ idempotency_key: 'thisoneisdifferent'
499
+ }}
397
500
 
398
- charge1 = Stripe::Charge.create(idempotent_charge_params)
399
- 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)
400
504
 
401
- expect(charge1).not_to eq(charge2)
505
+ expect(charge1).not_to eq(charge2)
506
+ end
402
507
  end
403
508
  end
404
509
 
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples 'Country Spec API' do
4
+ context 'retrieve country', live: true do
5
+ it 'retrieves a stripe country spec' do
6
+ country = Stripe::CountrySpec.retrieve('US')
7
+
8
+ expect(country).to be_a Stripe::CountrySpec
9
+ expect(country.id).to match /US/
10
+ end
11
+
12
+ it "cannot retrieve a stripe country that doesn't exist" do
13
+ expect { Stripe::CountrySpec.retrieve('nope') }
14
+ .to raise_error(Stripe::InvalidRequestError, /(nope is not currently supported by Stripe)|(Country 'nope' is unknown)/)
15
+
16
+ end
17
+ end
18
+ end
@@ -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
@@ -24,6 +24,27 @@ shared_examples 'Customer API' do
24
24
  expect { customer.source }.to raise_error
25
25
  end
26
26
 
27
+ it "creates a stripe customer with multiple cards and updates the default card" do
28
+ card_a = gen_card_tk
29
+ card_b = gen_card_tk
30
+ customer = Stripe::Customer.create({
31
+ email: 'johnny.multiple@appleseed.com',
32
+ source: card_a,
33
+ description: "a description"
34
+ })
35
+
36
+ original_card = customer.sources.data.first.id
37
+
38
+ customer.sources.create(source: card_b)
39
+ retrieved_customer = Stripe::Customer.retrieve(customer.id)
40
+
41
+ expect(retrieved_customer.sources.data.length).to eq(2)
42
+ retrieved_customer.default_source = retrieved_customer.sources.data.last.id
43
+ retrieved_customer.save
44
+ expect(Stripe::Customer.retrieve(customer.id).default_source).to eq(retrieved_customer.sources.data.last.id)
45
+ expect(Stripe::Customer.retrieve(customer.id).default_source).to_not eq(original_card)
46
+ end
47
+
27
48
  it "creates a stripe customer without a card" do
28
49
  customer = Stripe::Customer.create({
29
50
  email: 'cardless@appleseed.com',
@@ -57,6 +78,15 @@ shared_examples 'Customer API' do
57
78
  expect(customer.sources.data.first.exp_year).to eq 2024
58
79
  end
59
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
+
60
90
  it 'creates a customer with a plan' do
61
91
  plan = stripe_helper.create_plan(id: 'silver')
62
92
  customer = Stripe::Customer.create(id: 'test_cus_plan', source: gen_card_tk, :plan => 'silver')
@@ -194,6 +224,21 @@ shared_examples 'Customer API' do
194
224
  customer = Stripe::Customer.retrieve('test_cus_coupon')
195
225
  expect(customer.discount).to_not be_nil
196
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 }
197
242
  end
198
243
 
199
244
  it 'cannot create a customer with a coupon that does not exist' do
@@ -231,11 +276,25 @@ shared_examples 'Customer API' do
231
276
 
232
277
  expect(customer.id).to eq(original.id)
233
278
  expect(customer.email).to eq(original.email)
279
+ expect(customer.name).to eq(nil)
234
280
  expect(customer.default_source).to eq(original.default_source)
281
+ expect(customer.default_source).not_to be_a(Stripe::Card)
235
282
  expect(customer.subscriptions.count).to eq(0)
236
283
  expect(customer.subscriptions.data).to be_empty
237
284
  end
238
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
+
239
298
  it "cannot retrieve a customer that doesn't exist" do
240
299
  expect { Stripe::Customer.retrieve('nope') }.to raise_error {|e|
241
300
  expect(e).to be_a Stripe::InvalidRequestError
@@ -344,9 +403,40 @@ shared_examples 'Customer API' do
344
403
  expect(original.subscriptions.total_count).to eq(1)
345
404
  end
346
405
 
406
+ it "should add a customer to a subscription" do
407
+ plan = stripe_helper.create_plan(id: 'silver')
408
+ customer = Stripe::Customer.create(source: gen_card_tk)
409
+ customer.subscriptions.create(plan: plan.id)
410
+
411
+ expect(Stripe::Customer.retrieve(customer.id).subscriptions.total_count).to eq(1)
412
+ end
413
+
347
414
  it "deletes a customer" do
348
415
  customer = Stripe::Customer.create(id: 'test_customer_sub')
349
416
  customer = customer.delete
350
417
  expect(customer.deleted).to eq(true)
351
418
  end
419
+
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
+ 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
352
442
  end