Updated for local
This commit is contained in:
@@ -446,97 +446,3 @@ def authorize_saved_card(customer_id: int, transaction_req: schemas.TransactionA
|
||||
)
|
||||
|
||||
return db_transaction
|
||||
|
||||
|
||||
def _nuclear_e00121_payment_profile_cleanup(db: Session, customer_id: int, card_id: int, corrupted_profile_id: str, customer_profile_id: str) -> bool:
|
||||
"""
|
||||
DEDICATED E00121 NUKER: Forcefully removes problematic payment profile IDs from both database and Authorize.net.
|
||||
This is the nuclear option for when E00121 payment profile errors persist despite all attempts to recover.
|
||||
|
||||
Args:
|
||||
db: Database session
|
||||
customer_id: Customer ID
|
||||
card_id: Card ID that has the corrupted profile
|
||||
corrupted_profile_id: The problematic payment profile ID causing E00121
|
||||
customer_profile_id: Customer profile ID in Authorize.net
|
||||
|
||||
Returns:
|
||||
bool: True if cleanup successful and card can be retried
|
||||
"""
|
||||
print("💣 NUCLEAR E00121 CLEANUP INITIATED! 💣")
|
||||
print(f"💣 Target: Card {card_id}, Profile {corrupted_profile_id}")
|
||||
|
||||
try:
|
||||
from .. import crud
|
||||
from ..services import user_delete
|
||||
|
||||
# Step 1: Get the card from database
|
||||
card = crud.get_card_by_id(db, card_id)
|
||||
if not card:
|
||||
print(f"💣 ERROR: Card {card_id} not found in database")
|
||||
return False
|
||||
|
||||
# Step 2: NUKE FROM AUTHORIZE.NET FIRST
|
||||
print(f"💣 Deleting corrupted profile {corrupted_profile_id} from Authorize.net...")
|
||||
try:
|
||||
delete_success = user_delete._delete_payment_profile(customer_profile_id, corrupted_profile_id)
|
||||
if delete_success:
|
||||
print("✅ Successfully deleted corrupted profile from Authorize.net")
|
||||
else:
|
||||
print("⚠️ Profile may not have existed in Authorize.net or delete failed")
|
||||
except Exception as e:
|
||||
print(f"⚠️ Exception deleting from Authorize.net: {str(e)} - continuing anyway")
|
||||
|
||||
# Step 3: NUKE FROM DATABASE REGARDLESS
|
||||
print(f"💣 Clearing corrupted profile ID {corrupted_profile_id} from database card {card_id}")
|
||||
card.auth_net_payment_profile_id = None
|
||||
db.add(card)
|
||||
db.commit()
|
||||
print("✅ Successfully cleared corrupted profile ID from database")
|
||||
|
||||
# Step 4: Attempt immediate recreation of payment profile
|
||||
print("💣 Attempting immediate recreation of payment profile...")
|
||||
try:
|
||||
customer = crud.get_customer(db, customer_id)
|
||||
|
||||
# Format card data for recreation
|
||||
# Convert to string first to handle cases where database returns int instead of string
|
||||
exp_year_str = str(card.expiration_year)
|
||||
exp_month_str = str(card.expiration_month)
|
||||
exp_year = exp_year_str.zfill(4) if len(exp_year_str) < 4 else exp_year_str
|
||||
exp_month = exp_month_str.zfill(2) if len(exp_month_str) < 2 else exp_month_str
|
||||
exp_date = f"{exp_year}-{exp_month}"
|
||||
|
||||
card_create_data = schemas.CardCreate(
|
||||
card_number=card.card_number,
|
||||
expiration_date=exp_date,
|
||||
cvv=card.security_number
|
||||
)
|
||||
|
||||
from ..services import payment_service
|
||||
new_profile_id = payment_service.add_payment_profile_to_customer(
|
||||
customer_profile_id, customer, card_create_data, is_default=(card.main_card == True)
|
||||
)
|
||||
|
||||
if new_profile_id:
|
||||
print(f"✅ IMMEDIATE RECREATION SUCCESSFUL: New profile {new_profile_id}")
|
||||
card.auth_net_payment_profile_id = str(new_profile_id)
|
||||
db.add(card)
|
||||
db.commit()
|
||||
print(f"💣 Nuclear cleanup COMPLETE - Card {card_id} has new profile {new_profile_id}")
|
||||
return True
|
||||
else:
|
||||
print("❌ Immediate recreation failed - no new profile ID")
|
||||
return False
|
||||
|
||||
except Exception as recreate_e:
|
||||
print(f"❌ Immediate recreation failed: {str(recreate_e)}")
|
||||
print("💣 Database cleared but recreation failed - needs manual recreation later")
|
||||
return False
|
||||
|
||||
except Exception as nuke_e:
|
||||
print(f"💣 CRITICAL ERROR in nuclear cleanup: {str(nuke_e)}")
|
||||
db.rollback()
|
||||
return False
|
||||
|
||||
# --- CAPTURE ENDPOINT MOVED TO TRANSACTION ROUTER ---
|
||||
|
||||
Reference in New Issue
Block a user