This repository has been archived by the owner on Dec 28, 2018. It is now read-only.
forked from sakjur/battlehack
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
83 lines (76 loc) · 2.71 KB
/
server.py
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
from flask import Flask, request, render_template
from urllib.parse import parse_qs
import braintree
import apini
import elks
import os
import re
app = Flask(__name__)
baseurl = 'https://apini.theusr.org/pay/%s'
@app.route('/')
def index():
return render_template('index.html')
@app.route('/incomingsms', methods=['POST'])
def incomingsms():
rawmessage = request.form['message'].strip()
rawmessage = rawmessage.split()
rawmessage = list(map(lambda x: x.strip(), rawmessage))
sender = request.form['from']
if len(rawmessage) < 2:
return ''
if re.match("\+[0-9]*", rawmessage[0]):
recipient = rawmessage[0]
amount = rawmessage[2]
else:
recipient = sender
amount = rawmessage[1]
amount = int(amount)
if amount > 15000:
return 'Wow. That\'s way too much money for a little bee. Nope.\n'
url = apini.insert_transaction(amount, sender, recipient)
if recipient == sender:
return "Hey there, the payment of SEK %s may now be payed at\n%s" % (
amount, baseurl % url)
else:
elks.send_url(baseurl % url, amount, sender, recipient)
return ''
@app.route('/pay', methods=['POST'])
def post_handler():
uri = request.form['paytoken']
if apini.is_payed(uri):
return render_template('resultpage.html', paid=True, good=False)
else:
result = braintree.Transaction.sale({
"amount": apini.get_amount(uri),
"payment_method_nonce": request.form['payment_method_nonce'],
"options": {
"submit_for_settlement": True
}
})
if result.is_success:
apini.mark_as_payed(uri)
parts = apini.get_transaction_parts(uri)
sender = parts[0]
recipient = parts[1]
amount = parts[2]
elks.has_payed(recipient, sender, amount)
return render_template('resultpage.html', good=True, paid=False)
else:
return render_template('resultpage.html', good=False, paid=False, uri=uri)
@app.route('/pay/<payid>')
def pay(payid):
if apini.is_payed(payid):
return render_template('resultpage.html', paid=True, good=False)
braintree.Configuration.configure(braintree.Environment.Sandbox,
merchant_id=os.environ['BT_MERCHANT_ID'],
public_key=os.environ['BT_PUBLIC_KEY'],
private_key=os.environ['BT_PRIVATE_KEY'])
token = braintree.ClientToken.generate()
amount = apini.get_amount(payid)
return render_template('payform.html',
amount=amount,
token=token,
uri=payid)
if __name__ == '__main__':
print('Starting server...')
app.run(debug=True)