Standardize YooMoney wallet display across payment handlers and localization

- Updated the display logic for YooMoney wallet in payment and subscription handlers to ensure consistent naming and avoid leaking sensitive account information.
- Introduced a new localization string for wallet display, enhancing clarity in user-facing messages.
- Refactored relevant sections to improve overall code maintainability and user experience.
This commit is contained in:
machka-pasla
2025-09-04 18:26:15 +03:00
parent cb464059d9
commit ba8be7c3f9
4 changed files with 32 additions and 9 deletions
+4 -2
View File
@@ -151,7 +151,8 @@ async def process_successful_payment(session: AsyncSession, bot: Bot,
display_network = card.get("card_type") or title or "Card" display_network = card.get("card_type") or title or "Card"
display_last4 = card.get("last4") display_last4 = card.get("last4")
elif (pm_type or "").lower() in {"yoo_money", "yoomoney", "yoo-money", "wallet"}: elif (pm_type or "").lower() in {"yoo_money", "yoomoney", "yoo-money", "wallet"}:
display_network = title or "YooMoney" # Normalize wallet display name to avoid leaking full account from title
display_network = "YooMoney"
if isinstance(account_number, str) and len(account_number) >= 4: if isinstance(account_number, str) and len(account_number) >= 4:
display_last4 = account_number[-4:] display_last4 = account_number[-4:]
else: else:
@@ -500,7 +501,8 @@ async def yookassa_webhook_route(request: web.Request):
display_network = card.get("card_type") or title or "Card" display_network = card.get("card_type") or title or "Card"
display_last4 = card.get("last4") display_last4 = card.get("last4")
elif (pm_type or "").lower() in {"yoo_money", "yoomoney", "yoo-money", "wallet"}: elif (pm_type or "").lower() in {"yoo_money", "yoomoney", "yoo-money", "wallet"}:
display_network = title or "YooMoney" # Normalize wallet display name to avoid leaking full account from title
display_network = "YooMoney"
if isinstance(account_number, str) and len(account_number) >= 4: if isinstance(account_number, str) and len(account_number) >= 4:
display_last4 = account_number[-4:] display_last4 = account_number[-4:]
else: else:
+26 -7
View File
@@ -321,7 +321,8 @@ async def pay_yk_callback_handler(
display_network = card.get('card_type') or title or 'Card' display_network = card.get('card_type') or title or 'Card'
display_last4 = card.get('last4') display_last4 = card.get('last4')
elif (pm_type or '').lower() in {"yoo_money", "yoomoney", "yoo-money", "wallet"}: elif (pm_type or '').lower() in {"yoo_money", "yoomoney", "yoo-money", "wallet"}:
display_network = title or 'YooMoney' # Normalize wallet display name to avoid leaking full account from title
display_network = 'YooMoney'
display_last4 = account_number[-4:] if isinstance(account_number, str) and len(account_number) >= 4 else None display_last4 = account_number[-4:] if isinstance(account_number, str) and len(account_number) >= 4 else None
else: else:
display_network = title or (pm_type.upper() if pm_type else 'Payment method') display_network = title or (pm_type.upper() if pm_type else 'Payment method')
@@ -618,7 +619,10 @@ async def payment_methods_manage(callback: types.CallbackQuery, settings: Settin
cards: List[tuple] = [] cards: List[tuple] = []
for m in methods: for m in methods:
if m.card_last4: if m.card_last4:
title = get_text("payment_method_card_title", network=m.card_network or "Card", last4=m.card_last4) if (m.card_network or '').lower() in {"yoomoney", "yoo money", "yoo-money", "yoomoney wallet", "yoomoney кошелек", "yoomoney кошелёк"} or (m.card_network or '') == "YooMoney":
title = get_text("payment_method_wallet_title", last4=m.card_last4)
else:
title = get_text("payment_method_card_title", network=m.card_network or "Card", last4=m.card_last4)
else: else:
title = get_text("payment_method_generic_title", network=m.card_network or "Payment method") title = get_text("payment_method_generic_title", network=m.card_network or "Payment method")
cards.append((str(m.method_id), title if not m.is_default else f"{title}")) cards.append((str(m.method_id), title if not m.is_default else f"{title}"))
@@ -700,7 +704,10 @@ async def payment_method_delete(callback: types.CallbackQuery, settings: Setting
cards = [] cards = []
for m in methods: for m in methods:
if m.card_last4: if m.card_last4:
title = _("payment_method_card_title", network=m.card_network or "Card", last4=m.card_last4) if (m.card_network or '').lower() in {"yoomoney", "yoo money", "yoo-money", "yoomoney wallet", "yoomoney кошелек", "yoomoney кошелёк"} or (m.card_network or '') == "YooMoney":
title = _("payment_method_wallet_title", last4=m.card_last4)
else:
title = _("payment_method_card_title", network=m.card_network or "Card", last4=m.card_last4)
else: else:
title = _("payment_method_generic_title", network=m.card_network or "Payment method") title = _("payment_method_generic_title", network=m.card_network or "Payment method")
cards.append((str(m.method_id), title if not m.is_default else f"{title}")) cards.append((str(m.method_id), title if not m.is_default else f"{title}"))
@@ -733,7 +740,10 @@ async def payment_method_delete(callback: types.CallbackQuery, settings: Setting
cards = [] cards = []
for m in methods: for m in methods:
if m.card_last4: if m.card_last4:
title = _("payment_method_card_title", network=m.card_network or "Card", last4=m.card_last4) if (m.card_network or '').lower() in {"yoomoney", "yoo money", "yoo-money", "yoomoney wallet", "yoomoney кошелек", "yoomoney кошелёк"} or (m.card_network or '') == "YooMoney":
title = _("payment_method_wallet_title", last4=m.card_last4)
else:
title = _("payment_method_card_title", network=m.card_network or "Card", last4=m.card_last4)
else: else:
title = _("payment_method_generic_title", network=m.card_network or "Payment method") title = _("payment_method_generic_title", network=m.card_network or "Payment method")
cards.append((str(m.method_id), title if not m.is_default else f"{title}")) cards.append((str(m.method_id), title if not m.is_default else f"{title}"))
@@ -766,7 +776,10 @@ async def payment_method_view(callback: types.CallbackQuery, settings: Settings,
# Map: # Map:
sel = next((m for m in methods if str(m.method_id) == pm_id or m.provider_payment_method_id == pm_id), methods[0]) sel = next((m for m in methods if str(m.method_id) == pm_id or m.provider_payment_method_id == pm_id), methods[0])
if sel.card_last4: if sel.card_last4:
title = _("payment_method_card_title", network=sel.card_network or "Card", last4=sel.card_last4) if (sel.card_network or '').lower() in {"yoomoney", "yoo money", "yoo-money", "yoomoney wallet", "yoomoney кошелек", "yoomoney кошелёк"} or (sel.card_network or '') == "YooMoney":
title = _("payment_method_wallet_title", last4=sel.card_last4)
else:
title = _("payment_method_card_title", network=sel.card_network or "Card", last4=sel.card_last4)
else: else:
title = _("payment_method_generic_title", network=sel.card_network or "Payment method") title = _("payment_method_generic_title", network=sel.card_network or "Payment method")
added_at = sel.created_at.strftime('%Y-%m-%d') if getattr(sel, 'created_at', None) else "" added_at = sel.created_at.strftime('%Y-%m-%d') if getattr(sel, 'created_at', None) else ""
@@ -817,7 +830,10 @@ async def payment_method_view(callback: types.CallbackQuery, settings: Settings,
except Exception: except Exception:
pass pass
if billing.card_last4: if billing.card_last4:
title = _("payment_method_card_title", network=billing.card_network or "Card", last4=billing.card_last4) if (billing.card_network or '').lower() in {"yoomoney", "yoo money", "yoo-money", "yoomoney wallet", "yoomoney кошелек", "yoomoney кошелёк"} or (billing.card_network or '') == "YooMoney":
title = _("payment_method_wallet_title", last4=billing.card_last4)
else:
title = _("payment_method_card_title", network=billing.card_network or "Card", last4=billing.card_last4)
else: else:
title = _("payment_method_generic_title", network=billing.card_network or "Payment method") title = _("payment_method_generic_title", network=billing.card_network or "Payment method")
details = f"{title}\n{_('payment_method_added_at', date=added_at)}\n{_('payment_method_last_tx', date=last_tx)}" details = f"{title}\n{_('payment_method_added_at', date=added_at)}\n{_('payment_method_last_tx', date=last_tx)}"
@@ -876,7 +892,10 @@ async def payment_methods_list(callback: types.CallbackQuery, settings: Settings
methods = await list_user_payment_methods(session, callback.from_user.id) methods = await list_user_payment_methods(session, callback.from_user.id)
for m in methods: for m in methods:
if m.card_last4: if m.card_last4:
title = get_text("payment_method_card_title", network=m.card_network or "Card", last4=m.card_last4) if (m.card_network or '').lower() in {"yoomoney", "yoo money", "yoo-money", "yoomoney wallet", "yoomoney кошелек", "yoomoney кошелёк"} or (m.card_network or '') == "YooMoney":
title = get_text("payment_method_wallet_title", last4=m.card_last4)
else:
title = get_text("payment_method_card_title", network=m.card_network or "Card", last4=m.card_last4)
else: else:
title = get_text("payment_method_generic_title", network=m.card_network or "Payment method") title = get_text("payment_method_generic_title", network=m.card_network or "Payment method")
cards.append((str(m.method_id), title if not m.is_default else f"{title}")) cards.append((str(m.method_id), title if not m.is_default else f"{title}"))
+1
View File
@@ -386,6 +386,7 @@
"payment_method_delete_confirm": "Remove saved payment method?", "payment_method_delete_confirm": "Remove saved payment method?",
"payment_method_card_title": "💳 {network} ••••{last4}", "payment_method_card_title": "💳 {network} ••••{last4}",
"payment_method_generic_title": "💳 {network}", "payment_method_generic_title": "💳 {network}",
"payment_method_wallet_title": "💼 YooMoney wallet ••••{last4}",
"payment_method_added_at": "Added: {date}", "payment_method_added_at": "Added: {date}",
"payment_method_last_tx": "Last transaction: {date}", "payment_method_last_tx": "Last transaction: {date}",
"payment_method_tx_history_title": "📜 Transactions history", "payment_method_tx_history_title": "📜 Transactions history",
+1
View File
@@ -385,6 +385,7 @@
"payment_method_delete_confirm": "Удалить сохранённый способ оплаты?", "payment_method_delete_confirm": "Удалить сохранённый способ оплаты?",
"payment_method_card_title": "💳 {network} ••••{last4}", "payment_method_card_title": "💳 {network} ••••{last4}",
"payment_method_generic_title": "💳 {network}", "payment_method_generic_title": "💳 {network}",
"payment_method_wallet_title": "💼 Кошелёк YooMoney ••••{last4}",
"payment_method_added_at": "Добавлена: {date}", "payment_method_added_at": "Добавлена: {date}",
"payment_method_last_tx": "Последняя операция: {date}", "payment_method_last_tx": "Последняя операция: {date}",
"payment_method_tx_history_title": "📜 История операций", "payment_method_tx_history_title": "📜 История операций",