From 8d9ecf69353befdc1f976ce1ba9a18e877c341e5 Mon Sep 17 00:00:00 2001 From: Edwin Eames Date: Mon, 15 Sep 2025 15:30:16 -0400 Subject: [PATCH] Adding authnet not tested --- app/classes/cards.py | 2 +- app/classes/customer.py | 1 + app/delivery_data/views.py | 8 ++--- app/payment/views.py | 70 ++++++++++++++++++++++---------------- 4 files changed, 46 insertions(+), 35 deletions(-) diff --git a/app/classes/cards.py b/app/classes/cards.py index 21ab6c4..c8c06df 100755 --- a/app/classes/cards.py +++ b/app/classes/cards.py @@ -23,7 +23,7 @@ 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) class Card_Card_schema(ma.SQLAlchemyAutoSchema): class Meta: model = Card_Card diff --git a/app/classes/customer.py b/app/classes/customer.py index 9e63af3..8ff23d8 100755 --- a/app/classes/customer.py +++ b/app/classes/customer.py @@ -11,6 +11,7 @@ class Customer_Customer(db.Model): primary_key=True, autoincrement=True, unique=False) + auth_net_profile_id = db.Column(db.String, unique=True, index=True, nullable=True) account_number = db.Column(db.VARCHAR(25)) customer_last_name = db.Column(db.VARCHAR(250)) customer_first_name = db.Column(db.VARCHAR(250)) diff --git a/app/delivery_data/views.py b/app/delivery_data/views.py index cbfb9a5..938d9d6 100755 --- a/app/delivery_data/views.py +++ b/app/delivery_data/views.py @@ -49,11 +49,9 @@ def office_finalize_delivery(delivery_id): .query(Customer_Description) \ .filter(Customer_Description.customer_id == get_delivery.customer_id) \ .first() - delivery_driver_id = request.json["driver_employee_id"] - if delivery_driver_id is None: - delivery_driver_id = 2 - if delivery_driver_id == 0: - delivery_driver_id = 2 + + #TODO hardcode for now + delivery_driver_id = 2 get_driver = (db.session .query(Employee_Employee) diff --git a/app/payment/views.py b/app/payment/views.py index 963a73e..320a741 100755 --- a/app/payment/views.py +++ b/app/payment/views.py @@ -54,9 +54,9 @@ def get_user_cards(user_id): """ gets all cards of a user """ - get_u_cards = (db.session - .query(Card_Card) - .filter(Card_Card.user_id == user_id) + get_u_cards = (db.session + .query(Card_Card) + .filter(Card_Card.user_id == user_id) .all()) card_schema = Card_Card_schema(many=True) @@ -144,19 +144,25 @@ def remove_user_card(card_id): return jsonify({"ok": True}), 200 +# In your Flask payment routes file (e.g., app/routes/payment.py) + +# ... (your existing imports: jsonify, request, db, Customer_Customer, Card_Card) ... + @payment.route("/card/create/", methods=["POST"]) def create_user_card(user_id): """ - adds a card of a user + Adds a card for a user to the local database. This is its only job. """ get_customer = (db.session .query(Customer_Customer) .filter(Customer_Customer.id == user_id) .first()) + + if not get_customer: + return jsonify({"ok": False, "error": "Customer not found"}), 404 data = request.get_json() - # FIX: Use .get() for safety and get the correct key 'name_on_card' - name_on_card = data.get("name_on_card") # <-- This now matches the frontend + name_on_card = data.get("name_on_card") expiration_month = data.get("expiration_month") expiration_year = data.get("expiration_year") type_of_card = data.get("type_of_card") @@ -164,34 +170,40 @@ def create_user_card(user_id): main_card = data.get("main_card", False) zip_code = data.get("zip_code") card_number = data.get("card_number") - - # FIX: Correctly slice the last four digits last_four = card_number[-4:] if card_number else "" - create_new_card = Card_Card( - user_id=get_customer.id, - card_number=card_number, - last_four_digits=last_four, # <-- Use the correctly sliced value - name_on_card=name_on_card, - expiration_month=expiration_month, - expiration_year=expiration_year, - type_of_card=type_of_card, - security_number=security_number, - accepted_or_declined=None, - main_card=main_card, - zip_code=zip_code - ) - db.session.add(create_new_card) - db.session.flush() + try: + create_new_card = Card_Card( + user_id=get_customer.id, + card_number=card_number, + last_four_digits=last_four, + name_on_card=name_on_card, + expiration_month=expiration_month, + expiration_year=expiration_year, + type_of_card=type_of_card, + security_number=security_number, + accepted_or_declined=None, # This is correct, as we don't know the status yet + main_card=main_card, + zip_code=zip_code + ) + db.session.add(create_new_card) + db.session.flush() - if main_card: - set_card_main(user_id=get_customer.id, card_id=create_new_card.id) + if main_card: + # Assuming set_card_main is another function you have + set_card_main(user_id=get_customer.id, card_id=create_new_card.id) - db.session.commit() + db.session.commit() + print(f"SUCCESS: Card saved locally for user {user_id} with new ID {create_new_card.id}") + except Exception as e: + db.session.rollback() + 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 jsonify({"ok": True}), 200 - @payment.route("/card/edit/", methods=["PUT"]) def update_user_card(card_id): """ @@ -225,8 +237,8 @@ def update_user_card(card_id): get_card.zip_code = zip_code # FIX: Correctly slice the last four digits on edit - if card_number: - get_card.last_four_digits = card_number[-4:] + if card_number and card_number[-4:].isdigit(): + get_card.last_four_digits = int(card_number[-4:]) if main_card: set_card_main(user_id=get_card.user_id, card_id=get_card.id)