fix payment
This commit is contained in:
@@ -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), [])))
|
||||
|
||||
Reference in New Issue
Block a user