diff --git a/app/database/models.py b/app/database/models.py index 8272e63b..2cfbf892 100644 --- a/app/database/models.py +++ b/app/database/models.py @@ -87,6 +87,7 @@ class PaymentMethod(Enum): WATA = "wata" PLATEGA = "platega" CLOUDPAYMENTS = "cloudpayments" + FREEKASSA = "freekassa" MANUAL = "manual" @@ -545,6 +546,73 @@ class CloudPaymentsPayment(Base): ) +class FreekassaPayment(Base): + __tablename__ = "freekassa_payments" + + id = Column(Integer, primary_key=True, index=True) + user_id = Column(Integer, ForeignKey("users.id"), nullable=False) + + # Идентификаторы + order_id = Column(String(64), unique=True, nullable=False, index=True) # Наш ID заказа + freekassa_order_id = Column(String(64), unique=True, nullable=True, index=True) # intid от Freekassa + + # Суммы + amount_kopeks = Column(Integer, nullable=False) + currency = Column(String(10), nullable=False, default="RUB") + description = Column(Text, nullable=True) + + # Статусы + status = Column(String(32), nullable=False, default="pending") # pending, success, failed, expired + is_paid = Column(Boolean, default=False) + + # Данные платежа + payment_url = Column(Text, nullable=True) + payment_system_id = Column(Integer, nullable=True) # ID платежной системы FK + + # Метаданные + metadata_json = Column(JSON, nullable=True) + callback_payload = Column(JSON, nullable=True) + + # Временные метки + paid_at = Column(DateTime, nullable=True) + expires_at = Column(DateTime, nullable=True) + created_at = Column(DateTime, default=func.now()) + updated_at = Column(DateTime, default=func.now(), onupdate=func.now()) + + # Связь с транзакцией + transaction_id = Column(Integer, ForeignKey("transactions.id"), nullable=True) + + # Relationships + user = relationship("User", backref="freekassa_payments") + transaction = relationship("Transaction", backref="freekassa_payment") + + @property + def amount_rubles(self) -> float: + return self.amount_kopeks / 100 + + @property + def is_pending(self) -> bool: + return self.status == "pending" + + @property + def is_success(self) -> bool: + return self.status == "success" and self.is_paid + + @property + def is_failed(self) -> bool: + return self.status in ["failed", "expired"] + + def __repr__(self) -> str: # pragma: no cover - debug helper + return ( + "".format( + self.id, + self.order_id, + self.amount_rubles, + self.status, + ) + ) + + class PromoGroup(Base): __tablename__ = "promo_groups" @@ -2040,4 +2108,4 @@ class CabinetRefreshToken(Base): def __repr__(self) -> str: status = "valid" if self.is_valid else ("revoked" if self.is_revoked else "expired") - return f"" \ No newline at end of file + return f""