stripe-ruby-mock 2.5.8 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (61) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -1
  3. data/.travis.yml +6 -6
  4. data/README.md +8 -8
  5. data/lib/stripe_mock.rb +3 -0
  6. data/lib/stripe_mock/api/errors.rb +31 -28
  7. data/lib/stripe_mock/api/webhooks.rb +3 -0
  8. data/lib/stripe_mock/data.rb +140 -13
  9. data/lib/stripe_mock/instance.rb +10 -3
  10. data/lib/stripe_mock/request_handlers/balance_transactions.rb +2 -2
  11. data/lib/stripe_mock/request_handlers/helpers/subscription_helpers.rb +1 -1
  12. data/lib/stripe_mock/request_handlers/helpers/token_helpers.rb +1 -1
  13. data/lib/stripe_mock/request_handlers/invoices.rb +6 -1
  14. data/lib/stripe_mock/request_handlers/payment_intents.rb +175 -0
  15. data/lib/stripe_mock/request_handlers/payment_methods.rb +138 -0
  16. data/lib/stripe_mock/request_handlers/products.rb +1 -0
  17. data/lib/stripe_mock/request_handlers/setup_intents.rb +93 -0
  18. data/lib/stripe_mock/request_handlers/subscriptions.rb +17 -4
  19. data/lib/stripe_mock/request_handlers/validators/param_validators.rb +95 -9
  20. data/lib/stripe_mock/test_strategies/base.rb +42 -8
  21. data/lib/stripe_mock/test_strategies/live.rb +23 -12
  22. data/lib/stripe_mock/test_strategies/mock.rb +6 -2
  23. data/lib/stripe_mock/version.rb +1 -1
  24. data/lib/stripe_mock/webhook_fixtures/charge.failed.json +166 -38
  25. data/lib/stripe_mock/webhook_fixtures/customer.created.json +1 -0
  26. data/lib/stripe_mock/webhook_fixtures/customer.updated.json +1 -0
  27. data/lib/stripe_mock/webhook_fixtures/product.created.json +34 -0
  28. data/lib/stripe_mock/webhook_fixtures/product.deleted.json +34 -0
  29. data/lib/stripe_mock/webhook_fixtures/product.updated.json +38 -0
  30. data/spec/instance_spec.rb +6 -6
  31. data/spec/server_spec.rb +2 -1
  32. data/spec/shared_stripe_examples/account_examples.rb +1 -1
  33. data/spec/shared_stripe_examples/balance_transaction_examples.rb +3 -3
  34. data/spec/shared_stripe_examples/bank_examples.rb +3 -3
  35. data/spec/shared_stripe_examples/card_examples.rb +4 -4
  36. data/spec/shared_stripe_examples/charge_examples.rb +9 -22
  37. data/spec/shared_stripe_examples/coupon_examples.rb +1 -1
  38. data/spec/shared_stripe_examples/customer_examples.rb +44 -30
  39. data/spec/shared_stripe_examples/dispute_examples.rb +2 -2
  40. data/spec/shared_stripe_examples/error_mock_examples.rb +8 -7
  41. data/spec/shared_stripe_examples/external_account_examples.rb +3 -3
  42. data/spec/shared_stripe_examples/invoice_examples.rb +41 -39
  43. data/spec/shared_stripe_examples/invoice_item_examples.rb +1 -1
  44. data/spec/shared_stripe_examples/payment_intent_examples.rb +137 -0
  45. data/spec/shared_stripe_examples/payment_method_examples.rb +185 -0
  46. data/spec/shared_stripe_examples/payout_examples.rb +2 -2
  47. data/spec/shared_stripe_examples/plan_examples.rb +135 -92
  48. data/spec/shared_stripe_examples/product_examples.rb +155 -0
  49. data/spec/shared_stripe_examples/refund_examples.rb +25 -21
  50. data/spec/shared_stripe_examples/setup_intent_examples.rb +68 -0
  51. data/spec/shared_stripe_examples/subscription_examples.rb +281 -310
  52. data/spec/shared_stripe_examples/subscription_items_examples.rb +3 -2
  53. data/spec/shared_stripe_examples/transfer_examples.rb +6 -6
  54. data/spec/shared_stripe_examples/webhook_event_examples.rb +11 -11
  55. data/spec/spec_helper.rb +3 -4
  56. data/spec/stripe_mock_spec.rb +2 -2
  57. data/spec/support/shared_contexts/stripe_validator_spec.rb +8 -0
  58. data/spec/support/stripe_examples.rb +3 -0
  59. data/stripe-ruby-mock.gemspec +2 -2
  60. metadata +47 -27
  61. data/spec/shared_stripe_examples/product_example.rb +0 -65
