stripe-ruby-mock 2.2.4 → 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/lib/stripe_mock/api/bank_tokens.rb +5 -5
  4. data/lib/stripe_mock/api/card_tokens.rb +4 -4
  5. data/lib/stripe_mock/api/client.rb +6 -3
  6. data/lib/stripe_mock/api/errors.rb +4 -4
  7. data/lib/stripe_mock/api/server.rb +0 -1
  8. data/lib/stripe_mock/client.rb +12 -7
  9. data/lib/stripe_mock/data.rb +48 -2
  10. data/lib/stripe_mock/error_queue.rb +5 -1
  11. data/lib/stripe_mock/instance.rb +5 -3
  12. data/lib/stripe_mock/request_handlers/balance_transactions.rb +21 -0
  13. data/lib/stripe_mock/request_handlers/charges.rb +17 -10
  14. data/lib/stripe_mock/request_handlers/customers.rb +1 -0
  15. data/lib/stripe_mock/request_handlers/helpers/card_helpers.rb +35 -0
  16. data/lib/stripe_mock/request_handlers/helpers/token_helpers.rb +2 -2
  17. data/lib/stripe_mock/request_handlers/sources.rb +1 -1
  18. data/lib/stripe_mock/request_handlers/subscriptions.rb +79 -17
  19. data/lib/stripe_mock/request_handlers/tokens.rb +14 -4
  20. data/lib/stripe_mock/server.rb +21 -15
  21. data/lib/stripe_mock/test_strategies/base.rb +15 -0
  22. data/lib/stripe_mock/version.rb +1 -1
  23. data/lib/stripe_mock.rb +2 -1
  24. data/spec/server_spec.rb +1 -1
  25. data/spec/shared_stripe_examples/balance_transaction_examples.rb +35 -0
  26. data/spec/shared_stripe_examples/bank_examples.rb +202 -0
  27. data/spec/shared_stripe_examples/bank_token_examples.rb +8 -0
  28. data/spec/shared_stripe_examples/card_token_examples.rb +10 -0
  29. data/spec/shared_stripe_examples/charge_examples.rb +27 -1
  30. data/spec/shared_stripe_examples/customer_examples.rb +2 -1
  31. data/spec/shared_stripe_examples/invoice_examples.rb +12 -12
  32. data/spec/shared_stripe_examples/refund_examples.rb +13 -0
  33. data/spec/shared_stripe_examples/subscription_examples.rb +47 -56
  34. data/spec/support/stripe_examples.rb +5 -3
  35. data/stripe-ruby-mock.gemspec +2 -2
  36. metadata +14 -9
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 907a296926746f9f85a5971db9272ffe4c7cd8e7
4
- data.tar.gz: 41deab127c933031f4e08fe4b54d0e62b5268cdb
3
+ metadata.gz: e17b14c466a95e28e23f56859b0f20ce0fc0e6f2
4
+ data.tar.gz: 14b2121916a871814374262884df72a0501c32d9
5
5
  SHA512:
6
- metadata.gz: 55104b30501886b7a11efbef32fb28eb1c5eb2c2d130bd9370e532f2ef417ee9a4338e0a08d1993f6bc62fe1a659ac7596aa51ff7a851c225b1054d228cd1b61
7
- data.tar.gz: 430679dd30c713bd45a8ae8831c855862848706c01f46c871e66a2ae3e906a85944f11179dc596d6d729705287193ecb9146cc960636f4bf69434faf06794914
6
+ metadata.gz: 1cf3ee166d1a5e9cac2b1c61bf50822568986b84fdaff85166739137d936bfe34f0cdf08ddc25ee6781412cb8a84dd55b4b4beae2a440fecf4966f6c042a7316
7
+ data.tar.gz: 8237f3fbfbc338f16e64ac7671acf8715254fd35fd6ab992e55c34c68dd79bc15c4526831694e954df9327428c55cca9d32394d1d3b7fa89e2e7d4846e69f5e1
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.2.4', :require => 'stripe_mock'
15
+ gem 'stripe-ruby-mock', '~> 2.3.0', :require => 'stripe_mock'
16
16
 
17
17
  ## Features
18
18
 
@@ -1,13 +1,13 @@
1
1
  module StripeMock
2
2
 
3
- def self.generate_bank_token(bank_params)
4
- if @state == 'local'
3
+ def self.generate_bank_token(bank_params = {})
4
+ case @state
5
+ when 'local'
5
6
  instance.generate_bank_token(bank_params)
