Enhance error handling and logging in CryptoPayService payment processing

- Added error handling for payment record creation and updates, ensuring database transactions are rolled back on failure.
- Implemented logging for errors during payment record creation and updates, improving traceability and debugging capabilities.
- Committed changes to ensure that payment records are properly persisted or rolled back in case of exceptions.
This commit is contained in:
machka-pasla
2025-09-07 12:55:54 +05:00
parent c208541525
commit 79ac5cb797
+19
View File
@@ -67,6 +67,8 @@ class CryptoPayService:
logging.error("CryptoPayService not configured") logging.error("CryptoPayService not configured")
return None return None
# Create pending payment in DB and commit to persist
try:
payment_record = await payment_dal.create_payment_record( payment_record = await payment_dal.create_payment_record(
session, session,
{ {
@@ -79,6 +81,14 @@ class CryptoPayService:
"provider": "cryptopay", "provider": "cryptopay",
}, },
) )
await session.commit()
except Exception as e_db_create:
await session.rollback()
logging.error(
f"Failed to create cryptopay payment record for user {user_id}: {e_db_create}",
exc_info=True,
)
return None
payload = json.dumps({ payload = json.dumps({
"user_id": str(user_id), "user_id": str(user_id),
"subscription_months": str(months), "subscription_months": str(months),
@@ -93,12 +103,21 @@ class CryptoPayService:
description=description, description=description,
payload=payload, payload=payload,
) )
try:
await payment_dal.update_provider_payment_and_status( await payment_dal.update_provider_payment_and_status(
session, session,
payment_record.payment_id, payment_record.payment_id,
str(invoice.invoice_id), str(invoice.invoice_id),
str(invoice.status), str(invoice.status),
) )
await session.commit()
except Exception as e_db_update:
await session.rollback()
logging.error(
f"Failed to update cryptopay payment record {payment_record.payment_id}: {e_db_update}",
exc_info=True,
)
return None
return invoice.bot_invoice_url return invoice.bot_invoice_url
except Exception as e: except Exception as e:
logging.error(f"CryptoPay invoice creation failed: {e}", exc_info=True) logging.error(f"CryptoPay invoice creation failed: {e}", exc_info=True)