@@ -71,14 +71,14 @@ shared_examples 'Dispute API' do
71
71
  describe "listing disputes" do
72
72
 
73
73
  it "retrieves all disputes" do
74
- disputes = Stripe::Dispute.all
74
+ disputes = Stripe::Dispute.list
75
75
 
76
76
  expect(disputes.count).to eq(10)
77
77
  expect(disputes.map &:id).to include('dp_05RsQX2eZvKYlo2C0FRTGSSA','dp_15RsQX2eZvKYlo2C0ERTYUIA', 'dp_25RsQX2eZvKYlo2C0ZXCVBNM', 'dp_35RsQX2eZvKYlo2C0QAZXSWE', 'dp_45RsQX2eZvKYlo2C0EDCVFRT', 'dp_55RsQX2eZvKYlo2C0OIKLJUY', 'dp_65RsQX2eZvKYlo2C0ASDFGHJ', 'dp_75RsQX2eZvKYlo2C0EDCXSWQ', 'dp_85RsQX2eZvKYlo2C0UJMCDET', 'dp_95RsQX2eZvKYlo2C0EDFRYUI')
78
78
  end
79
79
 
80
80
  it "retrieves disputes with a limit(3)" do
81
- disputes = Stripe::Dispute.all(limit: 3)
81
+ disputes = Stripe::Dispute.list(limit: 3)
82
82
 
83
83
  expect(disputes.count).to eq(3)
84
84
  expected = ['dp_95RsQX2eZvKYlo2C0EDFRYUI','dp_85RsQX2eZvKYlo2C0UJMCDET', 'dp_75RsQX2eZvKYlo2C0EDCXSWQ']
@@ -6,13 +6,14 @@ def expect_card_error(code, param)
6
6
  expect(e.http_status).to eq(402)
7
7
  expect(e.code).to eq(code)
8
8
  expect(e.param).to eq(param)
9
+ expect(e.http_body).to eq(e.json_body.to_json)
9
10
  }
10
11
  end
11
12
 
12
13
  shared_examples 'Stripe Error Mocking' do
13
14
 
14
15
  it "mocks a manually given stripe card error" do
15
- error = Stripe::CardError.new('Test Msg', 'param_name', 'bad_code', http_status: 444, http_body: 'body', json_body: 'json body')
16
+ error = Stripe::CardError.new('Test Msg', 'param_name', code: 'bad_code', http_status: 444, http_body: 'body', json_body: {})
16
17
  StripeMock.prepare_error(error)
17
18
 
18
19
  expect { Stripe::Customer.create() }.to raise_error {|e|
@@ -23,14 +24,14 @@ shared_examples 'Stripe Error Mocking' do
23
24
 
24
25
  expect(e.http_status).to eq(444)
25
26
  expect(e.http_body).to eq('body')
26
- expect(e.json_body).to eq('json body')
27
+ expect(e.json_body).to eq({})
27
28
  }
28
29
  end
29
30
 
30
31
 
31
32
  it "mocks a manually gives stripe invalid request error" do
32
33
 
33
- error = Stripe::InvalidRequestError.new('Test Invalid', 'param', http_status: 987, http_body: 'ibody', json_body: 'json ibody')
34
+ error = Stripe::InvalidRequestError.new('Test Invalid', 'param', http_status: 987, http_body: 'ibody', json_body: {})
34
35
  StripeMock.prepare_error(error)
35
36
 
36
37
  expect { Stripe::Charge.create(amount: 1, currency: 'usd') }.to raise_error {|e|
@@ -40,22 +41,22 @@ shared_examples 'Stripe Error Mocking' do
40
41
 
41
42
  expect(e.http_status).to eq(987)
42
43
  expect(e.http_body).to eq('ibody')
43
- expect(e.json_body).to eq('json ibody')
44
+ expect(e.json_body).to eq({})
44
45
  }
45
46
  end
46
47
 
47
48
 
48
49
  it "mocks a manually gives stripe invalid auth error" do
49
- error = Stripe::AuthenticationError.new('Bad Auth', http_status: 499, http_body: 'abody', json_body: 'json abody')
50
+ error = Stripe::AuthenticationError.new('Bad Auth', http_status: 499, http_body: 'abody', json_body: {})
50
51
  StripeMock.prepare_error(error)
