forked from stripe-ruby-mock/stripe-ruby-mock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
payment_intents.rb
204 lines (172 loc) · 7.17 KB
/
payment_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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
module StripeMock
module RequestHandlers
module PaymentIntents
ALLOWED_PARAMS = [:description, :metadata, :receipt_email, :shipping, :destination, :payment_method, :payment_method_types, :setup_future_usage, :transfer_data, :amount, :currency, :statement_descriptor_suffix]
def PaymentIntents.included(klass)
klass.add_handler 'post /v1/payment_intents', :new_payment_intent
klass.add_handler 'get /v1/payment_intents', :get_payment_intents
klass.add_handler 'get /v1/payment_intents/((?!search).*)', :get_payment_intent
klass.add_handler 'get /v1/payment_intents/search', :search_payment_intents
klass.add_handler 'post /v1/payment_intents/(.*)/confirm', :confirm_payment_intent
klass.add_handler 'post /v1/payment_intents/(.*)/capture', :capture_payment_intent
klass.add_handler 'post /v1/payment_intents/(.*)/cancel', :cancel_payment_intent
klass.add_handler 'post /v1/payment_intents/(.*)', :update_payment_intent
end
def new_payment_intent(route, method_url, params, headers)
id = new_id('pi')
ensure_payment_intent_required_params(params)
status = case params[:amount]
when 3184 then 'requires_action'
when 3178 then 'requires_payment_method'
when 3055 then 'requires_capture'
else
'succeeded'
end
last_payment_error = params[:amount] == 3178 ? last_payment_error_generator(code: 'card_declined', decline_code: 'insufficient_funds', message: 'Not enough funds.') : nil
statement_descriptor_suffix = params[:statement_descriptor_suffix]
payment_intents[id] = Data.mock_payment_intent(
params.merge(
id: id,
status: status,
last_payment_error: last_payment_error,
statement_descriptor_suffix: statement_descriptor_suffix
)
)
if params[:confirm] && status == 'succeeded'
payment_intents[id] = succeeded_payment_intent(payment_intents[id])
end
payment_intents[id].clone
end
def update_payment_intent(route, method_url, params, headers)
route =~ method_url
id = $1
payment_intent = assert_existence :payment_intent, id, payment_intents[id]
payment_intents[id] = Util.rmerge(payment_intent, params.select{ |k,v| ALLOWED_PARAMS.include?(k)})
end
def get_payment_intents(route, method_url, params, headers)
params[:offset] ||= 0
params[:limit] ||= 10
clone = payment_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_payment_intent(route, method_url, params, headers)
route =~ method_url
payment_intent_id = $1 || params[:payment_intent]
payment_intent = assert_existence :payment_intent, payment_intent_id, payment_intents[payment_intent_id]
payment_intent = payment_intent.clone
payment_intent
end
SEARCH_FIELDS = ["amount", "currency", "customer", "status"].freeze
def search_payment_intents(route, method_url, params, headers)
require_param(:query) unless params[:query]
results = search_results(payment_intents.values, params[:query], fields: SEARCH_FIELDS, resource_name: "payment_intents")
Data.mock_list_object(results, params)
end
def capture_payment_intent(route, method_url, params, headers)
route =~ method_url
payment_intent = assert_existence :payment_intent, $1, payment_intents[$1]
succeeded_payment_intent(payment_intent)
end
def confirm_payment_intent(route, method_url, params, headers)
route =~ method_url
payment_intent = assert_existence :payment_intent, $1, payment_intents[$1]
if params[:payment_method]
payment_intent[:payment_method] = params[:payment_method]
end
succeeded_payment_intent(payment_intent)
end
def cancel_payment_intent(route, method_url, params, headers)
route =~ method_url
payment_intent = assert_existence :payment_intent, $1, payment_intents[$1]
payment_intent[:status] = 'canceled'
payment_intent
end
private
def ensure_payment_intent_required_params(params)
if params[:amount].nil?
require_param(:amount)
elsif params[:currency].nil?
require_param(:currency)
elsif non_integer_charge_amount?(params)
raise Stripe::InvalidRequestError.new("Invalid integer: #{params[:amount]}", 'amount', http_status: 400)
elsif non_positive_charge_amount?(params)
raise Stripe::InvalidRequestError.new('Invalid positive integer', 'amount', http_status: 400)
end
end
def non_integer_charge_amount?(params)
params[:amount] && !params[:amount].is_a?(Integer)
end
def non_positive_charge_amount?(params)
params[:amount] && params[:amount] < 1
end
def last_payment_error_generator(code: nil, message: nil, decline_code: nil)
{
code: code,
doc_url: "https://stripe.com/docs/error-codes/payment-intent-authentication-failure",
message: message,
decline_code: decline_code,
payment_method: {
id: "pm_1EwXFA2eZvKYlo2C0tlY091l",
object: "payment_method",
billing_details: {
address: {
city: nil,
country: nil,
line1: nil,
line2: nil,
postal_code: nil,
state: nil
},
email: nil,
name: "seller_08072019090000",
phone: nil
},
card: {
brand: "visa",
checks: {
address_line1_check: nil,
address_postal_code_check: nil,
cvc_check: "unchecked"
},
country: "US",
exp_month: 12,
exp_year: 2021,
fingerprint: "LQBhEmJnItuj3mxf",
funding: "credit",
generated_from: nil,
last4: "1629",
three_d_secure_usage: {
supported: true
},
wallet: nil
},
created: 1563208900,
customer: nil,
livemode: false,
metadata: {},
type: "card"
},
type: "invalid_request_error"
}
end
def succeeded_payment_intent(payment_intent)
payment_intent[:status] = 'succeeded'
btxn = new_balance_transaction('txn', { source: payment_intent[:id] })
charge_id = new_id('ch')
charges[charge_id] = Data.mock_charge(
id: charge_id,
balance_transaction: btxn,
payment_intent: payment_intent[:id],
amount: payment_intent[:amount],
currency: payment_intent[:currency],
payment_method: payment_intent[:payment_method]
)
payment_intent[:charges][:data] << charges[charge_id].clone
payment_intent
end
end
end
end