Skip to content

Commit

Permalink
qrcode working, previous bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhongxuanWang committed Dec 1, 2023
1 parent 861ed42 commit c9be2d5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 24 deletions.
52 changes: 31 additions & 21 deletions src/transactions/routes.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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']
Expand All @@ -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))
19 changes: 16 additions & 3 deletions src/transactions/utils.py
Original file line number Diff line number Diff line change
@@ -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):
"""
Expand Down Expand Up @@ -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)
Expand All @@ -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

0 comments on commit c9be2d5

Please sign in to comment.