fix: preserve payment initiation time in transaction created_at
Transaction created_at and completed_at showed identical timestamps because webhook handlers created transactions with is_completed=True in a single step. Now all 10 payment providers pass payment.created_at to the transaction so created_at reflects when the user initiated the payment, not when the webhook processed it. Also: remove duplicate datetime import in inline.py, upgrade button stats DB error logging from debug to warning, add index on button_click_logs.button_type for analytics queries.
This commit is contained in:
@@ -37,6 +37,7 @@ async def create_transaction(
|
||||
payment_method: PaymentMethod | None = None,
|
||||
external_id: str | None = None,
|
||||
is_completed: bool = True,
|
||||
created_at: datetime | None = None,
|
||||
) -> Transaction:
|
||||
transaction = Transaction(
|
||||
user_id=user_id,
|
||||
@@ -47,6 +48,7 @@ async def create_transaction(
|
||||
external_id=external_id,
|
||||
is_completed=is_completed,
|
||||
completed_at=datetime.utcnow() if is_completed else None,
|
||||
**({'created_at': created_at} if created_at else {}),
|
||||
)
|
||||
|
||||
db.add(transaction)
|
||||
|
||||
@@ -2398,7 +2398,7 @@ class ButtonClickLog(Base):
|
||||
clicked_at = Column(DateTime, default=func.now(), index=True)
|
||||
|
||||
# Дополнительная информация
|
||||
button_type = Column(String(20), nullable=True) # builtin, callback, url, mini_app
|
||||
button_type = Column(String(20), nullable=True, index=True) # builtin, callback, url, mini_app
|
||||
button_text = Column(String(255), nullable=True) # Текст кнопки на момент клика
|
||||
|
||||
__table_args__ = (
|
||||
|
||||
@@ -47,8 +47,6 @@ async def get_main_menu_keyboard_async(
|
||||
Иначе делегирует в синхронную версию.
|
||||
"""
|
||||
if settings.MENU_LAYOUT_ENABLED:
|
||||
from datetime import datetime
|
||||
|
||||
from app.services.menu_layout_service import MenuContext, MenuLayoutService
|
||||
|
||||
# Получаем данные для плейсхолдеров
|
||||
|
||||
@@ -204,6 +204,6 @@ class ButtonStatsMiddleware(BaseMiddleware):
|
||||
button_text=button_text,
|
||||
)
|
||||
except Exception as e:
|
||||
logger.debug(f'Ошибка записи клика в БД {button_id}: {e}')
|
||||
logger.warning(f'Ошибка записи клика в БД {button_id}: {e}')
|
||||
except Exception as e:
|
||||
logger.debug(f'Ошибка создания сессии БД для логирования клика: {e}')
|
||||
logger.warning(f'Ошибка создания сессии БД для логирования клика: {e}')
|
||||
|
||||
@@ -237,6 +237,7 @@ class CloudPaymentsPaymentMixin:
|
||||
payment_method=PaymentMethod.CLOUDPAYMENTS,
|
||||
external_id=str(transaction_id_cp) if transaction_id_cp else invoice_id,
|
||||
is_completed=True,
|
||||
created_at=getattr(payment, 'created_at', None),
|
||||
)
|
||||
|
||||
payment.transaction_id = transaction.id
|
||||
|
||||
@@ -251,6 +251,7 @@ class CryptoBotPaymentMixin:
|
||||
payment_method=PaymentMethod.CRYPTOBOT,
|
||||
external_id=invoice_id,
|
||||
is_completed=True,
|
||||
created_at=getattr(updated_payment, 'created_at', None),
|
||||
)
|
||||
|
||||
await cryptobot_crud.link_cryptobot_payment_to_transaction(db, invoice_id, transaction.id)
|
||||
|
||||
@@ -279,6 +279,7 @@ class FreekassaPaymentMixin:
|
||||
payment_method=PaymentMethod.FREEKASSA,
|
||||
external_id=str(intid) if intid else payment.order_id,
|
||||
is_completed=True,
|
||||
created_at=getattr(payment, 'created_at', None),
|
||||
)
|
||||
|
||||
# Связываем платеж с транзакцией
|
||||
|
||||
@@ -325,6 +325,7 @@ class HeleketPaymentMixin:
|
||||
payment_method=PaymentMethod.HELEKET,
|
||||
external_id=updated_payment.uuid,
|
||||
is_completed=True,
|
||||
created_at=getattr(updated_payment, 'created_at', None),
|
||||
)
|
||||
|
||||
linked_payment = await heleket_crud.link_heleket_payment_to_transaction(
|
||||
|
||||
@@ -272,6 +272,7 @@ class KassaAiPaymentMixin:
|
||||
payment_method=PaymentMethod.KASSA_AI,
|
||||
external_id=str(intid) if intid else payment.order_id,
|
||||
is_completed=True,
|
||||
created_at=getattr(payment, 'created_at', None),
|
||||
)
|
||||
|
||||
# Связываем платеж с транзакцией
|
||||
|
||||
@@ -253,6 +253,7 @@ class MulenPayPaymentMixin:
|
||||
payment_method=PaymentMethod.MULENPAY,
|
||||
external_id=payment.uuid,
|
||||
is_completed=True,
|
||||
created_at=getattr(payment, 'created_at', None),
|
||||
)
|
||||
|
||||
await payment_module.link_mulenpay_payment_to_transaction(
|
||||
|
||||
@@ -385,6 +385,7 @@ class Pal24PaymentMixin:
|
||||
payment_method=PaymentMethod.PAL24,
|
||||
external_id=str(payment_id) if payment_id else payment.bill_id,
|
||||
is_completed=True,
|
||||
created_at=getattr(payment, 'created_at', None),
|
||||
)
|
||||
|
||||
await payment_module.link_pal24_payment_to_transaction(db, payment, transaction.id)
|
||||
|
||||
@@ -366,6 +366,7 @@ class PlategaPaymentMixin:
|
||||
payment_method=PaymentMethod.PLATEGA,
|
||||
external_id=transaction_external_id or payment.correlation_id,
|
||||
is_completed=True,
|
||||
created_at=getattr(payment, 'created_at', None),
|
||||
)
|
||||
created_transaction = True
|
||||
|
||||
|
||||
@@ -472,6 +472,7 @@ class WataPaymentMixin:
|
||||
payment_method=PaymentMethod.WATA,
|
||||
external_id=transaction_external_id or payment.payment_link_id,
|
||||
is_completed=True,
|
||||
created_at=getattr(payment, 'created_at', None),
|
||||
)
|
||||
|
||||
await payment_module.link_wata_payment_to_transaction(db, payment, transaction.id)
|
||||
|
||||
@@ -589,6 +589,7 @@ class YooKassaPaymentMixin:
|
||||
payment_method=PaymentMethod.YOOKASSA,
|
||||
external_id=payment.yookassa_payment_id,
|
||||
is_completed=True,
|
||||
created_at=getattr(payment, 'created_at', None),
|
||||
)
|
||||
|
||||
if not getattr(payment, 'transaction_id', None):
|
||||
|
||||
Reference in New Issue
Block a user