Refactor payment service, fix DB session, and consolidate endpoints

- Fix critical NameError in database.py by restoring Session factory
- Refactor payment_service.py and crud.py to use shared constants.py and utils.py
- Deduplicate state mapping and input sanitization logic
- Move transaction amount calculation logic from CRUD to Router layer
- Enforce type safety in schemas using IntEnum for TransactionType/Status
- Move capture endpoint from transaction.py to payment.py (now /payments/capture)
- Update create_customer_profile signature for clarity
This commit is contained in:
2026-02-01 12:31:42 -05:00
parent 449eb74279
commit 97261f6c51
13 changed files with 335 additions and 428 deletions

View File

@@ -15,27 +15,13 @@ from . import payment_service
from .. import schemas
# Load Authorize.net credentials
from config import load_config, API_LOGIN_ID, TRANSACTION_KEY, VALIDATION_MODE, ENVIRONMENT
# Load Authorize.net environment variables
ApplicationConfig = load_config()
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
# Set environment
constants.environment = ENVIRONMENT
@@ -242,7 +228,7 @@ def create_user_account(db: Session, customer_id: int) -> dict:
}
except Exception as e:
logger.debug(f"Critical exception during user account creation for customer {customer_id}: {traceback.format_exc()}")
logger.error(f"Critical exception during user account creation for customer {customer_id}: {traceback.format_exc()}")
db.rollback()
return {
"success": False,
@@ -311,7 +297,7 @@ def refresh_customer_payment_profiles(db: Session, customer_id: int, auth_profil
)
# Check test result
_, _, test_reason = payment_service._parse_authnet_response(test_response)
_, _, test_reason = payment_service.parse_authnet_response(test_response)
if "E00121" in str(test_reason):
cards_need_update.append(card)
logger.debug(f"🔄 Card {card.id} has profile {card.auth_net_payment_profile_id} that EXISTS but is CORRUPTED - NEEDS RECREATION")
@@ -373,7 +359,7 @@ def refresh_customer_payment_profiles(db: Session, customer_id: int, auth_profil
recreated_cards.append(card)
logger.debug(f"✅ Successfully recreated payment profile {new_payment_profile_id} for card {card.id}")
else:
logger.debug(f"❌ Failed to recreate payment profile for card {card.id} - no ID returned")
logger.error(f"❌ Failed to recreate payment profile for card {card.id} - no ID returned")
except Exception as e:
logger.debug(f"❌ Failed to recreate payment profile for card {card.id}: {str(e)}")
@@ -383,7 +369,7 @@ def refresh_customer_payment_profiles(db: Session, customer_id: int, auth_profil
db.commit()
logger.debug(f"✅ Successfully recreated and saved {len(recreated_cards)} payment profiles")
else:
logger.debug("❌ No payment profiles could be recreated - this is a critical failure")
logger.error("❌ No payment profiles could be recreated - this is a critical failure")
return False
else:
logger.debug(f"🔄 All {len(cards_before)} cards have valid payment profile IDs")