From 908100514fc16f67ab98f4afe1784eec7ccae52e Mon Sep 17 00:00:00 2001 From: Edwin Eames Date: Tue, 16 Sep 2025 12:45:32 -0400 Subject: [PATCH] Working flow authorize --- app/classes/cards.py | 3 +- app/delivery/views.py | 6 ++-- app/payment/views.py | 70 ++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 74 insertions(+), 5 deletions(-) diff --git a/app/classes/cards.py b/app/classes/cards.py index c8c06df..a39ee2f 100755 --- a/app/classes/cards.py +++ b/app/classes/cards.py @@ -23,7 +23,8 @@ class Card_Card(db.Model): accepted_or_declined = db.Column(db.INTEGER()) main_card = db.Column(db.BOOLEAN()) zip_code = db.Column(db.VARCHAR(20)) - auth_net_payment_profile_id = db.Column(db.String, unique=True, index=True, nullable=False) + auth_net_payment_profile_id = db.Column(db.String, nullable=True) + class Card_Card_schema(ma.SQLAlchemyAutoSchema): class Meta: model = Card_Card diff --git a/app/delivery/views.py b/app/delivery/views.py index adf14d7..3bb41a3 100755 --- a/app/delivery/views.py +++ b/app/delivery/views.py @@ -337,10 +337,10 @@ def get_deliveries_tommorrow(page): else: offset_limit = (per_page_amount * page) - per_page_amount - deliveries = (db.session - .query(Delivery_Delivery) + deliveries = (db.session + .query(Delivery_Delivery) .filter(Delivery_Delivery.delivery_status == 3) - .order_by(Delivery_Delivery.when_ordered.desc()) + .order_by(Delivery_Delivery.id.desc()) .limit(per_page_amount).offset(offset_limit)) diff --git a/app/payment/views.py b/app/payment/views.py index 320a741..39f1e39 100755 --- a/app/payment/views.py +++ b/app/payment/views.py @@ -201,9 +201,32 @@ def create_user_card(user_id): print(f"DATABASE ERROR: Could not save card for user {user_id}. Error: {e}") return jsonify({"ok": False, "error": "Failed to save card information."}), 500 - # Return a success response because the primary goal was achieved. + # Return a success response with the card_id + return jsonify({"ok": True, "card_id": create_new_card.id}), 200 + +@payment.route("/card/update_payment_profile/", methods=["PUT"]) +def update_card_payment_profile(card_id): + """ + Updates the auth_net_payment_profile_id for a card + """ + get_card = (db.session + .query(Card_Card) + .filter(Card_Card.id == card_id) + .first()) + if not get_card: + return jsonify({"ok": False, "error": "Card not found"}), 404 + + data = request.get_json() + auth_net_payment_profile_id = data.get("auth_net_payment_profile_id") + + get_card.auth_net_payment_profile_id = auth_net_payment_profile_id + + db.session.add(get_card) + db.session.commit() + return jsonify({"ok": True}), 200 + @payment.route("/card/edit/", methods=["PUT"]) def update_user_card(card_id): """ @@ -226,6 +249,7 @@ def update_user_card(card_id): card_number = data.get("card_number") main_card = data.get("main_card", False) zip_code = data.get("zip_code") + auth_net_payment_profile_id = data.get("auth_net_payment_profile_id") get_card.card_number = card_number get_card.name_on_card = name_on_card @@ -235,6 +259,7 @@ def update_user_card(card_id): get_card.security_number = security_number get_card.main_card = main_card get_card.zip_code = zip_code + get_card.auth_net_payment_profile_id = auth_net_payment_profile_id # FIX: Correctly slice the last four digits on edit if card_number and card_number[-4:].isdigit(): @@ -289,3 +314,46 @@ def get_authorize_transactions(page): except Exception as e: return jsonify({"ok": False, "error": str(e)}), 500 + + +@payment.route("/transactions/customer//", methods=["GET"]) +def get_customer_transactions(customer_id, page): + """ + Gets transactions for a specific customer + """ + try: + per_page = 50 + offset = (page - 1) * per_page + + query = ( + db.session + .query(Transaction, Customer_Customer) + .join(Customer_Customer, Transaction.customer_id == Customer_Customer.id) + .filter(Transaction.customer_id == customer_id) + .order_by(Transaction.created_at.desc()) + .offset(offset) + .limit(per_page) + ) + + results = query.all() + + transactions_data = [] + for transaction, customer in results: + transactions_data.append({ + "id": transaction.id, + "preauthorize_amount": transaction.preauthorize_amount, + "charge_amount": transaction.charge_amount, + "transaction_type": transaction.transaction_type, + "status": transaction.status, + "customer_name": f"{customer.customer_first_name} {customer.customer_last_name}", + "created_at": transaction.created_at.isoformat(), + "auth_net_transaction_id": transaction.auth_net_transaction_id, + "rejection_reason": transaction.rejection_reason, + "delivery_id": transaction.delivery_id, + "service_id": transaction.service_id, + }) + + return jsonify(transactions_data), 200 + + except Exception as e: + return jsonify({"ok": False, "error": str(e)}), 500