diff --git a/src/transactions/routes.py b/src/transactions/routes.py index 3582bb7..5ae3a50 100644 --- a/src/transactions/routes.py +++ b/src/transactions/routes.py @@ -1,9 +1,5 @@ -from flask import request +from flask import request, redirect import asyncio - -import random -import string - from src.extensions import db from src.extensions import client from src.transactions import bp @@ -14,6 +10,10 @@ @bp.route('/verify/', methods=['POST']) def verify_transaction(): + """ + Verify if a transaction is made + :return: success if the transaction is made. Unsuccessful if not. Success returns the transaction_id and amount paid + """ try: json_data = json.loads(request.data) buyer_venmo_id = json_data['buyer_venmo_id'] @@ -23,29 +23,39 @@ def verify_transaction(): except json.decoder.JSONDecodeError as e: return failure_message(FAIL_MSG.POST_FORM.ERROR + str(e)) - status, amount = asyncio.run(get_transaction(buyer_id=buyer_venmo_id, club_id=club_venmo_id)) + try: + status, tran = asyncio.run(get_transaction(buyer_id=buyer_venmo_id, club_id=club_venmo_id)) + except Exception as e: + return failure_message(FAIL_MSG.VENMO.UNABLE_GET_TRANSACTION + str(e)) if status == -2: return failure_message(FAIL_MSG.VENMO.TIMEOUT) if status == -1: return failure_message(FAIL_MSG.VENMO.UNABLE_GET_TRANSACTION) - amount = 0.0 if amount is None else amount + amount = 0.0 if tran.amount is None else tran.amount - return success_message(amount) + return success_message({'transaction_id': tran.payment_id, 'amount': amount}) -@bp.route('/create/', methods=['POST']) -def create_transaction(): - pass - +@bp.route('/qrcode/', methods=['POST']) +def create_qrcode(): + try: + json_data = json.loads(request.data) + transaction_id = json_data['transaction_id'] + except KeyError as e: + return failure_message(FAIL_MSG.POST_FORM.FIELD_NAME_WRONG + str(e)) + except json.decoder.JSONDecodeError as e: + return failure_message(FAIL_MSG.POST_FORM.ERROR + str(e)) -# def get_qr_code_link(transaction_id, reference_string): -# return f'https://chart.googleapis.com/chart?cht=qr&chl={transaction_id}__{reference_string}&chs=500x500' -# -# -# def create_reference_string(transaction_id): -# transaction_id * 76622729181571704961 -# characters = string.ascii_letters + string.digits -# random_string = [''.join(random.choice(characters)) for _ in range(15)] -# return random_string + return redirect(get_qr_code_link(transaction_id)) + +# @bp.route('/create/', methods=['POST']) +# def create_transaction(): +# try: +# json_data = json.loads(request.data) +# transaction_id = json_data['transaction_id'] +# except KeyError as e: +# return failure_message(FAIL_MSG.POST_FORM.FIELD_NAME_WRONG + str(e)) +# except json.decoder.JSONDecodeError as e: +# return failure_message(FAIL_MSG.POST_FORM.ERROR + str(e)) diff --git a/src/transactions/utils.py b/src/transactions/utils.py index 34ccbbc..e361063 100644 --- a/src/transactions/utils.py +++ b/src/transactions/utils.py @@ -1,6 +1,9 @@ from src.extensions import client import asyncio +import random +import string + async def get_user_by_username(username: str) -> (int, object): """ @@ -33,7 +36,7 @@ async def get_transaction(buyer_id: int, club_id: int) -> (int, float): Get the transaction between a buyer and a club :param buyer_id: the venmo id of the buyer :param club_id: the venmo id of the club - :return: (status_code: 0 means ok, -1 means didn't get the transaction, -2 means time out), (amount paid) + :return: (status_code: 0 means ok, -1 means didn't get the transaction, -2 means time out), (transaction object) """ async def async_get_transaction(): return client.user.get_user_transactions(user_id=buyer_id, limit=10) @@ -47,7 +50,17 @@ async def async_get_transaction(): for tran in transactions: if tran.target.id == club_id: - amount = tran.amount - return 0, amount + return 0, tran return -1, None + + +def get_qr_code_link(transaction_id, reference_string=None): + return f'https://chart.googleapis.com/chart?cht=qr&chl={transaction_id}__{reference_string}&chs=500x500' + +def create_reference_string(transaction_id): + transaction_id * 76622729181571704961 + characters = string.ascii_letters + string.digits + random_string = [''.join(random.choice(characters)) for _ in range(15)] + return random_string +