This commit is contained in:
IGNY8 VPS (Salman)
2025-12-08 07:47:01 +00:00
parent 42d04fb7f2
commit 40b7aced14
10 changed files with 172 additions and 44 deletions

View File

@@ -288,14 +288,27 @@ class RegisterSerializer(serializers.Serializer):
from igny8_core.business.billing.models import CreditTransaction
with transaction.atomic():
# ALWAYS assign the existing free plan for simple signup
# No fallbacks: if free plan is misconfigured, surface error immediately
try:
plan = Plan.objects.get(slug='free', is_active=True)
except Plan.DoesNotExist:
raise serializers.ValidationError({
"plan": "Free plan not configured. Please contact support."
})
plan_slug = validated_data.get('plan_slug')
paid_plans = ['starter', 'growth', 'scale']
if plan_slug and plan_slug in paid_plans:
try:
plan = Plan.objects.get(slug=plan_slug, is_active=True)
except Plan.DoesNotExist:
raise serializers.ValidationError({
"plan": f"Plan '{plan_slug}' not available. Please contact support."
})
account_status = 'pending_payment'
initial_credits = 0
else:
try:
plan = Plan.objects.get(slug='free', is_active=True)
except Plan.DoesNotExist:
raise serializers.ValidationError({
"plan": "Free plan not configured. Please contact support."
})
account_status = 'trial'
initial_credits = plan.get_effective_credits_per_month()
# Generate account name if not provided
account_name = validated_data.get('account_name')
@@ -338,32 +351,31 @@ class RegisterSerializer(serializers.Serializer):
slug = f"{base_slug}-{counter}"
counter += 1
# Get trial credits from plan
trial_credits = plan.get_effective_credits_per_month()
# Create account with trial status and credits seeded
# Create account with status and credits seeded (0 for paid pending)
account = Account.objects.create(
name=account_name,
slug=slug,
owner=user,
plan=plan,
credits=trial_credits, # CRITICAL: Seed initial credits
status='trial' # CRITICAL: Set as trial account
credits=initial_credits,
status=account_status,
payment_method=validated_data.get('payment_method') or 'bank_transfer',
)
# Log initial credit transaction for transparency
CreditTransaction.objects.create(
account=account,
transaction_type='subscription',
amount=trial_credits,
balance_after=trial_credits,
description=f'Free plan credits from {plan.name}',
metadata={
'plan_slug': plan.slug,
'registration': True,
'trial': True
}
)
# Log initial credit transaction only for free/trial accounts with credits
if initial_credits > 0:
CreditTransaction.objects.create(
account=account,
transaction_type='subscription',
amount=initial_credits,
balance_after=initial_credits,
description=f'Free plan credits from {plan.name}',
metadata={
'plan_slug': plan.slug,
'registration': True,
'trial': True
}
)
# Update user to reference the new account
user.account = account