6
- elsif @state == 'remote'
7
+ when 'remote'
7
8
  client.generate_bank_token(bank_params)
8
9
  else
9
10
  raise UnstartedStateError
10
11
  end
11
12
  end
12
-
13
- end
13
+ end
@@ -1,13 +1,13 @@
1
1
  module StripeMock
2
2
 
3
- def self.generate_card_token(card_params)
4
- if @state == 'local'
3
+ def self.generate_card_token(card_params = {})
4
+ case @state
5
+ when 'local'
5
6
  instance.generate_card_token(card_params)
6
- elsif @state == 'remote'
7
+ when 'remote'
7
8
  client.generate_card_token(card_params)
8
9
  else
9
10
  raise UnstartedStateError
10
11
  end
11
12
  end
12
-
13
13
  end
@@ -1,6 +1,8 @@
1
1
  module StripeMock
2
2
 
3
- def self.client; @client; end
3
+ def self.client
4
+ @client
5
+ end
4
6
 
5
7
  def self.start_client(port=4999)
6
8
  return false if @state == 'live'
@@ -27,11 +29,12 @@ module StripeMock
27
29
 
28
30
  def self.redirect_to_mock_server(method, url, api_key, params={}, headers={}, api_base_url=nil)
29
31
  handler = Instance.handler_for_method_url("#{method} #{url}")
30
- mock_error = client.error_queue.error_for_handler_name(handler[:name])
31
- if mock_error
32
+
33
+ if mock_error = client.error_queue.error_for_handler_name(handler[:name])
32
34
  client.error_queue.dequeue
33
35
  raise mock_error
34
36
  end
37
+
35
38
  Stripe::Util.symbolize_names client.mock_request(method, url, api_key, params, headers)
36
39
  end
37
40
 
@@ -4,12 +4,12 @@ module StripeMock
4
4
  handler_names.push(:all) if handler_names.count == 0
5
5
 
6
6
  if @state == 'local'
7
- instance.error_queue.queue(stripe_error, handler_names)
7
+ instance
8
8
  elsif @state == 'remote'
9
- client.error_queue.queue(stripe_error, handler_names)
9
+ client
10
10
  else
11
11
  raise UnstartedStateError
12
- end
12
+ end.error_queue.queue stripe_error, handler_names
13
13
  end
14
14
 
15
15
  def self.prepare_card_error(code, *handler_names)
@@ -17,7 +17,7 @@ module StripeMock
17
17
 
18
18
  args = CardErrors.argument_map[code]
19
19
  raise StripeMockError.new("Unrecognized stripe card error code: #{code}") if args.nil?
20
- self.prepare_error Stripe::CardError.new(*args), *handler_names
20
+ self.prepare_error Stripe::CardError.new(*args), *handler_names
21
21
  end
22
22
 
23
23
  module CardErrors
@@ -1,5 +1,4 @@
1
1
  module StripeMock
2
-
3
2
  @default_server_pid_path = './stripe-mock-server.pid'
4
3
  @default_server_log_path = './stripe-mock-server.log'
5
4
 
@@ -1,23 +1,24 @@
1
1
  module StripeMock
2
-
3
2
  class Client
4
- attr_reader :port, :state, :error_queue
3
+ attr_reader :port, :state
5
4
 
6
5
  def initialize(port)
7
6
  @port = port
8
- @pipe = Jimson::Client.new("http://0.0.0.0:#{port}")
7
+
8
+ DRb.start_service
9
+ @pipe = DRbObject.new_with_uri "druby://localhost:#{port}"
10
+
9
11
  # Ensure client can connect to server
10
12
  timeout_wrap(5) { @pipe.ping }
11
13
  @state = 'ready'
12
- @error_queue = ErrorQueue.new
13
14
  end
14
15
 
15
16
  def mock_request(method, url, api_key, params={}, headers={})
16
17
  timeout_wrap do
17
18
  @pipe.mock_request(method, url, api_key, params, headers).tap {|result|
18
19
  response, api_key = result
19
- if response.is_a?(Hash) && response['error_raised'] == 'invalid_request'
20
- raise Stripe::InvalidRequestError.new(*response['error_params'])
20
+ if response.is_a?(Hash) && response[:error_raised] == 'invalid_request'
21
+ raise Stripe::InvalidRequestError.new(*response[:error_params])
21
22
  end
22
23
  }
