feat: refunds, failed and pending payments work

This commit is contained in:
2026-05-20 20:27:32 +02:00
parent 3338a1d3e7
commit ffc53f1b54
5 changed files with 636 additions and 59 deletions

View File

@@ -121,7 +121,10 @@ class CoursePage(Page):
return True
return CoursePurchase.objects.filter(
user=user, course=self, refunded=False
user=user,
course=self,
refunded=False,
status=CoursePurchase.Status.PAID,
).exists()
def _user_purchase_id(self, user):
@@ -129,7 +132,10 @@ class CoursePage(Page):
return None
purchase = CoursePurchase.objects.filter(
user=user, course=self, refunded=False
user=user,
course=self,
refunded=False,
status=CoursePurchase.Status.PAID,
).first()
print(f"User {user} purchase for course {self}: {purchase}")
return purchase.id if purchase else None
@@ -139,15 +145,19 @@ class CoursePage(Page):
if not user.is_authenticated:
return False
obj, created = CoursePurchase.objects.get_or_create(
user=user, course=self, refunded=False
user=user,
course=self,
refunded=False,
defaults={"status": CoursePurchase.Status.PAID},
)
if obj.status != CoursePurchase.Status.PAID or obj.refunded:
obj.status = CoursePurchase.Status.PAID
obj.refunded = False
obj.save(update_fields=["status", "refunded"])
# Add user to dedicated access group for this course
group_name = f"course_{self.id}_access"
group, _ = Group.objects.get_or_create(name=group_name)
user.groups.add(group)
# Ensure allowed_groups only includes this access group
if not self.allowed_groups.filter(id=group.id).exists():
self.allowed_groups.add(group)
return created
def save(self, *args, **kwargs):