-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
462 lines (408 loc) · 12.3 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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
require 'rubygems'
require 'sinatra/base'
require 'dm-core'
require 'dm-migrations'
require 'dm-timestamps'
require 'dm-types'
require 'dm-validations'
require 'bcrypt'
require 'sinatra/flash'
require 'stripe'
require 'aws-sdk'
require 'pony'
require 'rack/ssl'
require 'dotenv'
require './models'
require './app'
class TheKeynoteStore < Sinatra::Base
Dotenv.load
configure :production, :development do
set :app_file, __FILE__
set :port, ENV['PORT']
set :public_folder, File.dirname(__FILE__) + '/public'
use Rack::Session::Pool, :expire_after => 2592000
use Rack::SSL
register Sinatra::Flash
enable :logging
end
helpers do
include Rack::Utils
alias_method :h, :escape_html
end
before do
@theme = Theme.all(:order => [ :list_order.asc ])
@complete_set = Theme.first(:name => "Complete Set")
if session['purchase']
@discount_percentage = 0.1
@purchase_count = session['purchase'].count
@purchase_hash = Hash.new(0)
session['purchase'].each {|v| @purchase_hash[v] +=1 }
@price = []
@purchase_hash.each {|key, value|
@price.push(Theme.get(key.to_i).price * value.to_i)
}
@purchase_total = @price.inject {|sum, key| sum + key }
@purchase_discount = @purchase_total - (@purchase_total * @discount_percentage)
if @purchase_count >= 3
@amount = @purchase_discount.to_i * 100
else
@amount = @purchase_total.to_i * 100
end
end
end
after do
if session['purchase']
if session['purchase'].empty?
session['purchase'] = nil
end
end
end
not_found do
@heading = "Whoops!"
erb :not_found
end
error do
"Y U NO WORK?"
end
# Theme Site
get '/' do
@heading = "Welcome."
erb :home
end
get '/catalogue' do
@heading = "Our Themes."
erb :catalogue
end
get '/key/five/:id' do
@current_theme = Theme.get(params[:id])
@heading = "Our Themes."
erb :five
end
get '/checkout' do
@heading = "Your Order."
@order_number = rand(1000000000000000..9999999999999999)
erb :checkout
end
post '/payment' do
begin
@order = Order.new(params[:order])
if @order.save
Stripe.api_key = ENV['STRIPE_API_KEY']
logger.info "PHILLIP GORE - Stripe api_key: #{ENV['STRIPE_API_KEY']}"
@charge = Stripe::Charge.create(
:amount => @amount,
:currency => "usd",
:card => @order.stripe_token,
:description => @order.order_email
)
@purchase_hash.each do |key, value|
@theme = Theme.get(key.to_i)
logger.info "PHILLIP GORE - AWS access_key_id: #{ENV['AWS_ACCESS_KEY_ID']}"
logger.info "PHILLIP GORE - AWS secret_access_key: #{ENV['AWS_SECRET_ACCESS_KEY']}"
logger.info "PHILLIP GORE - AWS bucket: #{ENV['AWS_BUCKET']}"
Aws.config.update({
region: 'us-west-2',
credentials: Aws::Credentials.new(ENV['AWS_ACCESS_KEY_ID'], ENV['AWS_SECRET_ACCESS_KEY'])
})
@signer = Aws::S3::Presigner.new
@url = @signer.presigned_url(:get_object,
bucket: ENV['AWS_BUCKET'],
key: "#{@theme.name.downcase.gsub(" ", "-")}.zip",
expires_in: 86400
).to_s
logger.info "PHILLIP GORE - #{@url}"
@purchase = @order.purchases.create(
:item_name => @theme.name,
:item_id => @theme.id,
:item_quantity => value.to_i,
:item_price => @theme.price,
:item_url => @url
)
end
Pony.mail(
:from => 'The Keynote Store <sales@keynotestore.com>',
:to => "#{@order.order_email}",
:via => :smtp,
:via_options => {
:address => 'smtp.sendgrid.net',
:port => '587',
:enable_starttls_auto => true,
:user_name => ENV['USER_NAME'],
:password => ENV['PASSWORD'],
:authentication => :plain,
:domain => 'www.keynotestore.com'
},
:subject => 'Your order from The Keynote Store. Thanks!',
:html_body => "<table width='600' align='center' style='font-family: Arial, Helvetica, sans-serif; font-size: 14px; color: #666; line-height: 26px; text-align: justify;'>
<tr style='font-family: Garamond, Georgia, Times, serif; color: black; font-size: 24px'>
<td>#{@order.order_first_name},</td>
</tr>
<tr>
<td><img src='https://s3.amazonaws.com/keynote_store/spacer.png'></td>
</tr>
<tr>
<td>You can view your receipt and download your order through the button below. You will only be able to download your order for 24 hours from the time of purchase.</td>
</tr>
<tr>
<td><img src='https://s3.amazonaws.com/keynote_store/spacer.png'></td>
</tr>
<tr>
<td><a href='http://www.keynotestore.com/order/#{@order.order_number}/#{@order.id}'><img width='143' height='30' src='https://s3.amazonaws.com/keynote_store/button-email-order@2x.png' alt='Your Order.'></a></td>
</tr>
<tr>
<td><img src='https://s3.amazonaws.com/keynote_store/spacer.png'></td>
</tr>
<tr>
<td>We truly appreciate your business. If you need any assistance now or in the future please contact us at <a style='color: #1E4CB1; text-decoration: none;' href='mailto:sales@keynotestore.com?Subject=Keynote%20Theme%20Support'>support@keynotestore.com</a>.</td>
</tr>
<tr>
<td><img src='https://s3.amazonaws.com/keynote_store/spacer.png'></td>
</tr>
<tr>
<td style='font-family: Garamond, Georgia, Times, serif; color: black; font-size: 24px'>Thank You!</td>
</tr>
<tr>
<td>The Keynote Store</td>
</tr>
<tr>
<td><img src='https://s3.amazonaws.com/keynote_store/spacer.png'></td>
</tr>
<tr>
<td><img src='https://s3.amazonaws.com/keynote_store/spacer.png'></td>
</tr>
</table>",
)
logger.info "PHILLIP GORE - Pony user_name: #{ENV['USER_NAME']}"
logger.info "PHILLIP GORE - Pony password: #{ENV['PASSWORD']}"
session['purchase'] = nil
redirect "/order/#{@order.order_number}/#{@order.id}"
end
rescue Stripe::StripeError => e
logger.info "PHILLIP GORE - Stripe Error: #{e.message}"
flash.next[:notice] = "Sorry. #{e.message}"
redirect '/checkout'
end
end
get '/order/:number/:id' do
@heading = "Thank You."
@order = Order.get(params[:id])
@purchase = @order.purchases.all(:order => [ :created_at.desc ])
erb :order_summary
end
get '/print/order/:id' do
@heading = "Thank You."
@order = Order.get(params[:id])
@purchase = @order.purchases.all(:order => [ :created_at.desc ])
erb :print_order_summary, :layout => false
end
get '/free' do
@heading = "Help Yourself."
erb :freebies
end
get '/tutorials' do
@heading = "Tips & Tricks."
erb :tutorials
end
get '/support' do
@heading = "Get Some Help."
erb :support
end
get '/theme/:id' do
@current_theme = Theme.get(params[:id])
@heading_selector = Random.rand(8)
@heading_array = ["Good Choice.", "Great Pick.", "Nice Thinking.", "We Agree.", "Love it.", "Yes.", "Awesome.", "Great Idea."]
@heading = @heading_array[@heading_selector]
if @current_theme.id === 49
redirect "/"
else
erb :theme
end
end
get '/buy/:id' do
if session['purchase']
session['purchase'].push(params[:id].to_i)
else
@purchase_array = []
@purchase_array.push(params[:id].to_i)
session['purchase'] = @purchase_array
end
redirect '/checkout'
end
get '/remove/:id' do
session['purchase'].delete(params[:id].to_i)
redirect '/checkout'
end
post '/update/:id' do
@existing_count = session['purchase'].count(params[:id].to_i)
@new_count = params[:new_count].to_i
session['purchase'].delete(params[:id].to_i)
@new_count.times{
session['purchase'].push(params[:id].to_i)
}
redirect '/checkout'
end
# Admin Login
get '/login' do
@heading = "Login to Admin."
erb :admin_login, :layout => :admin_alt
end
post '/login' do
@user = User.first(:user_email => params[:user_email])
if @user
if @user.password == params[:password]
session['user_id'] = @user.id
session['expires'] = 86400
redirect '/admin/edit'
else
flash.next[:notice] = '<div id="flash">Sorry, invalid password.</div>'
redirect '/login'
end
else
flash.next[:notice] = '<div id="flash">Sorry, invalid username.</div>'
redirect '/login'
end
end
get '/logout' do
session['user_id'] = nil
redirect '/login'
end
# Admin Site
before '/admin/*' do
unless session['user_id']
redirect '/login'
end
end
get '/admin' do
if session['user_id']
redirect '/admin/edit'
else
redirect '/login'
end
end
get '/admin/edit' do
@heading = "Edit A Theme."
erb :admin_edit, :layout => :admin
end
get '/admin/edit/:id' do
@theme = Theme.get(params[:id])
@banner_id = "#{@theme.name}"
@heading = "Edit #{@theme.name}."
@sub_heading = "The Keynote Store"
@theme_list = Theme.all()
erb :admin_update, :layout => :admin
end
post '/admin/update/theme/:id' do
@theme = Theme.get(params[:id])
@theme.update(params[:theme])
if @theme.save
redirect "/admin/edit"
else
flash.now[:notice] = '<div id="flash">Hmm - Something went wrong. Better try again.</div>'
end
@heading = "Add A Theme."
@theme = Theme.all()
erb :admin_edit, :layout => :admin
end
get '/admin/add' do
@heading = "Add A Theme."
@theme = Theme.all()
erb :admin_add, :layout => :admin
end
post '/admin/add/theme' do
@new_theme = Theme.new(params[:theme])
if @new_theme.save
redirect '/admin/edit'
else
flash.now[:notice] = '<div id="flash">Hmm - Something went wrong. Better try again.</div>'
end
@heading = "Add A Theme."
@theme = Theme.all()
erb :admin_add, :layout => :admin
end
get '/admin/delete/:id' do
Theme.get(params[:id]).destroy!
redirect '/admin/edit'
end
get '/admin/users' do
@heading = "Manage Users."
@users = User.all(:order => [:user_last_name.asc])
erb :admin_users, :layout => :admin
end
post '/admin/add/user' do
@user = User.new(params[:user])
if @user.save
redirect '/admin/users'
else
flash.next[:notice] = '<div id="flash">All fields are required. Please format correctly.</div>'
redirect "/admin/users"
end
end
get '/admin/edit/user/:id' do
@heading = "Update User."
@user = User.get(params[:id])
erb :admin_user_edit, :layout => :admin
end
post '/admin/update/user/:id' do
@user = User.get(params[:id])
@user.update(params[:user])
if @user.save
redirect "/admin/users"
else
flash.next[:notice] = '<div id="flash">All fields are required. Please format correctly.</div>'
redirect "/admin/edit/user/#{@user.id}"
end
end
get '/admin/change/password/:id' do
@heading = "Change Password."
@user = User.get(params[:id])
erb :admin_user_password_edit, :layout => :admin
end
post '/admin/update/password/:id' do
@current_user = "#{session['user_id']}"
@user = User.get(params[:id])
if @user.password == params[:password]
if params[:new_password] == params[:retype_password]
@user = User.update(:password => params[:new_password])
if @current_user == params[:id]
session['user_id'] = nil
flash.next[:notice] = '<div id="flash">New password. Please log in again.</div>'
redirect '/login'
else
flash.next[:notice] = '<div id="flash">Password has been updated.</div>'
redirect "/admin/users"
end
else
flash.next[:notice] = '<div id="flash">Sorry, new passwords do not match.</div>'
redirect "/admin/change/password/#{@user.id}"
end
else
flash.next[:notice] = '<div id="flash">Sorry, invalid password.</div>'
redirect "/admin/change/password/#{@user.id}"
end
end
get '/admin/delete/user/:id' do
if User.count > 1
@user = User.get(params[:id]).destroy!
redirect "/admin/users"
else
flash.next[:notice] = '<div id="flash">You must maintain at least one user account.</div>'
redirect "/admin/users"
end
end
get '/admin/orders' do
@heading = "Find Orders."
@order = Order.all(:order => [ :created_at.desc ])
erb :admin_orders, :layout => :admin
end
# get '/admin/delete/order/:id' do
# @order = Order.get(params[:id]).destroy!
# redirect "/admin/orders"
# end
get '/admin/receipt/:id' do
@heading = "Review Receipt."
@order = Order.get(params[:id])
@purchase = @order.purchases.all(:order => [ :created_at.desc ])
erb :admin_receipt, :layout => :admin
end
TheKeynoteStore.run!
end