23
24
  end
@@ -32,6 +33,10 @@ module StripeMock
32
33
  }
33
34
  end
34
35
 
36
+ def error_queue
37
+ timeout_wrap { @pipe.error_queue }
38
+ end
39
+
35
40
  def set_server_debug(toggle)
36
41
  timeout_wrap { @pipe.set_debug(toggle) }
37
42
  end
@@ -86,7 +91,7 @@ module StripeMock
86
91
  yield
87
92
  rescue ClosedClientConnectionError
88
93
  raise
89
- rescue Errno::ECONNREFUSED => e
94
+ rescue Errno::ECONNREFUSED, DRb::DRbConnError => e
90
95
  tries -= 1
91
96
  if tries > 0
92
97
  if tries == original_tries - 1
@@ -192,7 +192,9 @@ module StripeMock
192
192
  object: "refund",
193
193
  balance_transaction: "txn_4fWh2RKvgxcXqV",
194
194
  metadata: {},
195
- charge: "ch_4fWhYjzQ23UFWT"
195
+ charge: "ch_4fWhYjzQ23UFWT",
196
+ receipt_number: nil,
197
+ status: "succeeded"
196
198
  }.merge(params)
197
199
  end
198
200
 
@@ -695,9 +697,53 @@ module StripeMock
695
697
  }
696
698
  end
697
699
 
698
- def self.mock_list_object(data, params = {})
700
+ def self.mock_list_object(data, params={})
699
701
  list = StripeMock::Data::List.new(data, params)
700
702
  list.to_h
701
703
  end
704
+
705
+ def self.mock_balance_transactions(ids=[])
706
+ bts = {}
707
+ ids.each do |id|
708
+ bts[id] = self.mock_balance_transaction(id: id)
709
+ end
710
+ bts
711
+ end
712
+
713
+ def self.mock_balance_transaction(params = {})
714
+ bt_id = params[:id] || 'test_txn_default'
715
+ source = params[:source] || 'ch_test_charge'
716
+ {
717
+ id: bt_id,
718
+ object: "balance_transaction",
719
+ amount: 10000,
720
+ available_on: 1462406400,
721
+ created: 1461880226,
722
+ currency: "usd",
723
+ description: nil,
724
+ fee: 320,
725
+ fee_details: [
726
+ {
727
+ amount: 320,
728
+ application: nil,
729
+ currency: "usd",
730
+ description: "Stripe processing fees",
731
+ type: "stripe_fee"
732
+ }
733
+ ],
734
+ net: 9680,
735
+ source: source,
736
+ sourced_transfers: {
737
+ object: "list",
738
+ data: [],
739
+ has_more: false,
740
+ total_count: 0,
741
+ url: "/v1/transfers?source_transaction=#{source}"
742
+ },
743
+ status: "pending",
744
+ type: "charge"
745
+ }.merge(params)
746
+ end
747
+
702
748
  end
703
749
  end
@@ -1,5 +1,9 @@
1
+ require 'drb/drb'
2
+
1
3
  module StripeMock
2
4
  class ErrorQueue
5
+ include DRb::DRbUndumped
6
+ extend DRb::DRbUndumped
3
7
 
4
8
  def initialize
5
9
  @queue = []
@@ -20,4 +24,4 @@ module StripeMock
20
24
  end
21
25
 
22
26
  end
23
- end
27
+ end
@@ -21,6 +21,7 @@ module StripeMock
21
21
  end
22
22
 
23
23
  include StripeMock::RequestHandlers::Accounts
24
+ include StripeMock::RequestHandlers::BalanceTransactions
24
25
  include StripeMock::RequestHandlers::Charges
25
26
  include StripeMock::RequestHandlers::Cards
26
27
  include StripeMock::RequestHandlers::Sources
@@ -38,14 +39,15 @@ module StripeMock
38
39
  include StripeMock::RequestHandlers::Tokens
39
40
 
40
41
 
41
- attr_reader :accounts, :bank_tokens, :charges, :coupons, :customers, :disputes, :events,
42
- :invoices, :invoice_items, :orders, :plans, :recipients, :transfers,
43
- :subscriptions
42
+ attr_reader :accounts, :balance_transactions, :bank_tokens, :charges, :coupons, :customers,
43
+ :disputes, :events, :invoices, :invoice_items, :orders, :plans, :recipients,
44
+ :transfers, :subscriptions
44
45
 
