From bb7641bb744732537bd54ddc4e659b7036db50df Mon Sep 17 00:00:00 2001 From: machka-pasla Date: Thu, 7 Aug 2025 23:39:48 +0300 Subject: [PATCH] Improve amount conversion in TributeService to handle minor units - Updated the amount handling logic to convert minor currency units (kopecks/cents) to major units before persisting. - Added error handling for invalid amount inputs to ensure robustness in processing payment data. - Ensured that the amount is rounded to two decimal places for accurate representation in the system. --- bot/services/tribute_service.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/bot/services/tribute_service.py b/bot/services/tribute_service.py index 5401922..b65b7a6 100644 --- a/bot/services/tribute_service.py +++ b/bot/services/tribute_service.py @@ -96,10 +96,17 @@ class TributeService: period_val = data.get("period") months = convert_period_to_months(period_val) - # Price/amount from spec is integer cents in currency; we store float in Payment + # Tribute sends amount in minor units (kopecks/cents). Convert to major units before persisting. amount_value = data.get("amount") or data.get("price") currency = (data.get("currency") or settings.DEFAULT_CURRENCY_SYMBOL or "RUB").upper() - amount_float = float(amount_value) if amount_value is not None else 0.0 + if amount_value is not None: + try: + amount_minor_units = float(amount_value) + except (TypeError, ValueError): + amount_minor_units = 0.0 + amount_float = round(amount_minor_units / 100.0, 2) + else: + amount_float = 0.0 async with async_session_factory() as session: if event_name == "new_subscription":