forked from stripe/example-mobile-backend
-
Notifications
You must be signed in to change notification settings - Fork 3
/
web.rb
52 lines (41 loc) · 1.02 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
require 'sinatra'
require 'stripe'
require 'dotenv'
Dotenv.load
Stripe.api_key = ENV['STRIPE_TEST_SECRET_KEY']
get '/' do
status 200
return "This is Lobby Boy's sample backend that does Stripe Transactions!"
end
post '/user' do
# Get the credit card details submitted by the form
token = params[:stripeToken]
begin
# Create a Customer
customer = Stripe::Customer.create(
:source => token,
:email => params[:email],
:metadata => { :name => params[:name] },
)
rescue => e
status 402
return "Error creating customer"
end
status 200
return customer.id
end
post '/charge' do
# Create the charge on Stripe's servers - this will charge the user's card
begin
charge = Stripe::Charge.create(
:amount => params[:amount], # this number should be in cents
:currency => "usd",
:customer => params[:customerId]
)
rescue Stripe::CardError => e
status 402
return "Error creating charge."
end
status 200
return "Order successfully created"
end