45
46
  attr_accessor :error_queue, :debug
46
47
 
47
48
  def initialize
48
49
  @accounts = {}
50
+ @balance_transactions = Data.mock_balance_transactions(['txn_05RsQX2eZvKYlo2C0FRTGSSA','txn_15RsQX2eZvKYlo2C0ERTYUIA', 'txn_25RsQX2eZvKYlo2C0ZXCVBNM', 'txn_35RsQX2eZvKYlo2C0QAZXSWE', 'txn_45RsQX2eZvKYlo2C0EDCVFRT', 'txn_55RsQX2eZvKYlo2C0OIKLJUY', 'txn_65RsQX2eZvKYlo2C0ASDFGHJ', 'txn_75RsQX2eZvKYlo2C0EDCXSWQ', 'txn_85RsQX2eZvKYlo2C0UJMCDET', 'txn_95RsQX2eZvKYlo2C0EDFRYUI'])
49
51
  @bank_tokens = {}
50
52
  @card_tokens = {}
51
53
  @customers = {}
@@ -0,0 +1,21 @@
1
+ module StripeMock
2
+ module RequestHandlers
3
+ module BalanceTransactions
4
+
5
+ def BalanceTransactions.included(klass)
6
+ klass.add_handler 'get /v1/balance/history/(.*)', :get_balance_transaction
7
+ klass.add_handler 'get /v1/balance/history', :list_balance_transactions
8
+ end
9
+
10
+ def get_balance_transaction(route, method_url, params, headers)
11
+ route =~ method_url
12
+ assert_existence :balance_transaction, $1, balance_transactions[$1]
13
+ end
14
+
15
+ def list_balance_transactions(route, method_url, params, headers)
16
+ Data.mock_list_object(balance_transactions.values, params)
17
+ end
18
+
19
+ end
20
+ end
21
+ end
@@ -15,17 +15,24 @@ module StripeMock
15
15
  def new_charge(route, method_url, params, headers)
16
16
  id = new_id('ch')
17
17
 
18
- if params[:source] && params[:source].is_a?(String)
19
- # if a customer is provided, the card parameter is assumed to be the actual
20
- # card id, not a token. in this case we'll find the card in the customer
21
- # object and return that.
22
- if params[:customer]
23
- params[:source] = get_card(customers[params[:customer]], params[:source])
24
- else
25
- params[:source] = get_card_by_token(params[:source])
18
+ if params[:source]
19
+ if params[:source].is_a?(String)
20
+ # if a customer is provided, the card parameter is assumed to be the actual
21
+ # card id, not a token. in this case we'll find the card in the customer
22
+ # object and return that.
23
+ if params[:customer]
24
+ params[:source] = get_card(customers[params[:customer]], params[:source])
25
+ else
26
+ params[:source] = get_card_by_token(params[:source])
27
+ end
28
+ elsif params[:source][:id]
29
+ raise Stripe::InvalidRequestError.new("Invalid token id: #{params[:source]}", 'card', 400)
30
+ end
31
+ elsif params[:customer]
32
+ customer = customers[params[:customer]]
33
+ if customer && customer[:default_source]
34
+ params[:source] = get_card(customer, customer[:default_source])
26
35
  end
27
- elsif params[:source] && params[:source][:id]
28
- raise Stripe::InvalidRequestError.new("Invalid token id: #{params[:card]}", 'card', 400)
29
36
  end
30
37
 
31
38
  ensure_required_params(params)
@@ -41,6 +41,7 @@ module StripeMock
41
41
  subscription = Data.mock_subscription({ id: new_id('su') })
42
42
  subscription.merge!(custom_subscription_params(plan, customers[ params[:id] ], params))
43
43
  add_subscription_to_customer(customers[ params[:id] ], subscription)
44
+ subscriptions[subscription[:id]] = subscription
44
45
  elsif params[:trial_end]
45
46
  raise Stripe::InvalidRequestError.new('Received unknown parameter: trial_end', nil, 400)
46
47
  end
@@ -17,6 +17,23 @@ module StripeMock
17
17
  card
18
18
  end
19
19
 
