-
Notifications
You must be signed in to change notification settings - Fork 126
/
web.rb
344 lines (297 loc) · 10.9 KB
/
web.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
require 'sinatra'
require 'stripe'
require 'dotenv'
require 'json'
require 'sinatra/cross_origin'
# Browsers require that external servers enable CORS when the server is at a different origin than the website.
# https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
# This enables the requires CORS headers to allow the browser to make the requests from the JS Example App.
configure do
enable :cross_origin
end
before do
response.headers['Access-Control-Allow-Origin'] = '*'
end
options "*" do
response.headers["Allow"] = "GET, POST, OPTIONS"
response.headers["Access-Control-Allow-Headers"] = "Authorization, Content-Type, Accept, X-User-Email, X-Auth-Token"
response.headers["Access-Control-Allow-Origin"] = "*"
200
end
Dotenv.load
Stripe.api_key = ENV['STRIPE_ENV'] == 'production' ? ENV['STRIPE_SECRET_KEY'] : ENV['STRIPE_TEST_SECRET_KEY']
Stripe.api_version = '2020-03-02'
def log_info(message)
puts "\n" + message + "\n\n"
return message
end
get '/' do
status 200
send_file 'index.html'
end
def validateApiKey
if Stripe.api_key.nil? || Stripe.api_key.empty?
return "Error: you provided an empty secret key. Please provide your test mode secret key. For more information, see https://stripe.com/docs/keys"
end
if Stripe.api_key.start_with?('pk')
return "Error: you used a publishable key to set up the example backend. Please use your test mode secret key. For more information, see https://stripe.com/docs/keys"
end
if Stripe.api_key.start_with?('sk_live')
return "Error: you used a live mode secret key to set up the example backend. Please use your test mode secret key. For more information, see https://stripe.com/docs/keys#test-live-modes"
end
return nil
end
# This endpoint registers a Verifone P400 reader to your Stripe account.
# https://stripe.com/docs/terminal/readers/connecting/verifone-p400#register-reader
post '/register_reader' do
validationError = validateApiKey
if !validationError.nil?
status 400
return log_info(validationError)
end
begin
reader = Stripe::Terminal::Reader.create(
:registration_code => params[:registration_code],
:label => params[:label],
:location => params[:location]
)
rescue Stripe::StripeError => e
status 402
return log_info("Error registering reader! #{e.message}")
end
log_info("Reader registered: #{reader.id}")
status 200
# Note that returning the Stripe reader object directly creates a dependency between your
# backend's Stripe.api_version and your clients, making future upgrades more complicated.
# All clients must also be ready for backwards-compatible changes at any time:
# https://stripe.com/docs/upgrades#what-changes-does-stripe-consider-to-be-backwards-compatible
return reader.to_json
end
# This endpoint creates a ConnectionToken, which gives the SDK permission
# to use a reader with your Stripe account.
# https://stripe.com/docs/terminal/sdk/js#connection-token
# https://stripe.com/docs/terminal/sdk/ios#connection-token
# https://stripe.com/docs/terminal/sdk/android#connection-token
#
# The example backend does not currently support connected accounts.
# To create a ConnectionToken for a connected account, see
# https://stripe.com/docs/terminal/features/connect#direct-connection-tokens
post '/connection_token' do
validationError = validateApiKey
if !validationError.nil?
status 400
return log_info(validationError)
end
begin
token = Stripe::Terminal::ConnectionToken.create
rescue Stripe::StripeError => e
status 402
return log_info("Error creating ConnectionToken! #{e.message}")
end
content_type :json
status 200
return {:secret => token.secret}.to_json
end
# This endpoint creates a PaymentIntent.
# https://stripe.com/docs/terminal/payments#create
#
# The example backend does not currently support connected accounts.
# To create a PaymentIntent for a connected account, see
# https://stripe.com/docs/terminal/features/connect#direct-payment-intents-server-side
post '/create_payment_intent' do
validationError = validateApiKey
if !validationError.nil?
status 400
return log_info(validationError)
end
begin
payment_intent = Stripe::PaymentIntent.create(
:payment_method_types => params[:payment_method_types] || ['card_present'],
:capture_method => params[:capture_method] || 'manual',
:amount => params[:amount],
:currency => params[:currency] || 'usd',
:description => params[:description] || 'Example PaymentIntent',
:payment_method_options => params[:payment_method_options] || [],
:receipt_email => params[:receipt_email],
)
rescue Stripe::StripeError => e
status 402
return log_info("Error creating PaymentIntent! #{e.message}")
end
log_info("PaymentIntent successfully created: #{payment_intent.id}")
status 200
return {:intent => payment_intent.id, :secret => payment_intent.client_secret}.to_json
end
# This endpoint captures a PaymentIntent.
# https://stripe.com/docs/terminal/payments#capture
post '/capture_payment_intent' do
begin
id = params["payment_intent_id"]
if !params["amount_to_capture"].nil?
payment_intent = Stripe::PaymentIntent.capture(id, :amount_to_capture => params["amount_to_capture"])
else
payment_intent = Stripe::PaymentIntent.capture(id)
end
rescue Stripe::StripeError => e
status 402
return log_info("Error capturing PaymentIntent! #{e.message}")
end
log_info("PaymentIntent successfully captured: #{id}")
# Optionally reconcile the PaymentIntent with your internal order system.
status 200
return {:intent => payment_intent.id, :secret => payment_intent.client_secret}.to_json
end
# This endpoint cancels a PaymentIntent.
# https://stripe.com/docs/api/payment_intents/cancel
post '/cancel_payment_intent' do
begin
id = params["payment_intent_id"]
payment_intent = Stripe::PaymentIntent.cancel(id)
rescue Stripe::StripeError => e
status 402
return log_info("Error canceling PaymentIntent! #{e.message}")
end
log_info("PaymentIntent successfully canceled: #{id}")
# Optionally reconcile the PaymentIntent with your internal order system.
status 200
return {:intent => payment_intent.id, :secret => payment_intent.client_secret}.to_json
end
# This endpoint creates a SetupIntent.
# https://stripe.com/docs/api/setup_intents/create
post '/create_setup_intent' do
validationError = validateApiKey
if !validationError.nil?
status 400
return log_info(validationError)
end
begin
setup_intent_params = {
:payment_method_types => params[:payment_method_types] || ['card_present'],
}
if !params[:customer].nil?
setup_intent_params[:customer] = params[:customer]
end
if !params[:description].nil?
setup_intent_params[:description] = params[:description]
end
if !params[:on_behalf_of].nil?
setup_intent_params[:on_behalf_of] = params[:on_behalf_of]
end
setup_intent = Stripe::SetupIntent.create(setup_intent_params)
rescue Stripe::StripeError => e
status 402
return log_info("Error creating SetupIntent! #{e.message}")
end
log_info("SetupIntent successfully created: #{setup_intent.id}")
status 200
return {:intent => setup_intent.id, :secret => setup_intent.client_secret}.to_json
end
# Looks up or creates a Customer on your stripe account
# with email "[email protected]".
def lookupOrCreateExampleCustomer
customerEmail = "[email protected]"
begin
customerList = Stripe::Customer.list(email: customerEmail, limit: 1).data
if (customerList.length == 1)
return customerList[0]
else
return Stripe::Customer.create(email: customerEmail)
end
rescue Stripe::StripeError => e
status 402
return log_info("Error creating or retreiving customer! #{e.message}")
end
end
# This endpoint attaches a PaymentMethod to a Customer.
# https://stripe.com/docs/terminal/payments/saving-cards#read-reusable-card
post '/attach_payment_method_to_customer' do
begin
customer = lookupOrCreateExampleCustomer
payment_method = Stripe::PaymentMethod.attach(
params[:payment_method_id],
{
customer: customer.id,
expand: ["customer"],
})
rescue Stripe::StripeError => e
status 402
return log_info("Error attaching PaymentMethod to Customer! #{e.message}")
end
log_info("Attached PaymentMethod to Customer: #{customer.id}")
status 200
# Note that returning the Stripe payment_method object directly creates a dependency between your
# backend's Stripe.api_version and your clients, making future upgrades more complicated.
# All clients must also be ready for backwards-compatible changes at any time:
# https://stripe.com/docs/upgrades#what-changes-does-stripe-consider-to-be-backwards-compatible
return payment_method.to_json
end
# This endpoint updates the PaymentIntent represented by 'payment_intent_id'.
# It currently only supports updating the 'receipt_email' property.
#
# https://stripe.com/docs/api/payment_intents/update
post '/update_payment_intent' do
payment_intent_id = params["payment_intent_id"]
if payment_intent_id.nil?
status 400
return log_info("'payment_intent_id' is a required parameter")
end
begin
allowed_keys = ["receipt_email"]
update_params = params.select { |k, _| allowed_keys.include?(k) }
payment_intent = Stripe::PaymentIntent.update(
payment_intent_id,
update_params
)
log_info("Updated PaymentIntent #{payment_intent_id}")
rescue Stripe::StripeError => e
status 402
return log_info("Error updating PaymentIntent #{payment_intent_id}. #{e.message}")
end
status 200
return {:intent => payment_intent.id, :secret => payment_intent.client_secret}.to_json
end
# This endpoint lists the first 100 Locations. If you will have more than 100
# Locations, you'll likely want to implement pagination in your application so that
# you can efficiently fetch Locations as needed.
# https://stripe.com/docs/api/terminal/locations
get '/list_locations' do
validationError = validateApiKey
if !validationError.nil?
status 400
return log_info(validationError)
end
begin
locations = Stripe::Terminal::Location.list(
limit: 100
)
rescue Stripe::StripeError => e
status 402
return log_info("Error fetching Locations! #{e.message}")
end
log_info("#{locations.data.size} Locations successfully fetched")
status 200
content_type :json
return locations.data.to_json
end
# This endpoint creates a Location.
# https://stripe.com/docs/api/terminal/locations
post '/create_location' do
validationError = validateApiKey
if !validationError.nil?
status 400
return log_info(validationError)
end
begin
location = Stripe::Terminal::Location.create(
display_name: params[:display_name],
address: params[:address]
)
rescue Stripe::StripeError => e
status 402
return log_info("Error creating Location! #{e.message}")
end
log_info("Location successfully created: #{location.id}")
status 200
content_type :json
return location.to_json
end