mirror of
https://github.com/BEDOLAGA-DEV/remnawave-bedolaga-telegram-bot.git
synced 2026-02-26 14:21:25 +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
154 lines
4.4 KiB
Python
154 lines
4.4 KiB
Python
"""CRUD helpers for PayPalych (Pal24) payments."""
|
|
|
|
from __future__ import annotations
|
|
|
|
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 Pal24Payment
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
async def create_pal24_payment(
|
|
db: AsyncSession,
|
|
*,
|
|
user_id: int,
|
|
bill_id: str,
|
|
amount_kopeks: int,
|
|
description: str | None,
|
|
status: str,
|
|
type_: str,
|
|
currency: str,
|
|
link_url: str | None,
|
|
link_page_url: str | None,
|
|
order_id: str | None = None,
|
|
ttl: int | None = None,
|
|
metadata: dict[str, Any] | None = None,
|
|
) -> Pal24Payment:
|
|
payment = Pal24Payment(
|
|
user_id=user_id,
|
|
bill_id=bill_id,
|
|
order_id=order_id,
|
|
amount_kopeks=amount_kopeks,
|
|
currency=currency,
|
|
description=description,
|
|
status=status,
|
|
type=type_,
|
|
link_url=link_url,
|
|
link_page_url=link_page_url,
|
|
metadata_json=metadata or {},
|
|
ttl=ttl,
|
|
)
|
|
|
|
db.add(payment)
|
|
await db.commit()
|
|
await db.refresh(payment)
|
|
|
|
logger.info(
|
|
'Создан Pal24 платеж #%s для пользователя %s: %s копеек (статус %s)',
|
|
payment.id,
|
|
user_id,
|
|
amount_kopeks,
|
|
status,
|
|
)
|
|
|
|
return payment
|
|
|
|
|
|
async def get_pal24_payment_by_id(db: AsyncSession, payment_id: int) -> Pal24Payment | None:
|
|
result = await db.execute(select(Pal24Payment).where(Pal24Payment.id == payment_id))
|
|
return result.scalar_one_or_none()
|
|
|
|
|
|
async def get_pal24_payment_by_bill_id(db: AsyncSession, bill_id: str) -> Pal24Payment | None:
|
|
result = await db.execute(select(Pal24Payment).where(Pal24Payment.bill_id == bill_id))
|
|
return result.scalar_one_or_none()
|
|
|
|
|
|
async def get_pal24_payment_by_order_id(db: AsyncSession, order_id: str) -> Pal24Payment | None:
|
|
result = await db.execute(select(Pal24Payment).where(Pal24Payment.order_id == order_id))
|
|
return result.scalar_one_or_none()
|
|
|
|
|
|
async def update_pal24_payment_status(
|
|
db: AsyncSession,
|
|
payment: Pal24Payment,
|
|
*,
|
|
status: str,
|
|
is_active: bool | None = None,
|
|
is_paid: bool | None = None,
|
|
paid_at: datetime | None = None,
|
|
payment_id: str | None = None,
|
|
payment_status: str | None = None,
|
|
payment_method: str | None = None,
|
|
balance_amount: str | None = None,
|
|
balance_currency: str | None = None,
|
|
payer_account: str | None = None,
|
|
callback_payload: dict[str, Any] | None = None,
|
|
metadata: dict[str, Any] | None = None,
|
|
) -> Pal24Payment:
|
|
update_values: dict[str, Any] = {
|
|
'status': status,
|
|
}
|
|
|
|
if is_active is not None:
|
|
update_values['is_active'] = is_active
|
|
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 payment_id is not None:
|
|
update_values['payment_id'] = payment_id
|
|
if payment_status is not None:
|
|
update_values['payment_status'] = payment_status
|
|
if payment_method is not None:
|
|
update_values['payment_method'] = payment_method
|
|
if balance_amount is not None:
|
|
update_values['balance_amount'] = balance_amount
|
|
if balance_currency is not None:
|
|
update_values['balance_currency'] = balance_currency
|
|
if payer_account is not None:
|
|
update_values['payer_account'] = payer_account
|
|
if callback_payload is not None:
|
|
update_values['callback_payload'] = callback_payload
|
|
if metadata is not None:
|
|
update_values['metadata_json'] = metadata
|
|
|
|
update_values['last_status'] = status
|
|
|
|
await db.execute(update(Pal24Payment).where(Pal24Payment.id == payment.id).values(**update_values))
|
|
|
|
await db.commit()
|
|
await db.refresh(payment)
|
|
|
|
logger.info(
|
|
'Обновлен Pal24 платеж %s: статус=%s, is_paid=%s',
|
|
payment.bill_id,
|
|
payment.status,
|
|
payment.is_paid,
|
|
)
|
|
|
|
return payment
|
|
|
|
|
|
async def link_pal24_payment_to_transaction(
|
|
db: AsyncSession,
|
|
payment: Pal24Payment,
|
|
transaction_id: int,
|
|
) -> Pal24Payment:
|
|
await db.execute(update(Pal24Payment).where(Pal24Payment.id == payment.id).values(transaction_id=transaction_id))
|
|
await db.commit()
|
|
await db.refresh(payment)
|
|
logger.info(
|
|
'Pal24 платеж %s привязан к транзакции %s',
|
|
payment.bill_id,
|
|
transaction_id,
|
|
)
|
|
return payment
|