20
+ def add_source_to_object(type, source, object, replace_current=false)
21
+ source[type] = object[:id]
22
+ sources = object[:sources]
23
+
24
+ if replace_current && sources[:data]
25
+ sources[:data].delete_if {|source| source[:id] == object[:default_source]}
26
+ object[:default_source] = source[:id]
27
+ sources[:data] = [source]
28
+ else
29
+ sources[:total_count] = (sources[:total_count] || 0) + 1
30
+ (sources[:data] ||= []) << source
31
+ end
32
+ object[:default_source] = source[:id] if object[:default_source].nil?
33
+
34
+ source
35
+ end
36
+
20
37
  def add_card_to_object(type, card, object, replace_current=false)
21
38
  card[type] = object[:id]
22
39
  cards_or_sources = object[:cards] || object[:sources]
@@ -64,6 +81,24 @@ module StripeMock
64
81
  card
65
82
  end
66
83
 
84
+ def add_source_to(type, type_id, params, objects)
85
+ resource = assert_existence type, type_id, objects[type_id]
86
+
87
+ source =
88
+ if params[:card]
89
+ card_from_params(params[:card])
90
+ elsif params[:bank_account]
91
+ get_bank_by_token(params[:bank_account])
92
+ else
93
+ begin
94
+ get_card_by_token(params[:source])
95
+ rescue Stripe::InvalidRequestError
96
+ get_bank_by_token(params[:source])
97
+ end
98
+ end
99
+ add_source_to_object(type, source, resource)
100
+ end
101
+
67
102
  def add_card_to(type, type_id, params, objects)
68
103
  resource = assert_existence type, type_id, objects[type_id]
69
104
 
@@ -2,14 +2,14 @@ module StripeMock
2
2
  module RequestHandlers
3
3
  module Helpers
4
4
 
5
- def generate_bank_token(bank_params)
5
+ def generate_bank_token(bank_params = {})
6
6
  token = new_id 'btok'
7
7
  bank_params[:id] = new_id 'bank_account'
8
8
  @bank_tokens[token] = Data.mock_bank_account bank_params
9
9
  token
10
10
  end
11
11
 
12
- def generate_card_token(card_params)
12
+ def generate_card_token(card_params = {})
13
13
  token = new_id 'tok'
14
14
  card_params[:id] = new_id 'cc'
15
15
  @card_tokens[token] = Data.mock_card symbolize_names(card_params)
@@ -13,7 +13,7 @@ module StripeMock
13
13
 
14
14
  def create_source(route, method_url, params, headers)
15
15
  route =~ method_url
16
- add_card_to(:customer, $1, params, customers)
16
+ add_source_to(:customer, $1, params, customers)
17
17
  end
18
18
 
19
19
  def retrieve_sources(route, method_url, params, headers)
@@ -3,20 +3,41 @@ module StripeMock
3
3
  module Subscriptions
4
4
 
5
5
  def Subscriptions.included(klass)
6
- klass.add_handler 'get /v1/customers/(.*)/subscriptions', :retrieve_subscriptions
7
- klass.add_handler 'post /v1/customers/(.*)/subscriptions', :create_subscription
8
- klass.add_handler 'get /v1/customers/(.*)/subscriptions/(.*)', :retrieve_subscription
9
- klass.add_handler 'post /v1/customers/(.*)/subscriptions/(.*)', :update_subscription
10
- klass.add_handler 'delete /v1/customers/(.*)/subscriptions/(.*)', :cancel_subscription
6
+ klass.add_handler 'get /v1/subscriptions', :retrieve_subscriptions
7
+ klass.add_handler 'post /v1/subscriptions', :create_subscription
8
+ klass.add_handler 'get /v1/subscriptions/(.*)', :retrieve_subscription
9
+ klass.add_handler 'post /v1/subscriptions/(.*)', :update_subscription
10
+ klass.add_handler 'delete /v1/subscriptions/(.*)', :cancel_subscription
11
+
12
+ klass.add_handler 'post /v1/customers/(.*)/subscriptions', :create_customer_subscription
13
+ klass.add_handler 'get /v1/customers/(.*)/subscriptions/(.*)', :retrieve_customer_subscription
14
+ klass.add_handler 'get /v1/customers/(.*)/subscriptions', :retrieve_customer_subscriptions
11
15
  end
12
16
 
13
- def create_subscription(route, method_url, params, headers)
17
+ def retrieve_customer_subscription(route, method_url, params, headers)
18
+ route =~ method_url
19
+
20
+ customer = :customer, $1, customers[$1]
21
+ subscription = get_customer_subscription(customer, $2)
22
+
23
+ assert_existence :subscription, $2, subscription
24
+ end
25
+
26
+ def retrieve_customer_subscriptions(route, method_url, params, headers)
14
27
  route =~ method_url
