stripe-ruby-mock 2.4.1 → 2.5.0

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 (44) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +6 -4
  3. data/README.md +10 -4
  4. data/lib/stripe_mock/api/client.rb +3 -3
  5. data/lib/stripe_mock/api/errors.rb +25 -14
  6. data/lib/stripe_mock/api/instance.rb +4 -4
  7. data/lib/stripe_mock/api/webhooks.rb +1 -1
  8. data/lib/stripe_mock/client.rb +2 -2
  9. data/lib/stripe_mock/data.rb +51 -26
  10. data/lib/stripe_mock/instance.rb +7 -3
  11. data/lib/stripe_mock/request_handlers/charges.rb +6 -6
  12. data/lib/stripe_mock/request_handlers/coupons.rb +3 -2
  13. data/lib/stripe_mock/request_handlers/customers.rb +16 -7
  14. data/lib/stripe_mock/request_handlers/helpers/card_helpers.rb +2 -2
  15. data/lib/stripe_mock/request_handlers/helpers/coupon_helpers.rb +6 -1
  16. data/lib/stripe_mock/request_handlers/helpers/subscription_helpers.rb +5 -5
  17. data/lib/stripe_mock/request_handlers/helpers/token_helpers.rb +2 -2
  18. data/lib/stripe_mock/request_handlers/invoices.rb +63 -11
  19. data/lib/stripe_mock/request_handlers/orders.rb +4 -4
  20. data/lib/stripe_mock/request_handlers/refunds.rb +3 -3
  21. data/lib/stripe_mock/request_handlers/subscriptions.rb +8 -8
  22. data/lib/stripe_mock/request_handlers/tokens.rb +1 -1
  23. data/lib/stripe_mock/request_handlers/transfers.rb +1 -1
  24. data/lib/stripe_mock/server.rb +1 -1
  25. data/lib/stripe_mock/version.rb +1 -1
  26. data/spec/instance_spec.rb +4 -4
  27. data/spec/integration_examples/prepare_error_examples.rb +6 -6
  28. data/spec/readme_spec.rb +2 -0
  29. data/spec/server_spec.rb +2 -2
  30. data/spec/shared_stripe_examples/card_examples.rb +3 -3
  31. data/spec/shared_stripe_examples/coupon_examples.rb +6 -0
  32. data/spec/shared_stripe_examples/customer_examples.rb +31 -3
  33. data/spec/shared_stripe_examples/dispute_examples.rb +9 -8
  34. data/spec/shared_stripe_examples/error_mock_examples.rb +3 -3
  35. data/spec/shared_stripe_examples/extra_features_examples.rb +2 -0
  36. data/spec/shared_stripe_examples/invoice_examples.rb +232 -48
  37. data/spec/shared_stripe_examples/recipient_examples.rb +7 -7
  38. data/spec/shared_stripe_examples/subscription_examples.rb +52 -16
  39. data/spec/shared_stripe_examples/transfer_examples.rb +8 -6
  40. data/spec/shared_stripe_examples/webhook_event_examples.rb +3 -3
  41. data/spec/spec_helper.rb +6 -5
  42. data/spec/stripe_mock_spec.rb +3 -3
  43. data/stripe-ruby-mock.gemspec +1 -1
  44. metadata +4 -10
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a861480c10df772899ac63e55dbc97d84ff3eb2c
4
- data.tar.gz: 3d44f183f03df9af022be2b36b49fe45a01ed1e6
3
+ metadata.gz: ce6d1ae38721fc9f43fa69e3d82217593b0789d9
4
+ data.tar.gz: abbbfb0dd70438e23ac276215602a3d3a77705b1
5
5
  SHA512:
6
- metadata.gz: 38a0c8592b9dc4181fdd8730e9b2d2496f852911a480eddc3621a7061d7cd0e1a6e6268a4f722be98483b58b32dd03eb7664135caaa99a638fce5dc70d20d10c
7
- data.tar.gz: 44f924ab949f7101b1854f413b7120a0cafc5432c83ae23ab993c4cde6e879445f33ef8fa863a066d24bc06c14d3f9c299a5d6917f58890fd32a76a07ed72be9
6
+ metadata.gz: 49d92a3efee46c78cd157fd84de5e8eb456b6686b06c00a23262648395b0dd2cb647620304de703e52e5919102ce6c782a4947b50c39aef90428a02af9cfe9b0
7
+ data.tar.gz: c87d09dd495bdca773e0ea39169f04900e7467f315f220ca556d907e9c0f294ddad399a73c2765e27e9ebe319b42f1b68b9ef825a02707c4877096777154a8c4
@@ -1,12 +1,14 @@
1
1
  dist: trusty
