Fixed e00007 error
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
import logging
|
||||
import traceback
|
||||
from authorizenet import apicontractsv1
|
||||
from authorizenet.apicontrollers import (
|
||||
@@ -9,19 +8,19 @@ from authorizenet.constants import constants
|
||||
from config import load_config
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Load Authorize.net credentials
|
||||
ApplicationConfig = load_config()
|
||||
API_LOGIN_ID = ApplicationConfig.API_LOGIN_ID
|
||||
TRANSACTION_KEY = ApplicationConfig.TRANSACTION_KEY
|
||||
|
||||
|
||||
# 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
|
||||
else:
|
||||
constants.environment = constants.SANDBOX
|
||||
|
||||
API_LOGIN_ID = ApplicationConfig.API_LOGIN_ID
|
||||
TRANSACTION_KEY = ApplicationConfig.TRANSACTION_KEY
|
||||
constants.show_url_on_request = True
|
||||
|
||||
|
||||
@@ -44,7 +43,7 @@ def _get_authnet_error_message(response):
|
||||
text = msg.text if hasattr(msg, 'text') else 'No details provided.'
|
||||
return f"Error {code}: {text}"
|
||||
except Exception as e:
|
||||
logger.error(f"Error while parsing Auth.Net error message: {e}")
|
||||
print(f"Error while parsing Auth.Net error message: {e}")
|
||||
return "An unparsable error occurred with the payment gateway."
|
||||
|
||||
return "An unknown error occurred with the payment gateway."
|
||||
@@ -89,29 +88,29 @@ def delete_user_account(db: Session, customer_id: int) -> dict:
|
||||
# Get customer's payment profiles/cards from database
|
||||
cards = crud.get_customer_cards(db, customer_id)
|
||||
|
||||
logger.info(f"Starting deletion of Authorize.net account for customer {customer_id} (Profile ID: {profile_id_to_delete})")
|
||||
print(f"Starting deletion of Authorize.net account for customer {customer_id} (Profile ID: {profile_id_to_delete})")
|
||||
|
||||
# Step 1: Delete payment profiles first (must delete these before customer profile)
|
||||
deleted_payment_profiles = []
|
||||
if cards:
|
||||
logger.info(f"Found {len(cards)} cards to delete from Authorize.net")
|
||||
print(f"Found {len(cards)} cards to delete from Authorize.net")
|
||||
|
||||
for card_index, card in enumerate(cards):
|
||||
if card.auth_net_payment_profile_id:
|
||||
try:
|
||||
logger.info(f"Deleting payment profile {card.auth_net_payment_profile_id} for card {card.id}")
|
||||
print(f"Deleting payment profile {card.auth_net_payment_profile_id} for card {card.id}")
|
||||
|
||||
# Delete payment profile from Authorize.net
|
||||
success = _delete_payment_profile(profile_id_to_delete, card.auth_net_payment_profile_id)
|
||||
|
||||
if success:
|
||||
deleted_payment_profiles.append(card.auth_net_payment_profile_id)
|
||||
logger.info(f"Successfully deleted payment profile {card.auth_net_payment_profile_id}")
|
||||
print(f"Successfully deleted payment profile {card.auth_net_payment_profile_id}")
|
||||
else:
|
||||
logger.warning(f"Failed to delete payment profile {card.auth_net_payment_profile_id} - it may not exist or already deleted")
|
||||
print(f"Failed to delete payment profile {card.auth_net_payment_profile_id} - it may not exist or already deleted")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error deleting payment profile {card.auth_net_payment_profile_id}: {str(e)}")
|
||||
print(f"Error deleting payment profile {card.auth_net_payment_profile_id}: {str(e)}")
|
||||
# Continue with other payment profiles - we want to delete as much as possible
|
||||
|
||||
# Always null out the payment profile ID in database (even if API delete failed)
|
||||
@@ -119,7 +118,7 @@ def delete_user_account(db: Session, customer_id: int) -> dict:
|
||||
db.add(card)
|
||||
|
||||
# Step 2: Delete customer profile
|
||||
logger.info(f"Deleting customer profile {profile_id_to_delete}")
|
||||
print(f"Deleting customer profile {profile_id_to_delete}")
|
||||
profile_deleted_success = _delete_customer_profile(profile_id_to_delete)
|
||||
|
||||
# Step 3: Update database regardless of API results
|
||||
@@ -130,7 +129,7 @@ def delete_user_account(db: Session, customer_id: int) -> dict:
|
||||
db.commit()
|
||||
|
||||
if profile_deleted_success:
|
||||
logger.info(f"Successfully deleted Authorize.net account for customer {customer_id}")
|
||||
print(f"Successfully deleted Authorize.net account for customer {customer_id}")
|
||||
return {
|
||||
"success": True,
|
||||
"message": f"Successfully deleted Authorize.net account with profile ID {profile_id_to_delete}",
|
||||
@@ -139,7 +138,7 @@ def delete_user_account(db: Session, customer_id: int) -> dict:
|
||||
"deleted_payment_profiles": deleted_payment_profiles
|
||||
}
|
||||
else:
|
||||
logger.warning(f"Customer profile {profile_id_to_delete} may not have been completely removed from Authorize.net, but database has been updated")
|
||||
print(f"Customer profile {profile_id_to_delete} may not have been completely removed from Authorize.net, but database has been updated")
|
||||
return {
|
||||
"success": False,
|
||||
"message": f"Profile {profile_id_to_delete} may not have been completely removed from Authorize.net, but database has been cleaned up",
|
||||
@@ -149,7 +148,7 @@ def delete_user_account(db: Session, customer_id: int) -> dict:
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Critical exception during account deletion for customer {customer_id}: {traceback.format_exc()}")
|
||||
print(f"Critical exception during account deletion for customer {customer_id}: {traceback.format_exc()}")
|
||||
db.rollback()
|
||||
return {
|
||||
"success": False,
|
||||
@@ -180,27 +179,28 @@ def _delete_customer_profile(profile_id: str) -> bool:
|
||||
)
|
||||
|
||||
controller = deleteCustomerProfileController(request)
|
||||
controller.setenvironment(constants.PRODUCTION)
|
||||
controller.execute()
|
||||
response = controller.getresponse()
|
||||
|
||||
if response is None:
|
||||
logger.warning(f"No response received when trying to delete profile {profile_id}")
|
||||
print(f"No response received when trying to delete profile {profile_id}")
|
||||
return False
|
||||
|
||||
if hasattr(response, 'messages') and response.messages.resultCode == "Ok":
|
||||
logger.info(f"Successfully deleted customer profile {profile_id}")
|
||||
print(f"Successfully deleted customer profile {profile_id}")
|
||||
return True
|
||||
else:
|
||||
error_msg = _get_authnet_error_message(response)
|
||||
logger.warning(f"Failed to delete customer profile {profile_id}: {error_msg}")
|
||||
print(f"Failed to delete customer profile {profile_id}: {error_msg}")
|
||||
# Still count as success if the profile was already deleted/not found
|
||||
if "not found" in error_msg.lower() or "E00040" in error_msg or "E00035" in error_msg:
|
||||
logger.info(f"Profile {profile_id} was already deleted or doesn't exist")
|
||||
print(f"Profile {profile_id} was already deleted or doesn't exist")
|
||||
return True
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Exception during delete customer profile {profile_id}: {str(e)}")
|
||||
print(f"Exception during delete customer profile {profile_id}: {str(e)}")
|
||||
return False
|
||||
|
||||
|
||||
@@ -228,25 +228,26 @@ def _delete_payment_profile(customer_profile_id: str, payment_profile_id: str) -
|
||||
)
|
||||
|
||||
controller = deleteCustomerPaymentProfileController(request)
|
||||
controller.setenvironment(constants.PRODUCTION)
|
||||
controller.execute()
|
||||
response = controller.getresponse()
|
||||
|
||||
if response is None:
|
||||
logger.warning(f"No response received when trying to delete payment profile {payment_profile_id}")
|
||||
print(f"No response received when trying to delete payment profile {payment_profile_id}")
|
||||
return False
|
||||
|
||||
if hasattr(response, 'messages') and response.messages.resultCode == "Ok":
|
||||
logger.info(f"Successfully deleted payment profile {payment_profile_id}")
|
||||
print(f"Successfully deleted payment profile {payment_profile_id}")
|
||||
return True
|
||||
else:
|
||||
error_msg = _get_authnet_error_message(response)
|
||||
logger.warning(f"Failed to delete payment profile {payment_profile_id}: {error_msg}")
|
||||
print(f"Failed to delete payment profile {payment_profile_id}: {error_msg}")
|
||||
# Still count as success if the payment profile was already deleted/not found
|
||||
if "not found" in error_msg.lower() or "E00040" in error_msg or "E00035" in error_msg:
|
||||
logger.info(f"Payment profile {payment_profile_id} was already deleted or doesn't exist")
|
||||
print(f"Payment profile {payment_profile_id} was already deleted or doesn't exist")
|
||||
return True
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Exception during delete payment profile {payment_profile_id}: {str(e)}")
|
||||
print(f"Exception during delete payment profile {payment_profile_id}: {str(e)}")
|
||||
return False
|
||||
|
||||
Reference in New Issue
Block a user