forked from stripe/example-mobile-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb.rb
115 lines (86 loc) · 2.45 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
require 'sinatra'
require 'stripe'
require 'dotenv'
require 'json'
Dotenv.load
Stripe.api_key = ENV['STRIPE_TEST_SECRET_KEY']
get '/' do
status 200
return "Great, your backend is set up. Now you can configure the Stripe example iOS apps to point here."
end
post '/charge' do
# Get the credit card details submitted by the form
source = params[:source] || params[:stripe_token] || params[:stripeToken]
customer = params[:customer]
# 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 => customer,
:source => source,
:description => "Example Charge"
)
rescue Stripe::StripeError => e
status 402
return "Error creating charge: #{e.message}"
end
status 200
return "Charge successfully created"
end
get '/customers/:customer' do
customer = params[:customer]
begin
# Retrieves the customer's cards
customer = Stripe::Customer.retrieve(customer)
rescue Stripe::StripeError => e
status 402
return "Error retrieving customer: #{e.message}"
end
status 200
content_type :json
customer.to_json
end
post '/customers/:customer/sources' do
source = params[:source]
customer = params[:customer]
# Adds the token to the customer's sources
begin
customer = Stripe::Customer.retrieve(customer)
customer.sources.create({:source => source})
rescue Stripe::StripeError => e
status 402
return "Error adding token to customer: #{e.message}"
end
status 200
return "Successfully added source."
end
post '/customers/:customer/select_source' do
source = params[:source]
customer = params[:customer]
# Sets the customer's default source
begin
customer = Stripe::Customer.retrieve(customer)
customer.default_source = source
customer.save
rescue Stripe::StripeError => e
status 402
return "Error selecting default source: #{e.message}"
end
status 200
return "Successfully selected default source."
end
delete '/customers/:customer/cards/:card' do
card = params[:card]
customer = params[:customer]
# Deletes the source from the customer
begin
customer = Stripe::Customer.retrieve(customer)
customer.sources.retrieve(card).delete()
rescue Stripe::StripeError => e
status 402
return "Error deleting card"
end
status 200
return "Successfully deleted card."
end