2
+ group: deprecated-2017Q2
2
3
  sudo: required
3
4
  language: ruby
4
5
  rvm:
5
- - 1.9.3
6
6
  - 2.0.0
7
- - 2.1.6
7
+ - 2.1.10
8
+ - 2.2.7
9
+ - 2.3.4
8
10
  before_install:
9
- - rvm 1.9.3 do gem install mime-types -v 2.6.2
11
+ - rvm 2.1.10 do gem install mime-types -v 2.6.2
10
12
  - gem install bundler
11
13
  before_script:
12
14
  - "sudo touch /var/log/stripe-mock-server.log"
@@ -15,7 +17,7 @@ script: "bundle exec rspec && bundle exec rspec -t live"
15
17
 
16
18
  env:
17
19
  global:
18
- - IS_TRAVIS=true STRIPE_TEST_SECRET_KEY_A=sk_test_sXdhUWu3NhrB7r1tkK0zZfMW STRIPE_TEST_SECRET_KEY_B=sk_test_v2K2hV9VJ8AoRKTeDRkQ3rG3 STRIPE_TEST_SECRET_KEY_C=sk_test_QNgKQJhW4fWYT63be2AUSVjJ
20
+ - IS_TRAVIS=true STRIPE_TEST_SECRET_KEY_A=sk_test_sXdhUWu3NhrB7r1tkK0zZfMW STRIPE_TEST_SECRET_KEY_B=sk_test_uPfIX9ziFNloXwtSdDPJTdnh STRIPE_TEST_SECRET_KEY_C=sk_test_waSy1TaP2RNEpoz9t2pFCysm STRIPE_TEST_SECRET_KEY_D=sk_test_Z1mQZNehRFmI3EN9mHnMafnq
19
21
 
20
22
  notifications:
21
23
  webhooks:
data/README.md CHANGED
@@ -12,7 +12,7 @@ This gem has unexpectedly grown in popularity and I've gotten pretty busy, so I'
12
12
 
13
13
  In your gemfile:
14
14
 
15
- gem 'stripe-ruby-mock', '~> 2.4.1', :require => 'stripe_mock'
15
+ gem 'stripe-ruby-mock', '~> 2.5.0', :require => 'stripe_mock'
16
16
 
17
17
  ## Features
18
18
 
@@ -21,12 +21,18 @@ In your gemfile:
21
21
  * Mock and customize stripe webhooks
22
22
  * Flip a switch to run your tests against Stripe's **live test servers**
23
23
 
24
+ ### Requirements
25
+
26
+ * ruby >= 2.0.0
27
+ * stripe >= 2.0.3
28
+
24
29
  ### Specifications
25
30
 
26
- **STRIPE API TARGET VERSION:** 2015-09-08 (master)
31
+ **STRIPE API TARGET VERSION:** 2017-06-05 (master)
27
32
 
28
33
  Older API version branches:
29
34
 
