fix: record transactions for free tariff switches and admin tariff changes

- Add transaction records for free tariff switches (downgrade, upgrade_cost=0) in miniapp and cabinet
- Add atomic transaction records for admin tariff changes in bot handler and cabinet API
- Use commit=False for admin flows to ensure subscription change and transaction are committed together
This commit is contained in:
Fringg
2026-03-11 02:17:35 +03:00
parent 2879996455
commit 864a4ed700
4 changed files with 44 additions and 0 deletions
+12
View File
@@ -1134,6 +1134,18 @@ async def update_user_subscription(
if settings.RESET_TRAFFIC_ON_TARIFF_SWITCH:
subscription.traffic_used_gb = 0.0
# Записываем транзакцию о смене тарифа
from app.database.crud.transaction import create_transaction
await create_transaction(
db=db,
user_id=user.id,
type=TransactionType.SUBSCRIPTION_PAYMENT,
amount_kopeks=0,
description=f"Смена тарифа администратором на '{tariff.name}'",
commit=False,
)
await db.commit()
await db.refresh(subscription)
+10
View File
@@ -4350,6 +4350,16 @@ async def switch_tariff(
description=description,
payment_method=PaymentMethod.BALANCE,
)
else:
# Free switch (downgrade) — record in history
description = f"Переход на тариф '{new_tariff.name}'"
await create_transaction(
db=db,
user_id=user.id,
type=TransactionType.SUBSCRIPTION_PAYMENT,
amount_kopeks=0,
description=description,
)
# Update subscription
old_tariff_name = current_tariff.name if current_tariff else 'Unknown'
+12
View File
@@ -5329,6 +5329,18 @@ async def confirm_admin_tariff_change(callback: types.CallbackQuery, db_user: Us
if settings.RESET_TRAFFIC_ON_TARIFF_SWITCH:
subscription.traffic_used_gb = 0.0
# Записываем транзакцию о смене тарифа
from app.database.crud.transaction import create_transaction
await create_transaction(
db=db,
user_id=user.id,
type=TransactionType.SUBSCRIPTION_PAYMENT,
amount_kopeks=0,
description=f"Смена тарифа администратором на '{tariff.name}'",
commit=False,
)
await db.commit()
# Синхронизируем с RemnaWave (сброс трафика по админ-настройке)
+10
View File
@@ -7061,6 +7061,16 @@ async def switch_tariff_endpoint(
amount_kopeks=upgrade_cost,
description=description,
)
else:
# Бесплатный переход (downgrade) — записываем в историю
description = f"Переход на тариф '{new_tariff.name}'"
await create_transaction(
db=db,
user_id=user.id,
type=TransactionType.SUBSCRIPTION_PAYMENT,
amount_kopeks=0,
description=description,
)
# Получаем список серверов из тарифа
squads = new_tariff.allowed_squads or []