forked from stripe/example-terminal-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb.rb
158 lines (137 loc) · 4.76 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
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_TEST_SECRET_KEY']
def log_info(message)
puts "\n" + message + "\n\n"
return message
end
get '/' do
status 200
return log_info("Great, your backend is set up! Now you can configure the Stripe Terminal example apps to point here.")
end
# This endpoint is used by the example apps to retrieve a ConnectionToken
# from Stripe.
# iOS https://stripe.com/docs/terminal/ios#connection-token
# JavaScript https://stripe.com/docs/terminal/js#connection-token
# Android Coming Soon
post '/connection_token' do
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
token.to_json
end
# This endpoint is used by the example apps to capture a PaymentIntent.
# iOS https://stripe.com/docs/terminal/ios/payment#capture
# JavaScript https://stripe.com/docs/terminal/js/payment#capture
# Android Coming Soon
post '/capture_payment_intent' do
begin
id = params["payment_intent_id"]
payment_intent = Stripe::PaymentIntent.retrieve(id)
payment_intent.capture
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 is used by the JavaScript example app to create a PaymentIntent.
# The iOS and Android example apps create the PaymentIntent client-side
# using the SDK.
# https://stripe.com/docs/terminal/js/payment#create
post '/create_payment_intent' do
begin
payment_intent = Stripe::PaymentIntent.create(
:allowed_source_types => ['card_present'],
:capture_method => 'manual',
:amount => params[:amount],
:currency => params[:currency] || 'usd',
:description => params[:description] || 'Example PaymentIntent',
)
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 is used by the JS example app to register a Verifone P400 reader
# to your Stripe account.
# https://stripe.com/docs/api/terminal/readers/create
post '/register_reader' do
begin
reader = Stripe::Terminal::Reader.create(
:registration_code => params[:registration_code],
:label => params[:label]
)
rescue Stripe::StripeError => e
status 402
return log_info("Error registering reader! #{e.message}")
end
log_info("Reader registered: #{reader.id}")
status 200
return reader.to_json
end
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 is used to create a customer and save a card source to it.
# https://stripe.com/docs/terminal/js/workflows#save-source
post '/save_card_to_customer' do
begin
card_source = Stripe::Source.create(
:type => "card",
:card => {
:card_present_source => params[:card_present_source_id],
},
)
customer = lookupOrCreateExampleCustomer
customer.source = card_source.id # obtained with Stripe.js
customer.save
rescue Stripe::StripeError => e
status 402
return log_info("Error creating customer with reusable card source! #{e.message}")
end
log_info("Customer created with card source: #{customer.id}")
status 200
return customer.to_json
end