import logging from authorizenet import apicontractsv1 from authorizenet.apicontrollers import createTransactionController from .. import schemas from config import load_config # Set up logging logger = logging.getLogger(__name__) # Load Authorize.net credentials from config ApplicationConfig = load_config() API_LOGIN_ID = ApplicationConfig.API_LOGIN_ID TRANSACTION_KEY = ApplicationConfig.TRANSACTION_KEY # For sandbox, endpoint is https://apitest.authorize.net/xml/v1/request.api def safe_string_convert(value): """ Safely convert any value to string, handling lxml objects properly. """ if value is None: return None # Check if it's an lxml object with text attribute if hasattr(value, 'text'): return value.text # Otherwise use standard string conversion return str(value) def charge_credit_card(transaction: schemas.TransactionCreate): logger.info(f"Processing charge for amount: {transaction.charge_amount}") merchantAuth = apicontractsv1.merchantAuthenticationType() merchantAuth.name = API_LOGIN_ID merchantAuth.transactionKey = TRANSACTION_KEY creditCard = apicontractsv1.creditCardType() creditCard.cardNumber = transaction.card_number creditCard.expirationDate = transaction.expiration_date creditCard.cardCode = transaction.cvv payment = apicontractsv1.paymentType() payment.creditCard = creditCard transactionRequest = apicontractsv1.transactionRequestType() transactionRequest.transactionType = "authCaptureTransaction" transactionRequest.amount = transaction.charge_amount # ✅ Fixed: Use charge_amount transactionRequest.payment = payment createtransactionrequest = apicontractsv1.createTransactionRequest() createtransactionrequest.merchantAuthentication = merchantAuth createtransactionrequest.refId = "ref_id" # Optional reference ID createtransactionrequest.transactionRequest = transactionRequest controller = createTransactionController(createtransactionrequest) controller.execute() response = controller.getresponse() # Log response status if payment failed if response is not None and response.messages is not None: logger.info(f"Charge response: {response.messages.resultCode}") if hasattr(response.messages, 'message') and len(response.messages.message) > 0: for msg in response.messages.message: logger.info(f"Message: {msg.text.text if msg.text else 'No message text'}") else: logger.error("No response from Authorize.net") return response def authorize_credit_card(transaction: schemas.TransactionAuthorize): logger.info(f"Processing preauthorization for amount: {transaction.preauthorize_amount}") merchantAuth = apicontractsv1.merchantAuthenticationType() merchantAuth.name = API_LOGIN_ID merchantAuth.transactionKey = TRANSACTION_KEY creditCard = apicontractsv1.creditCardType() creditCard.cardNumber = transaction.card_number creditCard.expirationDate = transaction.expiration_date creditCard.cardCode = transaction.cvv payment = apicontractsv1.paymentType() payment.creditCard = creditCard transactionRequest = apicontractsv1.transactionRequestType() transactionRequest.transactionType = "authOnlyTransaction" transactionRequest.amount = transaction.preauthorize_amount # ✅ Fixed: Use preauthorize_amount transactionRequest.payment = payment createtransactionrequest = apicontractsv1.createTransactionRequest() createtransactionrequest.merchantAuthentication = merchantAuth createtransactionrequest.refId = "ref_id" createtransactionrequest.transactionRequest = transactionRequest controller = createTransactionController(createtransactionrequest) controller.execute() response = controller.getresponse() # Log response status if response is not None and response.messages is not None: logger.info(f"Preauthorization response: {response.messages.resultCode}") if hasattr(response.messages, 'message') and len(response.messages.message) > 0: for msg in response.messages.message: logger.info(f"Message: {msg.text.text if msg.text else 'No message text'}") else: logger.error("No response from Authorize.net for preauthorization") return response def capture_authorized_transaction(transaction: schemas.TransactionCapture): merchantAuth = apicontractsv1.merchantAuthenticationType() merchantAuth.name = API_LOGIN_ID merchantAuth.transactionKey = TRANSACTION_KEY transactionRequest = apicontractsv1.transactionRequestType() transactionRequest.transactionType = "priorAuthCaptureTransaction" transactionRequest.amount = transaction.charge_amount # ✅ Fixed: Use charge_amount transactionRequest.refTransId = transaction.auth_net_transaction_id createtransactionrequest = apicontractsv1.createTransactionRequest() createtransactionrequest.merchantAuthentication = merchantAuth createtransactionrequest.refId = "ref_id" createtransactionrequest.transactionRequest = transactionRequest controller = createTransactionController(createtransactionrequest) controller.execute() response = controller.getresponse() # Log response status if response is not None and response.messages is not None: logger.info(f"Capture response: {response.messages.resultCode}") if hasattr(response.messages, 'message') and len(response.messages.message) > 0: for msg in response.messages.message: logger.info(f"Message: {msg.text.text if msg.text else 'No message text'}") else: logger.error("No response from Authorize.net for capture") return response