· 9 years ago · Sep 22, 2016, 12:54 AM
1PUBLISHABLE_KEY=pk_test_y3sVBLgiI9N1nZ6vKcSI3WBL SECRET_KEY=sk_test_WFBmt4bOZGbdBqK4WsjsUK58 ruby app.rb
2
3require 'sinatra'
4require 'stripe'
5
6set :publishable_key, ENV['PUBLISHABLE_KEY']
7set :secret_key, ENV['SECRET_KEY']
8
9Stripe.api_key = settings.secret_key
10
11get '/' do
12 erb :index
13end
14
15post '/charge' do
16 @amount = 500
17
18 customer = Stripe::Customer.create(
19 :email => 'customer@example.com',
20 :card => params[:stripeToken]
21 )
22
23 charge = Stripe::Charge.create(
24 :amount => @amount,
25 :description => 'Sinatra Charge',
26 :currency => 'usd',
27 :customer => customer
28 )
29
30 erb :charge
31end
32
33__END__
34
35@@ layout
36 <!DOCTYPE html>
37 <html>
38 <head></head>
39 <body>
40 <%= yield %>
41 </body>
42 </html>
43
44@@index
45 <form action="/charge" method="post">
46 <article>
47 <label class="amount">
48 <span>Amount: $5.00</span>
49 </label>
50 </article>
51
52 <script src="https://checkout.stripe.com/v3/checkout.js" class="stripe- button" data-key="<%= settings.publishable_key %>"></script>
53 </form>
54
55@@charge
56 <h2>Thanks, you paid <strong>$5.00</strong>!</h2>