aboutsummaryrefslogtreecommitdiff
path: root/models/payment.py
blob: ea61817d3f4122036bd0ff7a6a0642f478e77a43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from datetime import datetime
from enum import StrEnum, auto

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,
        caption: str | None = None,
        reply_markup: ReplyMarkupUnion | None = None,
    ) -> Message:
        if self.type == ReceiptFileType.DOCUMENT:
            return await bot.send_document(
                user_id,
                document=self.file_id,
                caption=caption,
                reply_markup=reply_markup,
            )
        else:
            return await bot.send_photo(
                user_id,
                photo=self.file_id,
                caption=caption,
                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: 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()