28
+
15
29
  customer = assert_existence :customer, $1, customers[$1]
30
+ customer[:subscriptions]
31
+ end
32
+
33
+ def create_customer_subscription(route, method_url, params, headers)
34
+ route =~ method_url
16
35
 
17
36
  plan_id = params[:plan].to_s
18
37
  plan = assert_existence :plan, plan_id, plans[plan_id]
19
38
 
39
+ customer = assert_existence :customer, $1, customers[$1]
40
+
20
41
  if params[:source]
21
42
  new_card = get_card_by_token(params.delete(:source))
22
43
  add_card_to_object(:customer, new_card, customer)
@@ -42,32 +63,72 @@ module StripeMock
42
63
  subscription[:discount] = Stripe::Util.convert_to_stripe_object({ coupon: coupon }, {})
43
64
  end
44
65
 
66
+ subscriptions[subscription[:id]] = subscription
45
67
  add_subscription_to_customer(customer, subscription)
46
68
 
69
+ subscriptions[subscription[:id]]
70
+ end
47
71
 
48
- subscription
72
+ def create_subscription(route, method_url, params, headers)
73
+ route =~ method_url
74
+
75
+ plan_id = params[:plan].to_s
76
+ plan = assert_existence :plan, plan_id, plans[plan_id]
77
+
78
+ customer_id = params[:customer].to_s
79
+ customer = assert_existence :customer, customer_id, customers[customer_id]
80
+
81
+ if params[:source]
82
+ new_card = get_card_by_token(params.delete(:source))
83
+ add_card_to_object(:customer, new_card, customer)
84
+ customer[:default_source] = new_card[:id]
85
+ end
86
+
87
+ # Ensure customer has card to charge if plan has no trial and is not free
88
+ verify_card_present(customer, plan, params)
89
+
90
+ subscription = Data.mock_subscription({ id: (params[:id] || new_id('su')) })
91
+ subscription.merge!(custom_subscription_params(plan, customer, params))
92
+
93
+ if params[:coupon]
94
+ coupon_id = params[:coupon]
95
+
96
+ raise Stripe::InvalidRequestError.new("No such coupon: #{coupon_id}", 'coupon', 400) unless coupons[coupon_id]
97
+
98
+ # FIXME assert_existence returns 404 error code but Stripe returns 400
99
+ # coupon = assert_existence :coupon, coupon_id, coupons[coupon_id]
100
+
101
+ coupon = Data.mock_coupon({ id: coupon_id })
102
+
103
+ subscription[:discount] = Stripe::Util.convert_to_stripe_object({ coupon: coupon }, {})
104
+ end
105
+
106
+ subscriptions[subscription[:id]] = subscription
107
+ add_subscription_to_customer(customer, subscription)
108
+
109
+ subscriptions[subscription[:id]]
49
110
  end
50
111
 
51
112
  def retrieve_subscription(route, method_url, params, headers)
52
113
  route =~ method_url
53
114
 
54
- customer = assert_existence :customer, $1, customers[$1]
55
- assert_existence :subscription, $2, get_customer_subscription(customer, $2)
115
+ assert_existence :subscription, $1, subscriptions[$1]
56
116
  end
57
117
 
58
118
  def retrieve_subscriptions(route, method_url, params, headers)
59
119
  route =~ method_url
60
120
 
61
- customer = assert_existence :customer, $1, customers[$1]
62
- customer[:subscriptions]
121
+ Data.mock_list_object(subscriptions.values, params)
122
+ #customer = assert_existence :customer, $1, customers[$1]
123
+ #customer[:subscriptions]
63
124
  end
64
125
 
65
126
  def update_subscription(route, method_url, params, headers)
66
127
  route =~ method_url
67
- customer = assert_existence :customer, $1, customers[$1]
128
+ subscription = assert_existence :subscription, $1, subscriptions[$1]
68
129
 
69
- subscription = get_customer_subscription(customer, $2)
70
- assert_existence :subscription, $2, subscription
130
+ customer_id = subscription[:customer]
131
+ customer = assert_existence :customer, customer_id, customers[customer_id]
71
132
 
72
133
  if params[:source]
73
134
  new_card = get_card_by_token(params.delete(:source))
