fix(payments): enhance YooKassa webhook processing and notification handling
Improve error handling for YooKassa payment events by adding verification for the payment service configuration. Refactor notification sending for expired discounts to batch messages before sending, ensuring better performance and error logging.
This commit is contained in:
@@ -626,6 +626,13 @@ async def yookassa_webhook_route(request: web.Request):
|
||||
async with async_session_factory() as session:
|
||||
try:
|
||||
if notification_object.event == YOOKASSA_EVENT_PAYMENT_SUCCEEDED:
|
||||
if not yookassa_service or not yookassa_service.configured:
|
||||
logging.critical(
|
||||
"YooKassa webhook rejected: verification service is not configured for succeeded event (payment_id=%s)",
|
||||
payment_dict_for_processing.get("id"),
|
||||
)
|
||||
return web.Response(status=503, text="yookassa_verification_required")
|
||||
|
||||
if payment_dict_for_processing.get(
|
||||
"paid") and payment_dict_for_processing.get(
|
||||
"status") == "succeeded":
|
||||
|
||||
@@ -81,6 +81,8 @@ class PromoCodeService:
|
||||
return
|
||||
|
||||
now_utc = datetime.now(timezone.utc)
|
||||
notifications_to_send: list[tuple[int, str]] = []
|
||||
|
||||
async with self._async_session_factory() as session:
|
||||
expired_discounts = await active_discount_dal.get_expired_active_discounts(
|
||||
session,
|
||||
@@ -116,17 +118,7 @@ class PromoCodeService:
|
||||
code_part=(f" (<code>{promo_code}</code>)" if promo_code else ""),
|
||||
)
|
||||
|
||||
try:
|
||||
await self.bot.send_message(
|
||||
chat_id=expired.user_id,
|
||||
text=message_text,
|
||||
parse_mode="HTML",
|
||||
)
|
||||
except Exception:
|
||||
logging.exception(
|
||||
"Failed to send discount expiration message to user %s",
|
||||
expired.user_id,
|
||||
)
|
||||
notifications_to_send.append((expired.user_id, message_text))
|
||||
|
||||
logging.info(
|
||||
"Expired discount reservation removed: user=%s, promo=%s",
|
||||
@@ -136,6 +128,19 @@ class PromoCodeService:
|
||||
|
||||
await session.commit()
|
||||
|
||||
for user_id, message_text in notifications_to_send:
|
||||
try:
|
||||
await self.bot.send_message(
|
||||
chat_id=user_id,
|
||||
text=message_text,
|
||||
parse_mode="HTML",
|
||||
)
|
||||
except Exception:
|
||||
logging.exception(
|
||||
"Failed to send discount expiration message to user %s",
|
||||
user_id,
|
||||
)
|
||||
|
||||
async def apply_promo_code(
|
||||
self,
|
||||
session: AsyncSession,
|
||||
|
||||
Reference in New Issue
Block a user