-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb.rb
More file actions
137 lines (110 loc) · 3.13 KB
/
Copy pathweb.rb
File metadata and controls
137 lines (110 loc) · 3.13 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
require 'sinatra'
require 'stripe'
require 'sqlite3'
require 'json'
require 'rest-client'
require 'pry'
require_relative 'stripe_keys'
Stripe.api_key = $PRIVATE_TEST_KEY
# home page route
get '/' do
# get list of products -- we'll include these
# on our store page eventually
db = SQLite3::Database.new("stripe_store.db")
db.results_as_hash = true
@products = db.execute("SELECT * from products")
erb :index
end
# order submission route
post '/purchase' do
# put form data into variables
token = params[:stripeToken]
product_id = params[:product_id].to_i
customer_email = params[:email]
product_id = 1
# look up price of product
db = SQLite3::Database.new("stripe_store.db")
db.results_as_hash = true
product = db.execute("SELECT * from PRODUCTS where id=?", product_id).last
price = product['amount']
# create the charge
begin
charge = Stripe::Charge.create(
:amount => price,
:currency => "usd",
:source => token,
:description => customer_email
)
rescue Stripe::CardError => e
body = e.json_body
err = body[:error]
puts "Status is: #{e.http_status}"
puts "Type is: #{err[:type]}"
puts "Code is: #{err[:code]}"
end
# print the charge to the server console
p charge[:succeeded]
if charge[:succeeded]
redirect '/purchase_confirmation'
else
"Charge declined"
end
end
#endpoint for Stripe Connect Oauth
get '/connect' do
#gets two params from Stripe OAUTH
scope = params[:scope]
auth_code = params[:code]
#creates account with Stripe or returns error to Sinatra
begin
response = RestClient.post 'https://connect.stripe.com/oauth/token', {
client_secret: $PRIVATE_TEST_KEY,
code: auth_code,
grant_type: 'authorization_code'}
response = JSON.parse(response.to_s)
db = SQLite3::Database.new("stripe_store.db")
db.execute("INSERT INTO accounts (
authorization_code,
token_type,
stripe_publishable_key,
scope,
livemode,
stripe_user_id,
refresh_token,
access_token) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", [
auth_code,
response['token_type'],
response['stripe_publishable_key'],
response['scope'],
response['livemode'].to_s,
response['stripe_user_id'],
response['refresh_token'],
response['access_token']
])
puts "Account #{response['stripe_user_id']} created."
redirect "/account_confirmation?acct_id=#{response['stripe_user_id']}"
#error handling for restclient and other errors (not really)
rescue => e
if e.class.to_s == 'RestClient::BadRequest'
e = JSON.parse(e.response.to_s)
p e.class
"#{e['error']}: #{e['error_description']}"
else
p e.class
p e.to_s
p e.cause
p e.message
p e.backtrace
end
end
end
get '/purchase_confirmation' do
"Thank you for your purchase."
end
get '/account_confirmation' do
#full confirmation page
db = SQLite3::Database.new("stripe_store.db")
db.results_as_hash = true
@account = db.execute("SELECT * from accounts where stripe_user_id = ?", params[:acct_id]).last
erb :account_confirmation
end