From a510ab10212bbbb543b9946626e27da9140e5e49 Mon Sep 17 00:00:00 2001 From: machka-pasla Date: Thu, 4 Sep 2025 17:32:27 +0300 Subject: [PATCH] Refactor payment methods management to utilize a paginated list view - Updated the payment methods management handler to directly build and display a paginated list of user payment methods, enhancing user experience. - Removed legacy support for single card checks and deprecated keyboard functions in favor of the new list view. - Improved localization handling for payment method titles and added fallback options for missing data. --- bot/handlers/user/subscription.py | 22 ++++++++++++++-------- bot/keyboards/inline/user_keyboards.py | 8 ++------ 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/bot/handlers/user/subscription.py b/bot/handlers/user/subscription.py index 8d74983..c70ec19 100644 --- a/bot/handlers/user/subscription.py +++ b/bot/handlers/user/subscription.py @@ -598,14 +598,20 @@ async def payment_methods_manage(callback: types.CallbackQuery, settings: Settin i18n: Optional[JsonI18n] = i18n_data.get("i18n_instance") _ = lambda key, **kwargs: i18n.gettext(current_lang, key, **kwargs) if i18n else key - # New list view relies on multi-card; but keep has_card for legacy text - billing = await user_billing_dal.get_user_billing(session, callback.from_user.id) - has_card = bool(billing and billing.yookassa_payment_method_id) - text = _("payment_methods_title") - if not has_card: - text += "\n\n" + _("payment_method_none") - # Redirect users to the new paginated list - await callback.message.edit_text(text, reply_markup=get_payment_methods_manage_keyboard(current_lang, i18n, has_card)) + # Build and show the paginated list directly (page 0) + from db.dal.user_billing_dal import list_user_payment_methods + get_text = _ + methods = await list_user_payment_methods(session, callback.from_user.id) + cards: List[tuple] = [] + for m in methods: + title = get_text("payment_method_card_title", network=m.card_network or "Card", last4=m.card_last4 or "????") + cards.append((str(m.method_id), title if not m.is_default else f"⭐ {title}")) + + text = get_text("payment_methods_title") + if not cards: + text += "\n\n" + get_text("payment_method_none") + + await callback.message.edit_text(text, reply_markup=get_payment_methods_list_keyboard(cards, 0, current_lang, i18n)) try: await callback.answer() except Exception: diff --git a/bot/keyboards/inline/user_keyboards.py b/bot/keyboards/inline/user_keyboards.py index 3bc958c..db2ca99 100644 --- a/bot/keyboards/inline/user_keyboards.py +++ b/bot/keyboards/inline/user_keyboards.py @@ -235,10 +235,6 @@ def get_payment_methods_manage_keyboard(lang: str, i18n_instance, has_card: bool """Deprecated in favor of get_payment_methods_list_keyboard. Kept for backward compatibility.""" _ = lambda key, **kwargs: i18n_instance.gettext(lang, key, **kwargs) builder = InlineKeyboardBuilder() - # Route to the new list view - builder.row( - InlineKeyboardButton(text=_(key="payment_methods_title"), callback_data="pm:list:0") - ) builder.row( InlineKeyboardButton(text=_(key="payment_method_bind_button"), callback_data="pm:bind") ) @@ -305,7 +301,7 @@ def get_payment_method_details_keyboard(pm_id: str, lang: str, i18n_instance) -> InlineKeyboardButton(text=_(key="payment_method_delete_button"), callback_data=f"pm:delete_confirm:{pm_id}") ) builder.row( - InlineKeyboardButton(text=_(key="payment_methods_title"), callback_data="pm:list:0") + InlineKeyboardButton(text=_(key="back_to_main_menu_button"), callback_data="pm:list:0") ) return builder.as_markup() @@ -322,5 +318,5 @@ def get_bind_url_keyboard(bind_url: str, lang: str, i18n_instance) -> InlineKeyb def get_back_to_payment_methods_keyboard(lang: str, i18n_instance) -> InlineKeyboardMarkup: _ = lambda key, **kwargs: i18n_instance.gettext(lang, key, **kwargs) builder = InlineKeyboardBuilder() - builder.row(InlineKeyboardButton(text=_(key="payment_methods_title"), callback_data="pm:list:0")) + builder.row(InlineKeyboardButton(text=_(key="back_to_main_menu_button"), callback_data="pm:list:0")) return builder.as_markup()