-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.coffee
100 lines (75 loc) · 2.33 KB
/
server.coffee
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
express = require 'express'
app = express()
bodyParser = require 'body-parser'
nocache = require 'nocache'
axios = require 'axios'
fs = require 'fs'
http = require 'http'
https = require 'https'
app.use bodyParser.urlencoded extended: true
app.use bodyParser.json()
app.use nocache()
httpPort = process.env.PORT || 80
httpsPort = 443
router = express.Router()
router.post '/', (req, res)->
payload = req.body
accessToken = payload.accessToken
if accessToken != 'JReHjrvJSyHah4MCfZTmTbbtpefZRgruNfraMVURCzfkzWAj5hRzHWhqzKqWkAZH'
console.log 'Invalid Token', accessToken
return
ord = payload.data
console.log 'Receiving', req.body
name = ord.shippingAddress.name
split = name.indexOf(' ')
firstName = name.substr 0, split
lastName = name.substr split + 1
tickets = []
numTickets = 0
for item in ord.items
numTickets += item.quantity
console.log "Buying #{ numTickets } Tickets"
while numTickets > 0
console.log "Ticket Sale"
tickets.push
email: ord.email ? ''
first_name: firstName ? ''
last_name: lastName ? ''
ticket_price:
ticket_price_id: 124081
numTickets--
console.log "Confirm Sale of #{ tickets.length } Tickets"
data =
data:
attributes:
event_id: 138036
invoice:
email: ord.email ? ''
first_name: firstName ? ''
last_name: lastName ? ''
tickets: tickets
type: 'checkout'
console.log 'Posting Checkout Data', JSON.stringify(data)
axios.post 'https://api.picatic.com/v2/checkout', data
.then (req) ->
checkoutData = req.data
console.log 'Posting Checkout Confirmation Data', checkoutData.data.id
return axios.post "https://api.picatic.com/v2/checkout/#{ checkoutData.data.id }/confirm"
.then (req) ->
console.log 'SUCCESS!'
.catch (err) ->
console.log 'Error', err
res.send ord
app.use '/webhook', router
app.use express.static('public')
console.log 'Starting Server'
options =
key: fs.readFileSync('./server.key'),
cert: fs.readFileSync('./server.crt'),
requestCert: false,
rejectUnauthorized: false
http.createServer (req, res)->
res.writeHead(301, { "Location": "https://" + req.headers['host'] + req.url })
res.end()
.listen 80
https.createServer(options, app).listen 443