Updated charge close to working
This commit is contained in:
@@ -34,19 +34,39 @@ def _parse_authnet_response(response: Optional[AuthNetResponse]) -> Tuple[Transa
|
||||
Parses the response from the Authorize.Net service.
|
||||
(This is the same helper from before, it's a good change to keep)
|
||||
"""
|
||||
if response and hasattr(response, 'messages') and response.messages.resultCode == "Ok":
|
||||
if response is not None and hasattr(response, 'messages') and response.messages.resultCode == "Ok":
|
||||
status = TransactionStatus.APPROVED
|
||||
auth_net_transaction_id = str(response.transactionResponse.transId) if hasattr(response, 'transactionResponse') else None
|
||||
rejection_reason = None
|
||||
else:
|
||||
status = TransactionStatus.DECLINED
|
||||
auth_net_transaction_id = None
|
||||
if hasattr(response, '_rejection_reason'):
|
||||
|
||||
# Improved rejection reason extraction
|
||||
rejection_reason = "Payment declined by gateway."
|
||||
if hasattr(response, '_rejection_reason') and response._rejection_reason:
|
||||
rejection_reason = str(response._rejection_reason)
|
||||
elif response is not None:
|
||||
# Check transaction response for errors
|
||||
if hasattr(response, 'transactionResponse') and response.transactionResponse:
|
||||
tr = response.transactionResponse
|
||||
if hasattr(tr, 'errors') and tr.errors:
|
||||
for error in tr.errors:
|
||||
if hasattr(error, 'errorCode') and hasattr(error, 'errorText'):
|
||||
error_code = error.errorCode.text if error.errorCode else "Unknown"
|
||||
error_text = error.errorText.text if error.errorText else "No error details"
|
||||
rejection_reason = f"{error_code}: {error_text}"
|
||||
break
|
||||
elif hasattr(tr, 'messages') and tr.messages:
|
||||
if len(tr.messages) > 0:
|
||||
msg = tr.messages[0]
|
||||
if hasattr(msg, 'code') and hasattr(msg, 'description'):
|
||||
code = msg.code.text if msg.code else "Unknown"
|
||||
desc = msg.description.text if msg.description else "No description"
|
||||
rejection_reason = f"{code}: {desc}"
|
||||
elif response is None:
|
||||
rejection_reason = "No response received from payment gateway."
|
||||
else:
|
||||
rejection_reason = "Payment declined by gateway."
|
||||
|
||||
return status, auth_net_transaction_id, rejection_reason
|
||||
|
||||
|
||||
@@ -74,9 +94,6 @@ def authorize_card(customer_id: int, transaction: schemas.TransactionAuthorize,
|
||||
|
||||
@router.post("/charge/{customer_id}", response_model=schemas.Transaction)
|
||||
def charge_card(customer_id: int, transaction: schemas.TransactionCreate, db: Session = Depends(database.get_db)):
|
||||
# Add debug logging
|
||||
print(f"DEBUG: Received charge request for customer_id: {customer_id}")
|
||||
print(f"DEBUG: Transaction data: {transaction.dict() if hasattr(transaction, 'dict') else transaction}")
|
||||
|
||||
try:
|
||||
auth_net_response = payment_service.charge_credit_card(transaction)
|
||||
@@ -90,7 +107,6 @@ def charge_card(customer_id: int, transaction: schemas.TransactionCreate, db: Se
|
||||
card_id=transaction.card_id,
|
||||
rejection_reason=rejection_reason
|
||||
)
|
||||
print(f"DEBUG: Transaction data to create: {transaction_data.dict()}")
|
||||
|
||||
result = crud.create_transaction(
|
||||
db=db,
|
||||
@@ -99,7 +115,6 @@ def charge_card(customer_id: int, transaction: schemas.TransactionCreate, db: Se
|
||||
status=status,
|
||||
auth_net_transaction_id=auth_net_transaction_id
|
||||
)
|
||||
print(f"DEBUG: Created transaction: {result.dict()}")
|
||||
return result
|
||||
except Exception as e:
|
||||
print(f"DEBUG: Exception in charge_card: {str(e)}")
|
||||
|
||||
Reference in New Issue
Block a user