Module: StripeMock::RequestHandlers::Transfers

Included in:
Instance
Defined in:
lib/stripe_mock/request_handlers/transfers.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



5
6
7
8
9
10
# File 'lib/stripe_mock/request_handlers/transfers.rb', line 5

def Transfers.included(klass)
  klass.add_handler 'post /v1/transfers',             :new_transfer
  klass.add_handler 'get /v1/transfers',              :get_all_transfers
  klass.add_handler 'get /v1/transfers/(.*)',         :get_transfer
  klass.add_handler 'post /v1/transfers/(.*)/cancel', :cancel_transfer
end

Instance Method Details

#cancel_transfer(route, method_url, params, headers) ⇒ Object



68
69
70
71
72
73
# File 'lib/stripe_mock/request_handlers/transfers.rb', line 68

def cancel_transfer(route, method_url, params, headers)
  route =~ method_url
  assert_existence :transfer, $1, transfers[$1]
  t = transfers[$1] ||= Data.mock_transfer(:id => $1)
  t.merge!({:status => "canceled"})
end

#get_all_transfers(route, method_url, params, headers) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/stripe_mock/request_handlers/transfers.rb', line 12

def get_all_transfers(route, method_url, params, headers)
  extra_params = params.keys - [:created, :destination, :ending_before,
    :limit, :starting_after, :transfer_group]
  unless extra_params.empty?
    raise Stripe::InvalidRequestError.new("Received unknown parameter: #{extra_params[0]}", extra_params[0].to_s, http_status: 400)
  end

  if destination = params[:destination]
    assert_existence :destination, destination, accounts[destination]
  end

  _transfers = transfers.each_with_object([]) do |(_, transfer), array|
    if destination
      array << transfer if transfer[:destination] == destination
    else
      array << transfer
    end
  end

  if params[:limit]
    _transfers = _transfers.first([params[:limit], _transfers.size].min)
  end

  Data.mock_list_object(_transfers, params)
end

#get_transfer(route, method_url, params, headers) ⇒ Object



62
63
64
65
66
# File 'lib/stripe_mock/request_handlers/transfers.rb', line 62

def get_transfer(route, method_url, params, headers)
  route =~ method_url
  assert_existence :transfer, $1, transfers[$1]
  transfers[$1] ||= Data.mock_transfer(:id => $1)
end

#new_transfer(route, method_url, params, headers) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/stripe_mock/request_handlers/transfers.rb', line 38

def new_transfer(route, method_url, params, headers)
  id = new_id('tr')
  if params[:bank_account]
    params[:account] = get_bank_by_token(params.delete(:bank_account))
  end

  unless params[:amount].is_a?(Integer) || (params[:amount].is_a?(String) && /^\d+$/.match(params[:amount]))
    raise Stripe::InvalidRequestError.new("Invalid integer: #{params[:amount]}", 'amount', http_status: 400)
  end

  bal_trans_params = { amount: params[:amount].to_i, source: id }

  balance_transaction_id = new_balance_transaction('txn', bal_trans_params)

  transfers[id] = Data.mock_transfer(params.merge(id: id, balance_transaction: balance_transaction_id))

  transfer = transfers[id].clone
  if params[:expand] == ['balance_transaction']
    transfer[:balance_transaction] = balance_transactions[balance_transaction_id]
  end

  transfer
end