Updated charge close to working
This commit is contained in:
@@ -55,46 +55,15 @@ def charge_credit_card(transaction: schemas.TransactionCreate):
|
||||
|
||||
response = controller.getresponse()
|
||||
|
||||
# Extract rejection reason if payment failed
|
||||
rejection_reason = None
|
||||
# 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 payment was declined (resultCode is "Error"), extract error details
|
||||
if response.messages.resultCode == "Error":
|
||||
rejection_reason = "Authorize.Net Charge Error"
|
||||
if hasattr(response.messages, 'message'):
|
||||
try:
|
||||
if len(response.messages.message) > 0:
|
||||
for msg in response.messages.message:
|
||||
if hasattr(msg, 'code') and hasattr(msg, 'text'):
|
||||
# Convert lxml StringElement objects to Python strings
|
||||
code_str = msg.code.text if msg.code else "Unknown"
|
||||
text_str = msg.text.text if msg.text else "No details available"
|
||||
rejection_reason = f"{code_str}: {text_str}"
|
||||
break # Use the first error message
|
||||
elif hasattr(msg, 'text'):
|
||||
# Convert lxml StringElement to Python string
|
||||
text_str = msg.text.text if msg.text else "No details available"
|
||||
rejection_reason = f"Error: {text_str}"
|
||||
break
|
||||
else:
|
||||
rejection_reason = "Charge declined - no specific error details available"
|
||||
except Exception as e:
|
||||
rejection_reason = f"Charge declined - error details could not be parsed: {str(e)}"
|
||||
else:
|
||||
rejection_reason = "Charge declined - no error message available"
|
||||
|
||||
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")
|
||||
rejection_reason = "No response received from Authorize.Net"
|
||||
|
||||
# Attach rejection reason to response for the router to use
|
||||
if response is not None:
|
||||
response._rejection_reason = rejection_reason
|
||||
|
||||
return response
|
||||
|
||||
@@ -128,46 +97,15 @@ def authorize_credit_card(transaction: schemas.TransactionAuthorize):
|
||||
|
||||
response = controller.getresponse()
|
||||
|
||||
# Extract rejection reason if payment failed
|
||||
rejection_reason = None
|
||||
# Log response status
|
||||
if response is not None and response.messages is not None:
|
||||
logger.info(f"Preauthorization response: {response.messages.resultCode}")
|
||||
|
||||
# If payment was declined (resultCode is "Error"), extract error details
|
||||
if response.messages.resultCode == "Error":
|
||||
rejection_reason = "Authorize.Net Error"
|
||||
if hasattr(response.messages, 'message'):
|
||||
try:
|
||||
if len(response.messages.message) > 0:
|
||||
for msg in response.messages.message:
|
||||
if hasattr(msg, 'code') and hasattr(msg, 'text'):
|
||||
# Convert lxml StringElement objects to Python strings
|
||||
code_str = msg.code.text if msg.code else "Unknown"
|
||||
text_str = msg.text.text if msg.text else "No details available"
|
||||
rejection_reason = f"{code_str}: {text_str}"
|
||||
break # Use the first error message
|
||||
elif hasattr(msg, 'text'):
|
||||
# Convert lxml StringElement to Python string
|
||||
text_str = msg.text.text if msg.text else "No details available"
|
||||
rejection_reason = f"Error: {text_str}"
|
||||
break
|
||||
else:
|
||||
rejection_reason = "Payment declined - no specific error details available"
|
||||
except Exception as e:
|
||||
rejection_reason = f"Payment declined - error details could not be parsed: {str(e)}"
|
||||
else:
|
||||
rejection_reason = "Payment declined - no error message available"
|
||||
|
||||
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")
|
||||
rejection_reason = "No response received from Authorize.Net"
|
||||
|
||||
# Attach rejection reason to response for the router to use
|
||||
if response is not None:
|
||||
response._rejection_reason = rejection_reason
|
||||
|
||||
return response
|
||||
|
||||
@@ -191,45 +129,14 @@ def capture_authorized_transaction(transaction: schemas.TransactionCapture):
|
||||
|
||||
response = controller.getresponse()
|
||||
|
||||
# Extract rejection reason if capture failed
|
||||
rejection_reason = None
|
||||
# Log response status
|
||||
if response is not None and response.messages is not None:
|
||||
logger.info(f"Capture response: {response.messages.resultCode}")
|
||||
|
||||
# If capture was declined (resultCode is "Error"), extract error details
|
||||
if response.messages.resultCode == "Error":
|
||||
rejection_reason = "Authorize.Net Capture Error"
|
||||
if hasattr(response.messages, 'message'):
|
||||
try:
|
||||
if len(response.messages.message) > 0:
|
||||
for msg in response.messages.message:
|
||||
if hasattr(msg, 'code') and hasattr(msg, 'text'):
|
||||
# Convert lxml StringElement objects to Python strings
|
||||
code_str = msg.code.text if msg.code else "Unknown"
|
||||
text_str = msg.text.text if msg.text else "No details available"
|
||||
rejection_reason = f"{code_str}: {text_str}"
|
||||
break # Use the first error message
|
||||
elif hasattr(msg, 'text'):
|
||||
# Convert lxml StringElement to Python string
|
||||
text_str = msg.text.text if msg.text else "No details available"
|
||||
rejection_reason = f"Error: {text_str}"
|
||||
break
|
||||
else:
|
||||
rejection_reason = "Capture declined - no specific error details available"
|
||||
except Exception as e:
|
||||
rejection_reason = f"Capture declined - error details could not be parsed: {str(e)}"
|
||||
else:
|
||||
rejection_reason = "Capture declined - no error message available"
|
||||
|
||||
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")
|
||||
rejection_reason = "No response received from Authorize.Net for capture"
|
||||
|
||||
# Attach rejection reason to response for the router to use
|
||||
if response is not None:
|
||||
response._rejection_reason = rejection_reason
|
||||
|
||||
return response
|
||||
|
||||
Reference in New Issue
Block a user