diff --git a/.env.example b/.env.example index 6bb5c5e..09c9a0e 100644 --- a/.env.example +++ b/.env.example @@ -164,6 +164,7 @@ WEB_SERVER_PORT=8080 # Admin Panel Log Pagination LOGS_PAGE_SIZE=10 # Number of events in the log +LOG_LEVEL=INFO # Global log level (DEBUG, INFO, WARNING, ERROR, CRITICAL) # Admin Logging Configuration LOG_CHAT_ID=-1001234567890 # Telegram chat/group ID for admin notifications diff --git a/bot/handlers/user/payment.py b/bot/handlers/user/payment.py index d6567d3..05fc6de 100644 --- a/bot/handlers/user/payment.py +++ b/bot/handlers/user/payment.py @@ -164,8 +164,10 @@ async def process_successful_payment(session: AsyncSession, bot: Bot, should_send_nalogo_receipt = bool( nalogo_service and nalogo_service.configured + and payment_info_from_webhook.get("paid") is True + and payment_info_from_webhook.get("status") == "succeeded" and payment_before_update - and not payment_before_update.yookassa_payment_id + and payment_before_update.status != "succeeded" ) # Try to capture and save payment method for future charges if available try: diff --git a/config/settings.py b/config/settings.py index 08eebb2..12fa831 100644 --- a/config/settings.py +++ b/config/settings.py @@ -497,9 +497,22 @@ class Settings(BaseSettings): return methods or default_order # Logging Configuration + LOG_LEVEL: str = Field( + default="INFO", + description="Global log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)", + ) LOG_CHAT_ID: Optional[int] = Field(default=None, description="Telegram chat/group ID for sending notifications") LOG_THREAD_ID: Optional[int] = Field(default=None, description="Thread ID for supergroup messages (optional)") + @field_validator('LOG_LEVEL', mode='before') + @classmethod + def normalize_log_level(cls, v): + if isinstance(v, str): + v = v.strip().upper() + if not v: + return "INFO" + return v + @field_validator('LOG_CHAT_ID', 'LOG_THREAD_ID', mode='before') @classmethod def validate_optional_int_fields(cls, v): diff --git a/main.py b/main.py index c2744ed..9bff597 100644 --- a/main.py +++ b/main.py @@ -1,5 +1,6 @@ import asyncio import logging +import os import sys from dotenv import load_dotenv @@ -9,6 +10,21 @@ from config.settings import get_settings, Settings from db.database_setup import init_db, init_db_connection +def _resolve_log_level(value: str) -> int: + if not value: + return logging.INFO + if isinstance(value, str): + normalized = value.strip() + if not normalized: + return logging.INFO + if normalized.isdigit(): + return int(normalized) + level = getattr(logging, normalized.upper(), None) + if isinstance(level, int): + return level + return logging.INFO + + async def main(): load_dotenv() settings = get_settings() @@ -25,8 +41,9 @@ async def main(): if __name__ == "__main__": + load_dotenv() logging.basicConfig( - level=logging.INFO, + level=_resolve_log_level(os.getenv("LOG_LEVEL", "INFO")), stream=sys.stdout, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s') try: