Major refactor of commerce and Stripe integration

Refactored commerce models to support refunds, invoices, and improved carrier/payment logic. Added new serializers and viewsets for products, categories, images, discount codes, and refunds. Introduced Stripe client integration and removed legacy Stripe admin/model code. Updated Dockerfile for PDF generation dependencies. Removed obsolete migration files and updated configuration app initialization. Added invoice template and tasks for order cleanup.
This commit is contained in:
2025-11-18 01:00:03 +01:00
parent 7a715efeda
commit b8a1a594b2
35 changed files with 1215 additions and 332 deletions

View File

@@ -1,25 +1,68 @@
from django.db import models
from django.apps import apps
# Create your models here.
#TODO: logika a interakce bude na stripu (třeba aktualizovaní objednávky na zaplacenou apod.)
class StripePayment(models.Model):
STATUS_CHOICES = [
("pending", "Pending"),
("paid", "Paid"),
("failed", "Failed"),
("cancelled", "Cancelled"),
]
amount = models.DecimalField(max_digits=10, decimal_places=2)
currency = models.CharField(max_length=10, default="czk")
status = models.CharField(max_length=20, choices=STATUS_CHOICES, default="pending")
from .client import StripeClient
class StripeModel(models.Model):
class STATUS_CHOICES(models.TextChoices):
PENDING = "pending", "Čeká se na platbu"
PAID = "paid", "Zaplaceno"
FAILED = "failed", "Neúspěšné"
CANCELLED = "cancelled", "Zrušeno"
REFUNDING = "refunding", "Platba se vrací"
REFUNDED = "refunded", "Platba úspěšně vrácena"
status = models.CharField(max_length=20, choices=STATUS_CHOICES.choices, default=STATUS_CHOICES.PENDING)
stripe_session_id = models.CharField(max_length=255, blank=True, null=True)
stripe_payment_intent = models.CharField(max_length=255, blank=True, null=True)
stripe_session_url = models.URLField(blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True, null=True, blank=True)
def __str__(self):
return f"Order {self.id} - {self.status}"
return f"Order {self.id} - {self.status}"
def save(self, *args, **kwargs):
#if new
if self.pk:
Order = apps.get_model('commerce', 'Order')
Payment = apps.get_model('commerce', 'Payment')
order = Order.objects.get(payment=Payment.objects.get(stripe=self))
session = StripeClient.create_checkout_session(order)# <-- předáme self.StripePayment
self.stripe_session_id = session.id
self.stripe_payment_intent = session.payment_intent
self.stripe_session_url = session.url
else:
self.updated_at = models.DateTimeField(auto_now=True)
super().save(*args, **kwargs)
def paid(self):
self.status = self.STATUS_CHOICES.PAID
self.save()
def refund(self):
StripeClient.refund_order(self.stripe_payment_intent)
self.status = self.STATUS_CHOICES.REFUNDING
self.save()
def refund_confirmed(self):
self.status = self.STATUS_CHOICES.REFUNDED
self.save()
def cancel(self):
StripeClient.cancel_checkout_session(self.stripe_session_id)
self.status = self.STATUS_CHOICES.CANCELLED
self.save()