fix
This commit is contained in:
@@ -11,6 +11,7 @@ import hashlib
|
||||
import json
|
||||
import logging
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
|
||||
from aiohttp import web
|
||||
@@ -74,6 +75,21 @@ class HelketWebhookServer:
|
||||
await self._runner.cleanup()
|
||||
logger.info("Heleket webhook server stopped")
|
||||
|
||||
async def _save_lost_webhook(self, payload: dict) -> None:
|
||||
"""Сохраняет необработанный вебхук в файл и уведомляет админа."""
|
||||
lost_file = Path("data/lost_webhooks.json")
|
||||
try:
|
||||
data = []
|
||||
if lost_file.exists():
|
||||
with open(lost_file, "r", encoding="utf-8") as f:
|
||||
data = json.load(f)
|
||||
data.append({**payload, "_saved_at": datetime.now().isoformat(timespec="seconds")})
|
||||
with open(lost_file, "w", encoding="utf-8") as f:
|
||||
json.dump(data, f, ensure_ascii=False, indent=2)
|
||||
logger.info(f"Lost webhook saved: uuid={payload.get('uuid')}")
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to save lost webhook: {e}")
|
||||
|
||||
async def _handle_health(self, request: web.Request) -> web.Response:
|
||||
return web.Response(text="ok")
|
||||
|
||||
@@ -102,9 +118,15 @@ class HelketWebhookServer:
|
||||
logger.info(f"Webhook: uuid={payment_uuid} status={status} — ignored")
|
||||
return web.Response(text="ok")
|
||||
|
||||
payment = await self._payment_storage.get(payment_uuid)
|
||||
existing = await self._payment_storage.get(payment_uuid)
|
||||
if existing and existing.get("processed"):
|
||||
logger.info(f"Webhook: payment {payment_uuid} already processed — skipping duplicate")
|
||||
return web.Response(text="ok")
|
||||
|
||||
payment = await self._payment_storage.pop(payment_uuid)
|
||||
if not payment:
|
||||
logger.warning(f"Webhook: payment {payment_uuid} not found (already processed?)")
|
||||
logger.warning(f"Webhook: payment {payment_uuid} not found in storage")
|
||||
await self._save_lost_webhook(payload)
|
||||
return web.Response(text="ok")
|
||||
|
||||
user_id = payment.get("user_id")
|
||||
@@ -147,7 +169,6 @@ class HelketWebhookServer:
|
||||
}
|
||||
logger.info(f"Webhook: user={user_id} +{rub} RUB")
|
||||
|
||||
await self._payment_storage.delete(payment_uuid)
|
||||
await self._history_storage.add(user_id, history_record)
|
||||
|
||||
if chat_id:
|
||||
|
||||
Reference in New Issue
Block a user