35
+ - api-2015-09-08 - use gem version 2.4.1
30
36
  - [api-2014-06-17](https://github.com/rebelidealist/stripe-ruby-mock/tree/api-2014-06-17)
31
37
 
32
38
  ### Versioning System
@@ -294,9 +300,9 @@ it "mocks a stripe webhook" do
294
300
  end
295
301
 
296
302
  it "mocks stripe connect webhooks" do
297
- event = StripeMock.mock_webhook_event('customer.created', user_id: 'acc_123123')
303
+ event = StripeMock.mock_webhook_event('customer.created', account: 'acc_123123')
298
304
 
299
- expect(event.user_id).to eq('acc_123123')
305
+ expect(event.account).to eq('acc_123123')
300
306
  end
301
307
  ```
302
308
 
@@ -8,7 +8,7 @@ module StripeMock
8
8
  return false if @state == 'live'
9
9
  return @client unless @client.nil?
10
10
 
11
- alias_stripe_method :request, StripeMock.method(:redirect_to_mock_server)
11
+ alias_stripe_method :execute_request, StripeMock.method(:redirect_to_mock_server)
12
12
  @client = StripeMock::Client.new(port)
13
13
  @state = 'remote'
14
14
  @client
@@ -27,7 +27,7 @@ module StripeMock
27
27
 
28
28
  private
29
29
 
30
- def self.redirect_to_mock_server(method, url, api_key, params={}, headers={}, api_base_url=nil)
30
+ def self.redirect_to_mock_server(method, url, api_key: nil, api_base: nil, params: {}, headers: {})
31
31
  handler = Instance.handler_for_method_url("#{method} #{url}")
32
32
 
33
33
  if mock_error = client.error_queue.error_for_handler_name(handler[:name])
@@ -35,7 +35,7 @@ module StripeMock
35
35
  raise mock_error
36
36
  end
37
37
 
38
- Stripe::Util.symbolize_names client.mock_request(method, url, api_key, params, headers)
38
+ Stripe::Util.symbolize_names client.mock_request(method, url, api_key: api_key, params: params, headers: headers)
39
39
  end
40
40
 
41
41
  end
@@ -24,29 +24,40 @@ module StripeMock
24
24
 
25
25
  def self.argument_map
26
26
  @__map ||= {
27
- incorrect_number: add_json_body(["The card number is incorrect", 'number', 'incorrect_number', 402]),
28
- invalid_number: add_json_body(["The card number is not a valid credit card number", 'number', 'invalid_number', 402]),
29
- invalid_expiry_month: add_json_body(["The card's expiration month is invalid", 'exp_month', 'invalid_expiry_month', 402]),
30
- invalid_expiry_year: add_json_body(["The card's expiration year is invalid", 'exp_year', 'invalid_expiry_year', 402]),
31
- invalid_cvc: add_json_body(["The card's security code is invalid", 'cvc', 'invalid_cvc', 402]),
32
- expired_card: add_json_body(["The card has expired", 'exp_month', 'expired_card', 402]),
33
- incorrect_cvc: add_json_body(["The card's security code is incorrect", 'cvc', 'incorrect_cvc', 402]),
34
- card_declined: add_json_body(["The card was declined", nil, 'card_declined', 402]),
35
- missing: add_json_body(["There is no card on a customer that is being charged.", nil, 'missing', 402]),
36
- processing_error: add_json_body(["An error occurred while processing the card", nil, 'processing_error', 402]),
37
- card_error: add_json_body(['The card number is not a valid credit card number.', 'number', 'invalid_number', 402]),
38
- incorrect_zip: add_json_body(['The zip code you supplied failed validation.', 'address_zip', 'incorrect_zip', 402])
27
+ incorrect_number: add_json_body(["The card number is incorrect", 'number', 'incorrect_number', http_status: 402]),
28
+ invalid_number: add_json_body(["The card number is not a valid credit card number", 'number', 'invalid_number', http_status: 402]),
29
+ invalid_expiry_month: add_json_body(["The card's expiration month is invalid", 'exp_month', 'invalid_expiry_month', http_status: 402]),
30
+ invalid_expiry_year: add_json_body(["The card's expiration year is invalid", 'exp_year', 'invalid_expiry_year', http_status: 402]),
31
+ invalid_cvc: add_json_body(["The card's security code is invalid", 'cvc', 'invalid_cvc', http_status: 402]),
32
+ expired_card: add_json_body(["The card has expired", 'exp_month', 'expired_card', http_status: 402]),
33
+ incorrect_cvc: add_json_body(["The card's security code is incorrect", 'cvc', 'incorrect_cvc', http_status: 402]),
34
+ card_declined: add_json_body(["The card was declined", nil, 'card_declined', http_status: 402]),
35
+ missing: add_json_body(["There is no card on a customer that is being charged.", nil, 'missing', http_status: 402]),
36
+ processing_error: add_json_body(["An error occurred while processing the card", nil, 'processing_error', http_status: 402]),
37
+ card_error: add_json_body(['The card number is not a valid credit card number.', 'number', 'invalid_number', http_status: 402]),
38
+ incorrect_zip: add_json_body(['The zip code you supplied failed validation.', 'address_zip', 'incorrect_zip', http_status: 402])
39
39
  }
40
40
  end
41
41
 
42
+ def self.get_decline_code(code)
43
+ decline_code_map = {
44
+ card_declined: 'do_not_honor',
45
+ missing: nil
46
+ }
47
+ decline_code_map.default = code.to_s
48
+
49
+ code_key = code.to_sym
50
+ decline_code_map[code_key]
51
+ end
52
+
42
53
  def self.add_json_body(error_values)
43
54
  error_keys = [:message, :param, :code]
44
55
 
45
56
  json_hash = Hash[error_keys.zip error_values]
46
57
  json_hash[:type] = 'card_error'
58
+ json_hash[:decline_code] = get_decline_code(json_hash[:code])
47
59
 
48
- error_values.push(error: json_hash) # http_body
49
- error_values.push(error: json_hash) # json_body
60
+ error_values.last.merge!(json_body: { error: json_hash }, http_body: { error: json_hash })
50
61
 
51
62
  error_values
52
63
  end
@@ -2,18 +2,18 @@ module StripeMock
2
2
 
3
3
  @state = 'ready'
4
4
  @instance = nil
5
- @original_request_method = Stripe.method(:request)
5
+ @original_request_method = Stripe::StripeClient.active_client.method(:execute_request)
6
6
 
7
7
  def self.start
8
8
  return false if @state == 'live'
9
9
  @instance = Instance.new
10
- alias_stripe_method :request, @instance.method(:mock_request)
10
+ alias_stripe_method :execute_request, @instance.method(:mock_request)
11
11
  @state = 'local'
12
12
  end
13
13
 
14
14
  def self.stop
15
15
  return unless @state == 'local'
16
- alias_stripe_method :request, @original_request_method
16
+ alias_stripe_method :execute_request, @original_request_method
17
17
  @instance = nil
18
18
  @state = 'ready'
19
19
  end
@@ -29,7 +29,7 @@ module StripeMock
29
29
  end
30
30
 
31
31
  def self.alias_stripe_method(new_name, method_object)
32
- Stripe.define_singleton_method(new_name) {|*args| method_object.call(*args) }
32
+ Stripe::StripeClient.active_client.define_singleton_method(new_name) {|*args| method_object.call(*args) }
33
33
  end
34
34
 
35
35
  def self.instance; @instance; end
@@ -15,7 +15,7 @@ module StripeMock
15
15
 
16
16
  json = Stripe::Util.symbolize_names(json)
17
17
  params = Stripe::Util.symbolize_names(params)
18
- json[:user_id] = params.delete(:user_id) if params.key?(:user_id)
18
+ json[:account] = params.delete(:account) if params.key?(:account)
19
19
  json[:data][:object] = Util.rmerge(json[:data][:object], params)
20
20
  json.delete(:id)
21
21
 
@@ -13,9 +13,9 @@ module StripeMock
13
13
  @state = 'ready'
14
14
  end
15
15
 
16
- def mock_request(method, url, api_key, params={}, headers={})
16
+ def mock_request(method, url, api_key: nil, params: {}, headers: {})
17
17
  timeout_wrap do
18
- @pipe.mock_request(method, url, api_key, params, headers).tap {|result|
18
+ @pipe.mock_request(method, url, api_key: api_key, params: params, headers: headers).tap {|result|
19
19
  response, api_key = result
20
20
  if response.is_a?(Hash) && response[:error_raised] == 'invalid_request'
21
21
  raise Stripe::InvalidRequestError.new(*response[:error_params])
@@ -283,29 +283,44 @@ module StripeMock
283
283
  #FIXME nested overrides would be better than hardcoding plan_id
284
284
  def self.mock_subscription(params={})
285
285
  StripeMock::Util.rmerge({
286
- :created => 1478204116,
287
- :current_period_start => 1308595038,
288
- :current_period_end => 1308681468,
289
- :status => "trialing",
290
- :plan => {
291
- :interval => "month",
292
- :amount => 7500,
293
- :trial_period_days => 30,
294
- :object => "plan",
295
- :id => '__test_plan_id__'
286
+ created: 1478204116,
287
+ current_period_start: 1308595038,
288
+ current_period_end: 1308681468,
289
+ status: 'trialing',
290
+ plan: {
291
+ interval: 'month',
292
+ amount: 7500,
293
+ trial_period_days: 30,
294
+ object: 'plan',
295
+ id: '__test_plan_id__'
296
296
  },
297
- :cancel_at_period_end => false,
298
- :canceled_at => nil,
299
- :ended_at => nil,
300
- :start => 1308595038,
301
- :object => "subscription",
302
- :trial_start => 1308595038,
303
- :trial_end => 1308681468,
304
- :customer => "c_test_customer",
305
- :quantity => 1,
306
- :tax_percent => nil,
307
- :discount => nil,
308
- :metadata => {}
297
+ items: {
298
+ object: 'list',
299
+ data: [{
300
+ id: 'si_1AwFf62eZvKYlo2C9u6Dhf9',
301
+ created: 1504035973,
302
+ metadata: {},
303
+ object: 'subscription_item',
304
+ plan: {
305
+ amount: 999,
306
+ created: 1504035972,
307
+ currency: 'usd'
308
+ },
309
+ quantity: 1
310
+ }]
311
+ },
312
+ cancel_at_period_end: false,
313
+ canceled_at: nil,
314
+ ended_at: nil,
315
+ start: 1308595038,
316
+ object: 'subscription',
317
+ trial_start: 1308595038,
318
+ trial_end: 1308681468,
319
+ customer: 'c_test_customer',
320
+ quantity: 1,
321
+ tax_percent: nil,
322
+ discount: nil,
323
+ metadata: {}
309
324
  }, params)
310
325
  end
311
326
 
@@ -313,7 +328,7 @@ module StripeMock
313
328
  in_id = params[:id] || "test_in_default"
314
329
  currency = params[:currency] || 'usd'
315
330
  lines << Data.mock_line_item() if lines.empty?
316
- {
331
+ invoice = {
317
332
  id: 'in_test_invoice',
318
333
  date: 1349738950,
319
334
  period_end: 1349738950,
@@ -325,7 +340,6 @@ module StripeMock
325
340
  data: lines
326
341
  },
327
342
  subtotal: lines.map {|line| line[:amount]}.reduce(0, :+),
328
- total: lines.map {|line| line[:amount]}.reduce(0, :+),
329
343
  customer: "test_customer",
330
344
  object: 'invoice',
331
345
  attempted: false,
@@ -342,7 +356,7 @@ module StripeMock
342
356
  webhooks_delivered_at: 1349825350,
343
357
  livemode: false,
344
358
  attempt_count: 0,
345
- amount_due: lines.map {|line| line[:amount]}.reduce(0, :+),
359
+ amount_due: nil,
346
360
  currency: currency,
347
361
  starting_balance: 0,
348
362
  ending_balance: nil,
@@ -351,6 +365,15 @@ module StripeMock
351
365
  discount: nil,
352
366
  subscription: nil
353
367
  }.merge(params)
368
+ if invoice[:discount]
369
+ invoice[:total] = [0, invoice[:subtotal] - invoice[:discount][:coupon][:amount_off]].max if invoice[:discount][:coupon][:amount_off]
370
+ invoice[:total] = invoice[:subtotal] * invoice[:discount][:coupon][:percent_off] / 100 if invoice[:discount][:coupon][:percent_off]
371
+ else
372
+ invoice[:total] = invoice[:subtotal]
373
+ end
374
+ due = invoice[:total] + invoice[:starting_balance]
375
+ invoice[:amount_due] = due < 0 ? 0 : due
376
+ invoice
354
377
  end
355
378
 
356
379
  def self.mock_line_item(params = {})
@@ -582,6 +605,7 @@ module StripeMock
582
605
  :fee_details => [],
583
606
  :id => id,
584
607
  :livemode => false,
608
+ :metadata => {},
585
609
  :currency => currency,
586
610
  :object => "transfer",
587
611
  :date => 1304114826,
@@ -605,6 +629,7 @@ module StripeMock
605
629
  end
606
630
 
607
631
  def self.mock_dispute(params={})
632
+ @timestamp ||= Time.now.to_i
608
633
  currency = params[:currency] || 'usd'
609
634
  id = params[:id] || "dp_test_dispute"
610
635
  {
@@ -613,7 +638,7 @@ module StripeMock
613
638
  :amount => 195,
614
639
  :balance_transactions => [],
615
640
  :charge => "ch_15RsQR2eZvKYlo2CA8IfzCX0",
616
- :created => 1422915137,
641
+ :created => @timestamp += 1,
617
642
  :currency => currency,
618
643
  :evidence => self.mock_dispute_evidence,
619
644
  :evidence_details => self.mock_dispute_evidence_details,
@@ -77,7 +77,7 @@ module StripeMock
77
77
  @base_strategy = TestStrategies::Base.new
78
78
  end
79
79
 
80
- def mock_request(method, url, api_key, params={}, headers={}, api_base_url=nil)
80
+ def mock_request(method, url, api_key: nil, api_base: nil, params: {}, headers: {})
81
81
  return {} if method == :xtest
82
82
 
83
83
  api_key ||= (Stripe.api_key || DUMMY_API_KEY)
@@ -100,7 +100,7 @@ module StripeMock
100
100
  else
101
101
  res = self.send(handler[:name], handler[:route], method_url, params, headers)
102
102
  puts " [res] #{res}" if @debug == true
103
- [res, api_key]
103
+ [to_faraday_hash(res), api_key]
104
104
  end
105
105
  else
106
106
  puts "[StripeMock] Warning : Unrecognized endpoint + method : [#{method} #{url}]"
@@ -146,7 +146,7 @@ module StripeMock
146
146
  def assert_existence(type, id, obj, message=nil)
147
147
  if obj.nil?
148
148
  msg = message || "No such #{type}: #{id}"
149
- raise Stripe::InvalidRequestError.new(msg, type.to_s, 404)
149
+ raise Stripe::InvalidRequestError.new(msg, type.to_s, http_status: 404)
150
150
  end
151
151
  obj
152
152
  end
@@ -173,5 +173,9 @@ module StripeMock
173
173
  Stripe::Util.symbolize_names(hash)
174
174
  end
175
175
 
176
+ def to_faraday_hash(hash)
177
+ response = Struct.new(:data)
178
+ response.new(hash)
179
+ end
176
180
  end
177
181
  end
@@ -31,7 +31,7 @@ module StripeMock
31
31
  params[:source] = get_card_or_bank_by_token(params[:source])
32
32
  end
33
33
  elsif params[:source][:id]
34
- raise Stripe::InvalidRequestError.new("Invalid token id: #{params[:source]}", 'card', 400)
34
+ raise Stripe::InvalidRequestError.new("Invalid token id: #{params[:source]}", 'card', http_status: 400)
35
35
  end
36
36
  elsif params[:customer]
37
37
  customer = customers[params[:customer]]
@@ -66,7 +66,7 @@ module StripeMock
66
66
  allowed = allowed_params(params)
67
67
  disallowed = params.keys - allowed
68
68
  if disallowed.count > 0
69
- raise Stripe::InvalidRequestError.new("Received unknown parameters: #{disallowed.join(', ')}" , '', 400)
69
+ raise Stripe::InvalidRequestError.new("Received unknown parameters: #{disallowed.join(', ')}" , '', http_status: 400)
70
70
  end
71
71
 
72
72
  charges[id] = Util.rmerge(charge, params)
@@ -139,11 +139,11 @@ module StripeMock
139
139
  elsif params[:currency].nil?
140
140
  require_param(:currency)
141
141
  elsif non_integer_charge_amount?(params)
142
- raise Stripe::InvalidRequestError.new("Invalid integer: #{params[:amount]}", 'amount', 400)
142
+ raise Stripe::InvalidRequestError.new("Invalid integer: #{params[:amount]}", 'amount', http_status: 400)
143
143
  elsif non_positive_charge_amount?(params)
144
- raise Stripe::InvalidRequestError.new('Invalid positive integer', 'amount', 400)
144
+ raise Stripe::InvalidRequestError.new('Invalid positive integer', 'amount', http_status: 400)
145
145
  elsif params[:source].nil? && params[:customer].nil?
146
- raise Stripe::InvalidRequestError.new('Must provide source or customer.', nil)
146
+ raise Stripe::InvalidRequestError.new('Must provide source or customer.', http_status: nil)
147
147
  end
148
148
  end
149
149
 
@@ -156,7 +156,7 @@ module StripeMock
156
156
  end
157
157
 
158
158
  def require_param(param)
159
- raise Stripe::InvalidRequestError.new("Missing required param: #{param}", param.to_s, 400)
159
+ raise Stripe::InvalidRequestError.new("Missing required param: #{param}", param.to_s, http_status: 400)
160
160
  end
161
161
 
162
162
  def allowed_params(params)
@@ -11,8 +11,9 @@ module StripeMock
11
11
 
12
12
  def new_coupon(route, method_url, params, headers)
13
13
  params[:id] ||= new_id('coupon')
14
- raise Stripe::InvalidRequestError.new('Missing required param: duration', 'coupon', 400) unless params[:duration]
15
- coupons[ params[:id] ] = Data.mock_coupon(params)
14
+ raise Stripe::InvalidRequestError.new('Missing required param: duration', 'coupon', http_status: 400) unless params[:duration]
15
+ raise Stripe::InvalidRequestError.new('You must pass currency when passing amount_off', 'coupon', http_status: 400) if params[:amount_off] && !params[:currency]
16
+ coupons[ params[:id] ] = Data.mock_coupon({amount_off: nil, percent_off:nil}.merge(params))
16
17
  end
17
18
 
18
19
  def get_coupon(route, method_url, params, headers)
@@ -19,7 +19,7 @@ module StripeMock
19
19
  new_card =
20
20
  if params[:source].is_a?(Hash)
21
21
  unless params[:source][:object] && params[:source][:number] && params[:source][:exp_month] && params[:source][:exp_year]
22
- raise Stripe::InvalidRequestError.new('You must supply a valid card', nil, 400)
22
+ raise Stripe::InvalidRequestError.new('You must supply a valid card', nil, http_status: 400)
23
23
  end
24
24
  card_from_params(params[:source])
25
25
  else
@@ -36,7 +36,7 @@ module StripeMock
36
36
  plan = assert_existence :plan, plan_id, plans[plan_id]
37
37
 
38
38
  if params[:default_source].nil? && params[:trial_end].nil? && plan[:trial_period_days].nil? && plan[:amount] != 0
39
- raise Stripe::InvalidRequestError.new('You must supply a valid card', nil, 400)
39
+ raise Stripe::InvalidRequestError.new('You must supply a valid card', nil, http_status: 400)
40
40
  end
41
41
 
42
42
  subscription = Data.mock_subscription({ id: new_id('su') })
@@ -44,7 +44,7 @@ module StripeMock
44
44
  add_subscription_to_customer(customers[ params[:id] ], subscription)
45
45
  subscriptions[subscription[:id]] = subscription
46
46
  elsif params[:trial_end]
47
- raise Stripe::InvalidRequestError.new('Received unknown parameter: trial_end', nil, 400)
47
+ raise Stripe::InvalidRequestError.new('Received unknown parameter: trial_end', nil, http_status: 400)
48
48
  end
49
49
 
50
50
  if params[:coupon]
@@ -78,7 +78,7 @@ module StripeMock
78
78
  new_card = get_card_or_bank_by_token(params.delete(:source))
79
79
  elsif params[:source].is_a?(Hash)
80
80
  unless params[:source][:object] && params[:source][:number] && params[:source][:exp_month] && params[:source][:exp_year]
81
- raise Stripe::InvalidRequestError.new('You must supply a valid card', nil, 400)
81
+ raise Stripe::InvalidRequestError.new('You must supply a valid card', nil, http_status: 400)
82
82
  end
83
83
  new_card = card_from_params(params.delete(:source))
84
84
  end
@@ -108,7 +108,16 @@ module StripeMock
108
108
 
109
109
  def get_customer(route, method_url, params, headers)
110
110
  route =~ method_url
111
- assert_existence :customer, $1, customers[$1]
111
+ customer = assert_existence :customer, $1, customers[$1]
112
+
113
+ customer = customer.clone
114
+ if params[:expand] == ['default_source']
115
+ customer[:default_source] = customer[:sources][:data].detect do |source|
116
+ source[:id] == customer[:default_source]
117
+ end
118
+ end
119
+
120
+ customer
112
121
  end
113
122
 
114
123
  def list_customers(route, method_url, params, headers)
@@ -118,9 +127,9 @@ module StripeMock
118
127
  def delete_customer_discount(route, method_url, params, headers)
119
128
  route =~ method_url
120
129
  cus = assert_existence :customer, $1, customers[$1]
121
-
130
+
122
131
  cus[:discount] = nil
123
-
132
+
124
133
  cus
125
134
  end
126
135
  end