@@ -111,10 +172,11 @@ module StripeMock
111
172
 
112
173
  def cancel_subscription(route, method_url, params, headers)
113
174
  route =~ method_url
114
- customer = assert_existence :customer, $1, customers[$1]
115
175
 
116
- subscription = get_customer_subscription(customer, $2)
117
- assert_existence :subscription, $2, subscription
176
+ subscription = assert_existence :subscription, $1, subscriptions[$1]
177
+
178
+ customer_id = subscription[:customer]
179
+ customer = assert_existence :customer, customer_id, customers[customer_id]
118
180
 
119
181
  cancel_params = { canceled_at: Time.now.utc.to_i }
120
182
  cancelled_at_period_end = (params[:at_period_end] == true)
@@ -8,7 +8,7 @@ module StripeMock
8
8
  end
9
9
 
10
10
  def create_token(route, method_url, params, headers)
11
- if params[:customer].nil? && params[:card].nil?
11
+ if params[:customer].nil? && params[:card].nil? && params[:bank_account].nil?
12
12
  raise Stripe::InvalidRequestError.new('You must supply either a card, customer, or bank account to create a token.', nil, 400)
13
13
  end
14
14
 
@@ -31,15 +31,25 @@ module StripeMock
31
31
  params[:card][:fingerprint] = StripeMock::Util.fingerprint(params[:card][:number])
32
32
  params[:card][:last4] = params[:card][:number][-4,4]
33
33
  customer_card = params[:card]
34
+ elsif params[:bank_account]
35
+ # params[:card] is a hash of cc info; "Sanitize" the card number
36
+ bank_account = params[:bank_account]
34
37
  else
35
38
  customer = assert_existence :customer, cus_id, customers[cus_id]
36
39
  customer_card = get_card(customer, customer[:default_source])
37
40
  end
38
41
 
39
- token_id = generate_card_token(customer_card)
40
- card = @card_tokens[token_id]
42
+ if bank_account
43
+ token_id = generate_bank_token(bank_account)
44
+ bank_account = @bank_tokens[token_id]
45
+
46
+ Data.mock_bank_account_token(params.merge :id => token_id, :bank_account => bank_account)
47
+ else
48
+ token_id = generate_card_token(customer_card)
49
+ card = @card_tokens[token_id]
41
50
 
42
- Data.mock_card_token(params.merge :id => token_id, :card => card)
51
+ Data.mock_card_token(params.merge :id => token_id, :card => card)
52
+ end
43
53
  end
44
54
 
45
55
  def get_token(route, method_url, params, headers)
@@ -1,19 +1,15 @@
1
- require 'jimson-temp'
1
+ require 'drb/drb'
2
2
 
3
3
  module StripeMock
4
-
5
4
  class Server
6
- extend Jimson::Handler
7
-
8
5
  def self.start_new(opts)
9
6
  puts "Starting StripeMock server on port #{opts[:port] || 4999}"
10
- server = Jimson::Server.new(Server.new,
11
- :host => opts[:host] || '0.0.0.0',
12
- :port => opts[:port] || 4999,
13
- :server => opts[:server] || :thin,
14
- :show_errors => true
15
- )
16
- server.start
7
+
8
+ host = opts.fetch :host,'0.0.0.0'
9
+ port = opts.fetch :port, 4999
10
+
11
+ DRb.start_service "druby://#{host}:#{port}", Server.new
12
+ DRb.thread.join
17
13
  end
18
14
 
19
15
  def initialize
@@ -26,7 +22,9 @@ module StripeMock
26
22
  rescue Stripe::InvalidRequestError => e
27
23
  {
28
24
  :error_raised => 'invalid_request',
29
- :error_params => [e.message, e.param, e.http_status, e.http_body, e.json_body]
25
+ :error_params => [
26
+ e.message, e.param, e.http_status, e.http_body, e.json_body
27
+ ]
30
28
  }
31
29
  end
32
30
  end
@@ -67,8 +65,16 @@ module StripeMock
67
65
  @instance.generate_webhook_event(event_data)
68
66
  end
69
67
 
70
- def debug?; @instance.debug; end
71
- def ping; true; end
72
- end
68
+ def error_queue
69
+ @instance.error_queue
70
+ end
71
+
72
+ def debug?
73
+ @instance.debug
74
+ end
73
75
 
76
+ def ping
77
+ true
78
+ end
79
+ end
74
80
  end