-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyrestapi.py
144 lines (127 loc) · 5.6 KB
/
myrestapi.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
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
import json
import bottle
from bottle import route, run, request, abort, template, static_file
from bottle_rest import json_to_params, json_to_data
from pymongo import MongoClient
from bson.objectid import ObjectId
import requests
import base64
import json
connection = MongoClient('localhost', 27017)
db = connection.yojee
wallet = db.wallet
token = db.token
txn = db.txn
@bottle.error(405)
def method_not_allowed(res):
if request.method == 'OPTIONS':
new_res = bottle.HTTPResponse()
new_res.set_header('Access-Control-Allow-Origin', '*')
new_res.set_header('Access-Control-Allow-Methods', 'GET, POST, PUT, OPTIONS')
new_res.set_header('Access-Control-Allow-Headers', 'Origin, Accept, Content-Type, X-Requested-With, X-CSRF-Token')
return new_res
res.headers['Allow'] += ', OPTIONS'
return request.app.default_error_handler(res)
@bottle.hook('after_request')
def enableCORSAfterRequestHook():
bottle.response.set_header('Access-Control-Allow-Origin', '*')
bottle.response.set_header('Access-Control-Allow-Methods', 'GET, POST, PUT, OPTIONS')
bottle.response.set_header('Access-Control-Allow-Headers', 'Origin, Accept, Content-Type, X-Requested-With, X-CSRF-Token')
@route('/authenticate', method=['POST'])
@json_to_data
def authenticate(data):
if data is None:
raise ValueError
username = data['username']
password = data['password']
row = wallet.find_one({'username' : username, 'password' : password})
if row is None:
return {'success': False, 'message':'login failed'}
else:
token_id = token.insert_one({'userid': str(row['_id'])}).inserted_id
return {'success': True, 'userid': str(row['_id']), 'token': str(token_id)}
@route('/<userid>/balance', method=['GET'])
def balance(userid):
tokenObj = token.find_one({'_id' : ObjectId(request.query['token'])})
if tokenObj is None:
return {'success': False, 'message': 'Invalid token'}
if tokenObj['userid'] != userid:
return {'success': False, 'message': 'Invalid token'}
walletObj = wallet.find_one({'_id': ObjectId(userid)})
if walletObj is None:
return {'success': False, 'message': 'wallet not found'}
return {'success': True, 'balance': walletObj['balance']}
@route('/<userid>/recharge', method=['POST'])
@json_to_data
def recharge(userid, data):
tokenObj = token.find_one({'_id' : ObjectId(request.query['token'])})
if tokenObj is None:
return {'success': False, 'message': 'Invalid token'}
if tokenObj['userid'] != userid:
return {'success': False, 'message': 'Invalid token'}
result = wallet.update_one({'_id': ObjectId(userid)}, {'$inc': {'balance': data['amount']}})
walletObj = wallet.find_one({'_id': ObjectId(userid)})
return {'success': True, 'balance': walletObj['balance']}
@route('/<userid>/transaction', method=['POST'])
@json_to_data
def transaction(userid, data):
tokenObj = token.find_one({'_id' : ObjectId(request.query['token'])})
if tokenObj is None:
return {'success': False, 'message': 'Invalid token'}
if tokenObj['userid'] != userid:
return {'success': False, 'message': 'Invalid token'}
walletObj = wallet.find_one({'_id': ObjectId(userid)})
amount = data['amount']
if amount < walletObj['balance']:
walletAmount = amount
txnAmount = 0
else:
walletAmount = walletObj['balance']
txnAmount = amount - walletObj['balance']
trans = {'userid': userid, 'walletAmount': walletAmount, 'txnAmount': txnAmount, 'txnCompleted': False}
txn_id = txn.insert_one(trans).inserted_id
if txnAmount != 0:
url = 'https://app.sandbox.midtrans.com/snap/v1/transactions'
data = {'transaction_details': {
'order_id': str(txn_id),
'gross_amount': int(txnAmount)
},
'credit_card': {
'secure': True
}
}
headers = {'Authorization': 'Basic ' + base64.b64encode('VT-server-hQ3ySAyvozlOY9GRQssObufG:')}
response = requests.post(url, json=data, headers=headers)
return {'success': True, 'redirect': True, 'txn_id': str(txn_id), 'token': response.json()['token']}
else:
result = wallet.update_one({'_id': ObjectId(userid)}, {'$inc': {'balance': -walletAmount}})
result = txn.update_one({'_id': txn_id}, {'$set': {'txnCompleted': True}})
return {'success': True, 'redirect': False, 'txn_id': str(txn_id)}
@route('/notification', method=['POST'])
@json_to_data
def notification(data):
order_id = data['order_id']
trans = txn.find_one({'_id': ObjectId(str(order_id))})
if data['status_code'] == '200':
result = wallet.update_one({'_id': ObjectId(trans['userid'])}, {'$inc': {'balance': -trans['walletAmount']}})
result = txn.update_one({'_id': trans['_id']}, {'$set': {'txnCompleted': True}})
return ''
@route("/")
def index():
return template("app/index.html")
@route("/node_modules/<filename:re:.*>", name="node_modules")
def node_modules(filename):
return static_file(filename, root="node_modules")
@route("/assets/<filename:re:.*>", name="assets")
def assets(filename):
return static_file(filename, root="app/assets")
@route("/css/<filename:re:.*>", name="css")
def css(filename):
return static_file(filename, root="app/css")
@route("/js/<filename:re:.*>", name="js")
def js(filename):
return static_file(filename, root="app/js")
@route("/templates/<filename:re:.*>", name="templates")
def templates(filename):
return static_file(filename, root="app/templates")
run(host='localhost', port=8080, reLoader=True, debug=True)