diff --git a/app/payment/views.py b/app/payment/views.py index 14deb36..f847541 100755 --- a/app/payment/views.py +++ b/app/payment/views.py @@ -317,6 +317,35 @@ def get_authorize_transactions(page): return jsonify({"ok": False, "error": str(e)}), 500 +@payment.route("/authorize/cleanup/", methods=["POST"]) +def cleanup_authorize_profile(customer_id): + """ + Clean up Authorize.Net profile data in local database when API check fails. + Sets customer auth_net_profile_id to null and clears all card payment profile IDs. + """ + try: + # Get customer and set auth_net_profile_id to null + customer = db.session.query(Customer_Customer).filter(Customer_Customer.id == customer_id).first() + if not customer: + return jsonify({"ok": False, "error": "Customer not found"}), 404 + + customer.auth_net_profile_id = None + + # Get all cards for this customer and set their auth_net_payment_profile_id to null + cards = db.session.query(Card_Card).filter(Card_Card.user_id == customer_id).all() + for card in cards: + card.auth_net_payment_profile_id = None + + # Commit all changes + db.session.commit() + + return jsonify({"ok": True, "message": f"Cleaned up Authorize.Net data for customer {customer_id}"}), 200 + + except Exception as e: + db.session.rollback() + return jsonify({"ok": False, "error": f"Failed to cleanup profile: {str(e)}"}), 500 + + @payment.route("/authorize/", methods=["PUT"]) def update_delivery_payment_authorize(delivery_id): """