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
61
|
# File 'lib/stripe_mock/request_handlers/tokens.rb', line 10
def create_token(route, method_url, params, )
stripe_account = && [:stripe_account] || Stripe.api_key
if params[:customer].nil? && params[:card].nil? && params[:bank_account].nil?
raise Stripe::InvalidRequestError.new('You must supply either a card, customer, or bank account to create a token.', nil, http_status: 400)
end
cus_id = params[:customer]
if cus_id && params[:source]
customer = assert_existence :customer, cus_id, customers[stripe_account][cus_id]
customer_card = get_card(customer, params[:source])
assert_existence :card, params[:source], customer_card
elsif params[:card].is_a?(String)
customer = assert_existence :customer, cus_id, customers[stripe_account][cus_id]
customer_card = get_card(customer, params[:card])
assert_existence :card, params[:card], customer_card
elsif params[:card]
params[:card][:fingerprint] = StripeMock::Util.fingerprint(params[:card][:number])
params[:card][:last4] = params[:card][:number][-4,4]
customer_card = params[:card]
elsif params[:bank_account].is_a?(String)
customer = assert_existence :customer, cus_id, customers[stripe_account][cus_id]
bank_account = verify_bank_account(customer, params[:bank_account])
assert_existence :bank_account, params[:bank_account], bank_account
elsif params[:bank_account]
bank_account = params[:bank_account]
else
customer = assert_existence :customer, cus_id, customers[stripe_account][cus_id] || customers[Stripe.api_key][cus_id]
customer_card = get_card(customer, customer[:default_source])
end
if bank_account
token_id = generate_bank_token(bank_account.dup)
bank_account = @bank_tokens[token_id]
Data.mock_bank_account_token(params.merge :id => token_id, :bank_account => bank_account)
else
token_id = generate_card_token(customer_card.dup)
card = @card_tokens[token_id]
Data.mock_card_token(params.merge :id => token_id, :card => card)
end
end
|