forked from stripe-ruby-mock/stripe-ruby-mock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
recipients.rb
60 lines (48 loc) · 1.88 KB
/
recipients.rb
1
2
3
4
5
6
7
8
9
10
11
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
module StripeMock
module RequestHandlers
module Recipients
def Recipients.included(klass)
klass.add_handler 'post /v1/recipients', :new_recipient
klass.add_handler 'post /v1/recipients/(.*)', :update_recipient
klass.add_handler 'get /v1/recipients/(.*)', :get_recipient
end
def new_recipient(route, method_url, params, headers)
params[:id] ||= new_id('rp')
cards = []
if params[:name].nil?
raise StripeMock::StripeMockError.new("Missing required parameter name for recipients.")
end
if params[:type].nil?
raise StripeMock::StripeMockError.new("Missing required parameter type for recipients.")
end
unless %w(individual corporation).include?(params[:type])
raise StripeMock::StripeMockError.new("Type must be either individual or corporation..")
end
if params[:bank_account]
params[:active_account] = get_bank_by_token(params.delete(:bank_account))
end
if params[:card]
cards << get_card_by_token(params.delete(:card))
params[:default_card] = cards.first[:id]
end
recipients[ params[:id] ] = Data.mock_recipient(cards, params)
recipients[ params[:id] ]
end
def update_recipient(route, method_url, params, headers)
route =~ method_url
recipient = assert_existence :recipient, $1, recipients[$1]
recipient.merge!(params)
if params[:card]
new_card = get_card_by_token(params.delete(:card))
add_card_to_object(:recipient, new_card, recipient, true)
recipient[:default_card] = new_card[:id]
end
recipient
end
def get_recipient(route, method_url, params, headers)
route =~ method_url
assert_existence :recipient, $1, recipients[$1]
end
end
end
end