-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.rb
executable file
·118 lines (102 loc) · 2.85 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
require 'sinatra'
require 'stripe'
require 'dotenv'
require 'json'
require 'encrypted_cookie'
Dotenv.load
Stripe.api_key = ENV['sk_test_H007U9Xo8YRMHaCA2ldqT8P3']
use Rack::Session::EncryptedCookie,
:secret => 'replace_me_with_a_real_secret_key' # Actually use something secret here!
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
authenticate!
# Get the credit card details submitted by the form
source = params[:source]
# 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.id,
: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 '/customer' do
authenticate!
status 200
content_type :json
@customer.to_json
end
post '/customer/sources' do
authenticate!
source = params[:source]
# Adds the token to the customer's sources
begin
@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 '/customer/default_source' do
authenticate!
source = params[:source]
# Sets the customer's default source
begin
@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
def authenticate!
# This code simulates "loading the Stripe customer for your current session".
# Your own logic will likely look very different.
return @customer if @customer
if session.has_key?(:customer_id)
customer_id = session[:customer_id]
begin
@customer = Stripe::Customer.retrieve(customer_id)
rescue Stripe::InvalidRequestError
end
else
begin
@customer = Stripe::Customer.create(:description => "iOS SDK example customer")
rescue Stripe::InvalidRequestError
end
session[:customer_id] = @customer.id
end
@customer
end
# This endpoint is used by the Obj-C example app to create a charge.
post '/create_charge' do
# Create the charge on Stripe's servers
begin
charge = Stripe::Charge.create(
:amount => params[:amount], # this number should be in cents
:currency => "usd",
:source => params[:source],
:description => "Example Charge"
)
rescue Stripe::StripeError => e
status 402
return "Error creating charge: #{e.message}"
end
status 200
return "Charge successfully created"
end