51
52
 
52
- expect { stripe_helper.create_plan() }.to raise_error {|e|
53
+ expect { stripe_helper.create_plan(id: "test_plan") }.to raise_error {|e|
53
54
  expect(e).to be_a(Stripe::AuthenticationError)
54
55
  expect(e.message).to eq('Bad Auth')
55
56
 
56
57
  expect(e.http_status).to eq(499)
57
58
  expect(e.http_body).to eq('abody')
58
- expect(e.json_body).to eq('json abody')
59
+ expect(e.json_body).to eq({})
59
60
  }
60
61
  end
61
62
 
@@ -63,7 +63,7 @@ shared_examples 'External Account API' do
63
63
  let!(:bank) { account.external_accounts.create(external_account: bank_token) }
64
64
 
65
65
  it "can retrieve all account's banks" do
66
- retrieved = account.external_accounts.all
66
+ retrieved = account.external_accounts.list
67
67
  expect(retrieved.count).to eq(1)
68
68
  end
69
69
 
@@ -142,7 +142,7 @@ shared_examples 'External Account API' do
142
142
 
143
143
  account = Stripe::Account.retrieve('test_account')
144
144
 
145
- list = account.external_accounts.all
145
+ list = account.external_accounts.list
146
146
 
147
147
  expect(list.object).to eq("list")
148
148
  expect(list.count).to eq(2)
@@ -159,7 +159,7 @@ shared_examples 'External Account API' do
159
159
  Stripe::Account.create(id: 'no_banks', type: 'custom', country: "US")
160
160
  account = Stripe::Account.retrieve('no_banks')
161
161
 
162
- list = account.external_accounts.all
162
+ list = account.external_accounts.list
163
163
 
164
164
  expect(list.object).to eq("list")
165
165
  expect(list.count).to eq(0)
@@ -48,27 +48,28 @@ shared_examples 'Invoice API' do
48
48
  end
49
49
 
50
50
  it "stores invoices for a customer in memory" do
51
- expect(@customer.invoices.map(&:id)).to eq([@invoice.id])
51
+ invoices = Stripe::Invoice.list(customer: @customer.id)
52
+ expect(invoices.map(&:id)).to eq([@invoice.id])
52
53
  end
53
54
 
54
55
  it "stores all invoices in memory" do
55
- expect(Stripe::Invoice.all.map(&:id).sort).to eq([@invoice.id, @invoice2.id].sort)
56
+ expect(Stripe::Invoice.list.map(&:id)).to match_array([@invoice.id, @invoice2.id])
56
57
  end
57
58
 
58
59
  it "defaults count to 10 invoices" do
59
60
  11.times { Stripe::Invoice.create }
60
- expect(Stripe::Invoice.all.count).to eq(10)
61
+ expect(Stripe::Invoice.list.count).to eq(10)
61
62
  end
62
63
 
63
64
  it "is marked as having more when more objects exist" do
64
65
  11.times { Stripe::Invoice.create }
65
66
 
66
- expect(Stripe::Invoice.all.has_more).to eq(true)
67
+ expect(Stripe::Invoice.list.has_more).to eq(true)
67
68
  end
68
69
 
69
70
  context "when passing limit" do
70
71
  it "gets that many invoices" do
71
- expect(Stripe::Invoice.all(limit: 1).count).to eq(1)
72
+ expect(Stripe::Invoice.list(limit: 1).count).to eq(1)
72
73
  end
73
74
  end
74
75
  end
@@ -109,8 +110,9 @@ shared_examples 'Invoice API' do
109
110
  context "retrieving upcoming invoice" do
110
111
  let(:customer) { Stripe::Customer.create(source: stripe_helper.generate_card_token) }
111
112
  let(:coupon_amtoff) { stripe_helper.create_coupon(id: '100OFF', currency: 'usd', amount_off: 100_00, duration: 'repeating', duration_in_months: 6) }
112
- let(:coupon_pctoff) { stripe_helper.create_coupon(id: '50%OFF', currency: 'usd', percent_off: 50, amount_off: nil, duration: 'repeating', duration_in_months: 6) }
113
- let(:plan) { stripe_helper.create_plan(id: '50m', amount: 50_00, interval: 'month', name: '50m', currency: 'usd') }
113
+ let(:coupon_pctoff) { stripe_helper.create_coupon(id: '50OFF', currency: 'usd', percent_off: 50, amount_off: nil, duration: 'repeating', duration_in_months: 6) }
114
+ let(:product) { stripe_helper.create_product(id: "prod_123") }
115
+ let(:plan) { stripe_helper.create_plan(id: '50m', product: product.id, amount: 50_00, interval: 'month', nickname: '50m', currency: 'usd') }
114
116
  let(:quantity) { 3 }
115
117
  let(:subscription) { Stripe::Subscription.create(plan: plan.id, customer: customer.id, quantity: quantity) }
116
118
 
@@ -198,8 +200,8 @@ shared_examples 'Invoice API' do
198
200
  expect(upcoming.discount).not_to be_nil
199
201
  expect(upcoming.discount.coupon.id).to eq '100OFF'
200
202
  expect(upcoming.discount.customer).to eq customer.id
201
- expect(upcoming.discount.start).to be_within(5).of Time.now.to_i
202
- expect(upcoming.discount.end).to be_within(5).of (Time.now.to_datetime >> 6).to_time.to_i
203
+ expect(upcoming.discount.start).to be_within(60).of Time.now.to_i
204
+ expect(upcoming.discount.end).to be_within(60).of (Time.now.to_datetime >> 6).to_time.to_i
203
205
  expect(upcoming.amount_due).to eq plan.amount * quantity - 100_00
204
206
  expect(upcoming.subtotal).to eq(upcoming.lines.data[0].amount)
205
207
  expect(upcoming.total).to eq upcoming.subtotal - 100_00
@@ -211,10 +213,10 @@ shared_examples 'Invoice API' do
211
213
 
212
214
  # Then
213
215
  expect(upcoming.discount).not_to be_nil
214
- expect(upcoming.discount.coupon.id).to eq '50%OFF'
216
+ expect(upcoming.discount.coupon.id).to eq '50OFF'
215
217
  expect(upcoming.discount.customer).to eq customer.id
216
- expect(upcoming.discount.start).to be_within(5).of Time.now.to_i
217
- expect(upcoming.discount.end).to be_within(5).of (Time.now.to_datetime >> 6).to_time.to_i
218
+ expect(upcoming.discount.start).to be_within(60).of Time.now.to_i
219
+ expect(upcoming.discount.end).to be_within(60).of (Time.now.to_datetime >> 6).to_time.to_i
218
220
  expect(upcoming.amount_due).to eq (plan.amount * quantity) * 0.5
219
221
  expect(upcoming.subtotal).to eq(upcoming.lines.data[0].amount)
220
222
  expect(upcoming.total).to eq upcoming.subtotal * 0.5
@@ -223,7 +225,7 @@ shared_examples 'Invoice API' do
223
225
 
224
226
  describe 'proration' do
225
227
  shared_examples 'failing when proration date is outside of the subscription current period' do
226
- it 'fails', live: true do
228
+ it 'fails', live: true, skip: 'Stripe does not raise error anymore' do
227
229
  expect { Stripe::Invoice.upcoming(
228
230
  customer: customer.id,
229
231
  subscription: subscription.id,
@@ -248,9 +250,9 @@ shared_examples 'Invoice API' do
248
250
 
249
251
  [false, true].each do |with_trial|
250
252
  describe "prorating a subscription with a new plan, with_trial: #{with_trial}" do
251
- let(:new_monthly_plan) { stripe_helper.create_plan(id: '100m', amount: 100_00, interval: 'month', name: '100m', currency: 'usd') }
252
- let(:new_yearly_plan) { stripe_helper.create_plan(id: '100y', amount: 100_00, interval: 'year', name: '100y', currency: 'usd') }
253
- let(:plan) { stripe_helper.create_plan(id: '50m', amount: 50_00, interval: 'month', name: '50m', currency: 'usd') }
253
+ let(:new_monthly_plan) { stripe_helper.create_plan(id: '100m', product: product.id, amount: 100_00, interval: 'month', nickname: '100m', currency: 'usd') }
254
+ let(:new_yearly_plan) { stripe_helper.create_plan(id: '100y', product: product.id, amount: 100_00, interval: 'year', nickname: '100y', currency: 'usd') }
255
+ let(:plan) { stripe_helper.create_plan(id: '50m', product: product.id, amount: 50_00, interval: 'month', nickname: '50m', currency: 'usd') }
254
256
 
255
257
  it 'prorates while maintaining billing interval', live: true do
256
258
  # Given
@@ -271,19 +273,17 @@ shared_examples 'Invoice API' do
271
273
  # Then
272
274
  expect(upcoming).to be_a Stripe::Invoice
273
275
  expect(upcoming.customer).to eq(customer.id)
274
- if with_trial
275
- expect(upcoming.amount_due).to be_within(1).of 0
276
- else
277
- expect(upcoming.amount_due).to be_within(1).of prorated_amount_due - credit_balance
278
- end
279
276
  expect(upcoming.starting_balance).to eq -credit_balance
280
- expect(upcoming.ending_balance).to be_nil
281
277
  expect(upcoming.subscription).to eq(subscription.id)
282
278
 
283
279
  if with_trial
280
+ expect(upcoming.amount_due).to be_within(1).of 0
284
281
  expect(upcoming.lines.data.length).to eq(2)
282
+ expect(upcoming.ending_balance).to be_within(50).of -13540
285
283
  else
284
+ expect(upcoming.amount_due).to be_within(1).of prorated_amount_due - credit_balance
286
285
  expect(upcoming.lines.data.length).to eq(3)
286
+ expect(upcoming.ending_balance).to eq 0
287
287
  end
288
288
 
289
289
  expect(upcoming.lines.data[0].proration).to be_truthy
@@ -308,9 +308,10 @@ shared_examples 'Invoice API' do
308
308
  # Given
309
309
  proration_date = Time.now + 5 * 24 * 3600 # 5 days later
310
310
  new_quantity = 2
311
- unused_amount = plan.amount * quantity * (subscription.current_period_end - proration_date.to_i) / (subscription.current_period_end - subscription.current_period_start)
311
+ unused_amount = (plan.amount.to_f * quantity * (subscription.current_period_end - proration_date.to_i) / (subscription.current_period_end - subscription.current_period_start)).round
312
312
  prorated_amount_due = new_yearly_plan.amount * new_quantity - unused_amount
313
313
  credit_balance = 1000
314
+ amount_due = prorated_amount_due - credit_balance
314
315
  customer.account_balance = -credit_balance
315
316
  customer.save
316
317
  query = { customer: customer.id, subscription: subscription.id, subscription_plan: new_yearly_plan.id, subscription_proration_date: proration_date.to_i, subscription_quantity: new_quantity }
@@ -323,17 +324,18 @@ shared_examples 'Invoice API' do
323
324
  expect(upcoming).to be_a Stripe::Invoice
324
325
  expect(upcoming.customer).to eq(customer.id)
325
326
  if with_trial
327
+ expect(upcoming.ending_balance).to be_within(50).of -13540
326
328
  expect(upcoming.amount_due).to eq 0
327
329
  else
328
- expect(upcoming.amount_due).to be_within(1).of prorated_amount_due - credit_balance
330
+ expect(upcoming.ending_balance).to eq 0
331
+ expect(upcoming.amount_due).to eq amount_due
329
332
  end
330
333
  expect(upcoming.starting_balance).to eq -credit_balance
331
- expect(upcoming.ending_balance).to be_nil
332
334
  expect(upcoming.subscription).to eq(subscription.id)
333
335
 
334
336
  expect(upcoming.lines.data[0].proration).to be_truthy
335
337
  expect(upcoming.lines.data[0].plan.id).to eq '50m'
336
- expect(upcoming.lines.data[0].amount).to be_within(1).of -unused_amount
338
+ expect(upcoming.lines.data[0].amount).to eq -unused_amount
337
339
  expect(upcoming.lines.data[0].quantity).to eq quantity
338
340
 
339
341
  expect(upcoming.lines.data[1].proration).to be_falsey
@@ -351,7 +353,7 @@ shared_examples 'Invoice API' do
351
353
  it 'generates a preview without performing an actual proration', live: true do
352
354
  expect(preview.subtotal).to eq 150_00
353
355
  # this is a future invoice (generted at the end of the current subscription cycle), rather than a proration invoice
354
- expect(preview.created).to be_within(1).of subscription.current_period_end
356
+ expect(preview.due_date).to be_nil
355
357
  expect(preview.period_start).to eq subscription.current_period_start
356
358
  expect(preview.period_end).to eq subscription.current_period_end
357
359
  expect(preview.lines.count).to eq 1
@@ -393,7 +395,7 @@ shared_examples 'Invoice API' do
393
395
  end
394
396
 
395
397
  it 'sets the start and end of billing periods correctly when plan has an interval_count' do
396
- @oddplan = stripe_helper.create_plan(interval: "week", interval_count: 11)
398
+ @oddplan = stripe_helper.create_plan(product: product.id, interval: "week", interval_count: 11, id: "weekly_pl")
397
399
  @subscription = Stripe::Subscription.create(plan: @oddplan.id, customer: customer.id)
398
400
  @upcoming = Stripe::Invoice.upcoming(customer: customer.id)
399
401
 
@@ -404,9 +406,9 @@ shared_examples 'Invoice API' do
404
406
  end
405
407
 
406
408
  it 'chooses the most recent of multiple subscriptions' do
407
- @shortplan = stripe_helper.create_plan(id: 'a', interval: "week") # 1 week sub
408
- @plainplan = stripe_helper.create_plan(id: 'b') # 1 month sub
409
- @longplan = stripe_helper.create_plan(id: 'c', interval: "year") # 1 year sub
409
+ @shortplan = stripe_helper.create_plan(id: 'a', product: product.id, interval: "week") # 1 week sub
410
+ @plainplan = stripe_helper.create_plan(id: 'b', product: product.id, interval: "month") # 1 month sub
411
+ @longplan = stripe_helper.create_plan(id: 'c', product: product.id, interval: "year") # 1 year sub
410
412
 
411
413
  @plainsub = Stripe::Subscription.create(plan: @plainplan.id, customer: customer.id)
412
414
  @shortsub = Stripe::Subscription.create(plan: @shortplan.id, customer: customer.id)
@@ -428,7 +430,7 @@ shared_examples 'Invoice API' do
428
430
  context 'retrieving invoice line items' do
429
431
  it 'returns all line items for created invoice' do
430
432
  invoice = Stripe::Invoice.create(customer: customer.id)
431
- line_items = invoice.lines.all
433
+ line_items = invoice.lines.list
432
434
 
433
435
  expect(invoice).to be_a Stripe::Invoice
434
436
  expect(line_items.count).to eq(1)
@@ -438,7 +440,7 @@ shared_examples 'Invoice API' do
438
440
  end
439
441
 
440
442
  it 'returns all line items for upcoming invoice' do
441
- plan = stripe_helper.create_plan()
443
+ plan = stripe_helper.create_plan(product: product.id, id: "silver_pl")
442
444
  subscription = Stripe::Subscription.create(plan: plan.id, customer: customer.id)
443
445
  upcoming = Stripe::Invoice.upcoming(customer: customer.id)
444
446
  line_items = upcoming.lines
@@ -454,7 +456,7 @@ shared_examples 'Invoice API' do
454
456
  context 'calculates month and year offsets correctly' do
455
457
 
456
458
  it 'for one month plan on the 1st' do
457
- @plan = stripe_helper.create_plan()
459
+ @plan = stripe_helper.create_plan(product: product.id, id: "one_mo_plan")
458
460
  @sub = Stripe::Subscription.create(plan: @plan.id, customer: customer.id, current_period_start: Time.utc(2014,1,1,12).to_i)
459
461
  @upcoming = Stripe::Invoice.upcoming(customer: customer.id)
460
462
 
@@ -465,7 +467,7 @@ shared_examples 'Invoice API' do
465
467
  end
466
468
 
467
469
  it 'for one year plan on the 1st' do
468
- @plan = stripe_helper.create_plan(interval: "year")
470
+ @plan = stripe_helper.create_plan(interval: "year", product: product.id, id: "year_plan")
469
471
  @sub = Stripe::Subscription.create(plan: @plan.id, customer: customer.id, current_period_start: Time.utc(2012,1,1,12).to_i)
470
472
  @upcoming = Stripe::Invoice.upcoming(customer: customer.id)
471
473
 
@@ -476,7 +478,7 @@ shared_examples 'Invoice API' do
476
478
  end
477
479
 
478
480
  it 'for one month plan on the 31st' do
479
- @plan = stripe_helper.create_plan()
481
+ @plan = stripe_helper.create_plan(product: product.id, id: "one_mo_plan")
480
482
  @sub = Stripe::Subscription.create(plan: @plan.id, customer: customer.id, current_period_start: Time.utc(2014,1,31,12).to_i)
481
483
  @upcoming = Stripe::Invoice.upcoming(customer: customer.id)
482
484
 
@@ -487,7 +489,7 @@ shared_examples 'Invoice API' do
487
489
  end
488
490
 
489
491
  it 'for one year plan on feb. 29th' do
490
- @plan = stripe_helper.create_plan(interval: "year")
492
+ @plan = stripe_helper.create_plan(product: product.id, interval: "year", id: "year_plan")
491
493
  @sub = Stripe::Subscription.create(plan: @plan.id, customer: customer.id, current_period_start: Time.utc(2012,2,29,12).to_i)
492
494
  @upcoming = Stripe::Invoice.upcoming(customer: customer.id)
493
495
 
@@ -498,7 +500,7 @@ shared_examples 'Invoice API' do
498
500
  end
499
501
 
500
502
  it 'for two month plan on dec. 31st' do
501
- @plan = stripe_helper.create_plan(interval_count: 2)
503
+ @plan = stripe_helper.create_plan(product: product.id, interval_count: 2, id: 'two_mo_plan')
502
504
  @sub = Stripe::Subscription.create(plan: @plan.id, customer: customer.id, current_period_start: Time.utc(2013,12,31,12).to_i)
503
505
  @upcoming = Stripe::Invoice.upcoming(customer: customer.id)
504
506
 
@@ -509,7 +511,7 @@ shared_examples 'Invoice API' do
509
511
  end
510
512
 
511
513
  it 'for three month plan on nov. 30th' do
512
- @plan = stripe_helper.create_plan(interval_count: 3)
514
+ @plan = stripe_helper.create_plan(product: product.id, interval_count: 3)
513
515
  @sub = Stripe::Subscription.create(plan: @plan.id, customer: customer.id, current_period_start: Time.utc(2013,11,30,12).to_i)
514
516
  @upcoming = Stripe::Invoice.upcoming(customer: customer.id)
515
517
 
@@ -39,7 +39,7 @@ shared_examples 'Invoice Item API' do
39
39
  end
40
40
 
41
41
  it "retrieves all invoice items" do
42
- all = Stripe::InvoiceItem.all
42
+ all = Stripe::InvoiceItem.list
43
43
  expect(all.count).to eq(2)
44
44
  expect(all.map &:amount).to include(1075, 1540)
45
45
  end
@@ -0,0 +1,137 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples 'PaymentIntent API' do
4
+
5
+ it "creates a succeeded stripe payment_intent" do
6
+ payment_intent = Stripe::PaymentIntent.create(amount: 100, currency: "usd")
7
+
8
+ expect(payment_intent.id).to match(/^test_pi/)
9
+ expect(payment_intent.amount).to eq(100)
10
+ expect(payment_intent.currency).to eq('usd')
11
+ expect(payment_intent.metadata.to_hash).to eq({})
12
+ expect(payment_intent.status).to eq('succeeded')
13
+ end
14
+
15
+ it "creates a requires_action stripe payment_intent when amount matches 3184" do
16
+ payment_intent = Stripe::PaymentIntent.create(amount: 3184, currency: "usd")
17
+
18
+ expect(payment_intent.id).to match(/^test_pi/)
19
+ expect(payment_intent.amount).to eq(3184)
20
+ expect(payment_intent.currency).to eq('usd')
21
+ expect(payment_intent.metadata.to_hash).to eq({})
22
+ expect(payment_intent.status).to eq('requires_action')
23
+ expect(payment_intent.next_action.type).to eq('use_stripe_sdk')
24
+ end
25
+
26
+ it "creates a requires_payment_method stripe payment_intent when amount matches 3184" do
27
+ payment_intent = Stripe::PaymentIntent.create(amount: 3178, currency: "usd")
28
+
29
+ expect(payment_intent.id).to match(/^test_pi/)
30
+ expect(payment_intent.amount).to eq(3178)
31
+ expect(payment_intent.currency).to eq('usd')
32
+ expect(payment_intent.metadata.to_hash).to eq({})
33
+ expect(payment_intent.status).to eq('requires_payment_method')
34
+ expect(payment_intent.last_payment_error.code).to eq('card_declined')
35
+ expect(payment_intent.last_payment_error.decline_code).to eq('insufficient_funds')
36
+ expect(payment_intent.last_payment_error.message).to eq('Not enough funds.')
37
+ end
38
+
39
+ describe "listing payment_intent" do
40
+ before do
41
+ 3.times do
42
+ Stripe::PaymentIntent.create(amount: 100, currency: "usd")
43
+ end
44
+ end
45
+
46
+ it "without params retrieves all stripe payment_intent" do
47
+ expect(Stripe::PaymentIntent.list.count).to eq(3)
48
+ end
49
+
50
+ it "accepts a limit param" do
51
+ expect(Stripe::PaymentIntent.list(limit: 2).count).to eq(2)
52
+ end
53
+ end
54
+
55
+ it "retrieves a stripe payment_intent" do
56
+ original = Stripe::PaymentIntent.create(amount: 100, currency: "usd")
57
+ payment_intent = Stripe::PaymentIntent.retrieve(original.id)
58
+
59
+ expect(payment_intent.id).to eq(original.id)
60
+ expect(payment_intent.amount).to eq(original.amount)
61
+ expect(payment_intent.currency).to eq(original.currency)
62
+ expect(payment_intent.metadata.to_hash).to eq(original.metadata.to_hash)
63
+ end
64
+
65
+ it "cannot retrieve a payment_intent that doesn't exist" do
66
+ expect { Stripe::PaymentIntent.retrieve('nope') }.to raise_error {|e|
67
+ expect(e).to be_a Stripe::InvalidRequestError
68
+ expect(e.param).to eq('payment_intent')
69
+ expect(e.http_status).to eq(404)
70
+ }
71
+ end
72
+
73
+ it 'creates and confirms a stripe payment_intent with confirm flag to true' do
74
+ payment_intent = Stripe::PaymentIntent.create(
75
+ amount: 100, currency: 'usd', confirm: true
76
+ )
77
+ expect(payment_intent.status).to eq('succeeded')
78
+ expect(payment_intent.charges.data.size).to eq(1)
79
+ expect(payment_intent.charges.data.first.object).to eq('charge')
80
+ balance_txn = payment_intent.charges.data.first.balance_transaction
81
+ expect(balance_txn).to match(/^test_txn/)
82
+ expect(Stripe::BalanceTransaction.retrieve(balance_txn).id).to eq(balance_txn)
83
+ end
84
+
85
+ it "confirms a stripe payment_intent" do
86
+ payment_intent = Stripe::PaymentIntent.create(amount: 100, currency: "usd")
87
+ confirmed_payment_intent = payment_intent.confirm()
88
+ expect(confirmed_payment_intent.status).to eq("succeeded")
89
+ expect(confirmed_payment_intent.charges.data.size).to eq(1)
90
+ expect(confirmed_payment_intent.charges.data.first.object).to eq('charge')
91
+ end
92
+
93
+ it "captures a stripe payment_intent" do
94
+ payment_intent = Stripe::PaymentIntent.create(amount: 100, currency: "usd")
95
+ confirmed_payment_intent = payment_intent.capture()
96
+ expect(confirmed_payment_intent.status).to eq("succeeded")
97
+ expect(confirmed_payment_intent.charges.data.size).to eq(1)
98
+ expect(confirmed_payment_intent.charges.data.first.object).to eq('charge')
99
+ end
100
+
101
+ it "cancels a stripe payment_intent" do
102
+ payment_intent = Stripe::PaymentIntent.create(amount: 100, currency: "usd")
103
+ confirmed_payment_intent = payment_intent.cancel()
104
+ expect(confirmed_payment_intent.status).to eq("canceled")
105
+ end
106
+
107
+ it "updates a stripe payment_intent" do
108
+ original = Stripe::PaymentIntent.create(amount: 100, currency: "usd")
109
+ payment_intent = Stripe::PaymentIntent.retrieve(original.id)
110
+
111
+ payment_intent.amount = 200
112
+ payment_intent.save
113
+
114
+ updated = Stripe::PaymentIntent.retrieve(original.id)
115
+
116
+ expect(updated.amount).to eq(200)
117
+ end
118
+
119
+ it 'when amount is not integer', live: true do
120
+ expect { Stripe::PaymentIntent.create(amount: 400.2,
121
+ currency: 'usd') }.to raise_error { |e|
122
+ expect(e).to be_a Stripe::InvalidRequestError
123
+ expect(e.param).to eq('amount')
124
+ expect(e.http_status).to eq(400)
125
+ }
126
+ end
127
+
128
+ it 'when amount is negative', live: true do
129
+ expect { Stripe::PaymentIntent.create(amount: -400,
130
+ currency: 'usd') }.to raise_error { |e|
131
+ expect(e).to be_a Stripe::InvalidRequestError
132
+ expect(e.param).to eq('amount')
133
+ expect(e.message).to match(/^Invalid.*integer/)
134
+ expect(e.http_status).to eq(400)
135
+ }
136
+ end
137
+ end