Skip to content

Commit

Permalink
firebase bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhongxuanWang committed Dec 4, 2023
1 parent 951c8c9 commit 618f92c
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 30 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,5 @@ cython_debug/
.idea/usage.statistics.xml
.idea/shelf
/app.db

firebase_creds.json
13 changes: 0 additions & 13 deletions curaise-mvp-firebase-adminsdk-4nxtv-07a17b43c3.json

This file was deleted.

2 changes: 1 addition & 1 deletion src/clubs/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def create_club():

login_user(new_club, remember=True)

return success_message(user.uid)
return success_message(new_club.id)


@bp.route('/signin/', methods=['POST'])
Expand Down
2 changes: 1 addition & 1 deletion src/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
client = Client(access_token=os.environ["VENMO_TOKEN"], )
login_manager = LoginManager()

cred = credentials.Certificate("curaise-mvp-firebase-adminsdk-4nxtv-07a17b43c3.json")
cred = credentials.Certificate("firebase_creds.json")
firebase_admin.initialize_app(cred)

# firebase_config = {
Expand Down
6 changes: 3 additions & 3 deletions src/models/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ def serialize(self, simplified=False, ios_style=True):
return {
'id': self.id,
'referenceString': self.reference_string,
'fundraiser': Fundraiser.query.get({'id': self.fundraiser}),
'fundraiser': Fundraiser.query.get({'id': self.fundraiser}).serialize(ios_style=True),
'timestamp': str(self.added_timestamp.strftime(DATETIME_FORMAT)),
'items': [item.serialize(ios_style=True) for item in self.items],
'buyerId': self.payer,
'buyerId': self.payer_id,
'transactionComplete': self.status
}

Expand All @@ -51,7 +51,7 @@ def serialize(self, simplified=False, ios_style=True):
return {
'id': self.id,
'reference_string': self.reference_string,
'fundraiser': self.fundraiser,
'fundraiser': self.fundraiser.serialize(ios_style=True),
'timestamp': self.added_timestamp,
'item': self.item,
'payer': self.payer,
Expand Down
42 changes: 31 additions & 11 deletions src/transactions/routes.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from flask import request, redirect
import asyncio

from src.extensions import db
from src.extensions import client
from src.transactions import bp
from src.models import Transaction
from src.models import Transaction, FundraiserItem
from src.utils import *
from src.transactions.utils import *

Expand All @@ -16,15 +17,25 @@ def verify_transaction():
"""
try:
json_data = json.loads(request.data)
buyer_venmo_id = json_data['buyer_venmo_id']
club_venmo_id = json_data['club_venmo_id']
token = json_data['Authorization']
club_venmo_username = json_data['club_venmo_username']
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))

uid = verify_firebase_token(token)
if uid is None:
return failure_message(FAIL_MSG.TARGET_NOT_FOUND)

buyer = firebase_get_model(uid=uid)
club = Club.query.filter_by(venmo_username=club_venmo_username).first()

if club is None:
return failure_message(FAIL_MSG.TARGET_NOT_FOUND)

try:
status, tran = asyncio.run(get_transaction(buyer_id=buyer_venmo_id, club_id=club_venmo_id))
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))

Expand Down Expand Up @@ -61,11 +72,17 @@ def create_transaction():

try:
json_data = json.loads(request.data)
transaction_id = json_data['transaction_id']
# transaction_id = json_data['transaction_id']
fundraiser_id = json_data['fundraiser_id']
fundraiser_item_id = json_data['fundraiser_item_id']
token = json_data['Authorization']

uid = verify_firebase_token(token)
if uid is None:
return failure_message(FAIL_MSG.TARGET_NOT_FOUND)

club_id = json_data['club_id']
payer_id = json_data['payer_id']

referrer = None
if 'referrer' in json_data.keys():
referrer = json_data['referrer']
Expand All @@ -75,24 +92,27 @@ def create_transaction():
except json.decoder.JSONDecodeError as e:
return failure_message(FAIL_MSG.POST_FORM.ERROR + str(e))

reference_string = create_reference_string(transaction_id=transaction_id)
buyer = firebase_get_model(uid=uid)
reference_string = create_reference_string(transaction_id=8910234)

try:
status, tran = asyncio.run(get_transaction(buyer_id=payer_id, club_id=club_id))
status, tran = asyncio.run(get_transaction(buyer_id=buyer.venmo_id, club_id=club_id))
except Exception as e:
return failure_message(FAIL_MSG.VENMO.UNABLE_GET_TRANSACTION + str(e))

status_bool = (status == 0)

fundraiser_items = [FundraiserItem.query.filter_by(id=fundraiser_item).first() for fundraiser_item in fundraiser_item_id]

try:
new_tran = Transaction(
reference_string=reference_string,
fundraiser=fundraiser_id,
item=fundraiser_item_id,
items=fundraiser_items,
club=club_id,
payer=payer_id,
payer_id=buyer.id,
status=status_bool,
referrer=referrer,
# referrer_id=referrer,
)
db.session.add(new_tran)
db.session.commit()
Expand Down
2 changes: 1 addition & 1 deletion src/transactions/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def create_reference_string(transaction_id):
transaction_id * 76622729181571704961 # A large prime number
characters = string.ascii_letters + string.digits
random_string = [''.join(random.choice(characters)) for _ in range(15)]
return random_string
return ''.join(random_string)


def get_qr_code_link(transaction_id, reference_string):
Expand Down

0 comments on commit 618f92c

Please sign in to comment.