forked from stripe-ruby-mock/stripe-ruby-mock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
payment_methods.rb
128 lines (99 loc) · 4.1 KB
/
payment_methods.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
module StripeMock
module RequestHandlers
module PaymentMethods
ALLOWED_PARAMS = [:customer, :type]
def PaymentMethods.included(klass)
klass.add_handler 'post /v1/payment_methods', :new_payment_method
klass.add_handler 'get /v1/payment_methods/(.*)', :get_payment_method
klass.add_handler 'get /v1/payment_methods', :get_payment_methods
klass.add_handler 'post /v1/payment_methods/(.*)/attach', :attach_payment_method
klass.add_handler 'post /v1/payment_methods/(.*)/detach', :detach_payment_method
klass.add_handler 'post /v1/payment_methods/(.*)', :update_payment_method
end
# post /v1/payment_methods
def new_payment_method(route, method_url, params, headers)
id = new_id('pm')
ensure_payment_method_required_params(params)
payment_methods[id] = Data.mock_payment_method(
params.merge(
id: id
)
)
payment_methods[id].clone
end
#
# params: {:type=>"card", :customer=>"test_cus_3"}
#
# get /v1/payment_methods/:id
def get_payment_method(route, method_url, params, headers)
id = method_url.match(route)[1] || params[:payment_method]
payment_method = assert_existence :payment_method, id, payment_methods[id]
payment_method.clone
end
# get /v1/payment_methods
def get_payment_methods(route, method_url, params, headers)
params[:offset] ||= 0
params[:limit] ||= 10
clone = payment_methods.clone
if params[:customer]
clone.delete_if { |_k, v| v[:customer] != params[:customer] }
end
Data.mock_list_object(clone.values, params)
end
# post /v1/payment_methods/:id/attach
def attach_payment_method(route, method_url, params, headers)
stripe_account = headers && headers[:stripe_account] || Stripe.api_key
allowed_params = [:customer]
id = method_url.match(route)[1]
assert_existence :customer, params[:customer], customers[stripe_account][params[:customer]]
payment_method = assert_existence :payment_method, id, payment_methods[id]
payment_methods[id] = Util.rmerge(payment_method, params.select { |k, _v| allowed_params.include?(k) })
payment_methods[id].clone
end
# post /v1/payment_methods/:id/detach
def detach_payment_method(route, method_url, params, headers)
id = method_url.match(route)[1]
payment_method = assert_existence :payment_method, id, payment_methods[id]
payment_method[:customer] = nil
payment_method.clone
end
# post /v1/payment_methods/:id
def update_payment_method(route, method_url, params, headers)
allowed_params = [:billing_details, :card, :metadata]
disallowed_params = params.keys - allowed_params
unless disallowed_params.empty?
raise Stripe::InvalidRequestError.new("Received unknown parameter: #{disallowed_params.first}", disallowed_params.first)
end
id = method_url.match(route)[1]
payment_method = assert_existence :payment_method, id, payment_methods[id]
if payment_method[:customer].nil?
raise Stripe::InvalidRequestError.new(
'You must save this PaymentMethod to a customer before you can update it.',
nil,
http_status: 400
)
end
payment_methods[id] =
Util.rmerge(payment_method, params.select { |k, _v| allowed_params.include?(k)} )
payment_methods[id].clone
end
private
def ensure_payment_method_required_params(params)
require_param(:type) if params[:type].nil?
if invalid_type?(params[:type])
raise Stripe::InvalidRequestError.new(
'Invalid type: must be one of card, ideal or sepa_debit',
nil,
http_status: 400
)
end
end
def valid_types
%w(card ideal sepa_debit us_bank_account)
end
def invalid_type?(type)
!valid_types.include?(type)
end
end
end
end