|
| 1 | +"""The IsoCard payments web server.""" |
| 2 | +# Imports |
| 3 | +import json |
| 4 | +import random |
| 5 | +import logging |
| 6 | +from api import auth |
| 7 | +from flask import Flask |
| 8 | +from flask import request |
| 9 | +from framework.isobot import currency |
| 10 | +from threading import Thread |
| 11 | + |
| 12 | +# Configuration |
| 13 | +log = logging.getLogger('werkzeug') |
| 14 | +log.setLevel(logging.ERROR) |
| 15 | +app = Flask('') |
| 16 | +currency = currency.CurrencyAPI("database/currency.json", "logs/currency.log") |
| 17 | + |
| 18 | +def call_isocards_database() -> dict: |
| 19 | + """Calls all of the latest information from the IsoCards database.""" |
| 20 | + with open("database/isocard.json", 'r') as f: isocards = json.load(f) |
| 21 | + return isocards |
| 22 | + |
| 23 | +def save(data): |
| 24 | + """Dumps all cached databases to the local machine.""" |
| 25 | + with open("database/isocard_transactions.json", 'w+') as f: json.dump(data, f, indent=4) |
| 26 | + |
| 27 | +# Functions |
| 28 | +def generate_verification_code() -> int: |
| 29 | + """Generates a random 6 digit verification code.""" |
| 30 | + int_1 = str(random.randint(1, 9)) |
| 31 | + int_2 = str(random.randint(0, 9)) |
| 32 | + int_3 = str(random.randint(0, 9)) |
| 33 | + int_4 = str(random.randint(0, 9)) |
| 34 | + int_5 = str(random.randint(0, 9)) |
| 35 | + int_6 = str(random.randint(0, 9)) |
| 36 | + code: str = int_1 + int_2 + int_3 + int_4 + int_5 + int_6 |
| 37 | + return int(code) |
| 38 | + |
| 39 | +# API Commands |
| 40 | +@app.route('/', methods=["GET"]) |
| 41 | +def main(): |
| 42 | + return "Server is online." |
| 43 | + |
| 44 | +@app.route('/requestpayment', methods=["GET"]) |
| 45 | +def requestpayment(): |
| 46 | + try: |
| 47 | + isocards = call_isocards_database() |
| 48 | + with open("database/isocard_transactions.json", 'r') as f: transactions_db = json.load(f) |
| 49 | + args = request.args |
| 50 | + card_number = args.get("cardnumber") |
| 51 | + ssc = args.get("ssc") |
| 52 | + amount = args.get("amount") |
| 53 | + merchant_id = args.get("merchantid") |
| 54 | + if str(isocards[str(card_number)]["ssc"]) == ssc: |
| 55 | + verification_code = generate_verification_code() |
| 56 | + user_id = isocards[str(card_number)]["cardholder_user_id"] |
| 57 | + transactions_db[str(verification_code)] = { |
| 58 | + "payer_id": user_id, |
| 59 | + "merchant_id": merchant_id, |
| 60 | + "card_number": card_number, |
| 61 | + "user_id": user_id, |
| 62 | + "amount": int(amount), |
| 63 | + "status": "in_progress" |
| 64 | + } |
| 65 | + save(transactions_db) |
| 66 | + request_data = { |
| 67 | + "code": 200, |
| 68 | + "message": f"Payment requested to IsoCard number: {card_number}. Payment will be complete once user accepts this.", |
| 69 | + "verification_code": verification_code |
| 70 | + } |
| 71 | + return request_data, 200 |
| 72 | + else: return { |
| 73 | + "code": 401, |
| 74 | + "message": "Unable to authorize transaction." |
| 75 | + }, 401 |
| 76 | + except Exception as e: return { |
| 77 | + "code": 500, |
| 78 | + "message": f"Failed to process payment: {e}", |
| 79 | + "exception": type(e).__name__ |
| 80 | + }, 500 |
| 81 | + |
| 82 | +@app.route('/checkpayment', methods=["GET"]) |
| 83 | +def checkpayment(): |
| 84 | + try: |
| 85 | + with open("database/isocard_transactions.json", 'r') as f: transactions_db = json.load(f) |
| 86 | + args = request.args |
| 87 | + verification_code = args.get("verificationcode") |
| 88 | + if transactions_db[str(verification_code)]["status"] == "complete": |
| 89 | + if currency.get_bank(transactions_db[str(verification_code)]["payer_id"]) < transactions_db[str(verification_code)]["amount"]: |
| 90 | + del transactions_db[str(verification_code)] |
| 91 | + return { |
| 92 | + "code": 403, |
| 93 | + "message": "Transaction terminated: Insufficient payer balance.", |
| 94 | + "exception": "InsufficientFunds" |
| 95 | + }, 403 |
| 96 | + currency.bank_remove(transactions_db[str(verification_code)]["payer_id"], transactions_db[str(verification_code)]["amount"]) |
| 97 | + currency.bank_add(transactions_db[str(verification_code)]["merchant_id"], transactions_db[str(verification_code)]["amount"]) |
| 98 | + del transactions_db[str(verification_code)] |
| 99 | + save(transactions_db) |
| 100 | + return { |
| 101 | + "code": 200, |
| 102 | + "message": "Transaction complete." |
| 103 | + }, 200 |
| 104 | + else: return { |
| 105 | + "code": 202, |
| 106 | + "message": "Transaction still not approved." |
| 107 | + }, 202 |
| 108 | + except KeyError: return { |
| 109 | + "code": 404, |
| 110 | + "message": "Verification code does not point to an active transaction.", |
| 111 | + "exception": "TransactionNotFound" |
| 112 | + }, 404 |
| 113 | + except Exception as e: return { |
| 114 | + "code": 500, |
| 115 | + "message": f"Failed to process payment: {e}", |
| 116 | + "exception": type(e).__name__ |
| 117 | + }, 500 |
| 118 | + |
| 119 | +@app.route('/account', methods=["GET"]) |
| 120 | +def account(): |
| 121 | + try: |
| 122 | + isocards = call_isocards_database() |
| 123 | + args = request.args |
| 124 | + isocard_number = args.get("cardnumber") |
| 125 | + ssc = args.get("ssc") |
| 126 | + if isocards[str(isocard_number)]["ssc"] == ssc: |
| 127 | + card_data = isocards[str(isocard_number)] |
| 128 | + del card_data["config"] |
| 129 | + del card_data["ssc"] |
| 130 | + card_data["account_balance"] = currency.get_wallet(card_data["user_id"]) |
| 131 | + return { |
| 132 | + "code": 200, |
| 133 | + "card_info": card_data |
| 134 | + }, 200 |
| 135 | + else: return { |
| 136 | + "code": 403, |
| 137 | + "message": "Incorrect IsoCard SSC.", |
| 138 | + "exception": "InvalidSSC" |
| 139 | + } |
| 140 | + except KeyError: return { |
| 141 | + "code": 404, |
| 142 | + "message": "Card number is invalid.", |
| 143 | + "exception": "InvalidIsoCard" |
| 144 | + }, 404 |
| 145 | + except Exception as e: return { |
| 146 | + "code": 500, |
| 147 | + "message": f"Failed to fetch account info: {e}", |
| 148 | + "exception": type(e).__name__ |
| 149 | + } |
| 150 | + |
| 151 | +# Initialization |
| 152 | +def run(): app.run(host="0.0.0.0", port=4800) |
| 153 | + |
| 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() |
| 159 | + |
| 160 | + |
| 161 | +#btw i use arch |
0 commit comments