stripe-ruby-mock 2.5.6 → 3.1.0.rc2

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 (97) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +1 -1
  3. data/.travis.yml +6 -9
  4. data/CHANGELOG.md +35 -0
  5. data/Gemfile +1 -0
  6. data/README.md +17 -11
  7. data/lib/stripe_mock.rb +11 -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 +7 -0
  12. data/lib/stripe_mock/client.rb +2 -1
  13. data/lib/stripe_mock/data.rb +323 -13
  14. data/lib/stripe_mock/data/list.rb +42 -9
  15. data/lib/stripe_mock/instance.rb +52 -5
  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 +13 -12
  19. data/lib/stripe_mock/request_handlers/checkout.rb +15 -0
  20. data/lib/stripe_mock/request_handlers/checkout_session.rb +16 -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 +14 -10
  25. data/lib/stripe_mock/request_handlers/helpers/subscription_helpers.rb +22 -7
  26. data/lib/stripe_mock/request_handlers/helpers/token_helpers.rb +1 -1
  27. data/lib/stripe_mock/request_handlers/invoices.rb +11 -5
  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 +44 -0
  33. data/lib/stripe_mock/request_handlers/refunds.rb +6 -3
  34. data/lib/stripe_mock/request_handlers/setup_intents.rb +93 -0
  35. data/lib/stripe_mock/request_handlers/sources.rb +12 -6
  36. data/lib/stripe_mock/request_handlers/subscription_items.rb +36 -0
  37. data/lib/stripe_mock/request_handlers/subscriptions.rb +82 -41
  38. data/lib/stripe_mock/request_handlers/tax_rates.rb +36 -0
  39. data/lib/stripe_mock/request_handlers/tokens.rb +8 -6
  40. data/lib/stripe_mock/request_handlers/validators/param_validators.rb +130 -9
  41. data/lib/stripe_mock/test_strategies/base.rb +68 -8
  42. data/lib/stripe_mock/test_strategies/live.rb +23 -12
  43. data/lib/stripe_mock/test_strategies/mock.rb +6 -2
  44. data/lib/stripe_mock/version.rb +1 -1
  45. data/lib/stripe_mock/webhook_fixtures/balance.available.json +6 -0
  46. data/lib/stripe_mock/webhook_fixtures/charge.dispute.funds_reinstated.json +88 -0
  47. data/lib/stripe_mock/webhook_fixtures/charge.dispute.funds_withdrawn.json +88 -0
  48. data/lib/stripe_mock/webhook_fixtures/charge.failed.json +166 -38
  49. data/lib/stripe_mock/webhook_fixtures/customer.created.json +1 -0
  50. data/lib/stripe_mock/webhook_fixtures/customer.updated.json +1 -0
  51. data/lib/stripe_mock/webhook_fixtures/invoice.created.json +2 -1
  52. data/lib/stripe_mock/webhook_fixtures/invoice.payment_succeeded.json +1 -1
  53. data/lib/stripe_mock/webhook_fixtures/invoice.updated.json +2 -1
  54. data/lib/stripe_mock/webhook_fixtures/payment_intent.payment_failed.json +186 -0
  55. data/lib/stripe_mock/webhook_fixtures/payment_intent.succeeded.json +164 -0
  56. data/lib/stripe_mock/webhook_fixtures/product.created.json +34 -0
  57. data/lib/stripe_mock/webhook_fixtures/product.deleted.json +34 -0
  58. data/lib/stripe_mock/webhook_fixtures/product.updated.json +38 -0
  59. data/spec/instance_spec.rb +10 -12
  60. data/spec/list_spec.rb +38 -0
  61. data/spec/server_spec.rb +6 -3
  62. data/spec/shared_stripe_examples/account_examples.rb +1 -1
  63. data/spec/shared_stripe_examples/account_link_examples.rb +16 -0
  64. data/spec/shared_stripe_examples/balance_examples.rb +6 -0
  65. data/spec/shared_stripe_examples/balance_transaction_examples.rb +3 -3
  66. data/spec/shared_stripe_examples/bank_examples.rb +3 -3
  67. data/spec/shared_stripe_examples/card_examples.rb +4 -4
  68. data/spec/shared_stripe_examples/card_token_examples.rb +17 -21
  69. data/spec/shared_stripe_examples/charge_examples.rb +32 -36
  70. data/spec/shared_stripe_examples/checkout_examples.rb +38 -0
  71. data/spec/shared_stripe_examples/coupon_examples.rb +1 -1
  72. data/spec/shared_stripe_examples/customer_examples.rb +103 -53
  73. data/spec/shared_stripe_examples/dispute_examples.rb +2 -2
  74. data/spec/shared_stripe_examples/error_mock_examples.rb +8 -7
  75. data/spec/shared_stripe_examples/express_login_link_examples.rb +12 -0
  76. data/spec/shared_stripe_examples/external_account_examples.rb +3 -3
  77. data/spec/shared_stripe_examples/invoice_examples.rb +43 -41
  78. data/spec/shared_stripe_examples/invoice_item_examples.rb +1 -1
  79. data/spec/shared_stripe_examples/payment_intent_examples.rb +147 -0
  80. data/spec/shared_stripe_examples/payment_method_examples.rb +449 -0
  81. data/spec/shared_stripe_examples/payout_examples.rb +2 -2
  82. data/spec/shared_stripe_examples/plan_examples.rb +135 -77
  83. data/spec/shared_stripe_examples/price_examples.rb +183 -0
  84. data/spec/shared_stripe_examples/product_examples.rb +155 -0
  85. data/spec/shared_stripe_examples/refund_examples.rb +41 -31
  86. data/spec/shared_stripe_examples/setup_intent_examples.rb +68 -0
  87. data/spec/shared_stripe_examples/subscription_examples.rb +546 -295
  88. data/spec/shared_stripe_examples/subscription_items_examples.rb +76 -0
  89. data/spec/shared_stripe_examples/tax_rate_examples.rb +42 -0
  90. data/spec/shared_stripe_examples/transfer_examples.rb +9 -9
  91. data/spec/shared_stripe_examples/webhook_event_examples.rb +11 -11
  92. data/spec/spec_helper.rb +7 -4
  93. data/spec/stripe_mock_spec.rb +4 -4
  94. data/spec/support/shared_contexts/stripe_validator_spec.rb +8 -0
  95. data/spec/support/stripe_examples.rb +12 -2
  96. data/stripe-ruby-mock.gemspec +8 -3
  97. metadata +81 -32
