many fixes

This commit is contained in:
IGNY8 VPS (Salman)
2025-12-06 14:31:42 +00:00
parent 4a16a6a402
commit c455a5ad83
21 changed files with 1497 additions and 242 deletions

View File

@@ -2,7 +2,7 @@
Billing API Views
Comprehensive billing endpoints for invoices, payments, credit packages
"""
from rest_framework import viewsets, status
from rest_framework import viewsets, status, serializers
from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated
@@ -10,7 +10,14 @@ from django.http import HttpResponse
from django.shortcuts import get_object_or_404
from django.db import models
from .models import Invoice, Payment, CreditPackage, PaymentMethodConfig, CreditTransaction
from .models import (
Invoice,
Payment,
CreditPackage,
PaymentMethodConfig,
CreditTransaction,
AccountPaymentMethod,
)
from .services.invoice_service import InvoiceService
from .services.payment_service import PaymentService
@@ -172,6 +179,69 @@ class PaymentViewSet(viewsets.ViewSet):
}, status=status.HTTP_201_CREATED)
class AccountPaymentMethodSerializer(serializers.ModelSerializer):
class Meta:
model = AccountPaymentMethod
fields = [
'id',
'type',
'display_name',
'is_default',
'is_enabled',
'is_verified',
'country_code',
'instructions',
'metadata',
'created_at',
'updated_at',
]
read_only_fields = ['id', 'is_verified', 'created_at', 'updated_at']
class AccountPaymentMethodViewSet(viewsets.ModelViewSet):
"""
CRUD for account-scoped payment methods (Stripe/PayPal/manual bank/local_wallet).
"""
serializer_class = AccountPaymentMethodSerializer
permission_classes = [IsAuthenticated]
def get_queryset(self):
account = getattr(self.request.user, 'account', None)
qs = AccountPaymentMethod.objects.all()
if account:
qs = qs.filter(account=account)
else:
qs = qs.none()
return qs.order_by('-is_default', 'display_name', 'id')
def perform_create(self, serializer):
account = self.request.user.account
with models.transaction.atomic():
obj = serializer.save(account=account)
make_default = serializer.validated_data.get('is_default') or not AccountPaymentMethod.objects.filter(account=account, is_default=True).exists()
if make_default:
AccountPaymentMethod.objects.filter(account=account).exclude(id=obj.id).update(is_default=False)
obj.is_default = True
obj.save(update_fields=['is_default'])
def perform_update(self, serializer):
account = self.request.user.account
with models.transaction.atomic():
obj = serializer.save()
if serializer.validated_data.get('is_default'):
AccountPaymentMethod.objects.filter(account=account).exclude(id=obj.id).update(is_default=False)
@action(detail=True, methods=['post'])
def set_default(self, request, pk=None):
account = request.user.account
method = get_object_or_404(AccountPaymentMethod, id=pk, account=account)
with models.transaction.atomic():
AccountPaymentMethod.objects.filter(account=account).update(is_default=False)
method.is_default = True
method.save(update_fields=['is_default'])
return Response({'message': 'Default payment method updated', 'id': method.id})
class CreditPackageViewSet(viewsets.ViewSet):
"""Credit package endpoints"""
permission_classes = [IsAuthenticated]
@@ -485,14 +555,15 @@ class AdminBillingViewSet(viewsets.ViewSet):
# Account stats
total_accounts = Account.objects.count()
active_accounts = Account.objects.filter(is_active=True).count()
active_accounts = Account.objects.filter(status='active').count()
new_accounts_this_month = Account.objects.filter(
created_at__gte=this_month_start
).count()
# Subscription stats
# Subscriptions are linked via OneToOne "subscription"
active_subscriptions = Account.objects.filter(
subscriptions__status='active'
subscription__status='active'
).distinct().count()
# Revenue stats
@@ -513,8 +584,8 @@ class AdminBillingViewSet(viewsets.ViewSet):
created_at__gte=last_30_days
).aggregate(total=Sum('amount'))['total'] or 0
# Usage transactions are stored as deductions (negative amounts)
credits_used = abs(CreditTransaction.objects.filter(
transaction_type__in=['generate_content', 'keyword_research', 'ai_task'],
created_at__gte=last_30_days,
amount__lt=0
).aggregate(total=Sum('amount'))['total'] or 0)
@@ -533,18 +604,20 @@ class AdminBillingViewSet(viewsets.ViewSet):
status__in=['completed', 'succeeded']
).order_by('-processed_at')[:5]
recent_activity = [
{
recent_activity = []
for pay in recent_payments:
account_name = getattr(pay.account, 'name', 'Unknown')
currency = pay.currency or 'USD'
ts = pay.processed_at.isoformat() if pay.processed_at else now.isoformat()
recent_activity.append({
'id': pay.id,
'type': 'payment',
'account_name': pay.account.name,
'account_name': account_name,
'amount': str(pay.amount),
'currency': pay.currency,
'timestamp': pay.processed_at.isoformat(),
'description': f'Payment received via {pay.payment_method}'
}
for pay in recent_payments
]
'currency': currency,
'timestamp': ts,
'description': f'Payment received via {pay.payment_method or "unknown"}'
})
return Response({
'total_accounts': total_accounts,