diff --git a/handlers/keys/key_management.py b/handlers/keys/key_management.py index f3563035..29563bf3 100644 --- a/handlers/keys/key_management.py +++ b/handlers/keys/key_management.py @@ -28,7 +28,13 @@ from config import ( from database import add_connection, get_balance, store_key, update_balance from handlers.instructions.instructions import send_instructions from handlers.profile import process_callback_view_profile -from handlers.texts import KEY, KEY_TRIAL, NULL_BALANCE, key_message_success +from handlers.texts import ( + KEY, + KEY_TRIAL, + NULL_BALANCE, + RENEWAL_PLANS, + key_message_success, +) from handlers.utils import sanitize_key_name router = Router() @@ -115,7 +121,7 @@ async def confirm_create_new_key(callback_query: CallbackQuery, state: FSMContex server_id = data.get("selected_server_id") balance = await get_balance(tg_id) - if balance < 100: + if balance < RENEWAL_PLANS["1"]["price"]: replenish_button = InlineKeyboardButton( text="Перейти в профиль", callback_data="view_profile" ) @@ -184,7 +190,7 @@ async def handle_key_name_input(message: Message, state: FSMContext): expiry_time = current_time + timedelta(days=1, hours=3) else: balance = await get_balance(tg_id) - if balance < 100: + if balance < RENEWAL_PLANS["1"]["price"]: replenish_button = InlineKeyboardButton( text="Перейти в профиль", callback_data="view_profile" ) @@ -197,7 +203,7 @@ async def handle_key_name_input(message: Message, state: FSMContext): await state.clear() return - await update_balance(tg_id, -100) + await update_balance(tg_id, -RENEWAL_PLANS["1"]["price"]) expiry_time = current_time + timedelta(days=30, hours=3) expiry_timestamp = int(expiry_time.timestamp() * 1000) diff --git a/handlers/notifications.py b/handlers/notifications.py index 92e5d6a0..03f25b4a 100644 --- a/handlers/notifications.py +++ b/handlers/notifications.py @@ -15,6 +15,7 @@ from handlers.texts import ( KEY_EXPIRY_24H, KEY_RENEWAL_FAILED, KEY_RENEWED, + RENEWAL_PLANS, ) logging.basicConfig(level=logging.INFO) @@ -268,8 +269,8 @@ async def handle_expired_keys(bot: Bot, conn: asyncpg.Connection, current_time: ) keyboard = types.InlineKeyboardMarkup(inline_keyboard=[[button_profile]]) - if balance >= 100: - await update_balance(tg_id, -100) + if balance >= RENEWAL_PLANS["1"]["price"]: + await update_balance(tg_id, -RENEWAL_PLANS["1"]["price"]) new_expiry_time = int( (datetime.utcnow() + timedelta(days=30)).timestamp() * 1000 ) diff --git a/handlers/payment/freekassa_pay.py b/handlers/payment/freekassa_pay.py index fa8fe92a..e745d54f 100644 --- a/handlers/payment/freekassa_pay.py +++ b/handlers/payment/freekassa_pay.py @@ -13,6 +13,8 @@ from aiohttp import web from bot import bot from config import FREEKASSA_API_KEY, FREEKASSA_SHOP_ID from database import update_balance +from handlers.profile import process_callback_view_profile +from handlers.texts import PAYMENT_OPTIONS router = Router() logging.basicConfig(level=logging.DEBUG) @@ -94,24 +96,16 @@ async def process_callback_pay_freekassa( callback_query: types.CallbackQuery, state: FSMContext ): tg_id = callback_query.from_user.id + inline_keyboard = [] - amount_keyboard = InlineKeyboardMarkup( - inline_keyboard=[ - [ - InlineKeyboardButton(text="100 рублей", callback_data="amount|100"), - InlineKeyboardButton(text="500 рублей", callback_data="amount|500"), - ], - [ - InlineKeyboardButton(text="1000 рублей", callback_data="amount|1000"), - InlineKeyboardButton(text="5000 рублей", callback_data="amount|5000"), - ], - [ - InlineKeyboardButton( - text="Введите другую сумму", callback_data="enter_custom_amount" - ) - ], - ] - ) + for payment in PAYMENT_OPTIONS: + inline_keyboard.append( + InlineKeyboardButton( + text=payment.get("text"), callback_data=payment.get("callback_data") + ) + ) + + amount_keyboard = InlineKeyboardMarkup(inline_keyboard) await bot.delete_message( chat_id=tg_id, message_id=callback_query.message.message_id @@ -127,6 +121,13 @@ async def process_callback_pay_freekassa( await callback_query.answer() +@router.callback_query(lambda c: c.data == "back_to_profile") +async def back_to_profile_handler( + callback_query: types.CallbackQuery, state: FSMContext +): + await process_callback_view_profile(callback_query, state) + + @router.callback_query(lambda c: c.data.startswith("amount|")) async def process_amount_selection( callback_query: types.CallbackQuery, state: FSMContext diff --git a/handlers/payment/yookassa_pay.py b/handlers/payment/yookassa_pay.py index 756cc234..6acf13b9 100644 --- a/handlers/payment/yookassa_pay.py +++ b/handlers/payment/yookassa_pay.py @@ -66,6 +66,14 @@ async def process_callback_replenish_balance( callback_query: types.CallbackQuery, state: FSMContext ): tg_id = callback_query.from_user.id + inline_keyboard = [] + + for payment in PAYMENT_OPTIONS: + inline_keyboard.append( + InlineKeyboardButton( + text=payment.get("text"), callback_data=payment.get("callback_data") + ) + ) key_count = await get_key_count(tg_id) @@ -74,42 +82,7 @@ async def process_callback_replenish_balance( if not exists: await add_connection(tg_id, balance=0.0, trial=0) - amount_keyboard = InlineKeyboardMarkup( - inline_keyboard=[ - [ - InlineKeyboardButton( - text=PAYMENT_OPTIONS[0]["text"], - callback_data=PAYMENT_OPTIONS[0]["callback_data"], - ), - InlineKeyboardButton( - text=PAYMENT_OPTIONS[1]["text"], - callback_data=PAYMENT_OPTIONS[1]["callback_data"], - ), - ], - [ - InlineKeyboardButton( - text=PAYMENT_OPTIONS[2]["text"], - callback_data=PAYMENT_OPTIONS[2]["callback_data"], - ), - InlineKeyboardButton( - text=PAYMENT_OPTIONS[3]["text"], - callback_data=PAYMENT_OPTIONS[3]["callback_data"], - ), - ], - [ - InlineKeyboardButton( - text=PAYMENT_OPTIONS[4]["text"], - callback_data=PAYMENT_OPTIONS[4]["callback_data"], - ) - ], - [ - InlineKeyboardButton( - text=PAYMENT_OPTIONS[5]["text"], - callback_data=PAYMENT_OPTIONS[5]["callback_data"], - ) - ], - ] - ) + amount_keyboard = InlineKeyboardMarkup(inline_keyboard) await bot.delete_message( chat_id=tg_id, message_id=callback_query.message.message_id @@ -130,11 +103,11 @@ async def back_to_profile_handler( await process_callback_view_profile(callback_query, state) -@router.callback_query(lambda c: c.data.startswith("amount_")) +@router.callback_query(lambda c: c.data.startswith("amount|")) async def process_amount_selection( callback_query: types.CallbackQuery, state: FSMContext ): - data = callback_query.data.split("_", 1) + data = callback_query.data.split("|", 1) if len(data) != 2: await send_message_with_deletion(