-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
45 lines (36 loc) · 1.1 KB
/
app.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
require 'sinatra'
require 'stripe'
require 'yaml'
set :publishable_key, ENV['PUBLISHABLE_KEY']
set :secret_key, ENV['SECRET_KEY']
Stripe.api_key = settings.secret_key
PRODUCTS = YAML.load_file("./products.yml")
get '/' do
@products = PRODUCTS
erb :index
end
post '/charge/:product' do
@product = PRODUCTS[params[:product]] or raise "Nope!"
customer = Stripe::Customer.create(
:email => params[:stripeEmail],
:card => params[:stripeToken]
)
charge = Stripe::Charge.create(
:amount => @product.amount,
:description => 'CoworkBuffalo Shop',
:currency => 'usd',
:customer => customer.id,
:metadata => {
:shipping_name => params[:stripeShippingName],
:shipping_address => params[:stripeShippingAddressLine1],
:shipping_zip => params[:stripeShippingAddressZip],
:shipping_state => params[:stripeShippingAddressState],
:shipping_city => params[:stripeShippingAddressCity],
:shipping_country => params[:stripeShippingAddressCountry]
}
)
erb :charge
end
error Stripe::CardError do
env['sinatra.error'].message
end