@@ -0,0 +1,12 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples 'Express Login Link API' do
4
+ describe 'create an Express Login Link' do
5
+ it 'creates a login link' do
6
+ account_link = Stripe::Account.create_login_link('acct_103ED82ePvKYlo2C')
7
+
8
+ expect(account_link).to be_a Stripe::LoginLink
9
+ expect(account_link.url).to start_with('https://connect.stripe.com/express/')
10
+ end
11
+ end
12
+ end
@@ -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)).to eq([@invoice.id, @invoice2.id])
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
@@ -79,7 +80,7 @@ shared_examples 'Invoice API' do
79
80
  end
80
81
 
81
82
  it 'updates attempted and paid flags' do
82
- @invoice.pay
83
+ @invoice = @invoice.pay
83
84
  expect(@invoice.attempted).to eq(true)
84
85
  expect(@invoice.paid).to eq(true)
85
86
  end
@@ -89,7 +90,7 @@ shared_examples 'Invoice API' do
89
90
  end
90
91
 
91
92
  it 'sets the charge attribute' do
92
- @invoice.pay
93
+ @invoice = @invoice.pay
93
94
  expect(@invoice.charge).to be_a String
94
95
  expect(@invoice.charge.length).to be > 0
95
96
  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
 
@@ -144,7 +146,7 @@ shared_examples 'Invoice API' do
144
146
  expect { Stripe::Invoice.upcoming(gazebo: 'raindance') }.to raise_error {|e|
145
147
  expect(e).to be_a(Stripe::InvalidRequestError)
146
148
  expect(e.http_status).to eq(400)
147
- expect(e.message).to eq('Missing required param: customer') }
149
+ expect(e.message).to eq('Missing required param: customer if subscription is not provided') }
148
150
  end
149
151
 
150
152
  it 'fails without a subscription' do
