from django.db import models from django.conf import settings from django_tidb.fields import BigAutoRandomField class BillingCreditOrder(models.Model): class Status(models.TextChoices): CREATED = "created" CANCELLED = "cancelled" PROCESSING = "processing" REQUIRES_ACTION = "requires_action" SUCCEEDED = "succeeded" FAILED = "failed" class ThirdPartyType(models.TextChoices): STRIPE_PAYMENT_INTENT = "stripe_payment_intent" id = BigAutoRandomField(primary_key=True) user = models.ForeignKey( settings.AUTH_USER_MODEL, on_delete=models.DO_NOTHING, ) quantity = models.IntegerField() discount = models.DecimalField(max_digits=4, decimal_places=2, default=0) # Three-letter [ISO currency code](https://www.iso.org/iso-4217-currency-codes.html), in lowercase. # Must be a [supported currency](https://stripe.com/docs/currencies). currency = models.CharField(max_length=3, default="usd") amount_total = models.DecimalField(max_digits=10, decimal_places=2) product_amount = models.DecimalField(max_digits=10, decimal_places=2) tax_amount = models.DecimalField(max_digits=10, decimal_places=2, default=0) third_party_id = models.CharField(max_length=255) third_party_type = models.CharField( max_length=32, choices=ThirdPartyType.choices, default=ThirdPartyType.STRIPE_PAYMENT_INTENT, ) status = models.CharField( max_length=32, choices=Status.choices, default=Status.CREATED ) status_details = models.JSONField(default=dict) created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) class Meta: db_table = "billing_credit_orders" def to_frontend(self) -> dict: return { # javascript cannot handle bigint # convert it to string "id": str(self.id), "quantity": self.quantity, "price_details": { "currency": self.currency, "amount_total": str(self.amount_total), "product_amount": str(self.product_amount), "tax_amount": str(self.tax_amount), "discount": self.discount, }, "status": self.status, "created_at": self.created_at, "updated_at": self.updated_at, }