mirror of
https://github.com/BEDOLAGA-DEV/remnawave-bedolaga-telegram-bot.git
synced 2026-02-27 14:51:19 +00:00
- Add pyproject.toml with uv and ruff configuration - Pin Python version to 3.13 via .python-version - Add Makefile commands: lint, format, fix - Apply ruff formatting to entire codebase - Remove unused imports (base64 in yookassa/simple_subscription) - Update .gitignore for new config files
156 lines
4.2 KiB
Python
156 lines
4.2 KiB
Python
"""CRUD helpers for WATA payment records."""
|
|
|
|
import logging
|
|
from datetime import datetime
|
|
from typing import Any
|
|
|
|
from sqlalchemy import select, update
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.database.models import WataPayment
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
async def create_wata_payment(
|
|
db: AsyncSession,
|
|
*,
|
|
user_id: int,
|
|
payment_link_id: str,
|
|
amount_kopeks: int,
|
|
currency: str,
|
|
description: str | None,
|
|
status: str,
|
|
type_: str | None,
|
|
url: str | None,
|
|
order_id: str | None = None,
|
|
metadata: dict[str, Any] | None = None,
|
|
expires_at: datetime | None = None,
|
|
terminal_public_id: str | None = None,
|
|
success_redirect_url: str | None = None,
|
|
fail_redirect_url: str | None = None,
|
|
) -> WataPayment:
|
|
payment = WataPayment(
|
|
user_id=user_id,
|
|
payment_link_id=payment_link_id,
|
|
order_id=order_id,
|
|
amount_kopeks=amount_kopeks,
|
|
currency=currency,
|
|
description=description,
|
|
status=status,
|
|
type=type_,
|
|
url=url,
|
|
metadata_json=metadata or {},
|
|
expires_at=expires_at,
|
|
terminal_public_id=terminal_public_id,
|
|
success_redirect_url=success_redirect_url,
|
|
fail_redirect_url=fail_redirect_url,
|
|
)
|
|
|
|
db.add(payment)
|
|
await db.commit()
|
|
await db.refresh(payment)
|
|
|
|
logger.info(
|
|
'Создан Wata платеж #%s для пользователя %s: %s копеек (статус %s)',
|
|
payment.id,
|
|
user_id,
|
|
amount_kopeks,
|
|
status,
|
|
)
|
|
|
|
return payment
|
|
|
|
|
|
async def get_wata_payment_by_id(
|
|
db: AsyncSession,
|
|
payment_id: int,
|
|
) -> WataPayment | None:
|
|
result = await db.execute(select(WataPayment).where(WataPayment.id == payment_id))
|
|
return result.scalar_one_or_none()
|
|
|
|
|
|
async def get_wata_payment_by_link_id(
|
|
db: AsyncSession,
|
|
payment_link_id: str,
|
|
) -> WataPayment | None:
|
|
result = await db.execute(select(WataPayment).where(WataPayment.payment_link_id == payment_link_id))
|
|
return result.scalar_one_or_none()
|
|
|
|
|
|
async def get_wata_payment_by_order_id(
|
|
db: AsyncSession,
|
|
order_id: str,
|
|
) -> WataPayment | None:
|
|
result = await db.execute(select(WataPayment).where(WataPayment.order_id == order_id))
|
|
return result.scalar_one_or_none()
|
|
|
|
|
|
async def update_wata_payment_status(
|
|
db: AsyncSession,
|
|
payment: WataPayment,
|
|
*,
|
|
status: str | None = None,
|
|
is_paid: bool | None = None,
|
|
paid_at: datetime | None = None,
|
|
last_status: str | None = None,
|
|
url: str | None = None,
|
|
metadata: dict[str, Any] | None = None,
|
|
callback_payload: dict[str, Any] | None = None,
|
|
terminal_public_id: str | None = None,
|
|
) -> WataPayment:
|
|
update_values: dict[str, Any] = {}
|
|
|
|
if status is not None:
|
|
update_values['status'] = status
|
|
if is_paid is not None:
|
|
update_values['is_paid'] = is_paid
|
|
if paid_at is not None:
|
|
update_values['paid_at'] = paid_at
|
|
if last_status is not None:
|
|
update_values['last_status'] = last_status
|
|
if url is not None:
|
|
update_values['url'] = url
|
|
if metadata is not None:
|
|
update_values['metadata_json'] = metadata
|
|
if callback_payload is not None:
|
|
update_values['callback_payload'] = callback_payload
|
|
if terminal_public_id is not None:
|
|
update_values['terminal_public_id'] = terminal_public_id
|
|
|
|
if not update_values:
|
|
return payment
|
|
|
|
await db.execute(update(WataPayment).where(WataPayment.id == payment.id).values(**update_values))
|
|
|
|
await db.commit()
|
|
await db.refresh(payment)
|
|
|
|
logger.info(
|
|
'Обновлен Wata платеж %s: статус=%s, is_paid=%s',
|
|
payment.payment_link_id,
|
|
payment.status,
|
|
payment.is_paid,
|
|
)
|
|
|
|
return payment
|
|
|
|
|
|
async def link_wata_payment_to_transaction(
|
|
db: AsyncSession,
|
|
payment: WataPayment,
|
|
transaction_id: int,
|
|
) -> WataPayment:
|
|
await db.execute(update(WataPayment).where(WataPayment.id == payment.id).values(transaction_id=transaction_id))
|
|
await db.commit()
|
|
await db.refresh(payment)
|
|
|
|
logger.info(
|
|
'Wata платеж %s привязан к транзакции %s',
|
|
payment.payment_link_id,
|
|
transaction_id,
|
|
)
|
|
|
|
return payment
|