@@ -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') }
254
+ let(:new_yearly_plan) { stripe_helper.create_plan(id: '100y', product: product.id, amount: 100_00, interval: 'year') }
255
+ let(:plan) { stripe_helper.create_plan(id: '50m', product: product.id, amount: 50_00, interval: 'month') }
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 # -13322
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,12 +324,13 @@ 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 # -13322
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 be_within(1).of 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
@@ -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.date).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,147 @@
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
+ it "creates a requires_payment_method stripe payment_intent when amount matches 3055" do
40
+ payment_intent = Stripe::PaymentIntent.create(amount: 3055, currency: "usd")
41
+
42
+ expect(payment_intent.id).to match(/^test_pi/)
43
+ expect(payment_intent.amount).to eq(3055)
44
+ expect(payment_intent.currency).to eq('usd')
45
+ expect(payment_intent.metadata.to_hash).to eq({})
46
+ expect(payment_intent.status).to eq('requires_capture')
47
+ end
48
+
49
+ describe "listing payment_intent" do
50
+ before do
51
+ 3.times do
52
+ Stripe::PaymentIntent.create(amount: 100, currency: "usd")
53
+ end
54
+ end
55
+
56
+ it "without params retrieves all stripe payment_intent" do
57
+ expect(Stripe::PaymentIntent.list.count).to eq(3)
58
+ end
59
+
60
+ it "accepts a limit param" do
61
+ expect(Stripe::PaymentIntent.list(limit: 2).count).to eq(2)
62
+ end
63
+ end
64
+
65
+ it "retrieves a stripe payment_intent" do
66
+ original = Stripe::PaymentIntent.create(amount: 100, currency: "usd")
67
+ payment_intent = Stripe::PaymentIntent.retrieve(original.id)
68
+
69
+ expect(payment_intent.id).to eq(original.id)
70
+ expect(payment_intent.amount).to eq(original.amount)
71
+ expect(payment_intent.currency).to eq(original.currency)
72
+ expect(payment_intent.metadata.to_hash).to eq(original.metadata.to_hash)
73
+ end
74
+
75
+ it "cannot retrieve a payment_intent that doesn't exist" do
76
+ expect { Stripe::PaymentIntent.retrieve('nope') }.to raise_error {|e|
77
+ expect(e).to be_a Stripe::InvalidRequestError
78
+ expect(e.param).to eq('payment_intent')
79
+ expect(e.http_status).to eq(404)
80
+ }
81
+ end
82
+
83
+ it 'creates and confirms a stripe payment_intent with confirm flag to true' do
84
+ payment_intent = Stripe::PaymentIntent.create(
85
+ amount: 100, currency: 'usd', confirm: true
86
+ )
87
+ expect(payment_intent.status).to eq('succeeded')
88
+ expect(payment_intent.charges.data.size).to eq(1)
89
+ expect(payment_intent.charges.data.first.object).to eq('charge')
90
+ balance_txn = payment_intent.charges.data.first.balance_transaction
91
+ expect(balance_txn).to match(/^test_txn/)
92
+ expect(Stripe::BalanceTransaction.retrieve(balance_txn).id).to eq(balance_txn)
93
+ end
94
+
95
+ it "confirms a stripe payment_intent" do
96
+ payment_intent = Stripe::PaymentIntent.create(amount: 100, currency: "usd")
97
+ confirmed_payment_intent = payment_intent.confirm()
98
+ expect(confirmed_payment_intent.status).to eq("succeeded")
99
+ expect(confirmed_payment_intent.charges.data.size).to eq(1)
100
+ expect(confirmed_payment_intent.charges.data.first.object).to eq('charge')
101
+ end
102
+
103
+ it "captures a stripe payment_intent" do
104
+ payment_intent = Stripe::PaymentIntent.create(amount: 100, currency: "usd")
105
+ confirmed_payment_intent = payment_intent.capture()
106
+ expect(confirmed_payment_intent.status).to eq("succeeded")
107
+ expect(confirmed_payment_intent.charges.data.size).to eq(1)
108
+ expect(confirmed_payment_intent.charges.data.first.object).to eq('charge')
109
+ end
110
+
111
+ it "cancels a stripe payment_intent" do
112
+ payment_intent = Stripe::PaymentIntent.create(amount: 100, currency: "usd")
113
+ confirmed_payment_intent = payment_intent.cancel()
114
+ expect(confirmed_payment_intent.status).to eq("canceled")
115
+ end
116
+
117
+ it "updates a stripe payment_intent" do
118
+ original = Stripe::PaymentIntent.create(amount: 100, currency: "usd")
119
+ payment_intent = Stripe::PaymentIntent.retrieve(original.id)
120
+
121
+ payment_intent.amount = 200
122
+ payment_intent.save
123
+
124
+ updated = Stripe::PaymentIntent.retrieve(original.id)
125
+
126
+ expect(updated.amount).to eq(200)
127
+ end
128
+
129
+ it 'when amount is not integer', live: true do
130
+ expect { Stripe::PaymentIntent.create(amount: 400.2,
131
+ currency: 'usd') }.to raise_error { |e|
132
+ expect(e).to be_a Stripe::InvalidRequestError
133
+ expect(e.param).to eq('amount')
134
+ expect(e.http_status).to eq(400)
135
+ }
136
+ end
137
+
138
+ it 'when amount is negative', live: true do
139
+ expect { Stripe::PaymentIntent.create(amount: -400,
140
+ currency: 'usd') }.to raise_error { |e|
141
+ expect(e).to be_a Stripe::InvalidRequestError
142
+ expect(e.param).to eq('amount')
143
+ expect(e.message).to match(/^Invalid.*integer/)
144
+ expect(e.http_status).to eq(400)
145
+ }
146
+ end
147
+ end