forked from stripe-ruby-mock/stripe-ruby-mock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup_intents.rb
98 lines (78 loc) · 2.97 KB
/
setup_intents.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
module StripeMock
module RequestHandlers
module SetupIntents
ALLOWED_PARAMS = [
:confirm,
:customer,
:description,
:metadata,
:on_behalf_of,
:payment_method,
:payment_method_options,
:payment_method_types,
:return_url,
:usage
]
def SetupIntents.included(klass)
klass.add_handler 'post /v1/setup_intents', :new_setup_intent
klass.add_handler 'get /v1/setup_intents', :get_setup_intents
klass.add_handler 'get /v1/setup_intents/(.*)', :get_setup_intent
klass.add_handler 'post /v1/setup_intents/(.*)/confirm', :confirm_setup_intent
klass.add_handler 'post /v1/setup_intents/(.*)/cancel', :cancel_setup_intent
klass.add_handler 'post /v1/setup_intents/(.*)', :update_setup_intent
end
def new_setup_intent(route, method_url, params, headers)
id = new_id('si')
setup_intents[id] = Data.mock_setup_intent(
params.merge(
id: id
)
)
setup_intents[id].clone
end
def update_setup_intent(route, method_url, params, headers)
route =~ method_url
id = $1
setup_intent = assert_existence :setup_intent, id, setup_intents[id]
setup_intents[id] = Util.rmerge(setup_intent, params.select { |k, v| ALLOWED_PARAMS.include?(k) })
end
def get_setup_intents(route, method_url, params, headers)
params[:offset] ||= 0
params[:limit] ||= 10
clone = setup_intents.clone
if params[:customer]
clone.delete_if { |k, v| v[:customer] != params[:customer] }
end
Data.mock_list_object(clone.values, params)
end
def get_setup_intent(route, method_url, params, headers)
route =~ method_url
setup_intent_id = $1 || params[:setup_intent]
setup_intent = assert_existence :setup_intent, setup_intent_id, setup_intents[setup_intent_id]
setup_intent = setup_intent.clone
if params[:expand]&.include?("payment_method")
setup_intent[:payment_method] = assert_existence :payment_method, setup_intent[:payment_method], payment_methods[setup_intent[:payment_method]]
end
setup_intent
end
def capture_setup_intent(route, method_url, params, headers)
route =~ method_url
setup_intent = assert_existence :setup_intent, $1, setup_intents[$1]
setup_intent[:status] = 'succeeded'
setup_intent
end
def confirm_setup_intent(route, method_url, params, headers)
route =~ method_url
setup_intent = assert_existence :setup_intent, $1, setup_intents[$1]
setup_intent[:status] = 'succeeded'
setup_intent
end
def cancel_setup_intent(route, method_url, params, headers)
route =~ method_url
setup_intent = assert_existence :setup_intent, $1, setup_intents[$1]
setup_intent[:status] = 'canceled'
setup_intent
end
end
end
end