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
+37 -18
View File
@@ -67,18 +67,28 @@ class CryptoPayService:
logging.error("CryptoPayService not configured") logging.error("CryptoPayService not configured")
return None return None
payment_record = await payment_dal.create_payment_record( # Create pending payment in DB and commit to persist
session, try:
{ payment_record = await payment_dal.create_payment_record(
"user_id": user_id, session,
"amount": float(amount), {
"currency": self.settings.CRYPTOPAY_ASSET, "user_id": user_id,
"status": "pending_cryptopay", "amount": float(amount),
"description": description, "currency": self.settings.CRYPTOPAY_ASSET,
"subscription_duration_months": months, "status": "pending_cryptopay",
"provider": "cryptopay", "description": description,
}, "subscription_duration_months": months,
) "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,
) )
await payment_dal.update_provider_payment_and_status( try:
session, await payment_dal.update_provider_payment_and_status(
payment_record.payment_id, session,
str(invoice.invoice_id), payment_record.payment_id,
str(invoice.status), str(invoice.invoice_id),
) 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)