-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
302 lines (217 loc) · 8.13 KB
/
app.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
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
from genericpath import isfile
from flask import Flask, render_template, request, jsonify
# import requests
import hashlib
import string
import random
from decouple import config
import os
import sqlite3 as sql
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail
app = Flask(__name__)
app.debug = True
# #credentials
merchant_key = os.getenv('key', config('key'))
merchant_salt_v1 = os.getenv('salt', config('salt'))
gmail_user = os.getenv('gmail_user', config('gmail_user'))
gmail_pass = os.getenv('gmail_pass', config('gmail_pass'))
# database connection
if(not os.path.isfile('database/transactions.db')):
conn = sql.connect('database/transactions.db')
conn.execute(
"""create table transactions_details (
txnid varchar[25] primary key,
amount int,
firstname varchar[30],
email varchar[30],
status varchar[10],
txn_ref_id varchar[100]
);"""
)
conn.close()
@app.route('/')
def donate_page():
return render_template('donate.html')
@app.route('/infoForm.html')
def get_details():
return render_template('infoForm.html')
@app.route('/initiate', methods=['POST'])
def initiate():
# json data from user details request
data = request.json
# donation amount
try:
amount = str(int(data['amount']))
except:
amount = str("{0:.2f}".format(float(data['amount'])))
# product info
productinfo = 'donation'
# Payer Name
firstname = data['firstname']
lastname = data['lastname']
# Payer email
email = data['email']
# Payer Mob
phone = data['phone']
# Payer Address
address1 = data['address1']
city = data['city']
state = data['state']
country = data['country']
# Redirect link for successful Payment
surl = 'https://pay-gate1.herokuapp.com/success'
# Redirect link for Failure Payment
furl = 'https://pay-gate1.herokuapp.com/failure'
# service_provider
service_provider = 'payu_paisa'
# addres
zipcode = data['zipcode']
# for generating txnid
txnid = generate_txnid()
# Generating hash
post_hashseq = f'{merchant_key}|{txnid}|{amount}|{productinfo}|{firstname}|{email}|||||||||||{merchant_salt_v1}'
# Hash seq should be
# sha512(key|txnid|amonut|donation|Sameer|[email protected]|||||||||||abpR7ROd)sha512(key|txnid|amount|productinfo|firstname|email|||||||||||SALT)
post_hash = hashlib.sha512(
post_hashseq.encode("utf-8")).hexdigest().lower()
dbres = add_db_row(txnid, amount, firstname, email)
if(type(dbres) == Exception):
return f'Try Again, {dbres}'
res_data = {
'firstname': firstname,
'lastname': lastname,
'email': email,
'phone': int(phone),
'amount': float(amount),
'zipcode': zipcode,
'address1': address1,
'city': city,
'state': state,
'country': country,
'merchant_key': merchant_key,
'txnid': txnid,
'hash': post_hash,
'productinfo': productinfo,
'surl': surl,
'furl': furl
}
return jsonify(res_data)
@app.route('/success', methods=['POST'])
def success():
data = request.values.to_dict(flat=True)
# verify response hash
res = verify_resp_hash(data)
if(type(res) != dict or res['verify'] == 'fail'):
return render_template('error.html', msg='Transaction Verification Failed')
change_status(data['txnid'], data['status'], ref_id=data['mihpayid'])
# Sending mail
invoice_gen(data['firstname'], data['lastname'], data['email'], data['address1'], data['state'],
data['country'], data['amount'], data['addedon'], data['status'], data['txnid'])
return render_template('success_Trans.html', status=data['status'], txnid=data['txnid'], amount=data['amount'], mihpayid=data['mihpayid'])
@app.route('/failure', methods=['POST'])
def failure():
data = request.values.to_dict(flat=True)
# verify response hash
res = verify_resp_hash(data)
if(type(res) != dict or res['verify'] == 'fail'):
return render_template('error.html', msg='Transaction Verification Failed')
change_status(data['txnid'], data['status'], data['mihpayid'])
# sending invoice
invoice_gen(data['firstname'], data['lastname'], data['email'], data['address1'], data['state'],
data['country'], data['amount'], data['addedon'], data['status'], data['txnid'])
return render_template('failure_Trans.html', status=data['status'], txnid=data['txnid'], amount=data['amount'], mihpayid=data['mihpayid'])
#generate transaction ID
def generate_txnid():
# for generating txnid
seq = string.ascii_letters+string.digits
return "".join(random.choices(seq, k=20))
def verify_resp_hash(data: dict):
if(type(data) != dict):
return {'verify': 'fail'}
resp_amount = data['amount']
resp_status = data['status']
resp_txnid = data['txnid']
resp_hash = data['hash']
# now we will compare received hash with our data or our calculated hash to verify payment
try:
# retrive data for perticular txnid from database
db_data = get_db_row(txnid=resp_txnid)[0]
if(type(db_data) != tuple):
return
# Hash Generation Logic for Payment Response
# sha512(SALT|status||||||udf5|udf4|udf3|udf2|udf1|email|firstname|productinfo|amount|txnid|key)
calc_hashseq = f"{merchant_salt_v1}|{resp_status}|||||||||||{db_data[3]}|{db_data[2]}|donation|{str('{0:.2f}'.format(float(db_data[1])))}|{db_data[0]}|{merchant_key}"
calc_hash = hashlib.sha512(
calc_hashseq.encode("utf-8")).hexdigest().lower()
except :
return {'verify': 'fail'}
# comparing received hash and calculated hash
if(calc_hash == resp_hash and resp_amount == str('{0:.2f}'.format(float(db_data[1])))):
return {'verify': 'pass'}
else:
return {'verify': 'fail'}
# fucntion to change status of transaction in database
def change_status(txnid, status, ref_id):
try:
query = f"UPDATE transactions_details SET status = '{status}', txn_ref_id= '{ref_id}' WHERE txnid = '{txnid}' ;"
conn = sql.connect('database/transactions.db')
conn.execute(query)
conn.commit()
conn.close()
return 'done'
except:
return f'{Exception}'
# function to add transaction details
def add_db_row(txnid, amount, firstname, email, status='pending', ref_id='null'):
try:
query = f"INSERT INTO transactions_details VALUES ('{txnid}', {amount}, '{firstname}', '{email}', '{status}', '{ref_id}');"
conn = sql.connect('database/transactions.db')
conn.execute(query)
conn.commit()
conn.close()
return 'done'
except Exception as exp:
return exp
# function to retrive transaction details for verification
def get_db_row(txnid: str):
query = f"SELECT * FROM transactions_details WHERE txnid='{txnid}'"
conn = sql.connect('database/transactions.db')
mycur = conn.cursor()
mycur.execute(query)
# exmaple of db resp: [('txnid', 52, 'sameer', 'email', 'status', None)]
# ie. list of tuples, tuple contain row element
db_resp = mycur.fetchall()
conn.close()
return db_resp
def invoice_gen(firstname, lastname, email, address1, state, country, amount, date, status, txnid):
#Invoice is in html
html_temp = render_template(
'invoices.html',
firstname=firstname,
lastname=lastname,
email=email,
address1=address1,
state=state,
country=country,
amount=amount,
date=date,
status=status,
txnid=txnid
)
sendmail(html_temp, receiver = email)
def sendmail(html:str, receiver:str):
message = Mail(
from_email=gmail_user,
to_emails=receiver,
subject='Payment Invoice',
html_content=html
)
try:
sg = SendGridAPIClient(os.getenv('sendmail_key',config('sendmail_key')))
response = sg.send(message)
except Exception as exp:
print(exp.message)
# Start point of program
if(__name__ == '__main__'):
app.run()