diff --git a/app/cabinet/routes/balance.py b/app/cabinet/routes/balance.py index d1bb2842..f31c0b21 100644 --- a/app/cabinet/routes/balance.py +++ b/app/cabinet/routes/balance.py @@ -269,6 +269,7 @@ async def create_stars_invoice( # Create invoice through Telegram Bot API try: + from aiogram.exceptions import TelegramAPIError from aiogram.types import LabeledPrice async with create_bot() as bot: @@ -294,7 +295,7 @@ async def create_stars_invoice( amount_kopeks=normalized_kopeks, ) - except Exception as e: + except TelegramAPIError as e: logger.error('Error creating Stars invoice', error=e) raise HTTPException( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, diff --git a/app/cabinet/routes/gift.py b/app/cabinet/routes/gift.py index 0d107360..e934cff3 100644 --- a/app/cabinet/routes/gift.py +++ b/app/cabinet/routes/gift.py @@ -375,15 +375,19 @@ async def create_gift_purchase( bot = create_bot() - payment_service = PaymentService(bot=bot) - payment_result = await payment_service.create_guest_payment( - db=db, - amount_kopeks=price_kopeks, - payment_method=body.payment_method, - description=f'Gift: {tariff.name} ({body.period_days}d)', - purchase_token=purchase.token, - return_url=return_url, - ) + try: + payment_service = PaymentService(bot=bot) + payment_result = await payment_service.create_guest_payment( + db=db, + amount_kopeks=price_kopeks, + payment_method=body.payment_method, + description=f'Gift: {tariff.name} ({body.period_days}d)', + purchase_token=purchase.token, + return_url=return_url, + ) + finally: + if bot: + await bot.session.close() if payment_result is None: await db.rollback() diff --git a/app/cabinet/routes/wheel.py b/app/cabinet/routes/wheel.py index 9dcfe447..f081eb07 100644 --- a/app/cabinet/routes/wheel.py +++ b/app/cabinet/routes/wheel.py @@ -249,6 +249,7 @@ async def create_stars_invoice( # Создаем invoice через Telegram Bot API try: + from aiogram.exceptions import TelegramAPIError from aiogram.types import LabeledPrice from app.bot_factory import create_bot @@ -270,7 +271,7 @@ async def create_stars_invoice( stars_amount=stars_amount, ) - except Exception as e: + except TelegramAPIError as e: logger.error('Error creating invoice', error=e) raise HTTPException( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,