From 0444ff325490f24e9a8d35f83ba37a0bd95ab6c5 Mon Sep 17 00:00:00 2001 From: Tolmachev Igor Date: Mon, 23 Mar 2026 22:17:24 +0300 Subject: Add pay_invoice button --- models/payment.py | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 2 deletions(-) (limited to 'models/payment.py') diff --git a/models/payment.py b/models/payment.py index 2b1cb90..afae642 100644 --- a/models/payment.py +++ b/models/payment.py @@ -1,16 +1,64 @@ from datetime import datetime +from enum import StrEnum, auto -from sqlalchemy import ForeignKey +from aiogram import Bot +from aiogram.types import Message, ReplyMarkupUnion +from pydantic import BaseModel +from sqlalchemy import JSON, ForeignKey from sqlalchemy.orm import Mapped, mapped_column from models import BaseTable, Invoice, User +class ReceiptFileType(StrEnum): + PHOTO = auto() + DOCUMENT = auto() + + +class ReceiptFile(BaseModel): + type: ReceiptFileType + file_id: str + + async def send( + self, + bot: Bot, + user_id: int, + reply_markup: ReplyMarkupUnion | None = None, + ) -> Message: + if self.type == ReceiptFileType.DOCUMENT: + return await bot.send_document( + user_id, + document=self.file_id, + reply_markup=reply_markup, + ) + else: + return await bot.send_photo( + user_id, + photo=self.file_id, + reply_markup=reply_markup, + ) + + +class PaymentStatus(StrEnum): + PENDING = auto() + ACCEPTED = auto() + REJECTED = auto() + + class Payment(BaseTable): __tablename__ = "payment" id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True) user_id: Mapped[int] = mapped_column(ForeignKey(User.id)) invoice_id: Mapped[int] = mapped_column(ForeignKey(Invoice.id)) - receipt_file_id: Mapped[str] + __receipt_file: Mapped[str] = mapped_column("receipt_file", JSON()) + status: Mapped[PaymentStatus] = mapped_column(default=PaymentStatus.PENDING) datetime: Mapped[datetime] + + @property + def receipt_file(self) -> ReceiptFile: + return ReceiptFile.model_validate_json(self.__receipt_file) + + @receipt_file.setter + def receipt_file(self, value: ReceiptFile) -> None: + self.__receipt_file = value.model_dump_json() -- cgit v1.3