Improve payment method filtering logic in subscription history handler

- Added support for distinguishing between internal method IDs and direct provider payment method IDs, enhancing the accuracy of payment method filtering.
- Introduced a flag to track if a payment method filter was requested, allowing for clearer handling of cases where the method cannot be resolved.
- Updated error handling to ensure that empty payment histories are displayed appropriately when a filter is requested but cannot be matched.
This commit is contained in:
machka-pasla
2025-09-04 19:06:20 +03:00
parent 868c48ccb9
commit 72eaed991a
+20 -6
View File
@@ -926,17 +926,31 @@ async def payment_method_history(callback: types.CallbackQuery, settings: Settin
# If viewing a specific saved payment method, filter history by that method when possible # If viewing a specific saved payment method, filter history by that method when possible
selected_pm_provider_id: Optional[str] = None selected_pm_provider_id: Optional[str] = None
pm_filter_requested: bool = False
try: try:
split_a, split_b, split_pm_id = callback.data.split(":", 2) split_a, split_b, split_pm_id = callback.data.split(":", 2)
if split_pm_id: if split_pm_id:
# pm_id is our internal method_id; map to provider id pm_filter_requested = True
from db.dal.user_billing_dal import list_user_payment_methods # Two possible formats:
methods = await list_user_payment_methods(session, callback.from_user.id) # - Internal method_id (digits)
sel = next((m for m in methods if str(m.method_id) == split_pm_id), None) # - Direct provider payment_method.id (e.g., YooKassa 'pm_...')
if sel and sel.provider_payment_method_id: if split_pm_id.isdigit():
selected_pm_provider_id = sel.provider_payment_method_id # Map internal id to provider id
from db.dal.user_billing_dal import list_user_payment_methods
methods = await list_user_payment_methods(session, callback.from_user.id)
sel = next((m for m in methods if str(m.method_id) == split_pm_id), None)
if sel and sel.provider_payment_method_id:
selected_pm_provider_id = sel.provider_payment_method_id
else:
# Assume it's already a provider payment_method.id
selected_pm_provider_id = split_pm_id
except Exception: except Exception:
selected_pm_provider_id = None selected_pm_provider_id = None
pm_filter_requested = False
# If filter was explicitly requested but method can't be resolved (e.g., deleted), show empty history
if pm_filter_requested and not selected_pm_provider_id:
user_payments = []
if selected_pm_provider_id: if selected_pm_provider_id:
# Filter to rows we can confidently associate with the selected method # Filter to rows we can confidently associate with the selected method