3
3
import json
4
4
import random
5
5
import logging
6
+ from framework .isobot import isocardtxn as isocardtxn_
6
7
from api import auth
7
8
from flask import Flask
8
9
from flask import request
13
14
log = logging .getLogger ('werkzeug' )
14
15
log .setLevel (logging .ERROR )
15
16
app = Flask ('' )
17
+ isocardtxn = isocardtxn_ .IsoCardTxn ()
16
18
currency = currency .CurrencyAPI ("database/currency.json" , "logs/currency.log" )
17
19
18
20
def call_isocards_database () -> dict :
@@ -36,6 +38,16 @@ def generate_verification_code() -> int:
36
38
code : str = int_1 + int_2 + int_3 + int_4 + int_5 + int_6
37
39
return int (code )
38
40
41
+ def generate_txn_id () -> str :
42
+ """Generates a randomized transaction id, which is three *CAPITAL* letters followed by 6 numbers."""
43
+ txn_id = str ()
44
+ for _ in range (3 ):
45
+ txn_id += str (random .choice (('A' , 'B' , 'C' , 'D' , 'E' , 'F' , 'G' , 'H' , 'I' , 'J' , 'K' , 'L' , 'M' , 'N' , 'O' , 'P' , 'Q' , 'R' , 'S' , 'T' , 'U' , 'V' , 'W' , 'X' , 'Y' , 'Z' , )))
46
+ txn_id += "-"
47
+ for _ in range (6 ):
48
+ txn_id += str (random .randint (0 , 9 ))
49
+ return txn_id
50
+
39
51
# API Commands
40
52
@app .route ('/' , methods = ["GET" ])
41
53
def main ():
@@ -53,8 +65,10 @@ def requestpayment():
53
65
merchant_id = args .get ("merchantid" )
54
66
if str (isocards [str (card_number )]["ssc" ]) == ssc :
55
67
verification_code = generate_verification_code ()
68
+ txn_id = generate_txn_id ()
56
69
user_id = isocards [str (card_number )]["cardholder_user_id" ]
57
70
transactions_db [str (verification_code )] = {
71
+ "txn_id" : txn_id ,
58
72
"payer_id" : user_id ,
59
73
"merchant_id" : merchant_id ,
60
74
"card_number" : card_number ,
@@ -63,9 +77,17 @@ def requestpayment():
63
77
"status" : "in_progress"
64
78
}
65
79
save (transactions_db )
80
+ isocardtxn .write_to_log (
81
+ txn_id = txn_id ,
82
+ payer_id = user_id ,
83
+ reciever_id = merchant_id ,
84
+ data = f"New transaction request started (txn_amount: { amount } ; verification code: { verification_code } )"
85
+ )
86
+ isocardtxn .write_transaction (txn_id , user_id , merchant_id , card_number , user_id , int (amount ), "In Progress" )
66
87
request_data = {
67
88
"code" : 200 ,
68
89
"message" : f"Payment requested to IsoCard number: { card_number } . Payment will be complete once user accepts this." ,
90
+ "txn_id" : txn_id ,
69
91
"verification_code" : verification_code
70
92
}
71
93
return request_data , 200
@@ -81,38 +103,65 @@ def requestpayment():
81
103
82
104
@app .route ('/checkpayment' , methods = ["GET" ])
83
105
def checkpayment ():
106
+ with open ("database/isocard_transactions.json" , 'r' ) as f : transactions_db = json .load (f )
84
107
try :
85
- with open ("database/isocard_transactions.json" , 'r' ) as f : transactions_db = json .load (f )
86
108
args = request .args
87
109
verification_code = args .get ("verificationcode" )
110
+ txn_id : str = transactions_db [str (verification_code )]["txn_id" ]
88
111
if transactions_db [str (verification_code )]["status" ] == "complete" :
89
112
if currency .get_bank (transactions_db [str (verification_code )]["payer_id" ]) < transactions_db [str (verification_code )]["amount" ]:
113
+ isocardtxn .write_to_log (
114
+ txn_id = txn_id ,
115
+ payer_id = transactions_db [str (verification_code )]["payer_id" ],
116
+ reciever_id = transactions_db [str (verification_code )]["merchant_id" ],
117
+ data = "Transaction has been terminated (reason: insufficient balance of the payer)"
118
+ )
90
119
del transactions_db [str (verification_code )]
120
+ save (transactions_db )
121
+ isocardtxn .update_transaction_status (txn_id , "Terminated (insufficient balance)" )
91
122
return {
92
123
"code" : 403 ,
124
+ "txn_id" : txn_id ,
93
125
"message" : "Transaction terminated: Insufficient payer balance." ,
94
126
"exception" : "InsufficientFunds"
95
127
}, 403
96
128
currency .bank_remove (transactions_db [str (verification_code )]["payer_id" ], transactions_db [str (verification_code )]["amount" ])
97
129
currency .bank_add (transactions_db [str (verification_code )]["merchant_id" ], transactions_db [str (verification_code )]["amount" ])
130
+ isocardtxn .write_to_log (
131
+ txn_id = txn_id ,
132
+ payer_id = transactions_db [str (verification_code )]["payer_id" ],
133
+ reciever_id = transactions_db [str (verification_code )]["merchant_id" ],
134
+ data = f"Payment of { transactions_db [str (verification_code )]['amount' ]} coins has been successful"
135
+ )
98
136
del transactions_db [str (verification_code )]
99
137
save (transactions_db )
138
+ isocardtxn .update_transaction_status (txn_id , "Successful" )
100
139
return {
101
140
"code" : 200 ,
141
+ "txn_id" : txn_id ,
102
142
"message" : "Transaction complete."
103
143
}, 200
104
144
else : return {
105
145
"code" : 202 ,
146
+ "txn_id" : txn_id ,
106
147
"message" : "Transaction still not approved."
107
148
}, 202
108
149
except KeyError : return {
109
150
"code" : 404 ,
110
151
"message" : "Verification code does not point to an active transaction." ,
111
152
"exception" : "TransactionNotFound"
112
153
}, 404
113
- except Exception as e : return {
154
+ except Exception as e :
155
+ isocardtxn .write_to_log (
156
+ txn_id = txn_id ,
157
+ payer_id = transactions_db [str (verification_code )]["payer_id" ],
158
+ reciever_id = transactions_db [str (verification_code )]["merchant_id" ],
159
+ data = f"Failed to process payment due to a server error (error: { e } )"
160
+ )
161
+ isocardtxn .update_transaction_status (txn_id , "Failed (unable to process payment)" )
162
+ return {
114
163
"code" : 500 ,
115
- "message" : f"Failed to process payment: { e } " ,
164
+ "message" : f"Failed to process payment due to an unhandled server error : { e } " ,
116
165
"exception" : type (e ).__name__
117
166
}, 500
118
167
@@ -151,11 +200,13 @@ def account():
151
200
# Initialization
152
201
def run (): app .run (host = "0.0.0.0" , port = 4800 )
153
202
154
- if auth .get_runtime_options ()["isocard_server_enabled" ]: # Run server ONLY if its runtime option is enabled
155
- print ("[isocard/server] Starting IsoCard payments server..." )
156
- t = Thread (target = run )
157
- t .daemon = True
158
- t .start ()
203
+ def deploy_server ():
204
+ """Deploys the IsoCard Payments Server. (if the option is enabled in the runtimeconfig file)\n \n Runtimeconfig Option: `isocard_server_enabled`"""
205
+ if auth .get_runtime_options ()["isocard_server_enabled" ]: # Run server ONLY if its runtime option is enabled
206
+ print ("[isocard/server] Starting IsoCard payments server..." )
207
+ t = Thread (target = run )
208
+ t .daemon = True
209
+ t .start ()
159
210
160
211
161
212
#btw i use arch
0 commit comments