42 lines
1.7 KiB
Python
42 lines
1.7 KiB
Python
from django.db import models
|
|
|
|
from apps.core.models import TimeStampedModel, UUIDModel
|
|
|
|
|
|
class Payment(UUIDModel, TimeStampedModel):
|
|
class Status(models.TextChoices):
|
|
PENDING = "pending", "Pending"
|
|
SUCCESS = "success", "Success"
|
|
FAILED = "failed", "Failed"
|
|
CANCELLED = "cancelled", "Cancelled"
|
|
|
|
# A payment targets either a reservation (upfront) or a processing order.
|
|
reservation = models.ForeignKey(
|
|
"reservations.Reservation", null=True, blank=True, on_delete=models.PROTECT, related_name="payments"
|
|
)
|
|
order = models.ForeignKey(
|
|
"orders.ProcessingOrder", null=True, blank=True, on_delete=models.PROTECT, related_name="payments"
|
|
)
|
|
amount = models.DecimalField(max_digits=18, decimal_places=2)
|
|
currency = models.CharField(max_length=8, default="IRR")
|
|
gateway = models.CharField(max_length=40)
|
|
transaction_id = models.CharField(max_length=120)
|
|
authority = models.CharField(max_length=120, blank=True)
|
|
status = models.CharField(max_length=12, choices=Status.choices, default=Status.PENDING)
|
|
request_time = models.DateTimeField(null=True, blank=True)
|
|
callback_time = models.DateTimeField(null=True, blank=True)
|
|
verification_time = models.DateTimeField(null=True, blank=True)
|
|
error = models.TextField(blank=True)
|
|
|
|
class Meta:
|
|
constraints = [
|
|
models.UniqueConstraint(fields=["gateway", "transaction_id"], name="uniq_gateway_transaction"),
|
|
models.CheckConstraint(
|
|
condition=models.Q(order__isnull=False) | models.Q(reservation__isnull=False),
|
|
name="payment_has_target",
|
|
),
|
|
]
|
|
|
|
def __str__(self) -> str:
|
|
return f"{self.gateway}:{self.transaction_id}"
|