fix payment

This commit is contained in:
Yuriy Yuriev
2026-05-22 19:15:44 +07:00
parent b276794c0a
commit bad7966fc4
4 changed files with 126 additions and 42 deletions
+38
View File
@@ -341,3 +341,41 @@ class PaymentStorage:
if data.get("user_id") == user_id:
return {**data, "payment_id": pid}
return None
class PaymentHistoryStorage:
"""История завершённых платежей (последние N на пользователя)."""
MAX_PER_USER = 20
def __init__(self, file_path: str = "data/payment_history.json"):
self.file_path = Path(file_path)
self.file_path.parent.mkdir(parents=True, exist_ok=True)
self._lock = asyncio.Lock()
def _load_sync(self) -> dict:
if not self.file_path.exists():
return {}
try:
with open(self.file_path, "r", encoding="utf-8") as f:
return json.load(f)
except (json.JSONDecodeError, IOError):
return {}
def _save_sync(self, data: dict) -> None:
with open(self.file_path, "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False, indent=2)
async def add(self, user_id: int, record: dict) -> None:
async with self._lock:
data = await asyncio.to_thread(self._load_sync)
key = str(user_id)
history = data.get(key, [])
history.append(record)
data[key] = history[-self.MAX_PER_USER:]
await asyncio.to_thread(self._save_sync, data)
async def get_user_history(self, user_id: int) -> list:
async with self._lock:
data = await asyncio.to_thread(self._load_sync)
return list(reversed(data.get(str(user_id), [])))