HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //home/arjun/projects/unlimited-leads/Unlimited-Leads-Be/payment/models.py
from django.db import models
import uuid
from django.utils.timezone import now
from authorization.models import UnlimitedLeadUser


class Product(models.Model):
    BILLING_PERIOD_CHOICES = [
        ('day', 'Day'),
        ('month', 'Month'),
        ('year', 'Year')
    ]

    LIMIT_CHOICES = [
        ('Unlimited', 'Unlimited'),
        ('100', '100'),
        ('10000', '10000'),
    ]
    
    plan_name = models.CharField(max_length=255)
    amount = models.DecimalField(max_digits=10, decimal_places=2)
    description = models.TextField(blank=True) 
    currency = models.CharField(max_length=3) 
    product_id = models.CharField(default=uuid.uuid4, editable=False, unique=True)
    billing_period = models.CharField(
        max_length=10, 
        choices=BILLING_PERIOD_CHOICES, 
        blank=True, 
        null=True
    )
    is_active = models.BooleanField(default=True)
    is_free = models.BooleanField(default=False)
    limit = models.CharField(
            max_length=20,
            choices=LIMIT_CHOICES,
            default='0',
        )
    def __str__(self):
        return self.plan_name
    
class Transaction(models.Model):

    STATUS_CHOICES = [
        ('succeeded', 'Succeeded'),
        ('pending', 'Pending'),
        ('failed', 'Failed'),
    ]
    SUBSCRIPTION_STATUS_CHOICES = [
        ('active', 'Active'),
        ('cancelled', 'Cancelled'),
        ('past_due', 'Past Due'),
        ('unpaid', 'Unpaid'),
        ('incomplete', 'Incomplete'),
    ]

    customer = models.ForeignKey(UnlimitedLeadUser, on_delete=models.CASCADE)
    amount = models.DecimalField(max_digits=10, decimal_places=2)
    purpose = models.CharField(max_length=255) 
    payment_status = models.CharField(
        max_length=20,
        choices=STATUS_CHOICES,
        default='pending'
    )
    is_subscription = models.BooleanField(default=False)
    subscription_id = models.CharField(max_length=255, null=True, blank=True)  
    subscription_status = models.CharField(
        max_length=20, 
        choices=SUBSCRIPTION_STATUS_CHOICES, 
        null=True, 
        blank=True
    )
    subscription_start = models.DateTimeField(null=True, blank=True)
    subscription_end = models.DateTimeField(null=True, blank=True)
    invoice_id = models.CharField(max_length=255, null=True, blank=True)
    receipt_url = models.URLField(null=True, blank=True)
    product = models.ForeignKey(Product, null=True, blank=True, on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)
    transaction_id = models.CharField(max_length=255, null=True, blank=True) 
    transaction_date = models.DateTimeField(null=True, blank=True) 
    charge_id = models.CharField(max_length=255, null=True, blank=True)
    record_count = models.IntegerField(null=True, blank=True)
    is_cancelled = models.BooleanField(default=False)
    currency = models.CharField(max_length=3, default='usd') 
    cancelled_at = models.DateTimeField(null=True, blank=True)

    def __str__(self):
        return f"Transaction {self.id} - {self.customer.email} - {self.amount} {self.product.currency}"