from django.conf import settings from django.db import models from apps.bookings.models import Booking class PaymentProvider(models.TextChoices): HYPERPAY = "hyperpay", "HyperPay" PAYTABS = "paytabs", "PayTabs" MOYASAR = "moyasar", "Moyasar" TAP = "tap", "Tap" AMAZON_PAYMENT_SERVICES = "amazon_payment_services", "Amazon Payment Services" CHECKOUT = "checkout", "Checkout.com" OTHER = "other", "Other" class PaymentStatus(models.TextChoices): INITIATED = "initiated", "Initiated" CREATED = "created", "Created" AUTHORIZED = "authorized", "Authorized" CAPTURED = "captured", "Captured" PAID = "paid", "Paid" FAILED = "failed", "Failed" REFUNDED = "refunded", "Refunded" VOIDED = "voided", "Voided" VERIFIED = "verified", "Verified" class Payment(models.Model): booking = models.ForeignKey(Booking, on_delete=models.CASCADE, related_name="payments") provider = models.CharField(max_length=50, choices=PaymentProvider.choices) status = models.CharField(max_length=20, choices=PaymentStatus.choices, default=PaymentStatus.INITIATED) amount = models.DecimalField(max_digits=10, decimal_places=2) currency = models.CharField(max_length=10, default=getattr(settings, "DEFAULT_CURRENCY", "SAR")) external_id = models.CharField(max_length=200, null=True, blank=True, unique=True) idempotency_key = models.UUIDField(null=True, blank=True, unique=True) provider_payload = models.JSONField(null=True, blank=True) metadata = models.JSONField(default=dict, blank=True) authorized_at = models.DateTimeField(null=True, blank=True) captured_at = models.DateTimeField(null=True, blank=True) paid_at = models.DateTimeField(null=True, blank=True) failed_at = models.DateTimeField(null=True, blank=True) refunded_at = models.DateTimeField(null=True, blank=True) voided_at = models.DateTimeField(null=True, blank=True) verified_at = models.DateTimeField(null=True, blank=True) status_updated_at = models.DateTimeField(null=True, blank=True) created_at = models.DateTimeField(auto_now_add=True) def __str__(self): return f"{self.provider} {self.amount} {self.currency}"