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 ---
|
||||
|
||||
@@ -21,6 +21,11 @@ if ApplicationConfig.CURRENT_SETTINGS == 'PRODUCTION':
|
||||
VALIDATION_MODE = "liveMode"
|
||||
API_LOGIN_ID = ApplicationConfig.API_LOGIN_ID
|
||||
TRANSACTION_KEY = ApplicationConfig.TRANSACTION_KEY
|
||||
elif ApplicationConfig.CURRENT_SETTINGS == 'LOCAL':
|
||||
constants.environment = constants.PRODUCTION
|
||||
VALIDATION_MODE = "liveMode"
|
||||
API_LOGIN_ID = ApplicationConfig.API_LOGIN_ID
|
||||
TRANSACTION_KEY = ApplicationConfig.TRANSACTION_KEY
|
||||
else:
|
||||
constants.environment = constants.SANDBOX
|
||||
constants.show_url_on_request = True
|
||||
@@ -188,6 +193,9 @@ def _get_customer_profile(profile_id: str):
|
||||
if ApplicationConfig.CURRENT_SETTINGS == 'PRODUCTION':
|
||||
controller.setenvironment(constants.PRODUCTION)
|
||||
controller.execute()
|
||||
elif ApplicationConfig.CURRENT_SETTINGS == 'LOCAL':
|
||||
controller.setenvironment(constants.PRODUCTION)
|
||||
controller.execute()
|
||||
else:
|
||||
controller.execute()
|
||||
|
||||
|
||||
@@ -17,20 +17,27 @@ from config import load_config # Assuming you have this
|
||||
# Load Authorize.net credentials
|
||||
ApplicationConfig = load_config()
|
||||
|
||||
# Set Authorize.net environment based on configuration
|
||||
# Set Authorize.net environment based on configuration
|
||||
if ApplicationConfig.CURRENT_SETTINGS == 'PRODUCTION':
|
||||
constants.environment = constants.PRODUCTION
|
||||
VALIDATION_MODE = "liveMode"
|
||||
API_LOGIN_ID = ApplicationConfig.API_LOGIN_ID
|
||||
TRANSACTION_KEY = ApplicationConfig.TRANSACTION_KEY
|
||||
elif ApplicationConfig.CURRENT_SETTINGS == 'LOCAL':
|
||||
constants.environment = constants.PRODUCTION
|
||||
VALIDATION_MODE = "liveMode"
|
||||
API_LOGIN_ID = ApplicationConfig.API_LOGIN_ID
|
||||
TRANSACTION_KEY = ApplicationConfig.TRANSACTION_KEY
|
||||
else:
|
||||
constants.environment = constants.SANDBOX
|
||||
constants.show_url_on_request = True # Very useful for debugging
|
||||
constants.show_url_on_request = True
|
||||
VALIDATION_MODE = "testMode"
|
||||
API_LOGIN_ID = ApplicationConfig.API_LOGIN_ID
|
||||
TRANSACTION_KEY = ApplicationConfig.TRANSACTION_KEY
|
||||
|
||||
|
||||
|
||||
def _is_e00121_response(response):
|
||||
"""
|
||||
Check if the Authorize.Net response contains E00121 error (invalid payment profile ID).
|
||||
@@ -95,7 +102,8 @@ def create_customer_profile(customer: schemas.Customer, card_info: schemas.CardC
|
||||
This version sanitizes and trims customer data before sending.
|
||||
"""
|
||||
print(f"Attempting to create Auth.Net profile for customer ID: {customer.id}")
|
||||
|
||||
print(API_LOGIN_ID)
|
||||
print(TRANSACTION_KEY)
|
||||
try:
|
||||
merchantAuth = apicontractsv1.merchantAuthenticationType(name=API_LOGIN_ID, transactionKey=TRANSACTION_KEY)
|
||||
except Exception as e:
|
||||
@@ -121,6 +129,9 @@ def create_customer_profile(customer: schemas.Customer, card_info: schemas.CardC
|
||||
if ApplicationConfig.CURRENT_SETTINGS == 'PRODUCTION':
|
||||
controller.setenvironment(constants.PRODUCTION)
|
||||
controller.execute()
|
||||
elif ApplicationConfig.CURRENT_SETTINGS == 'LOCAL':
|
||||
controller.setenvironment(constants.PRODUCTION)
|
||||
controller.execute()
|
||||
else:
|
||||
controller.execute()
|
||||
response = controller.getresponse()
|
||||
@@ -245,6 +256,9 @@ def _perform_authorization(customer_profile_id: str, payment_profile_id: str, tr
|
||||
if ApplicationConfig.CURRENT_SETTINGS == 'PRODUCTION':
|
||||
controller.setenvironment(constants.PRODUCTION)
|
||||
controller.execute()
|
||||
elif ApplicationConfig.CURRENT_SETTINGS == 'LOCAL':
|
||||
controller.setenvironment(constants.PRODUCTION)
|
||||
controller.execute()
|
||||
else:
|
||||
controller.execute()
|
||||
|
||||
@@ -284,6 +298,9 @@ def capture_authorized_transaction(transaction_req: schemas.TransactionCapture):
|
||||
if ApplicationConfig.CURRENT_SETTINGS == 'PRODUCTION':
|
||||
controller.setenvironment(constants.PRODUCTION)
|
||||
controller.execute()
|
||||
elif ApplicationConfig.CURRENT_SETTINGS == 'LOCAL':
|
||||
controller.setenvironment(constants.PRODUCTION)
|
||||
controller.execute()
|
||||
else:
|
||||
controller.execute()
|
||||
|
||||
@@ -365,6 +382,9 @@ def add_payment_profile_to_customer(customer_profile_id: str, customer: schemas.
|
||||
if ApplicationConfig.CURRENT_SETTINGS == 'PRODUCTION':
|
||||
controller.setenvironment(constants.PRODUCTION)
|
||||
controller.execute()
|
||||
elif ApplicationConfig.CURRENT_SETTINGS == 'LOCAL':
|
||||
controller.setenvironment(constants.PRODUCTION)
|
||||
controller.execute()
|
||||
else:
|
||||
controller.execute()
|
||||
|
||||
@@ -412,6 +432,9 @@ def get_customer_payment_profiles(customer_profile_id: str):
|
||||
if ApplicationConfig.CURRENT_SETTINGS == 'PRODUCTION':
|
||||
controller.setenvironment(constants.PRODUCTION)
|
||||
controller.execute()
|
||||
elif ApplicationConfig.CURRENT_SETTINGS == 'LOCAL':
|
||||
controller.setenvironment(constants.PRODUCTION)
|
||||
controller.execute()
|
||||
else:
|
||||
controller.execute()
|
||||
|
||||
@@ -463,6 +486,9 @@ def charge_customer_profile(customer_profile_id: str, payment_profile_id: str, t
|
||||
if ApplicationConfig.CURRENT_SETTINGS == 'PRODUCTION':
|
||||
controller.setenvironment(constants.PRODUCTION)
|
||||
controller.execute()
|
||||
elif ApplicationConfig.CURRENT_SETTINGS == 'LOCAL':
|
||||
controller.setenvironment(constants.PRODUCTION)
|
||||
controller.execute()
|
||||
else:
|
||||
controller.execute()
|
||||
|
||||
|
||||
@@ -17,18 +17,25 @@ from .. import schemas
|
||||
ApplicationConfig = load_config()
|
||||
|
||||
|
||||
# Set Authorize.net environment based on configuration
|
||||
if ApplicationConfig.CURRENT_SETTINGS == 'PRODUCTION':
|
||||
#constants.environment = constants.PRODUCTION
|
||||
API_LOGIN_ID = ApplicationConfig.API_LOGIN_ID
|
||||
TRANSACTION_KEY = ApplicationConfig.TRANSACTION_KEY
|
||||
# Override to standard production endpoint
|
||||
else:
|
||||
constants.environment = constants.SANDBOX
|
||||
constants.environment = constants.PRODUCTION
|
||||
VALIDATION_MODE = "liveMode"
|
||||
API_LOGIN_ID = ApplicationConfig.API_LOGIN_ID
|
||||
TRANSACTION_KEY = ApplicationConfig.TRANSACTION_KEY
|
||||
|
||||
elif ApplicationConfig.CURRENT_SETTINGS == 'LOCAL':
|
||||
constants.environment = constants.PRODUCTION
|
||||
VALIDATION_MODE = "liveMode"
|
||||
API_LOGIN_ID = ApplicationConfig.API_LOGIN_ID
|
||||
TRANSACTION_KEY = ApplicationConfig.TRANSACTION_KEY
|
||||
|
||||
else:
|
||||
constants.environment = constants.SANDBOX
|
||||
constants.show_url_on_request = True
|
||||
VALIDATION_MODE = "testMode"
|
||||
API_LOGIN_ID = ApplicationConfig.API_LOGIN_ID
|
||||
TRANSACTION_KEY = ApplicationConfig.TRANSACTION_KEY
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -12,16 +12,26 @@ from sqlalchemy.orm import Session
|
||||
ApplicationConfig = load_config()
|
||||
|
||||
|
||||
# Set Authorize.net environment based on configuration
|
||||
|
||||
if ApplicationConfig.CURRENT_SETTINGS == 'PRODUCTION':
|
||||
constants.environment = constants.PRODUCTION
|
||||
VALIDATION_MODE = "liveMode"
|
||||
API_LOGIN_ID = ApplicationConfig.API_LOGIN_ID
|
||||
TRANSACTION_KEY = ApplicationConfig.TRANSACTION_KEY
|
||||
|
||||
elif ApplicationConfig.CURRENT_SETTINGS == 'LOCAL':
|
||||
constants.environment = constants.PRODUCTION
|
||||
VALIDATION_MODE = "liveMode"
|
||||
API_LOGIN_ID = ApplicationConfig.API_LOGIN_ID
|
||||
TRANSACTION_KEY = ApplicationConfig.TRANSACTION_KEY
|
||||
|
||||
else:
|
||||
constants.environment = constants.SANDBOX
|
||||
constants.show_url_on_request = True
|
||||
VALIDATION_MODE = "testMode"
|
||||
API_LOGIN_ID = ApplicationConfig.API_LOGIN_ID
|
||||
TRANSACTION_KEY = ApplicationConfig.TRANSACTION_KEY
|
||||
constants.show_url_on_request = True
|
||||
|
||||
|
||||
|
||||
def _get_authnet_error_message(response):
|
||||
@@ -179,8 +189,14 @@ def _delete_customer_profile(profile_id: str) -> bool:
|
||||
)
|
||||
|
||||
controller = deleteCustomerProfileController(request)
|
||||
controller.setenvironment(constants.PRODUCTION)
|
||||
controller.execute()
|
||||
if ApplicationConfig.CURRENT_SETTINGS == 'PRODUCTION':
|
||||
controller.setenvironment(constants.PRODUCTION)
|
||||
controller.execute()
|
||||
elif ApplicationConfig.CURRENT_SETTINGS == 'LOCAL':
|
||||
controller.setenvironment(constants.PRODUCTION)
|
||||
controller.execute()
|
||||
else:
|
||||
controller.execute()
|
||||
response = controller.getresponse()
|
||||
|
||||
if response is None:
|
||||
@@ -228,8 +244,14 @@ def _delete_payment_profile(customer_profile_id: str, payment_profile_id: str) -
|
||||
)
|
||||
|
||||
controller = deleteCustomerPaymentProfileController(request)
|
||||
controller.setenvironment(constants.PRODUCTION)
|
||||
controller.execute()
|
||||
if ApplicationConfig.CURRENT_SETTINGS == 'PRODUCTION':
|
||||
controller.setenvironment(constants.PRODUCTION)
|
||||
controller.execute()
|
||||
elif ApplicationConfig.CURRENT_SETTINGS == 'LOCAL':
|
||||
controller.setenvironment(constants.PRODUCTION)
|
||||
controller.execute()
|
||||
else:
|
||||
controller.execute()
|
||||
response = controller.getresponse()
|
||||
|
||||
if response is None:
|
||||
|
||||
Reference in New Issue
Block a user