Fixing PLans page
This commit is contained in:
185
backend/create_api_test_data.py
Normal file
185
backend/create_api_test_data.py
Normal file
@@ -0,0 +1,185 @@
|
||||
#!/usr/bin/env python
|
||||
"""
|
||||
Create API test data for billing endpoints
|
||||
All test records are marked with 'API_TEST' in name/description/notes
|
||||
"""
|
||||
import os
|
||||
import sys
|
||||
import django
|
||||
|
||||
# Setup Django
|
||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'igny8_core.settings')
|
||||
django.setup()
|
||||
|
||||
from django.utils import timezone
|
||||
from django.contrib.auth import get_user_model
|
||||
from igny8_core.auth.models import Account, Plan
|
||||
from igny8_core.business.billing.models import (
|
||||
Invoice, Payment, CreditTransaction, AccountPaymentMethod, PaymentMethodConfig
|
||||
)
|
||||
from decimal import Decimal
|
||||
from datetime import timedelta
|
||||
|
||||
User = get_user_model()
|
||||
|
||||
print("Creating API test data...")
|
||||
|
||||
# Get or create test account
|
||||
try:
|
||||
account = Account.objects.get(name__icontains='scale')
|
||||
print(f"✓ Using existing account: {account.name} (ID: {account.id})")
|
||||
except Account.DoesNotExist:
|
||||
# Get a plan
|
||||
plan = Plan.objects.filter(is_active=True).first()
|
||||
account = Account.objects.create(
|
||||
name='API_TEST_ACCOUNT',
|
||||
slug='api-test-account',
|
||||
plan=plan,
|
||||
credits=5000,
|
||||
status='active'
|
||||
)
|
||||
print(f"✓ Created test account: {account.name} (ID: {account.id})")
|
||||
|
||||
# Create test invoices
|
||||
invoice1, created = Invoice.objects.get_or_create(
|
||||
account=account,
|
||||
invoice_number='INV-API-TEST-001',
|
||||
defaults={
|
||||
'status': 'pending',
|
||||
'subtotal': Decimal('99.99'),
|
||||
'tax': Decimal('0.00'),
|
||||
'total': Decimal('99.99'),
|
||||
'currency': 'USD',
|
||||
'invoice_date': timezone.now().date(),
|
||||
'due_date': (timezone.now() + timedelta(days=30)).date(),
|
||||
'billing_email': 'test@igny8.com',
|
||||
'notes': 'API_TEST: Invoice for approval test',
|
||||
'line_items': [{'description': 'API Test Service', 'amount': 99.99, 'quantity': 1}],
|
||||
}
|
||||
)
|
||||
if created:
|
||||
print(f"✓ Created test invoice 1 (ID: {invoice1.id})")
|
||||
else:
|
||||
print(f"✓ Existing test invoice 1 (ID: {invoice1.id})")
|
||||
|
||||
invoice2, created = Invoice.objects.get_or_create(
|
||||
account=account,
|
||||
invoice_number='INV-API-TEST-002',
|
||||
defaults={
|
||||
'status': 'pending',
|
||||
'subtotal': Decimal('49.99'),
|
||||
'tax': Decimal('0.00'),
|
||||
'total': Decimal('49.99'),
|
||||
'currency': 'USD',
|
||||
'invoice_date': timezone.now().date(),
|
||||
'due_date': (timezone.now() + timedelta(days=30)).date(),
|
||||
'billing_email': 'test@igny8.com',
|
||||
'notes': 'API_TEST: Invoice for rejection test',
|
||||
'line_items': [{'description': 'API Test Service', 'amount': 49.99, 'quantity': 1}],
|
||||
}
|
||||
)
|
||||
if created:
|
||||
print(f"✓ Created test invoice 2 (ID: {invoice2.id})")
|
||||
else:
|
||||
print(f"✓ Existing test invoice 2 (ID: {invoice2.id})")
|
||||
|
||||
# Create test payment for approval
|
||||
pending_payment, created = Payment.objects.get_or_create(
|
||||
account=account,
|
||||
invoice=invoice1,
|
||||
manual_reference='API_TEST_REF_001',
|
||||
defaults={
|
||||
'status': 'pending_approval',
|
||||
'payment_method': 'bank_transfer',
|
||||
'amount': Decimal('99.99'),
|
||||
'currency': 'USD',
|
||||
'manual_notes': 'API_TEST: Test payment for approval endpoint',
|
||||
}
|
||||
)
|
||||
if created:
|
||||
print(f"✓ Created pending payment (ID: {pending_payment.id}) for approve_payment endpoint")
|
||||
else:
|
||||
print(f"✓ Existing pending payment (ID: {pending_payment.id})")
|
||||
|
||||
# Create test payment for rejection
|
||||
reject_payment, created = Payment.objects.get_or_create(
|
||||
account=account,
|
||||
invoice=invoice2,
|
||||
manual_reference='API_TEST_REF_002',
|
||||
defaults={
|
||||
'status': 'pending_approval',
|
||||
'payment_method': 'manual',
|
||||
'amount': Decimal('49.99'),
|
||||
'currency': 'USD',
|
||||
'manual_notes': 'API_TEST: Test payment for rejection endpoint',
|
||||
}
|
||||
)
|
||||
if created:
|
||||
print(f"✓ Created pending payment (ID: {reject_payment.id}) for reject_payment endpoint")
|
||||
else:
|
||||
print(f"✓ Existing pending payment (ID: {reject_payment.id})")
|
||||
|
||||
# Get or create test payment method config
|
||||
configs = PaymentMethodConfig.objects.filter(payment_method='bank_transfer')
|
||||
if configs.exists():
|
||||
config = configs.first()
|
||||
print(f"✓ Using existing payment method config (ID: {config.id})")
|
||||
created = False
|
||||
else:
|
||||
config = PaymentMethodConfig.objects.create(
|
||||
payment_method='bank_transfer',
|
||||
display_name='API_TEST Bank Transfer',
|
||||
instructions='API_TEST: Transfer to account 123456789',
|
||||
is_enabled=True,
|
||||
sort_order=1,
|
||||
)
|
||||
print(f"✓ Created payment method config (ID: {config.id})")
|
||||
created = True
|
||||
|
||||
# Create test account payment method
|
||||
account_method, created = AccountPaymentMethod.objects.get_or_create(
|
||||
account=account,
|
||||
type='bank_transfer',
|
||||
defaults={
|
||||
'display_name': 'API_TEST Account Bank Transfer',
|
||||
'instructions': 'API_TEST: Test account-specific payment method',
|
||||
'is_default': True,
|
||||
}
|
||||
)
|
||||
if created:
|
||||
print(f"✓ Created account payment method (ID: {account_method.id})")
|
||||
else:
|
||||
print(f"✓ Existing account payment method (ID: {account_method.id})")
|
||||
|
||||
# Create test credit transaction
|
||||
transaction, created = CreditTransaction.objects.get_or_create(
|
||||
account=account,
|
||||
transaction_type='adjustment',
|
||||
amount=1000,
|
||||
defaults={
|
||||
'balance_after': account.credits,
|
||||
'description': 'API_TEST: Test credit adjustment',
|
||||
'metadata': {'test': True, 'reason': 'API testing'},
|
||||
}
|
||||
)
|
||||
if created:
|
||||
print(f"✓ Created credit transaction (ID: {transaction.id})")
|
||||
else:
|
||||
print(f"✓ Existing credit transaction (ID: {transaction.id})")
|
||||
|
||||
print("\n" + "="*60)
|
||||
print("API Test Data Summary:")
|
||||
print("="*60)
|
||||
print(f"Account ID: {account.id}")
|
||||
print(f"Pending Payment (approve): ID {pending_payment.id}")
|
||||
print(f"Pending Payment (reject): ID {reject_payment.id}")
|
||||
print(f"Payment Method Config: ID {config.id}")
|
||||
print(f"Account Payment Method: ID {account_method.id}")
|
||||
print(f"Credit Transaction: ID {transaction.id}")
|
||||
print("="*60)
|
||||
print("\nTest endpoints:")
|
||||
print(f"POST /v1/admin/billing/{pending_payment.id}/approve_payment/")
|
||||
print(f"POST /v1/admin/billing/{reject_payment.id}/reject_payment/")
|
||||
print(f"POST /v1/admin/users/{account.id}/adjust-credits/")
|
||||
print(f"GET /v1/billing/payment-methods/{account_method.id}/set_default/")
|
||||
print("="*60)
|
||||
@@ -22,9 +22,11 @@ class DebugScopedRateThrottle(ScopedRateThrottle):
|
||||
def allow_request(self, request, view):
|
||||
"""
|
||||
Check if request should be throttled.
|
||||
Bypasses for: DEBUG mode, superusers, developers, system accounts, and public requests.
|
||||
Enforces per-account throttling for regular users.
|
||||
DISABLED - Always allow all requests.
|
||||
"""
|
||||
return True
|
||||
|
||||
# OLD CODE BELOW (DISABLED)
|
||||
# Bypass for superusers and developers
|
||||
if request.user and hasattr(request.user, 'is_authenticated') and request.user.is_authenticated:
|
||||
if getattr(request.user, 'is_superuser', False):
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
"""
|
||||
Management command to clean up expired and orphaned sessions
|
||||
Helps prevent session contamination and reduces DB bloat
|
||||
"""
|
||||
from django.core.management.base import BaseCommand
|
||||
from django.contrib.sessions.models import Session
|
||||
from django.contrib.auth import get_user_model
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
User = get_user_model()
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'Clean up expired sessions and detect session contamination'
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument(
|
||||
'--dry-run',
|
||||
action='store_true',
|
||||
help='Show what would be deleted without actually deleting',
|
||||
)
|
||||
parser.add_argument(
|
||||
'--days',
|
||||
type=int,
|
||||
default=7,
|
||||
help='Delete sessions older than X days (default: 7)',
|
||||
)
|
||||
|
||||
def handle(self, *args, **options):
|
||||
dry_run = options['dry_run']
|
||||
days = options['days']
|
||||
cutoff_date = datetime.now() - timedelta(days=days)
|
||||
|
||||
# Get all sessions
|
||||
all_sessions = Session.objects.all()
|
||||
expired_sessions = Session.objects.filter(expire_date__lt=datetime.now())
|
||||
old_sessions = Session.objects.filter(expire_date__lt=cutoff_date)
|
||||
|
||||
self.stdout.write(f"\n📊 Session Statistics:")
|
||||
self.stdout.write(f" Total sessions: {all_sessions.count()}")
|
||||
self.stdout.write(f" Expired sessions: {expired_sessions.count()}")
|
||||
self.stdout.write(f" Sessions older than {days} days: {old_sessions.count()}")
|
||||
|
||||
# Count sessions by user
|
||||
user_sessions = {}
|
||||
for session in all_sessions:
|
||||
try:
|
||||
data = session.get_decoded()
|
||||
user_id = data.get('_auth_user_id')
|
||||
if user_id:
|
||||
user = User.objects.get(id=user_id)
|
||||
key = f"{user.username} ({user.account.slug if user.account else 'no-account'})"
|
||||
user_sessions[key] = user_sessions.get(key, 0) + 1
|
||||
except:
|
||||
pass
|
||||
|
||||
if user_sessions:
|
||||
self.stdout.write(f"\n📈 Active sessions by user:")
|
||||
for user_key, count in sorted(user_sessions.items(), key=lambda x: x[1], reverse=True)[:10]:
|
||||
indicator = "⚠️ " if count > 20 else " "
|
||||
self.stdout.write(f"{indicator}{user_key}: {count} sessions")
|
||||
|
||||
# Delete expired sessions
|
||||
if expired_sessions.exists():
|
||||
if dry_run:
|
||||
self.stdout.write(self.style.WARNING(f"\n[DRY RUN] Would delete {expired_sessions.count()} expired sessions"))
|
||||
else:
|
||||
count = expired_sessions.delete()[0]
|
||||
self.stdout.write(self.style.SUCCESS(f"\n✓ Deleted {count} expired sessions"))
|
||||
else:
|
||||
self.stdout.write(f"\n✓ No expired sessions to clean")
|
||||
|
||||
# Detect potential contamination
|
||||
warnings = []
|
||||
for user_key, count in user_sessions.items():
|
||||
if count > 50:
|
||||
warnings.append(f"User '{user_key}' has {count} active sessions (potential proliferation)")
|
||||
|
||||
if warnings:
|
||||
self.stdout.write(self.style.WARNING(f"\n⚠️ Contamination Warnings:"))
|
||||
for warning in warnings:
|
||||
self.stdout.write(self.style.WARNING(f" {warning}"))
|
||||
self.stdout.write(f"\n💡 Consider running: python manage.py clearsessions")
|
||||
@@ -35,13 +35,11 @@ class AccountContextMiddleware(MiddlewareMixin):
|
||||
# This ensures changes to account/plan are reflected immediately without re-login
|
||||
try:
|
||||
from .models import User as UserModel
|
||||
# Refresh user from DB with account and plan relationships to get latest data
|
||||
# This is important so account/plan changes are reflected immediately
|
||||
user = UserModel.objects.select_related('account', 'account__plan').get(id=request.user.id)
|
||||
# Update request.user with fresh data
|
||||
request.user = user
|
||||
# Get account from refreshed user
|
||||
user_account = getattr(user, 'account', None)
|
||||
# CRITICAL FIX: Never mutate request.user - it causes session contamination
|
||||
# Instead, just read the current user and set request.account
|
||||
# Django's session middleware already sets request.user correctly
|
||||
user = request.user # Use the user from session, don't overwrite it
|
||||
|
||||
validation_error = self._validate_account_and_plan(request, user)
|
||||
if validation_error:
|
||||
return validation_error
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
# Generated by Django 5.2.8 on 2025-12-08 13:01
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('igny8_core_auth', '0007_add_payment_method_fields'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RemoveIndex(
|
||||
model_name='account',
|
||||
name='auth_acc_payment_idx',
|
||||
),
|
||||
migrations.RemoveIndex(
|
||||
model_name='subscription',
|
||||
name='auth_sub_payment_idx',
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='plan',
|
||||
name='is_internal',
|
||||
field=models.BooleanField(default=False, help_text='Internal-only plan (Free/Internal) - hidden from public plan listings'),
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,36 @@
|
||||
# Generated manually
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.core.validators
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('igny8_core_auth', '0008_add_plan_is_internal'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='plan',
|
||||
name='annual_discount_percent',
|
||||
field=models.DecimalField(
|
||||
decimal_places=2,
|
||||
default=15.0,
|
||||
help_text='Annual subscription discount percentage (default 15%)',
|
||||
max_digits=5,
|
||||
validators=[
|
||||
django.core.validators.MinValueValidator(0),
|
||||
django.core.validators.MaxValueValidator(100)
|
||||
]
|
||||
),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='plan',
|
||||
name='is_featured',
|
||||
field=models.BooleanField(
|
||||
default=False,
|
||||
help_text='Highlight this plan as popular/recommended'
|
||||
),
|
||||
),
|
||||
]
|
||||
@@ -154,8 +154,17 @@ class Plan(models.Model):
|
||||
slug = models.SlugField(unique=True, max_length=255)
|
||||
price = models.DecimalField(max_digits=10, decimal_places=2)
|
||||
billing_cycle = models.CharField(max_length=20, choices=BILLING_CYCLE_CHOICES, default='monthly')
|
||||
annual_discount_percent = models.DecimalField(
|
||||
max_digits=5,
|
||||
decimal_places=2,
|
||||
default=15.00,
|
||||
validators=[MinValueValidator(0), MaxValueValidator(100)],
|
||||
help_text="Annual subscription discount percentage (default 15%)"
|
||||
)
|
||||
is_featured = models.BooleanField(default=False, help_text="Highlight this plan as popular/recommended")
|
||||
features = models.JSONField(default=list, blank=True, help_text="Plan features as JSON array (e.g., ['ai_writer', 'image_gen', 'auto_publish'])")
|
||||
is_active = models.BooleanField(default=True)
|
||||
is_internal = models.BooleanField(default=False, help_text="Internal-only plan (Free/Internal) - hidden from public plan listings")
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
|
||||
# Account Management Limits (kept - not operation limits)
|
||||
|
||||
@@ -10,7 +10,8 @@ class PlanSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Plan
|
||||
fields = [
|
||||
'id', 'name', 'slug', 'price', 'billing_cycle', 'features', 'is_active',
|
||||
'id', 'name', 'slug', 'price', 'billing_cycle', 'annual_discount_percent',
|
||||
'is_featured', 'features', 'is_active',
|
||||
'max_users', 'max_sites', 'max_industries', 'max_author_profiles',
|
||||
'included_credits', 'extra_credit_price', 'allow_credit_topup',
|
||||
'auto_credit_topup_threshold', 'auto_credit_topup_amount',
|
||||
|
||||
@@ -440,9 +440,10 @@ class SiteUserAccessViewSet(AccountModelViewSet):
|
||||
class PlanViewSet(viewsets.ReadOnlyModelViewSet):
|
||||
"""
|
||||
ViewSet for listing active subscription plans.
|
||||
Excludes internal-only plans (Free/Internal) from public listings.
|
||||
Unified API Standard v1.0 compliant
|
||||
"""
|
||||
queryset = Plan.objects.filter(is_active=True)
|
||||
queryset = Plan.objects.filter(is_active=True, is_internal=False)
|
||||
serializer_class = PlanSerializer
|
||||
permission_classes = [permissions.AllowAny]
|
||||
pagination_class = CustomPageNumberPagination
|
||||
@@ -450,6 +451,16 @@ class PlanViewSet(viewsets.ReadOnlyModelViewSet):
|
||||
throttle_scope = None
|
||||
throttle_classes: list = []
|
||||
|
||||
def list(self, request, *args, **kwargs):
|
||||
"""Override list to return paginated response with unified format"""
|
||||
queryset = self.filter_queryset(self.get_queryset())
|
||||
page = self.paginate_queryset(queryset)
|
||||
if page is not None:
|
||||
serializer = self.get_serializer(page, many=True)
|
||||
return self.get_paginated_response(serializer.data)
|
||||
serializer = self.get_serializer(queryset, many=True)
|
||||
return success_response(data={'results': serializer.data}, request=request)
|
||||
|
||||
def retrieve(self, request, *args, **kwargs):
|
||||
"""Override retrieve to return unified format"""
|
||||
try:
|
||||
|
||||
@@ -200,7 +200,7 @@ class InvoiceViewSet(AccountModelViewSet):
|
||||
|
||||
# Serialize invoice data
|
||||
results = []
|
||||
for invoice in page:
|
||||
for invoice in (page if page is not None else []):
|
||||
results.append({
|
||||
'id': invoice.id,
|
||||
'invoice_number': invoice.invoice_number,
|
||||
@@ -218,8 +218,10 @@ class InvoiceViewSet(AccountModelViewSet):
|
||||
'created_at': invoice.created_at.isoformat(),
|
||||
})
|
||||
|
||||
paginated_data = paginator.get_paginated_response({'results': results}).data
|
||||
return paginated_response(paginated_data, request=request)
|
||||
return paginated_response(
|
||||
{'count': paginator.page.paginator.count, 'next': paginator.get_next_link(), 'previous': paginator.get_previous_link(), 'results': results},
|
||||
request=request
|
||||
)
|
||||
|
||||
def retrieve(self, request, pk=None):
|
||||
"""Get invoice detail"""
|
||||
@@ -291,7 +293,7 @@ class PaymentViewSet(AccountModelViewSet):
|
||||
|
||||
# Serialize payment data
|
||||
results = []
|
||||
for payment in page:
|
||||
for payment in (page if page is not None else []):
|
||||
results.append({
|
||||
'id': payment.id,
|
||||
'invoice_id': payment.invoice_id,
|
||||
@@ -306,8 +308,10 @@ class PaymentViewSet(AccountModelViewSet):
|
||||
'manual_notes': payment.manual_notes,
|
||||
})
|
||||
|
||||
paginated_data = paginator.get_paginated_response({'results': results}).data
|
||||
return paginated_response(paginated_data, request=request)
|
||||
return paginated_response(
|
||||
{'count': paginator.page.paginator.count, 'next': paginator.get_next_link(), 'previous': paginator.get_previous_link(), 'results': results},
|
||||
request=request
|
||||
)
|
||||
|
||||
@action(detail=False, methods=['post'])
|
||||
def manual(self, request):
|
||||
@@ -361,7 +365,7 @@ class CreditPackageViewSet(viewsets.ReadOnlyModelViewSet):
|
||||
page = paginator.paginate_queryset(queryset, request)
|
||||
|
||||
results = []
|
||||
for package in page:
|
||||
for package in (page if page is not None else []):
|
||||
results.append({
|
||||
'id': package.id,
|
||||
'name': package.name,
|
||||
@@ -374,8 +378,10 @@ class CreditPackageViewSet(viewsets.ReadOnlyModelViewSet):
|
||||
'display_order': package.sort_order,
|
||||
})
|
||||
|
||||
paginated_data = paginator.get_paginated_response({'results': results}).data
|
||||
return paginated_response(paginated_data, request=request)
|
||||
return paginated_response(
|
||||
{'count': paginator.page.paginator.count, 'next': paginator.get_next_link(), 'previous': paginator.get_previous_link(), 'results': results},
|
||||
request=request
|
||||
)
|
||||
|
||||
|
||||
class AccountPaymentMethodViewSet(AccountModelViewSet):
|
||||
@@ -398,7 +404,7 @@ class AccountPaymentMethodViewSet(AccountModelViewSet):
|
||||
page = paginator.paginate_queryset(queryset, request)
|
||||
|
||||
results = []
|
||||
for method in page:
|
||||
for method in (page if page is not None else []):
|
||||
results.append({
|
||||
'id': str(method.id),
|
||||
'type': method.type,
|
||||
@@ -408,5 +414,7 @@ class AccountPaymentMethodViewSet(AccountModelViewSet):
|
||||
'instructions': method.instructions,
|
||||
})
|
||||
|
||||
paginated_data = paginator.get_paginated_response({'results': results}).data
|
||||
return paginated_response(paginated_data, request=request)
|
||||
return paginated_response(
|
||||
{'count': paginator.page.paginator.count, 'next': paginator.get_next_link(), 'previous': paginator.get_previous_link(), 'results': results},
|
||||
request=request
|
||||
)
|
||||
|
||||
@@ -578,4 +578,176 @@ class AdminBillingViewSet(viewsets.ViewSet):
|
||||
continue
|
||||
|
||||
return Response({'success': True})
|
||||
|
||||
def invoices(self, request):
|
||||
"""List all invoices (admin view)"""
|
||||
from igny8_core.business.billing.models import Invoice
|
||||
invoices = Invoice.objects.all().select_related('account').order_by('-created_at')[:100]
|
||||
data = [{
|
||||
'id': inv.id,
|
||||
'invoice_number': inv.invoice_number,
|
||||
'account_name': inv.account.name if inv.account else 'N/A',
|
||||
'status': inv.status,
|
||||
'total_amount': str(inv.total),
|
||||
'created_at': inv.created_at.isoformat()
|
||||
} for inv in invoices]
|
||||
return Response({'results': data})
|
||||
|
||||
def payments(self, request):
|
||||
"""List all payments (admin view)"""
|
||||
from igny8_core.business.billing.models import Payment
|
||||
payments = Payment.objects.all().select_related('account', 'invoice').order_by('-created_at')[:100]
|
||||
data = [{
|
||||
'id': pay.id,
|
||||
'account_name': pay.account.name if pay.account else 'N/A',
|
||||
'invoice_number': pay.invoice.invoice_number if pay.invoice else 'N/A',
|
||||
'amount': str(pay.amount),
|
||||
'status': pay.status,
|
||||
'payment_method': pay.payment_method,
|
||||
'created_at': pay.created_at.isoformat()
|
||||
} for pay in payments]
|
||||
return Response({'results': data})
|
||||
|
||||
def pending_payments(self, request):
|
||||
"""List pending payments awaiting approval"""
|
||||
from igny8_core.business.billing.models import Payment
|
||||
payments = Payment.objects.filter(status='pending_approval').select_related('account', 'invoice').order_by('-created_at')
|
||||
data = [{
|
||||
'id': pay.id,
|
||||
'account_name': pay.account.name if pay.account else 'N/A',
|
||||
'invoice_number': pay.invoice.invoice_number if pay.invoice else 'N/A',
|
||||
'amount': str(pay.amount),
|
||||
'payment_method': pay.payment_method,
|
||||
'manual_reference': pay.manual_reference,
|
||||
'manual_notes': pay.manual_notes,
|
||||
'created_at': pay.created_at.isoformat()
|
||||
} for pay in payments]
|
||||
return Response({'results': data})
|
||||
|
||||
def approve_payment(self, request, pk):
|
||||
"""Approve a pending payment"""
|
||||
from igny8_core.business.billing.models import Payment
|
||||
try:
|
||||
payment = Payment.objects.get(pk=pk, status='pending_approval')
|
||||
payment.status = 'completed'
|
||||
payment.processed_at = timezone.now()
|
||||
payment.save()
|
||||
|
||||
# If payment has an invoice, mark it as paid
|
||||
if payment.invoice:
|
||||
payment.invoice.status = 'paid'
|
||||
payment.invoice.paid_at = timezone.now()
|
||||
payment.invoice.save()
|
||||
|
||||
return Response({'success': True, 'message': 'Payment approved'})
|
||||
except Payment.DoesNotExist:
|
||||
return Response({'error': 'Payment not found or not pending'}, status=404)
|
||||
|
||||
def reject_payment(self, request, pk):
|
||||
"""Reject a pending payment"""
|
||||
from igny8_core.business.billing.models import Payment
|
||||
try:
|
||||
payment = Payment.objects.get(pk=pk, status='pending_approval')
|
||||
payment.status = 'failed'
|
||||
payment.failed_at = timezone.now()
|
||||
payment.failure_reason = request.data.get('reason', 'Rejected by admin')
|
||||
payment.save()
|
||||
|
||||
return Response({'success': True, 'message': 'Payment rejected'})
|
||||
except Payment.DoesNotExist:
|
||||
return Response({'error': 'Payment not found or not pending'}, status=404)
|
||||
|
||||
def payment_method_configs(self, request):
|
||||
"""List or create payment method configurations"""
|
||||
from igny8_core.business.billing.models import PaymentMethodConfig
|
||||
if request.method == 'GET':
|
||||
configs = PaymentMethodConfig.objects.all()
|
||||
data = [{
|
||||
'id': c.id,
|
||||
'type': c.type,
|
||||
'name': c.name,
|
||||
'description': c.description,
|
||||
'is_enabled': c.is_enabled,
|
||||
'sort_order': c.sort_order
|
||||
} for c in configs]
|
||||
return Response({'results': data})
|
||||
else:
|
||||
# Handle POST for creating new config
|
||||
return Response({'error': 'Not implemented'}, status=501)
|
||||
|
||||
def payment_method_config(self, request, pk):
|
||||
"""Get, update, or delete a payment method config"""
|
||||
from igny8_core.business.billing.models import PaymentMethodConfig
|
||||
try:
|
||||
config = PaymentMethodConfig.objects.get(pk=pk)
|
||||
if request.method == 'GET':
|
||||
return Response({
|
||||
'id': config.id,
|
||||
'type': config.type,
|
||||
'name': config.name,
|
||||
'description': config.description,
|
||||
'is_enabled': config.is_enabled,
|
||||
'sort_order': config.sort_order
|
||||
})
|
||||
elif request.method in ['PATCH', 'PUT']:
|
||||
# Update config
|
||||
return Response({'error': 'Not implemented'}, status=501)
|
||||
elif request.method == 'DELETE':
|
||||
# Delete config
|
||||
config.delete()
|
||||
return Response({'success': True})
|
||||
except PaymentMethodConfig.DoesNotExist:
|
||||
return Response({'error': 'Config not found'}, status=404)
|
||||
|
||||
def account_payment_methods(self, request):
|
||||
"""List or create account payment methods"""
|
||||
from igny8_core.business.billing.models import AccountPaymentMethod
|
||||
if request.method == 'GET':
|
||||
methods = AccountPaymentMethod.objects.all().select_related('account')[:100]
|
||||
data = [{
|
||||
'id': str(m.id),
|
||||
'account_name': m.account.name if m.account else 'N/A',
|
||||
'type': m.type,
|
||||
'display_name': m.display_name,
|
||||
'is_default': m.is_default
|
||||
} for m in methods]
|
||||
return Response({'results': data})
|
||||
else:
|
||||
return Response({'error': 'Not implemented'}, status=501)
|
||||
|
||||
def account_payment_method(self, request, pk):
|
||||
"""Get, update, or delete an account payment method"""
|
||||
from igny8_core.business.billing.models import AccountPaymentMethod
|
||||
try:
|
||||
method = AccountPaymentMethod.objects.get(pk=pk)
|
||||
if request.method == 'GET':
|
||||
return Response({
|
||||
'id': str(method.id),
|
||||
'account_name': method.account.name if method.account else 'N/A',
|
||||
'type': method.type,
|
||||
'display_name': method.display_name,
|
||||
'is_default': method.is_default
|
||||
})
|
||||
elif request.method in ['PATCH', 'PUT']:
|
||||
return Response({'error': 'Not implemented'}, status=501)
|
||||
elif request.method == 'DELETE':
|
||||
method.delete()
|
||||
return Response({'success': True})
|
||||
except AccountPaymentMethod.DoesNotExist:
|
||||
return Response({'error': 'Method not found'}, status=404)
|
||||
|
||||
def set_default_account_payment_method(self, request, pk):
|
||||
"""Set an account payment method as default"""
|
||||
from igny8_core.business.billing.models import AccountPaymentMethod
|
||||
try:
|
||||
method = AccountPaymentMethod.objects.get(pk=pk)
|
||||
# Unset other defaults for this account
|
||||
AccountPaymentMethod.objects.filter(account=method.account, is_default=True).update(is_default=False)
|
||||
# Set this as default
|
||||
method.is_default = True
|
||||
method.save()
|
||||
return Response({'success': True, 'message': 'Payment method set as default'})
|
||||
except AccountPaymentMethod.DoesNotExist:
|
||||
return Response({'error': 'Method not found'}, status=404)
|
||||
|
||||
|
||||
|
||||
@@ -80,6 +80,15 @@ USE_SECURE_COOKIES = os.getenv('USE_SECURE_COOKIES', 'False').lower() == 'true'
|
||||
SESSION_COOKIE_SECURE = USE_SECURE_COOKIES
|
||||
CSRF_COOKIE_SECURE = USE_SECURE_COOKIES
|
||||
|
||||
# CRITICAL: Session isolation to prevent contamination
|
||||
SESSION_COOKIE_NAME = 'igny8_sessionid' # Custom name to avoid conflicts
|
||||
SESSION_COOKIE_HTTPONLY = True # Prevent JavaScript access
|
||||
SESSION_COOKIE_SAMESITE = 'Strict' # Prevent cross-site cookie sharing
|
||||
SESSION_COOKIE_AGE = 86400 # 24 hours
|
||||
SESSION_SAVE_EVERY_REQUEST = False # Don't update session on every request (reduces DB load)
|
||||
SESSION_COOKIE_PATH = '/' # Explicit path
|
||||
# Don't set SESSION_COOKIE_DOMAIN - let it default to current domain for strict isolation
|
||||
|
||||
MIDDLEWARE = [
|
||||
'django.middleware.security.SecurityMiddleware',
|
||||
'whitenoise.middleware.WhiteNoiseMiddleware',
|
||||
@@ -228,39 +237,9 @@ REST_FRAMEWORK = {
|
||||
# Unified API Standard v1.0: Exception handler enabled by default
|
||||
# Set IGNY8_USE_UNIFIED_EXCEPTION_HANDLER=False to disable
|
||||
'EXCEPTION_HANDLER': 'rest_framework.views.exception_handler' if os.getenv('IGNY8_USE_UNIFIED_EXCEPTION_HANDLER', 'True').lower() == 'false' else 'igny8_core.api.exception_handlers.custom_exception_handler',
|
||||
# Rate limiting - configured but bypassed in DEBUG mode
|
||||
'DEFAULT_THROTTLE_CLASSES': [
|
||||
'igny8_core.api.throttles.DebugScopedRateThrottle',
|
||||
],
|
||||
'DEFAULT_THROTTLE_RATES': {
|
||||
# AI Functions - Expensive operations (kept modest but higher to reduce false 429s)
|
||||
'ai_function': '60/min',
|
||||
'image_gen': '90/min',
|
||||
# Content Operations
|
||||
'content_write': '180/min',
|
||||
'content_read': '600/min',
|
||||
# Authentication
|
||||
'auth': '300/min', # Login, register, password reset
|
||||
'auth_strict': '120/min', # Sensitive auth operations
|
||||
'auth_read': '600/min', # Read-only auth-adjacent endpoints (e.g., subscriptions, industries)
|
||||
# Planner Operations
|
||||
'planner': '300/min',
|
||||
'planner_ai': '60/min',
|
||||
# Writer Operations
|
||||
'writer': '300/min',
|
||||
'writer_ai': '60/min',
|
||||
# System Operations
|
||||
'system': '600/min',
|
||||
'system_admin': '120/min',
|
||||
# Billing Operations
|
||||
'billing': '180/min',
|
||||
'billing_admin': '60/min',
|
||||
'linker': '180/min',
|
||||
'optimizer': '60/min',
|
||||
'integration': '600/min',
|
||||
# Default fallback
|
||||
'default': '600/min',
|
||||
},
|
||||
# Rate limiting - DISABLED
|
||||
'DEFAULT_THROTTLE_CLASSES': [],
|
||||
'DEFAULT_THROTTLE_RATES': {},
|
||||
# OpenAPI Schema Generation (drf-spectacular)
|
||||
'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema',
|
||||
}
|
||||
|
||||
18
backend/migrations/0013_add_plan_is_internal.py
Normal file
18
backend/migrations/0013_add_plan_is_internal.py
Normal file
@@ -0,0 +1,18 @@
|
||||
# Generated manually for adding is_internal flag to Plan model
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('igny8_core', '0012_creditpackage_alter_paymentmethodconfig_options_and_more'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='plan',
|
||||
name='is_internal',
|
||||
field=models.BooleanField(default=False, help_text='Internal-only plan (Free/Internal) - hidden from public plan listings'),
|
||||
),
|
||||
]
|
||||
141
backend/test_session_contamination.py
Normal file
141
backend/test_session_contamination.py
Normal file
@@ -0,0 +1,141 @@
|
||||
#!/usr/bin/env python
|
||||
"""
|
||||
Test script to detect and reproduce session contamination bugs
|
||||
Usage: docker exec igny8_backend python test_session_contamination.py
|
||||
"""
|
||||
import os
|
||||
import django
|
||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'igny8_core.settings')
|
||||
django.setup()
|
||||
|
||||
from django.contrib.sessions.models import Session
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.test import RequestFactory
|
||||
from django.contrib.sessions.middleware import SessionMiddleware
|
||||
from igny8_core.auth.middleware import AccountContextMiddleware
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
User = get_user_model()
|
||||
|
||||
def test_session_isolation():
|
||||
"""Test that sessions are properly isolated between users"""
|
||||
print("\n=== SESSION CONTAMINATION TEST ===\n")
|
||||
|
||||
# Get test users
|
||||
try:
|
||||
developer = User.objects.get(username='developer')
|
||||
scale_user = User.objects.filter(account__slug='scale-account').first()
|
||||
|
||||
if not scale_user:
|
||||
print("⚠️ No scale account user found, creating one...")
|
||||
from igny8_core.auth.models import Account
|
||||
scale_account = Account.objects.filter(slug='scale-account').first()
|
||||
if scale_account:
|
||||
scale_user = User.objects.create_user(
|
||||
username='scale_test',
|
||||
email='scale@test.com',
|
||||
password='testpass123',
|
||||
account=scale_account,
|
||||
role='owner'
|
||||
)
|
||||
else:
|
||||
print("❌ No scale account found")
|
||||
return False
|
||||
|
||||
print(f"✓ Developer user: {developer.username} (account: {developer.account.slug})")
|
||||
print(f"✓ Scale user: {scale_user.username} (account: {scale_user.account.slug if scale_user.account else 'None'})")
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Failed to get test users: {e}")
|
||||
return False
|
||||
|
||||
# Check active sessions
|
||||
active_sessions = Session.objects.filter(expire_date__gte=datetime.now())
|
||||
print(f"\n📊 Total active sessions: {active_sessions.count()}")
|
||||
|
||||
# Count sessions by user
|
||||
user_sessions = {}
|
||||
for session in active_sessions:
|
||||
try:
|
||||
data = session.get_decoded()
|
||||
user_id = data.get('_auth_user_id')
|
||||
if user_id:
|
||||
user = User.objects.get(id=user_id)
|
||||
key = f"{user.username} ({user.account.slug if user.account else 'no-account'})"
|
||||
user_sessions[key] = user_sessions.get(key, 0) + 1
|
||||
except:
|
||||
pass
|
||||
|
||||
print("\n📈 Sessions by user:")
|
||||
for user_key, count in sorted(user_sessions.items(), key=lambda x: x[1], reverse=True):
|
||||
print(f" {user_key}: {count} sessions")
|
||||
|
||||
# Check for session contamination patterns
|
||||
contamination_found = False
|
||||
|
||||
# Pattern 1: Too many sessions for one user
|
||||
for user_key, count in user_sessions.items():
|
||||
if count > 20:
|
||||
print(f"\n⚠️ WARNING: {user_key} has {count} sessions (possible proliferation)")
|
||||
contamination_found = True
|
||||
|
||||
# Pattern 2: Check session cookie settings
|
||||
from django.conf import settings
|
||||
print(f"\n🔧 Session Configuration:")
|
||||
print(f" SESSION_COOKIE_NAME: {settings.SESSION_COOKIE_NAME}")
|
||||
print(f" SESSION_COOKIE_DOMAIN: {getattr(settings, 'SESSION_COOKIE_DOMAIN', 'Not set (good)')}")
|
||||
print(f" SESSION_COOKIE_SAMESITE: {getattr(settings, 'SESSION_COOKIE_SAMESITE', 'Not set')}")
|
||||
print(f" SESSION_COOKIE_HTTPONLY: {settings.SESSION_COOKIE_HTTPONLY}")
|
||||
print(f" SESSION_ENGINE: {settings.SESSION_ENGINE}")
|
||||
|
||||
if getattr(settings, 'SESSION_COOKIE_SAMESITE', None) != 'Strict':
|
||||
print(f"\n⚠️ WARNING: SESSION_COOKIE_SAMESITE should be 'Strict' (currently: {getattr(settings, 'SESSION_COOKIE_SAMESITE', 'Not set')})")
|
||||
contamination_found = True
|
||||
|
||||
# Test middleware isolation
|
||||
print(f"\n🧪 Testing Middleware Isolation...")
|
||||
factory = RequestFactory()
|
||||
|
||||
# Simulate two requests from different users
|
||||
request1 = factory.get('/api/v1/test/')
|
||||
request1.user = developer
|
||||
request1.session = {}
|
||||
|
||||
request2 = factory.get('/api/v1/test/')
|
||||
request2.user = scale_user
|
||||
request2.session = {}
|
||||
|
||||
middleware = AccountContextMiddleware(lambda x: None)
|
||||
|
||||
# Process requests
|
||||
middleware.process_request(request1)
|
||||
middleware.process_request(request2)
|
||||
|
||||
# Check isolation
|
||||
account1 = getattr(request1, 'account', None)
|
||||
account2 = getattr(request2, 'account', None)
|
||||
|
||||
print(f" Request 1 account: {account1.slug if account1 else 'None'}")
|
||||
print(f" Request 2 account: {account2.slug if account2 else 'None'}")
|
||||
|
||||
if account1 and account2 and account1.id == account2.id:
|
||||
print(f"\n❌ CONTAMINATION DETECTED: Both requests have same account!")
|
||||
contamination_found = True
|
||||
else:
|
||||
print(f"\n✓ Middleware isolation working correctly")
|
||||
|
||||
# Final result
|
||||
if contamination_found:
|
||||
print(f"\n❌ SESSION CONTAMINATION DETECTED")
|
||||
print(f"\nRecommended fixes:")
|
||||
print(f"1. Set SESSION_COOKIE_SAMESITE='Strict' in settings.py")
|
||||
print(f"2. Clear all existing sessions: Session.objects.all().delete()")
|
||||
print(f"3. Ensure users logout and re-login with fresh cookies")
|
||||
return False
|
||||
else:
|
||||
print(f"\n✅ No contamination detected - sessions appear isolated")
|
||||
return True
|
||||
|
||||
if __name__ == '__main__':
|
||||
result = test_session_isolation()
|
||||
exit(0 if result else 1)
|
||||
@@ -63,7 +63,6 @@ const Transactions = lazy(() => import("./pages/Billing/Transactions"));
|
||||
const Usage = lazy(() => import("./pages/Billing/Usage"));
|
||||
const CreditsAndBilling = lazy(() => import("./pages/Settings/CreditsAndBilling"));
|
||||
const PurchaseCreditsPage = lazy(() => import("./pages/account/PurchaseCreditsPage"));
|
||||
const AccountBillingPage = lazy(() => import("./pages/account/AccountBillingPage"));
|
||||
const PlansAndBillingPage = lazy(() => import("./pages/account/PlansAndBillingPage"));
|
||||
const AccountSettingsPage = lazy(() => import("./pages/account/AccountSettingsPage"));
|
||||
const TeamManagementPage = lazy(() => import("./pages/account/TeamManagementPage"));
|
||||
@@ -385,11 +384,6 @@ export default function App() {
|
||||
<PlansAndBillingPage />
|
||||
</Suspense>
|
||||
} />
|
||||
<Route path="/account/billing" element={
|
||||
<Suspense fallback={null}>
|
||||
<AccountBillingPage />
|
||||
</Suspense>
|
||||
} />
|
||||
<Route path="/account/purchase-credits" element={
|
||||
<Suspense fallback={null}>
|
||||
<PurchaseCreditsPage />
|
||||
|
||||
@@ -20,7 +20,6 @@ export default function ProtectedRoute({ children }: ProtectedRouteProps) {
|
||||
const [errorMessage, setErrorMessage] = useState<string>('');
|
||||
const PLAN_ALLOWED_PATHS = [
|
||||
'/account/plans',
|
||||
'/account/billing',
|
||||
'/account/purchase-credits',
|
||||
'/account/settings',
|
||||
'/account/team',
|
||||
@@ -126,7 +125,7 @@ export default function ProtectedRoute({ children }: ProtectedRouteProps) {
|
||||
|
||||
if (!isPrivileged) {
|
||||
if (pendingPayment && !isPlanAllowedPath) {
|
||||
return <Navigate to="/account/billing" state={{ from: location }} replace />;
|
||||
return <Navigate to="/account/plans" state={{ from: location }} replace />;
|
||||
}
|
||||
if (accountInactive && !isPlanAllowedPath) {
|
||||
return <Navigate to="/account/plans" state={{ from: location }} replace />;
|
||||
|
||||
@@ -100,7 +100,7 @@ export default function SignUpForm({ planDetails: planDetailsProp, planLoading:
|
||||
|
||||
const status = user?.account?.status;
|
||||
if (status === "pending_payment") {
|
||||
navigate("/account/billing", { replace: true });
|
||||
navigate("/account/plans", { replace: true });
|
||||
} else {
|
||||
navigate("/sites", { replace: true });
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ export interface PricingPlan {
|
||||
name: string;
|
||||
price: string | number; // Current displayed price (will be calculated based on period)
|
||||
monthlyPrice?: string | number; // Base monthly price (used for annual discount calculation)
|
||||
annualDiscountPercent?: number; // Annual discount percentage from backend (default 15%)
|
||||
originalPrice?: string | number;
|
||||
period?: string; // "/month", "/year", "/Lifetime"
|
||||
description?: string;
|
||||
@@ -63,7 +64,7 @@ export default function PricingTable({
|
||||
return price;
|
||||
};
|
||||
|
||||
// Calculate price based on billing period with 20% annual discount
|
||||
// Calculate price based on billing period with discount from backend
|
||||
const getDisplayPrice = (plan: PricingPlan): { price: number; originalPrice?: number } => {
|
||||
const monthlyPrice = typeof plan.monthlyPrice === 'number'
|
||||
? plan.monthlyPrice
|
||||
@@ -72,8 +73,12 @@ export default function PricingTable({
|
||||
: parseFloat(String(plan.price || 0));
|
||||
|
||||
if (billingPeriod === 'annually' && showToggle) {
|
||||
// Annual price: monthly * 12 * 0.8 (20% discount)
|
||||
const annualPrice = monthlyPrice * 12 * 0.8;
|
||||
// Get discount percentage from plan (default 15%)
|
||||
const discountPercent = plan.annualDiscountPercent || 15;
|
||||
const discountMultiplier = (100 - discountPercent) / 100;
|
||||
|
||||
// Annual price: monthly * 12 * discount multiplier
|
||||
const annualPrice = monthlyPrice * 12 * discountMultiplier;
|
||||
const originalAnnualPrice = monthlyPrice * 12;
|
||||
return { price: annualPrice, originalPrice: originalAnnualPrice };
|
||||
}
|
||||
|
||||
@@ -201,11 +201,6 @@ const AppSidebar: React.FC = () => {
|
||||
{
|
||||
icon: <DollarLineIcon />,
|
||||
name: "Plans & Billing",
|
||||
path: "/account/billing",
|
||||
},
|
||||
{
|
||||
icon: <DollarLineIcon />,
|
||||
name: "Plans",
|
||||
path: "/account/plans",
|
||||
},
|
||||
{
|
||||
@@ -323,7 +318,35 @@ const AppSidebar: React.FC = () => {
|
||||
subItems: [
|
||||
{ name: "Function Testing", path: "/admin/function-testing" },
|
||||
{ name: "System Testing", path: "/admin/system-testing" },
|
||||
{ name: "UI Elements", path: "/admin/ui-elements" },
|
||||
],
|
||||
},
|
||||
{
|
||||
icon: <BoltIcon />,
|
||||
name: "UI Elements",
|
||||
subItems: [
|
||||
{ name: "Alerts", path: "/ui-elements/alerts" },
|
||||
{ name: "Avatars", path: "/ui-elements/avatars" },
|
||||
{ name: "Badges", path: "/ui-elements/badges" },
|
||||
{ name: "Breadcrumb", path: "/ui-elements/breadcrumb" },
|
||||
{ name: "Buttons", path: "/ui-elements/buttons" },
|
||||
{ name: "Buttons Group", path: "/ui-elements/buttons-group" },
|
||||
{ name: "Cards", path: "/ui-elements/cards" },
|
||||
{ name: "Carousel", path: "/ui-elements/carousel" },
|
||||
{ name: "Dropdowns", path: "/ui-elements/dropdowns" },
|
||||
{ name: "Images", path: "/ui-elements/images" },
|
||||
{ name: "Links", path: "/ui-elements/links" },
|
||||
{ name: "List", path: "/ui-elements/list" },
|
||||
{ name: "Modals", path: "/ui-elements/modals" },
|
||||
{ name: "Notifications", path: "/ui-elements/notifications" },
|
||||
{ name: "Pagination", path: "/ui-elements/pagination" },
|
||||
{ name: "Popovers", path: "/ui-elements/popovers" },
|
||||
{ name: "Pricing Table", path: "/ui-elements/pricing-table" },
|
||||
{ name: "Progressbar", path: "/ui-elements/progressbar" },
|
||||
{ name: "Ribbons", path: "/ui-elements/ribbons" },
|
||||
{ name: "Spinners", path: "/ui-elements/spinners" },
|
||||
{ name: "Tabs", path: "/ui-elements/tabs" },
|
||||
{ name: "Tooltips", path: "/ui-elements/tooltips" },
|
||||
{ name: "Videos", path: "/ui-elements/videos" },
|
||||
],
|
||||
},
|
||||
],
|
||||
|
||||
@@ -1,625 +0,0 @@
|
||||
/**
|
||||
* Account Billing Page
|
||||
* Consolidated billing dashboard with invoices, payments, and credit balance
|
||||
*/
|
||||
|
||||
import { useState, useEffect } from 'react';
|
||||
import { Link, useNavigate } from 'react-router-dom';
|
||||
import {
|
||||
CreditCard,
|
||||
Download,
|
||||
AlertCircle,
|
||||
Loader2,
|
||||
FileText,
|
||||
CheckCircle,
|
||||
XCircle,
|
||||
Clock,
|
||||
DollarSign,
|
||||
TrendingUp,
|
||||
} from 'lucide-react';
|
||||
import {
|
||||
getInvoices,
|
||||
getPayments,
|
||||
getCreditBalance,
|
||||
getCreditPackages,
|
||||
getAvailablePaymentMethods,
|
||||
downloadInvoicePDF,
|
||||
getPlans,
|
||||
getSubscriptions,
|
||||
type Invoice,
|
||||
type Payment,
|
||||
type CreditBalance,
|
||||
type CreditPackage,
|
||||
type PaymentMethod,
|
||||
type Plan,
|
||||
type Subscription,
|
||||
} from '../../services/billing.api';
|
||||
import { useAuthStore } from '../../store/authStore';
|
||||
import { Card } from '../../components/ui/card';
|
||||
import BillingRecentTransactions from '../../components/billing/BillingRecentTransactions';
|
||||
import PricingTable, { type PricingPlan } from '../../components/ui/pricing-table/PricingTable';
|
||||
|
||||
type TabType = 'overview' | 'plans' | 'invoices' | 'payments' | 'methods';
|
||||
|
||||
export default function AccountBillingPage() {
|
||||
const navigate = useNavigate();
|
||||
const [activeTab, setActiveTab] = useState<TabType>('overview');
|
||||
const [creditBalance, setCreditBalance] = useState<CreditBalance | null>(null);
|
||||
const [invoices, setInvoices] = useState<Invoice[]>([]);
|
||||
const [payments, setPayments] = useState<Payment[]>([]);
|
||||
const [creditPackages, setCreditPackages] = useState<CreditPackage[]>([]);
|
||||
const [paymentMethods, setPaymentMethods] = useState<PaymentMethod[]>([]);
|
||||
const [plans, setPlans] = useState<Plan[]>([]);
|
||||
const [subscriptions, setSubscriptions] = useState<Subscription[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string>('');
|
||||
const { user } = useAuthStore();
|
||||
|
||||
const planCatalog: PricingPlan[] = [
|
||||
{
|
||||
id: 1,
|
||||
name: 'Starter',
|
||||
price: 89,
|
||||
period: '/month',
|
||||
description: 'Good for small teams getting started',
|
||||
features: ['1,000 credits included', '1 site', '2 users'],
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
name: 'Growth',
|
||||
price: 139,
|
||||
period: '/month',
|
||||
description: 'For growing teams that need more volume',
|
||||
features: ['2,000 credits included', '3 sites', '3 users'],
|
||||
highlighted: true,
|
||||
},
|
||||
{
|
||||
id: 3,
|
||||
name: 'Scale',
|
||||
price: 229,
|
||||
period: '/month',
|
||||
description: 'Larger teams with higher usage',
|
||||
features: ['4,000 credits included', '5 sites', '5 users'],
|
||||
},
|
||||
];
|
||||
|
||||
useEffect(() => {
|
||||
loadData();
|
||||
}, []);
|
||||
|
||||
const loadData = async () => {
|
||||
try {
|
||||
setLoading(true);
|
||||
const [balanceRes, invoicesRes, paymentsRes, packagesRes, methodsRes, plansRes, subsRes] = await Promise.all([
|
||||
getCreditBalance(),
|
||||
getInvoices(),
|
||||
getPayments(),
|
||||
getCreditPackages(),
|
||||
getAvailablePaymentMethods(),
|
||||
getPlans(),
|
||||
getSubscriptions(),
|
||||
]);
|
||||
|
||||
setCreditBalance(balanceRes);
|
||||
setInvoices(invoicesRes.results);
|
||||
setPayments(paymentsRes.results);
|
||||
setCreditPackages(packagesRes.results || []);
|
||||
setPaymentMethods(methodsRes.results || []);
|
||||
setPlans((plansRes.results || []).filter((p) => p.is_active !== false));
|
||||
setSubscriptions(subsRes.results || []);
|
||||
} catch (err: any) {
|
||||
setError(err.message || 'Failed to load billing data');
|
||||
console.error('Billing data load error:', err);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleDownloadInvoice = async (invoiceId: number, invoiceNumber: string) => {
|
||||
try {
|
||||
const blob = await downloadInvoicePDF(invoiceId);
|
||||
const url = window.URL.createObjectURL(blob);
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = `invoice-${invoiceNumber}.pdf`;
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
window.URL.revokeObjectURL(url);
|
||||
document.body.removeChild(a);
|
||||
} catch (err) {
|
||||
alert('Failed to download invoice');
|
||||
}
|
||||
};
|
||||
|
||||
const getStatusBadge = (status: string) => {
|
||||
const styles: Record<string, { bg: string; text: string; icon: any }> = {
|
||||
paid: { bg: 'bg-green-100', text: 'text-green-800', icon: CheckCircle },
|
||||
succeeded: { bg: 'bg-green-100', text: 'text-green-800', icon: CheckCircle },
|
||||
completed: { bg: 'bg-green-100', text: 'text-green-800', icon: CheckCircle },
|
||||
pending: { bg: 'bg-yellow-100', text: 'text-yellow-800', icon: Clock },
|
||||
pending_approval: { bg: 'bg-blue-100', text: 'text-blue-800', icon: Clock },
|
||||
processing: { bg: 'bg-blue-100', text: 'text-blue-800', icon: Clock },
|
||||
failed: { bg: 'bg-red-100', text: 'text-red-800', icon: XCircle },
|
||||
refunded: { bg: 'bg-gray-100', text: 'text-gray-800', icon: XCircle },
|
||||
cancelled: { bg: 'bg-gray-100', text: 'text-gray-800', icon: XCircle },
|
||||
void: { bg: 'bg-gray-100', text: 'text-gray-800', icon: XCircle },
|
||||
uncollectible: { bg: 'bg-gray-100', text: 'text-gray-800', icon: XCircle },
|
||||
};
|
||||
|
||||
const style = styles[status] || styles.pending;
|
||||
const Icon = style.icon;
|
||||
|
||||
return (
|
||||
<span className={`inline-flex items-center gap-1 px-2.5 py-0.5 rounded-full text-xs font-medium ${style.bg} ${style.text}`}>
|
||||
<Icon className="w-3 h-3" />
|
||||
{status.replace('_', ' ').toUpperCase()}
|
||||
</span>
|
||||
);
|
||||
};
|
||||
|
||||
if (loading) {
|
||||
return (
|
||||
<div className="flex items-center justify-center min-h-screen">
|
||||
<Loader2 className="w-8 h-8 animate-spin text-blue-600" />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="container mx-auto px-6 py-8">
|
||||
<div className="max-w-7xl mx-auto">
|
||||
<div className="flex items-center justify-between mb-6">
|
||||
<div>
|
||||
<h1 className="text-3xl font-bold">Plans & Billing</h1>
|
||||
<p className="text-gray-600">Manage your subscription, credits, and billing</p>
|
||||
</div>
|
||||
<Link
|
||||
to="/account/purchase-credits"
|
||||
className="px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 flex items-center gap-2"
|
||||
>
|
||||
<CreditCard className="w-4 h-4" />
|
||||
Purchase Credits
|
||||
</Link>
|
||||
</div>
|
||||
|
||||
{error && (
|
||||
<div className="bg-red-50 border border-red-200 rounded-lg p-4 mb-6 flex items-start gap-2">
|
||||
<AlertCircle className="w-5 h-5 text-red-600 flex-shrink-0 mt-0.5" />
|
||||
<p className="text-red-800">{error}</p>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Tabs */}
|
||||
<div className="border-b border-gray-200 mb-6">
|
||||
<nav className="flex gap-8">
|
||||
{[
|
||||
{ id: 'overview', label: 'Overview' },
|
||||
{ id: 'plans', label: 'Plans & Credits' },
|
||||
{ id: 'invoices', label: 'Invoices' },
|
||||
{ id: 'payments', label: 'Payments' },
|
||||
{ id: 'methods', label: 'Payment Methods' },
|
||||
].map((tab) => (
|
||||
<button
|
||||
key={tab.id}
|
||||
onClick={() => setActiveTab(tab.id as TabType)}
|
||||
className={`py-3 border-b-2 font-medium transition-colors ${
|
||||
activeTab === tab.id
|
||||
? 'border-blue-600 text-blue-600'
|
||||
: 'border-transparent text-gray-600 hover:text-gray-900'
|
||||
}`}
|
||||
>
|
||||
{tab.label}
|
||||
</button>
|
||||
))}
|
||||
</nav>
|
||||
</div>
|
||||
|
||||
{/* Overview Tab */}
|
||||
{activeTab === 'overview' && creditBalance && (
|
||||
<div className="space-y-6">
|
||||
{/* Stats Cards */}
|
||||
<div className="grid grid-cols-1 md:grid-cols-4 gap-6">
|
||||
<Card className="p-6">
|
||||
<div className="flex items-center justify-between mb-2">
|
||||
<h3 className="text-sm font-medium text-gray-600 dark:text-gray-400">Current Balance</h3>
|
||||
<CreditCard className="w-5 h-5 text-blue-600" />
|
||||
</div>
|
||||
<div className="text-3xl font-bold text-gray-900 dark:text-white">
|
||||
{creditBalance?.credits?.toLocaleString() || '0'}
|
||||
</div>
|
||||
<p className="text-sm text-gray-500 dark:text-gray-400 mt-1">Available credits</p>
|
||||
</Card>
|
||||
|
||||
<Card className="p-6">
|
||||
<div className="flex items-center justify-between mb-2">
|
||||
<h3 className="text-sm font-medium text-gray-600 dark:text-gray-400">Monthly Allocation</h3>
|
||||
<TrendingUp className="w-5 h-5 text-green-600" />
|
||||
</div>
|
||||
<div className="text-3xl font-bold text-gray-900 dark:text-white">
|
||||
{creditBalance?.plan_credits_per_month?.toLocaleString() || '0'}
|
||||
</div>
|
||||
<p className="text-sm text-gray-500 dark:text-gray-400 mt-1">Credits per month</p>
|
||||
</Card>
|
||||
|
||||
<Card className="p-6">
|
||||
<div className="flex items-center justify-between mb-2">
|
||||
<h3 className="text-sm font-medium text-gray-600 dark:text-gray-400">Used This Month</h3>
|
||||
<DollarSign className="w-5 h-5 text-red-600" />
|
||||
</div>
|
||||
<div className="text-3xl font-bold text-red-600 dark:text-red-400">
|
||||
{creditBalance?.credits_used_this_month?.toLocaleString() || '0'}
|
||||
</div>
|
||||
<p className="text-sm text-gray-500 dark:text-gray-400 mt-1">Credits consumed</p>
|
||||
</Card>
|
||||
|
||||
<Card className="p-6">
|
||||
<div className="flex items-center justify-between mb-2">
|
||||
<h3 className="text-sm font-medium text-gray-600 dark:text-gray-400">Plan Status</h3>
|
||||
<TrendingUp className="w-5 h-5 text-indigo-600" />
|
||||
</div>
|
||||
<div className="text-lg font-semibold text-gray-900 dark:text-white">
|
||||
{creditBalance?.plan_credits_per_month ? 'Active Plan' : 'Pay as you go'}
|
||||
</div>
|
||||
<p className="text-sm text-gray-500 dark:text-gray-400 mt-1">
|
||||
{creditBalance?.plan_credits_per_month
|
||||
? `${creditBalance.plan_credits_per_month.toLocaleString()} credits per month`
|
||||
: 'Upgrade to a plan for predictable billing'}
|
||||
</p>
|
||||
<div className="mt-4">
|
||||
<button
|
||||
onClick={() => setActiveTab('plans')}
|
||||
className="text-blue-600 hover:text-blue-700 text-sm font-medium"
|
||||
>
|
||||
View plans
|
||||
</button>
|
||||
</div>
|
||||
</Card>
|
||||
</div>
|
||||
|
||||
{/* Quick Actions */}
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 gap-6">
|
||||
<Card className="p-6">
|
||||
<h3 className="text-lg font-semibold mb-4">Quick Actions</h3>
|
||||
<div className="space-y-2">
|
||||
<Link
|
||||
to="/account/purchase-credits"
|
||||
className="block w-full bg-blue-600 text-white text-center py-2 px-4 rounded hover:bg-blue-700 transition-colors"
|
||||
>
|
||||
Purchase Credits
|
||||
</Link>
|
||||
<Link
|
||||
to="/account/usage"
|
||||
className="block w-full bg-gray-100 text-gray-700 text-center py-2 px-4 rounded hover:bg-gray-200 transition-colors"
|
||||
>
|
||||
View Usage Analytics
|
||||
</Link>
|
||||
</div>
|
||||
</Card>
|
||||
|
||||
<Card className="p-6">
|
||||
<h3 className="text-lg font-semibold mb-4">Account Summary</h3>
|
||||
<div className="space-y-3">
|
||||
<div className="flex justify-between">
|
||||
<span className="text-gray-600">Remaining Credits:</span>
|
||||
<span className="font-semibold">{creditBalance?.credits_remaining?.toLocaleString() || '0'}</span>
|
||||
</div>
|
||||
<div className="flex justify-between">
|
||||
<span className="text-gray-600">Total Invoices:</span>
|
||||
<span className="font-semibold">{invoices.length}</span>
|
||||
</div>
|
||||
<div className="flex justify-between">
|
||||
<span className="text-gray-600">Paid Invoices:</span>
|
||||
<span className="font-semibold text-green-600">
|
||||
{invoices.filter(inv => inv.status === 'paid').length}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
</Card>
|
||||
</div>
|
||||
|
||||
{/* Recent Transactions */}
|
||||
<Card className="p-6">
|
||||
<div className="flex items-center justify-between mb-4">
|
||||
<h3 className="text-lg font-semibold">Recent Transactions</h3>
|
||||
<Link to="/account/usage" className="text-sm text-blue-600 hover:text-blue-700">
|
||||
View usage details
|
||||
</Link>
|
||||
</div>
|
||||
<BillingRecentTransactions variant="plain" />
|
||||
</Card>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Plans Tab */}
|
||||
{activeTab === 'plans' && (
|
||||
<div className="space-y-8">
|
||||
<Card className="p-6">
|
||||
<div className="flex flex-col lg:flex-row lg:items-center lg:justify-between gap-4">
|
||||
<div>
|
||||
<h2 className="text-xl font-semibold">Active plan</h2>
|
||||
<p className="text-gray-600">Your current subscription allocation</p>
|
||||
</div>
|
||||
<div className="text-right">
|
||||
<div className="text-sm text-gray-600">Monthly allocation</div>
|
||||
<div className="text-3xl font-bold text-blue-600">
|
||||
{creditBalance?.plan_credits_per_month?.toLocaleString() || '—'}
|
||||
</div>
|
||||
<div className="text-sm text-gray-500">
|
||||
Remaining: {creditBalance?.credits_remaining?.toLocaleString() ?? '—'} credits
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</Card>
|
||||
|
||||
<Card className="p-6">
|
||||
<div className="flex items-center justify-between mb-6">
|
||||
<div>
|
||||
<h3 className="text-lg font-semibold">Available plans</h3>
|
||||
<p className="text-gray-600">Choose the plan that fits your team (excluding free).</p>
|
||||
</div>
|
||||
</div>
|
||||
<PricingTable
|
||||
variant="2"
|
||||
className="w-full"
|
||||
plans={(plans.length ? plans : planCatalog)
|
||||
.filter((plan) => {
|
||||
const name = (plan.name || '').toLowerCase();
|
||||
const slug = (plan as any).slug ? (plan as any).slug.toLowerCase() : '';
|
||||
const isEnterprise = name.includes('enterprise') || slug === 'enterprise';
|
||||
return !isEnterprise && plan.name !== 'Free';
|
||||
})}
|
||||
onPlanSelect={() => {}}
|
||||
/>
|
||||
</Card>
|
||||
|
||||
<Card className="p-6">
|
||||
<div className="flex items-center justify-between mb-6">
|
||||
<div>
|
||||
<h3 className="text-lg font-semibold">Credit add-ons</h3>
|
||||
<p className="text-gray-600">One-time credit bundles to top up your balance.</p>
|
||||
</div>
|
||||
</div>
|
||||
{creditPackages.length === 0 ? (
|
||||
<div className="text-center text-gray-600 py-10">
|
||||
<FileText className="w-10 h-10 mx-auto mb-3 text-gray-400" />
|
||||
No packages available yet. Please check back soon.
|
||||
</div>
|
||||
) : (
|
||||
<PricingTable
|
||||
variant="1"
|
||||
className="w-full"
|
||||
plans={creditPackages.map((pkg) => {
|
||||
const plan: PricingPlan = {
|
||||
id: pkg.id,
|
||||
name: pkg.name,
|
||||
price: Number(pkg.price),
|
||||
period: '/one-time',
|
||||
description: pkg.description,
|
||||
features: [
|
||||
`${pkg.credits.toLocaleString()} credits`,
|
||||
pkg.discount_percentage > 0 ? `${pkg.discount_percentage}% discount applied` : 'Standard pricing',
|
||||
'Manual & online payments supported',
|
||||
],
|
||||
highlighted: pkg.is_featured,
|
||||
buttonText: 'Select',
|
||||
};
|
||||
return plan;
|
||||
})}
|
||||
onPlanSelect={() => navigate('/account/purchase-credits')}
|
||||
/>
|
||||
)}
|
||||
</Card>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Invoices Tab */}
|
||||
{activeTab === 'invoices' && (
|
||||
<div className="bg-white rounded-lg shadow overflow-hidden">
|
||||
<div className="overflow-x-auto">
|
||||
<table className="w-full">
|
||||
<thead className="bg-gray-50 border-b">
|
||||
<tr>
|
||||
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
|
||||
Invoice
|
||||
</th>
|
||||
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
|
||||
Date
|
||||
</th>
|
||||
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
|
||||
Amount
|
||||
</th>
|
||||
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
|
||||
Status
|
||||
</th>
|
||||
<th className="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase tracking-wider">
|
||||
Actions
|
||||
</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody className="divide-y divide-gray-200">
|
||||
{invoices.length === 0 ? (
|
||||
<tr>
|
||||
<td colSpan={5} className="px-6 py-8 text-center text-gray-500">
|
||||
<FileText className="w-12 h-12 mx-auto mb-2 text-gray-400" />
|
||||
No invoices yet
|
||||
</td>
|
||||
</tr>
|
||||
) : (
|
||||
invoices.map((invoice) => (
|
||||
<tr key={invoice.id} className="hover:bg-gray-50">
|
||||
<td className="px-6 py-4">
|
||||
<div className="font-medium">{invoice.invoice_number}</div>
|
||||
{invoice.line_items[0] && (
|
||||
<div className="text-sm text-gray-500">
|
||||
{invoice.line_items[0].description}
|
||||
</div>
|
||||
)}
|
||||
</td>
|
||||
<td className="px-6 py-4 text-sm text-gray-600">
|
||||
{new Date(invoice.created_at).toLocaleDateString()}
|
||||
</td>
|
||||
<td className="px-6 py-4 font-semibold">
|
||||
${invoice.total_amount}
|
||||
</td>
|
||||
<td className="px-6 py-4">{getStatusBadge(invoice.status)}</td>
|
||||
<td className="px-6 py-4 text-right">
|
||||
<button
|
||||
onClick={() =>
|
||||
handleDownloadInvoice(invoice.id, invoice.invoice_number)
|
||||
}
|
||||
className="text-blue-600 hover:text-blue-700 flex items-center gap-1 ml-auto"
|
||||
>
|
||||
<Download className="w-4 h-4" />
|
||||
Download
|
||||
</button>
|
||||
</td>
|
||||
</tr>
|
||||
))
|
||||
)}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Payments Tab */}
|
||||
{activeTab === 'payments' && (
|
||||
<div className="bg-white rounded-lg shadow overflow-hidden">
|
||||
<div className="overflow-x-auto">
|
||||
<table className="w-full">
|
||||
<thead className="bg-gray-50 border-b">
|
||||
<tr>
|
||||
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
|
||||
Date
|
||||
</th>
|
||||
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
|
||||
Method
|
||||
</th>
|
||||
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
|
||||
Reference
|
||||
</th>
|
||||
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
|
||||
Amount
|
||||
</th>
|
||||
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
|
||||
Status
|
||||
</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody className="divide-y divide-gray-200">
|
||||
{payments.length === 0 ? (
|
||||
<tr>
|
||||
<td colSpan={5} className="px-6 py-8 text-center text-gray-500">
|
||||
<CreditCard className="w-12 h-12 mx-auto mb-2 text-gray-400" />
|
||||
No payments yet
|
||||
</td>
|
||||
</tr>
|
||||
) : (
|
||||
payments.map((payment) => (
|
||||
<tr key={payment.id} className="hover:bg-gray-50">
|
||||
<td className="px-6 py-4 text-sm text-gray-600">
|
||||
{new Date(payment.created_at).toLocaleDateString()}
|
||||
</td>
|
||||
<td className="px-6 py-4">
|
||||
<div className="font-medium capitalize">
|
||||
{payment.payment_method.replace('_', ' ')}
|
||||
</div>
|
||||
</td>
|
||||
<td className="px-6 py-4 text-sm font-mono text-gray-600">
|
||||
{payment.transaction_reference || '-'}
|
||||
</td>
|
||||
<td className="px-6 py-4 font-semibold">
|
||||
${payment.amount}
|
||||
</td>
|
||||
<td className="px-6 py-4">{getStatusBadge(payment.status)}</td>
|
||||
</tr>
|
||||
))
|
||||
)}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Payment Methods Tab */}
|
||||
{activeTab === 'methods' && (
|
||||
<div className="space-y-6">
|
||||
<Card className="p-6">
|
||||
<div className="flex items-center justify-between mb-4">
|
||||
<div>
|
||||
<h3 className="text-lg font-semibold">Available payment methods</h3>
|
||||
<p className="text-sm text-gray-600">Use these options when purchasing credits.</p>
|
||||
</div>
|
||||
<Link
|
||||
to="/account/purchase-credits"
|
||||
className="px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700"
|
||||
>
|
||||
Go to purchase
|
||||
</Link>
|
||||
</div>
|
||||
|
||||
{paymentMethods.length === 0 ? (
|
||||
<div className="text-center text-gray-600 py-8">
|
||||
<CreditCard className="w-8 h-8 mx-auto mb-2 text-gray-400" />
|
||||
No payment methods available yet.
|
||||
</div>
|
||||
) : (
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
|
||||
{paymentMethods.map((method) => (
|
||||
<div
|
||||
key={method.id || method.type}
|
||||
className="border rounded-lg p-4 bg-white shadow-sm"
|
||||
>
|
||||
<div className="flex items-start justify-between mb-2">
|
||||
<div>
|
||||
<div className="text-sm text-gray-500 uppercase">{method.type}</div>
|
||||
<div className="text-lg font-semibold">{method.display_name || method.name}</div>
|
||||
</div>
|
||||
<span className="text-xs px-2 py-1 bg-green-100 text-green-800 rounded-full">
|
||||
{method.is_enabled ? 'Enabled' : 'Disabled'}
|
||||
</span>
|
||||
</div>
|
||||
{method.instructions && (
|
||||
<p className="text-sm text-gray-600 mb-3">{method.instructions}</p>
|
||||
)}
|
||||
{method.bank_details && (
|
||||
<div className="text-sm text-gray-700 space-y-1">
|
||||
<div className="flex justify-between">
|
||||
<span className="text-gray-500">Bank</span>
|
||||
<span className="font-mono">{method.bank_details.bank_name}</span>
|
||||
</div>
|
||||
<div className="flex justify-between">
|
||||
<span className="text-gray-500">Account</span>
|
||||
<span className="font-mono">{method.bank_details.account_number}</span>
|
||||
</div>
|
||||
{method.bank_details.routing_number && (
|
||||
<div className="flex justify-between">
|
||||
<span className="text-gray-500">Routing</span>
|
||||
<span className="font-mono">{method.bank_details.routing_number}</span>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
{method.wallet_details && (
|
||||
<div className="text-sm text-gray-700 space-y-1">
|
||||
<div className="flex justify-between">
|
||||
<span className="text-gray-500">Wallet</span>
|
||||
<span className="font-mono">{method.wallet_details.wallet_type}</span>
|
||||
</div>
|
||||
<div className="flex justify-between">
|
||||
<span className="text-gray-500">ID</span>
|
||||
<span className="font-mono break-all">{method.wallet_details.wallet_id}</span>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</Card>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
/**
|
||||
* Plans & Billing Page - Consolidated
|
||||
* Tabs: Current Plan, Upgrade/Downgrade, Credits Overview, Purchase Credits, Billing History, Payment Methods
|
||||
* Tabs: Current Plan, Credits Overview, Billing History
|
||||
*/
|
||||
|
||||
import { useState, useEffect, useRef } from 'react';
|
||||
@@ -12,6 +12,7 @@ import { Card } from '../../components/ui/card';
|
||||
import Badge from '../../components/ui/badge/Badge';
|
||||
import Button from '../../components/ui/button/Button';
|
||||
import { useToast } from '../../components/ui/toast/ToastContainer';
|
||||
import { PricingTable, PricingPlan } from '../../components/ui/pricing-table';
|
||||
import {
|
||||
getCreditBalance,
|
||||
getCreditPackages,
|
||||
@@ -38,7 +39,7 @@ import {
|
||||
} from '../../services/billing.api';
|
||||
import { useAuthStore } from '../../store/authStore';
|
||||
|
||||
type TabType = 'plan' | 'upgrade' | 'credits' | 'purchase' | 'invoices' | 'payments' | 'payment-methods';
|
||||
type TabType = 'plan' | 'credits' | 'invoices';
|
||||
|
||||
export default function PlansAndBillingPage() {
|
||||
const [activeTab, setActiveTab] = useState<TabType>('plan');
|
||||
@@ -339,12 +340,8 @@ export default function PlansAndBillingPage() {
|
||||
|
||||
const tabs = [
|
||||
{ id: 'plan' as TabType, label: 'Current Plan', icon: <Package className="w-4 h-4" /> },
|
||||
{ id: 'upgrade' as TabType, label: 'Upgrade/Downgrade', icon: <ArrowUpCircle className="w-4 h-4" /> },
|
||||
{ id: 'credits' as TabType, label: 'Credits Overview', icon: <TrendingUp className="w-4 h-4" /> },
|
||||
{ id: 'purchase' as TabType, label: 'Purchase Credits', icon: <CreditCard className="w-4 h-4" /> },
|
||||
{ id: 'invoices' as TabType, label: 'Billing History', icon: <FileText className="w-4 h-4" /> },
|
||||
{ id: 'payments' as TabType, label: 'Payments', icon: <Wallet className="w-4 h-4" /> },
|
||||
{ id: 'payment-methods' as TabType, label: 'Payment Methods', icon: <Wallet className="w-4 h-4" /> },
|
||||
];
|
||||
|
||||
return (
|
||||
@@ -403,177 +400,125 @@ export default function PlansAndBillingPage() {
|
||||
{/* Current Plan Tab */}
|
||||
{activeTab === 'plan' && (
|
||||
<div className="space-y-6">
|
||||
<Card className="p-6">
|
||||
<h2 className="text-lg font-semibold mb-4">Your Current Plan</h2>
|
||||
{!hasActivePlan && (
|
||||
<div className="p-4 mb-4 rounded-lg border border-amber-200 bg-amber-50 text-amber-800 dark:border-amber-800 dark:bg-amber-900/20 dark:text-amber-200">
|
||||
No active plan found. Please choose a plan to activate your account.
|
||||
</div>
|
||||
)}
|
||||
<div className="space-y-4">
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<div className="text-2xl font-bold text-gray-900 dark:text-white">
|
||||
{currentPlan?.name || 'No Plan Selected'}
|
||||
{/* 2/3 Current Plan + 1/3 Plan Features Layout */}
|
||||
<div className="grid grid-cols-1 lg:grid-cols-3 gap-6">
|
||||
{/* Current Plan Card - 2/3 width */}
|
||||
<Card className="p-6 lg:col-span-2">
|
||||
<h2 className="text-lg font-semibold mb-4">Your Current Plan</h2>
|
||||
{!hasActivePlan && (
|
||||
<div className="p-4 mb-4 rounded-lg border border-amber-200 bg-amber-50 text-amber-800 dark:border-amber-800 dark:bg-amber-900/20 dark:text-amber-200">
|
||||
No active plan found. Please choose a plan to activate your account.
|
||||
</div>
|
||||
)}
|
||||
<div className="space-y-4">
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<div className="text-2xl font-bold text-gray-900 dark:text-white">
|
||||
{currentPlan?.name || 'No Plan Selected'}
|
||||
</div>
|
||||
<div className="text-gray-600 dark:text-gray-400">
|
||||
{currentPlan?.description || 'Select a plan to unlock full access.'}
|
||||
</div>
|
||||
</div>
|
||||
<div className="text-gray-600 dark:text-gray-400">
|
||||
{currentPlan?.description || 'Select a plan to unlock full access.'}
|
||||
<Badge variant="light" color={hasActivePlan ? 'success' : 'warning'}>
|
||||
{hasActivePlan ? subscriptionStatus : 'plan required'}
|
||||
</Badge>
|
||||
</div>
|
||||
<div className="grid grid-cols-1 md:grid-cols-3 gap-4 mt-6">
|
||||
<div className="p-4 bg-gray-50 dark:bg-gray-800 rounded-lg">
|
||||
<div className="text-sm text-gray-600 dark:text-gray-400">Monthly Credits</div>
|
||||
<div className="text-2xl font-bold text-gray-900 dark:text-white">
|
||||
{creditBalance?.plan_credits_per_month?.toLocaleString?.() || 0}
|
||||
</div>
|
||||
</div>
|
||||
<div className="p-4 bg-gray-50 dark:bg-gray-800 rounded-lg">
|
||||
<div className="text-sm text-gray-600 dark:text-gray-400">Current Balance</div>
|
||||
<div className="text-2xl font-bold text-gray-900 dark:text-white">
|
||||
{creditBalance?.credits?.toLocaleString?.() || 0}
|
||||
</div>
|
||||
</div>
|
||||
<div className="p-4 bg-gray-50 dark:bg-gray-800 rounded-lg">
|
||||
<div className="text-sm text-gray-600 dark:text-gray-400">Period Ends</div>
|
||||
<div className="text-lg font-bold text-gray-900 dark:text-white">
|
||||
{currentSubscription?.current_period_end
|
||||
? new Date(currentSubscription.current_period_end).toLocaleDateString()
|
||||
: '—'}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<Badge variant="light" color={hasActivePlan ? 'success' : 'warning'}>
|
||||
{hasActivePlan ? subscriptionStatus : 'plan required'}
|
||||
</Badge>
|
||||
</div>
|
||||
<div className="grid grid-cols-1 md:grid-cols-3 gap-4 mt-6">
|
||||
<div className="p-4 bg-gray-50 dark:bg-gray-800 rounded-lg">
|
||||
<div className="text-sm text-gray-600 dark:text-gray-400">Monthly Credits</div>
|
||||
<div className="text-2xl font-bold text-gray-900 dark:text-white">
|
||||
{creditBalance?.plan_credits_per_month?.toLocaleString?.() || 0}
|
||||
</div>
|
||||
</div>
|
||||
<div className="p-4 bg-gray-50 dark:bg-gray-800 rounded-lg">
|
||||
<div className="text-sm text-gray-600 dark:text-gray-400">Current Balance</div>
|
||||
<div className="text-2xl font-bold text-gray-900 dark:text-white">
|
||||
{creditBalance?.credits?.toLocaleString?.() || 0}
|
||||
</div>
|
||||
</div>
|
||||
<div className="p-4 bg-gray-50 dark:bg-gray-800 rounded-lg">
|
||||
<div className="text-sm text-gray-600 dark:text-gray-400">Period Ends</div>
|
||||
<div className="text-2xl font-bold text-gray-900 dark:text-white text-base">
|
||||
{currentSubscription?.current_period_end
|
||||
? new Date(currentSubscription.current_period_end).toLocaleDateString()
|
||||
: '—'}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div className="mt-6 flex gap-3">
|
||||
<Button variant="primary" tone="brand" onClick={() => setActiveTab('upgrade')}>
|
||||
{hasActivePlan ? 'Change Plan' : 'Choose a Plan'}
|
||||
</Button>
|
||||
<Button variant="outline" tone="neutral" onClick={() => setActiveTab('purchase')}>
|
||||
Purchase Credits
|
||||
</Button>
|
||||
{hasActivePlan && (
|
||||
<Button
|
||||
variant="outline"
|
||||
tone="neutral"
|
||||
disabled={planLoadingId === currentSubscription?.id}
|
||||
onClick={handleCancelSubscription}
|
||||
>
|
||||
{planLoadingId === currentSubscription?.id ? 'Cancelling...' : 'Cancel Subscription'}
|
||||
<div className="mt-6 flex gap-3">
|
||||
<Button variant="outline" tone="neutral" onClick={() => setActiveTab('credits')}>
|
||||
Purchase Credits
|
||||
</Button>
|
||||
)}
|
||||
{hasActivePlan && (
|
||||
<Button
|
||||
variant="outline"
|
||||
tone="neutral"
|
||||
disabled={planLoadingId === currentSubscription?.id}
|
||||
onClick={handleCancelSubscription}
|
||||
>
|
||||
{planLoadingId === currentSubscription?.id ? 'Cancelling...' : 'Cancel Subscription'}
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</Card>
|
||||
</Card>
|
||||
|
||||
<Card className="p-6">
|
||||
<h2 className="text-lg font-semibold mb-4">Plan Features</h2>
|
||||
<ul className="space-y-3">
|
||||
{(currentPlan?.features && currentPlan.features.length > 0
|
||||
? currentPlan.features
|
||||
: ['Credits included each month', 'Module access per plan limits', 'Email support'])
|
||||
.map((feature) => (
|
||||
<li key={feature} className="flex items-center gap-2 text-gray-700 dark:text-gray-300">
|
||||
<CheckCircle className="w-5 h-5 text-green-600" />
|
||||
{feature}
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
</Card>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Upgrade/Downgrade Tab */}
|
||||
{activeTab === 'upgrade' && (
|
||||
<div className="space-y-6">
|
||||
<div className="mb-4">
|
||||
<h2 className="text-xl font-semibold mb-2">Available Plans</h2>
|
||||
<p className="text-gray-600 dark:text-gray-400">Choose the plan that best fits your needs</p>
|
||||
{/* Plan Features Card - 1/3 width with 2-column layout */}
|
||||
<Card className="p-6">
|
||||
<h2 className="text-lg font-semibold mb-4">Plan Features</h2>
|
||||
<div className="grid grid-cols-2 gap-3">
|
||||
{(currentPlan?.features && currentPlan.features.length > 0
|
||||
? currentPlan.features
|
||||
: ['ai_writer', 'image_gen', 'auto_publish', 'custom_prompts', 'email_support', 'api_access'])
|
||||
.map((feature) => (
|
||||
<div key={feature} className="flex items-start gap-2 text-sm">
|
||||
<CheckCircle className="w-4 h-4 text-green-600 mt-0.5 flex-shrink-0" />
|
||||
<span className="text-gray-700 dark:text-gray-300">{feature}</span>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</Card>
|
||||
</div>
|
||||
|
||||
{hasPaymentMethods ? (
|
||||
<div className="p-4 border border-gray-200 dark:border-gray-700 rounded-lg">
|
||||
<div className="text-sm font-medium text-gray-700 dark:text-gray-200 mb-2">Select payment method</div>
|
||||
<div className="flex flex-wrap gap-3">
|
||||
{paymentMethods.map((method) => (
|
||||
<label
|
||||
key={method.id}
|
||||
className={`px-3 py-2 rounded-lg border cursor-pointer text-sm ${
|
||||
selectedPaymentMethod === (method.type || method.id)
|
||||
? 'border-blue-500 bg-blue-50 dark:bg-blue-900/30'
|
||||
: 'border-gray-200 dark:border-gray-700'
|
||||
}`}
|
||||
>
|
||||
<input
|
||||
type="radio"
|
||||
className="sr-only"
|
||||
checked={selectedPaymentMethod === (method.type || method.id)}
|
||||
onChange={() => setSelectedPaymentMethod(method.type || method.id)}
|
||||
/>
|
||||
<div className="font-semibold text-gray-900 dark:text-white">{method.display_name}</div>
|
||||
<div className="text-xs text-gray-500 dark:text-gray-400">{method.type}</div>
|
||||
</label>
|
||||
))}
|
||||
</div>
|
||||
{/* Upgrade/Downgrade Section with Pricing Table */}
|
||||
<div className="mt-8 pt-8 border-t border-gray-200 dark:border-gray-700">
|
||||
<div className="mx-auto" style={{ maxWidth: '1200px' }}>
|
||||
<PricingTable
|
||||
variant="1"
|
||||
plans={plans.map(plan => {
|
||||
const discount = plan.annual_discount_percent || 15;
|
||||
return {
|
||||
id: plan.id,
|
||||
name: plan.name,
|
||||
monthlyPrice: plan.price || 0,
|
||||
price: plan.price || 0,
|
||||
annualDiscountPercent: discount,
|
||||
period: `/${plan.interval || 'month'}`,
|
||||
description: plan.description || 'Standard plan',
|
||||
features: plan.features && plan.features.length > 0
|
||||
? plan.features
|
||||
: ['Monthly credits included', 'Module access', 'Email support'],
|
||||
buttonText: plan.id === currentPlanId ? 'Current Plan' : 'Select Plan',
|
||||
highlighted: plan.is_featured || false,
|
||||
disabled: plan.id === currentPlanId || planLoadingId === plan.id,
|
||||
};
|
||||
})}
|
||||
showToggle={true}
|
||||
onPlanSelect={(plan) => plan.id && handleSelectPlan(plan.id)}
|
||||
/>
|
||||
</div>
|
||||
) : (
|
||||
<div className="p-4 rounded-lg border border-amber-200 bg-amber-50 text-amber-800 dark:border-amber-800 dark:bg-amber-900/20 dark:text-amber-200">
|
||||
No payment methods available. Please contact support or add one from the Payment Methods tab.
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
|
||||
{plans.map((plan) => {
|
||||
const isCurrent = plan.id === currentPlanId;
|
||||
const price = plan.price ? `$${plan.price}/${plan.interval || 'month'}` : 'Custom';
|
||||
return (
|
||||
<Card key={plan.id} className="p-6 relative border border-gray-200 dark:border-gray-700">
|
||||
<div className="mb-4">
|
||||
<h3 className="text-lg font-semibold">{plan.name}</h3>
|
||||
<div className="text-3xl font-bold text-gray-900 dark:text-white mt-2">{price}</div>
|
||||
<div className="text-sm text-gray-500">{plan.description || 'Standard plan'}</div>
|
||||
</div>
|
||||
<div className="space-y-3 mb-6">
|
||||
{(plan.features && plan.features.length > 0 ? plan.features : ['Monthly credits included', 'Module access per plan', 'Email support']).map((feature) => (
|
||||
<div key={feature} className="flex items-center gap-2 text-sm">
|
||||
<CheckCircle className="w-4 h-4 text-green-600" />
|
||||
<span>{feature}</span>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
<Button
|
||||
variant={isCurrent ? 'outline' : 'primary'}
|
||||
tone="brand"
|
||||
fullWidth
|
||||
disabled={isCurrent || planLoadingId === plan.id}
|
||||
onClick={() => handleSelectPlan(plan.id)}
|
||||
>
|
||||
{planLoadingId === plan.id
|
||||
? 'Updating...'
|
||||
: isCurrent
|
||||
? 'Current Plan'
|
||||
: 'Select Plan'}
|
||||
</Button>
|
||||
</Card>
|
||||
);
|
||||
})}
|
||||
{plans.length === 0 && (
|
||||
<div className="col-span-3 text-center py-12 text-gray-500">
|
||||
No plans available. Please contact support.
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<Card className="p-6 bg-blue-50 dark:bg-blue-900/20 border-blue-200 dark:border-blue-800">
|
||||
<h3 className="font-semibold text-blue-900 dark:text-blue-100 mb-2">Plan Change Policy</h3>
|
||||
<ul className="space-y-2 text-sm text-blue-800 dark:text-blue-200">
|
||||
<li>• Upgrades take effect immediately and you'll be charged a prorated amount</li>
|
||||
<li>• Downgrades take effect at the end of your current billing period</li>
|
||||
<li>• Unused credits from your current plan will carry over</li>
|
||||
<li>• You can cancel your subscription at any time</li>
|
||||
</ul>
|
||||
</Card>
|
||||
<Card className="p-6 bg-blue-50 dark:bg-blue-900/20 border-blue-200 dark:border-blue-800 mt-6">
|
||||
<h3 className="font-semibold text-blue-900 dark:text-blue-100 mb-2">Plan Change Policy</h3>
|
||||
<ul className="space-y-2 text-sm text-blue-800 dark:text-blue-200">
|
||||
<li>• Upgrades take effect immediately and you'll be charged a prorated amount</li>
|
||||
<li>• Downgrades take effect at the end of your current billing period</li>
|
||||
<li>• Unused credits from your current plan will carry over</li>
|
||||
<li>• You can cancel your subscription at any time</li>
|
||||
</ul>
|
||||
</Card>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
@@ -623,69 +568,53 @@ export default function PlansAndBillingPage() {
|
||||
</div>
|
||||
</div>
|
||||
</Card>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Purchase Credits Tab */}
|
||||
{activeTab === 'purchase' && (
|
||||
<div className="space-y-6">
|
||||
{hasPaymentMethods ? (
|
||||
<div className="p-4 border border-gray-200 dark:border-gray-700 rounded-lg">
|
||||
<div className="text-sm font-medium text-gray-700 dark:text-gray-200 mb-2">Select payment method</div>
|
||||
<div className="flex flex-wrap gap-3">
|
||||
{paymentMethods.map((method) => (
|
||||
<label
|
||||
key={method.id}
|
||||
className={`px-3 py-2 rounded-lg border cursor-pointer text-sm ${
|
||||
selectedPaymentMethod === (method.type || method.id)
|
||||
? 'border-blue-500 bg-blue-50 dark:bg-blue-900/30'
|
||||
: 'border-gray-200 dark:border-gray-700'
|
||||
}`}
|
||||
>
|
||||
<input
|
||||
type="radio"
|
||||
className="sr-only"
|
||||
checked={selectedPaymentMethod === (method.type || method.id)}
|
||||
onChange={() => setSelectedPaymentMethod(method.type || method.id)}
|
||||
/>
|
||||
<div className="font-semibold text-gray-900 dark:text-white">{method.display_name}</div>
|
||||
<div className="text-xs text-gray-500 dark:text-gray-400">{method.type}</div>
|
||||
</label>
|
||||
))}
|
||||
</div>
|
||||
{/* Purchase Credits Section - Single Row */}
|
||||
<div className="mt-8 pt-8 border-t border-gray-200 dark:border-gray-700">
|
||||
<div className="mb-6">
|
||||
<h2 className="text-xl font-semibold mb-2">Purchase Additional Credits</h2>
|
||||
<p className="text-gray-600 dark:text-gray-400">Top up your credit balance with our packages</p>
|
||||
</div>
|
||||
) : (
|
||||
<div className="p-4 rounded-lg border border-amber-200 bg-amber-50 text-amber-800 dark:border-amber-800 dark:bg-amber-900/20 dark:text-amber-200">
|
||||
No payment methods available. Please contact support or add one from the Payment Methods tab.
|
||||
</div>
|
||||
)}
|
||||
|
||||
<Card className="p-6">
|
||||
<h2 className="text-lg font-semibold mb-4">Credit Packages</h2>
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
|
||||
{packages.map((pkg) => (
|
||||
<div key={pkg.id} className="border border-gray-200 dark:border-gray-700 rounded-lg p-6 hover:border-blue-500 transition-colors">
|
||||
<div className="text-lg font-semibold text-gray-900 dark:text-white">{pkg.name}</div>
|
||||
<div className="text-3xl font-bold text-blue-600 dark:text-blue-400 mt-2">
|
||||
{pkg.credits.toLocaleString()} <span className="text-sm text-gray-500">credits</span>
|
||||
<div className="overflow-x-auto">
|
||||
<div className="flex gap-4 pb-4">
|
||||
{packages.map((pkg) => (
|
||||
<article key={pkg.id} className="rounded-2xl border border-gray-200 bg-white dark:border-gray-800 dark:bg-white/3 hover:border-blue-500 dark:hover:border-blue-500 transition-colors flex-shrink-0" style={{ minWidth: '280px' }}>
|
||||
<div className="relative p-5 pb-6">
|
||||
<div className="mb-3 inline-flex h-10 w-10 items-center justify-center rounded-lg bg-blue-50 dark:bg-blue-500/10">
|
||||
<svg className="w-6 h-6 text-blue-600 dark:text-blue-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 8c-1.657 0-3 .895-3 2s1.343 2 3 2 3 .895 3 2-1.343 2-3 2m0-8c1.11 0 2.08.402 2.599 1M12 8V7m0 1v8m0 0v1m0-1c-1.11 0-2.08-.402-2.599-1M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
|
||||
</svg>
|
||||
</div>
|
||||
<h3 className="mb-2 text-lg font-semibold text-gray-800 dark:text-white/90">
|
||||
{pkg.name}
|
||||
</h3>
|
||||
<div className="flex items-baseline gap-2 mb-1">
|
||||
<span className="text-3xl font-bold text-blue-600 dark:text-blue-400">{pkg.credits.toLocaleString()}</span>
|
||||
<span className="text-sm text-gray-500 dark:text-gray-400">credits</span>
|
||||
</div>
|
||||
<div className="text-2xl font-semibold text-gray-900 dark:text-white mb-2">
|
||||
${pkg.price}
|
||||
</div>
|
||||
{pkg.description && (
|
||||
<p className="text-sm text-gray-500 dark:text-gray-400">
|
||||
{pkg.description}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
<div className="border-t border-gray-200 p-4 dark:border-gray-800">
|
||||
<Button
|
||||
variant="primary"
|
||||
tone="brand"
|
||||
onClick={() => handlePurchase(pkg.id)}
|
||||
fullWidth
|
||||
size="md"
|
||||
disabled={purchaseLoadingId === pkg.id || (!hasPaymentMethods && paymentMethods.length > 0)}
|
||||
>
|
||||
{purchaseLoadingId === pkg.id ? 'Processing...' : 'Purchase'}
|
||||
</Button>
|
||||
</div>
|
||||
<div className="text-2xl font-semibold text-gray-900 dark:text-white mt-4">
|
||||
${pkg.price}
|
||||
</div>
|
||||
{pkg.description && (
|
||||
<div className="text-sm text-gray-600 dark:text-gray-400 mt-2">{pkg.description}</div>
|
||||
)}
|
||||
<Button
|
||||
variant="primary"
|
||||
tone="brand"
|
||||
onClick={() => handlePurchase(pkg.id)}
|
||||
fullWidth
|
||||
className="mt-6"
|
||||
disabled={purchaseLoadingId === pkg.id || (!hasPaymentMethods && paymentMethods.length > 0)}
|
||||
>
|
||||
{purchaseLoadingId === pkg.id ? 'Processing...' : 'Purchase'}
|
||||
</Button>
|
||||
</div>
|
||||
</article>
|
||||
))}
|
||||
{packages.length === 0 && (
|
||||
<div className="col-span-3 text-center py-12 text-gray-500">
|
||||
@@ -693,88 +622,88 @@ export default function PlansAndBillingPage() {
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</Card>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Billing History Tab */}
|
||||
{activeTab === 'invoices' && (
|
||||
<Card className="overflow-hidden">
|
||||
<div className="overflow-x-auto">
|
||||
<table className="w-full">
|
||||
<thead className="bg-gray-50 dark:bg-gray-800 border-b border-gray-200 dark:border-gray-700">
|
||||
<tr>
|
||||
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
|
||||
Invoice
|
||||
</th>
|
||||
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
|
||||
Date
|
||||
</th>
|
||||
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
|
||||
Amount
|
||||
</th>
|
||||
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
|
||||
Status
|
||||
</th>
|
||||
<th className="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase tracking-wider">
|
||||
Actions
|
||||
</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody className="divide-y divide-gray-200 dark:divide-gray-700">
|
||||
{invoices.length === 0 ? (
|
||||
<div className="space-y-6">
|
||||
{/* Invoices Section */}
|
||||
<Card className="overflow-hidden">
|
||||
<div className="p-6 border-b border-gray-200 dark:border-gray-700">
|
||||
<h2 className="text-lg font-semibold">Invoices</h2>
|
||||
</div>
|
||||
<div className="overflow-x-auto">
|
||||
<table className="w-full">
|
||||
<thead className="bg-gray-50 dark:bg-gray-800 border-b border-gray-200 dark:border-gray-700">
|
||||
<tr>
|
||||
<td colSpan={5} className="px-6 py-8 text-center text-gray-500">
|
||||
<FileText className="w-12 h-12 mx-auto mb-2 text-gray-400" />
|
||||
No invoices yet
|
||||
</td>
|
||||
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
|
||||
Invoice
|
||||
</th>
|
||||
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
|
||||
Date
|
||||
</th>
|
||||
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
|
||||
Amount
|
||||
</th>
|
||||
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
|
||||
Status
|
||||
</th>
|
||||
<th className="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase tracking-wider">
|
||||
Actions
|
||||
</th>
|
||||
</tr>
|
||||
) : (
|
||||
invoices.map((invoice) => (
|
||||
<tr key={invoice.id} className="hover:bg-gray-50 dark:hover:bg-gray-800">
|
||||
<td className="px-6 py-4 font-medium">{invoice.invoice_number}</td>
|
||||
<td className="px-6 py-4 text-sm text-gray-600 dark:text-gray-400">
|
||||
{new Date(invoice.created_at).toLocaleDateString()}
|
||||
</td>
|
||||
<td className="px-6 py-4 font-semibold">${invoice.total_amount}</td>
|
||||
<td className="px-6 py-4">
|
||||
<Badge
|
||||
variant="light"
|
||||
color={invoice.status === 'paid' ? 'success' : 'warning'}
|
||||
>
|
||||
{invoice.status}
|
||||
</Badge>
|
||||
</td>
|
||||
<td className="px-6 py-4 text-right">
|
||||
<Button
|
||||
variant="ghost"
|
||||
tone="brand"
|
||||
size="sm"
|
||||
startIcon={<Download className="w-4 h-4" />}
|
||||
className="ml-auto"
|
||||
onClick={() => handleDownloadInvoice(invoice.id)}
|
||||
>
|
||||
Download
|
||||
</Button>
|
||||
</thead>
|
||||
<tbody className="divide-y divide-gray-200 dark:divide-gray-700">
|
||||
{invoices.length === 0 ? (
|
||||
<tr>
|
||||
<td colSpan={5} className="px-6 py-8 text-center text-gray-500">
|
||||
<FileText className="w-12 h-12 mx-auto mb-2 text-gray-400" />
|
||||
No invoices yet
|
||||
</td>
|
||||
</tr>
|
||||
))
|
||||
)}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</Card>
|
||||
)}
|
||||
) : (
|
||||
invoices.map((invoice) => (
|
||||
<tr key={invoice.id} className="hover:bg-gray-50 dark:hover:bg-gray-800">
|
||||
<td className="px-6 py-4 font-medium">{invoice.invoice_number}</td>
|
||||
<td className="px-6 py-4 text-sm text-gray-600 dark:text-gray-400">
|
||||
{new Date(invoice.created_at).toLocaleDateString()}
|
||||
</td>
|
||||
<td className="px-6 py-4 font-semibold">${invoice.total_amount}</td>
|
||||
<td className="px-6 py-4">
|
||||
<Badge
|
||||
variant="light"
|
||||
color={invoice.status === 'paid' ? 'success' : 'warning'}
|
||||
>
|
||||
{invoice.status}
|
||||
</Badge>
|
||||
</td>
|
||||
<td className="px-6 py-4 text-right">
|
||||
<Button
|
||||
variant="ghost"
|
||||
tone="brand"
|
||||
size="sm"
|
||||
startIcon={<Download className="w-4 h-4" />}
|
||||
className="ml-auto"
|
||||
onClick={() => handleDownloadInvoice(invoice.id)}
|
||||
>
|
||||
Download
|
||||
</Button>
|
||||
</td>
|
||||
</tr>
|
||||
))
|
||||
)}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</Card>
|
||||
|
||||
{/* Payments Tab */}
|
||||
{activeTab === 'payments' && (
|
||||
<div className="space-y-6">
|
||||
<Card className="p-6">
|
||||
<div className="flex items-center justify-between mb-4">
|
||||
<div>
|
||||
<h2 className="text-lg font-semibold">Payments</h2>
|
||||
<p className="text-sm text-gray-600 dark:text-gray-400">Recent payments and manual submissions</p>
|
||||
</div>
|
||||
{/* Payments Section */}
|
||||
<Card className="overflow-hidden">
|
||||
<div className="p-6 border-b border-gray-200 dark:border-gray-700">
|
||||
<h2 className="text-lg font-semibold">Payments</h2>
|
||||
<p className="text-sm text-gray-600 dark:text-gray-400">Recent payments and manual submissions</p>
|
||||
</div>
|
||||
<div className="overflow-x-auto">
|
||||
<table className="w-full">
|
||||
@@ -831,113 +760,13 @@ export default function PlansAndBillingPage() {
|
||||
</div>
|
||||
</Card>
|
||||
|
||||
<Card className="p-6">
|
||||
<h3 className="text-lg font-semibold mb-4">Submit Manual Payment</h3>
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">Invoice ID (optional)</label>
|
||||
<input
|
||||
type="number"
|
||||
value={manualPayment.invoice_id}
|
||||
onChange={(e) => setManualPayment((p) => ({ ...p, invoice_id: e.target.value }))}
|
||||
className="w-full px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-800 text-gray-900 dark:text-white"
|
||||
placeholder="Invoice ID"
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">Amount</label>
|
||||
<input
|
||||
type="text"
|
||||
value={manualPayment.amount}
|
||||
onChange={(e) => setManualPayment((p) => ({ ...p, amount: e.target.value }))}
|
||||
className="w-full px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-800 text-gray-900 dark:text-white"
|
||||
placeholder="e.g., 99.00"
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">Payment Method</label>
|
||||
<input
|
||||
type="text"
|
||||
value={manualPayment.payment_method}
|
||||
onChange={(e) => setManualPayment((p) => ({ ...p, payment_method: e.target.value }))}
|
||||
className="w-full px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-800 text-gray-900 dark:text-white"
|
||||
placeholder="bank_transfer / local_wallet / manual"
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">Reference</label>
|
||||
<input
|
||||
type="text"
|
||||
value={manualPayment.reference}
|
||||
onChange={(e) => setManualPayment((p) => ({ ...p, reference: e.target.value }))}
|
||||
className="w-full px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-800 text-gray-900 dark:text-white"
|
||||
placeholder="Reference or transaction id"
|
||||
/>
|
||||
</div>
|
||||
<div className="md:col-span-2">
|
||||
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">Notes</label>
|
||||
<textarea
|
||||
value={manualPayment.notes}
|
||||
onChange={(e) => setManualPayment((p) => ({ ...p, notes: e.target.value }))}
|
||||
className="w-full px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-800 text-gray-900 dark:text-white"
|
||||
placeholder="Optional notes"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<div className="mt-4 flex justify-end">
|
||||
<Button variant="primary" tone="brand" onClick={handleSubmitManualPayment}>
|
||||
Submit Manual Payment
|
||||
</Button>
|
||||
</div>
|
||||
</Card>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Payment Methods Tab */}
|
||||
{activeTab === 'payment-methods' && (
|
||||
<div className="space-y-6">
|
||||
{/* Payment Methods Section */}
|
||||
<Card className="p-6">
|
||||
<div className="flex items-center justify-between mb-4">
|
||||
<h2 className="text-lg font-semibold">Payment Methods</h2>
|
||||
</div>
|
||||
<div className="grid grid-cols-1 md:grid-cols-3 gap-4 mb-4">
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">Type</label>
|
||||
<select
|
||||
value={newPaymentMethod.type}
|
||||
onChange={(e) => setNewPaymentMethod((p) => ({ ...p, type: e.target.value }))}
|
||||
className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-800 text-gray-900 dark:text-white"
|
||||
>
|
||||
<option value="bank_transfer">Bank Transfer</option>
|
||||
<option value="local_wallet">Local Wallet</option>
|
||||
<option value="manual">Manual</option>
|
||||
</select>
|
||||
<h2 className="text-lg font-semibold">Payment Methods</h2>
|
||||
<p className="text-sm text-gray-600 dark:text-gray-400">Manage your payment methods</p>
|
||||
</div>
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">Display Name</label>
|
||||
<input
|
||||
type="text"
|
||||
value={newPaymentMethod.display_name}
|
||||
onChange={(e) => setNewPaymentMethod((p) => ({ ...p, display_name: e.target.value }))}
|
||||
className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-800 text-gray-900 dark:text-white"
|
||||
placeholder="e.g., Bank Transfer (USD)"
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">Instructions (optional)</label>
|
||||
<input
|
||||
type="text"
|
||||
value={newPaymentMethod.instructions}
|
||||
onChange={(e) => setNewPaymentMethod((p) => ({ ...p, instructions: e.target.value }))}
|
||||
className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-800 text-gray-900 dark:text-white"
|
||||
placeholder="Where to send payment"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<div className="mb-4">
|
||||
<Button variant="primary" tone="brand" onClick={handleAddPaymentMethod}>
|
||||
Add Payment Method
|
||||
</Button>
|
||||
</div>
|
||||
<div className="space-y-4">
|
||||
{paymentMethods.map((method) => (
|
||||
|
||||
980
frontend/src/pages/account/PlansAndBillingPage.tsx.backup
Normal file
980
frontend/src/pages/account/PlansAndBillingPage.tsx.backup
Normal file
@@ -0,0 +1,980 @@
|
||||
/**
|
||||
* Plans & Billing Page - Consolidated
|
||||
* Tabs: Current Plan, Upgrade/Downgrade, Credits Overview, Purchase Credits, Billing History, Payment Methods
|
||||
*/
|
||||
|
||||
import { useState, useEffect, useRef } from 'react';
|
||||
import {
|
||||
CreditCard, Package, TrendingUp, FileText, Wallet, ArrowUpCircle,
|
||||
Loader2, AlertCircle, CheckCircle, Download
|
||||
} from 'lucide-react';
|
||||
import { Card } from '../../components/ui/card';
|
||||
import Badge from '../../components/ui/badge/Badge';
|
||||
import Button from '../../components/ui/button/Button';
|
||||
import { useToast } from '../../components/ui/toast/ToastContainer';
|
||||
import {
|
||||
getCreditBalance,
|
||||
getCreditPackages,
|
||||
getInvoices,
|
||||
getAvailablePaymentMethods,
|
||||
purchaseCreditPackage,
|
||||
downloadInvoicePDF,
|
||||
getPayments,
|
||||
submitManualPayment,
|
||||
createPaymentMethod,
|
||||
deletePaymentMethod,
|
||||
setDefaultPaymentMethod,
|
||||
type CreditBalance,
|
||||
type CreditPackage,
|
||||
type Invoice,
|
||||
type PaymentMethod,
|
||||
type Payment,
|
||||
getPlans,
|
||||
getSubscriptions,
|
||||
createSubscription,
|
||||
cancelSubscription,
|
||||
type Plan,
|
||||
type Subscription,
|
||||
} from '../../services/billing.api';
|
||||
import { useAuthStore } from '../../store/authStore';
|
||||
|
||||
type TabType = 'plan' | 'credits' | 'billing-history';
|
||||
|
||||
export default function PlansAndBillingPage() {
|
||||
const [activeTab, setActiveTab] = useState<TabType>('plan');
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string>('');
|
||||
const [planLoadingId, setPlanLoadingId] = useState<number | null>(null);
|
||||
const [purchaseLoadingId, setPurchaseLoadingId] = useState<number | null>(null);
|
||||
|
||||
// Data states
|
||||
const [creditBalance, setCreditBalance] = useState<CreditBalance | null>(null);
|
||||
const [packages, setPackages] = useState<CreditPackage[]>([]);
|
||||
const [invoices, setInvoices] = useState<Invoice[]>([]);
|
||||
const [payments, setPayments] = useState<Payment[]>([]);
|
||||
const [paymentMethods, setPaymentMethods] = useState<PaymentMethod[]>([]);
|
||||
const [plans, setPlans] = useState<Plan[]>([]);
|
||||
const [subscriptions, setSubscriptions] = useState<Subscription[]>([]);
|
||||
const [selectedPaymentMethod, setSelectedPaymentMethod] = useState<string | undefined>(undefined);
|
||||
const [manualPayment, setManualPayment] = useState({
|
||||
invoice_id: '',
|
||||
amount: '',
|
||||
payment_method: '',
|
||||
reference: '',
|
||||
notes: '',
|
||||
});
|
||||
const [newPaymentMethod, setNewPaymentMethod] = useState({
|
||||
type: 'bank_transfer',
|
||||
display_name: '',
|
||||
instructions: '',
|
||||
});
|
||||
const { user } = useAuthStore.getState();
|
||||
const hasLoaded = useRef(false);
|
||||
const isAwsAdmin = user?.account?.slug === 'aws-admin';
|
||||
const handleBillingError = (err: any, fallback: string) => {
|
||||
const message = err?.message || fallback;
|
||||
setError(message);
|
||||
toast?.error?.(message);
|
||||
};
|
||||
|
||||
const toast = useToast();
|
||||
|
||||
useEffect(() => {
|
||||
if (hasLoaded.current) return;
|
||||
hasLoaded.current = true;
|
||||
loadData();
|
||||
}, []);
|
||||
|
||||
const loadData = async (allowRetry = true) => {
|
||||
try {
|
||||
setLoading(true);
|
||||
// Fetch in controlled sequence to avoid burst 429s on auth/system scopes
|
||||
const balanceData = await getCreditBalance();
|
||||
|
||||
// Small gap between auth endpoints to satisfy tight throttles
|
||||
const wait = (ms: number) => new Promise((res) => setTimeout(res, ms));
|
||||
|
||||
const packagesPromise = getCreditPackages();
|
||||
const invoicesPromise = getInvoices({});
|
||||
const paymentsPromise = getPayments({});
|
||||
const methodsPromise = getAvailablePaymentMethods();
|
||||
|
||||
const plansData = await getPlans();
|
||||
await wait(400);
|
||||
|
||||
// Subscriptions: retry once on 429 after short backoff; do not hard-fail page
|
||||
let subsData: { results: Subscription[] } = { results: [] };
|
||||
try {
|
||||
subsData = await getSubscriptions();
|
||||
} catch (subErr: any) {
|
||||
if (subErr?.status === 429 && allowRetry) {
|
||||
await wait(2500);
|
||||
try {
|
||||
subsData = await getSubscriptions();
|
||||
} catch {
|
||||
subsData = { results: [] };
|
||||
}
|
||||
} else {
|
||||
subsData = { results: [] };
|
||||
}
|
||||
}
|
||||
|
||||
const [packagesData, invoicesData, paymentsData, methodsData] = await Promise.all([
|
||||
packagesPromise,
|
||||
invoicesPromise,
|
||||
paymentsPromise,
|
||||
methodsPromise,
|
||||
]);
|
||||
|
||||
setCreditBalance(balanceData);
|
||||
setPackages(packagesData.results || []);
|
||||
setInvoices(invoicesData.results || []);
|
||||
setPayments(paymentsData.results || []);
|
||||
|
||||
// Prefer manual payment method id 14 as default (tenant-facing)
|
||||
const methods = (methodsData.results || []).filter((m) => m.is_enabled !== false);
|
||||
setPaymentMethods(methods);
|
||||
if (methods.length > 0) {
|
||||
// Preferred ordering: bank_transfer (default), then manual
|
||||
const bank = methods.find((m) => m.type === 'bank_transfer');
|
||||
const manual = methods.find((m) => m.type === 'manual');
|
||||
const selected =
|
||||
bank ||
|
||||
manual ||
|
||||
methods.find((m) => m.is_default) ||
|
||||
methods[0];
|
||||
setSelectedPaymentMethod((prev) => prev || selected.type || selected.id);
|
||||
}
|
||||
|
||||
// Surface all active plans (avoid hiding plans and showing empty state)
|
||||
const activePlans = (plansData.results || []).filter((p) => p.is_active !== false);
|
||||
// Exclude Enterprise plan for non aws-admin accounts
|
||||
const filteredPlans = activePlans.filter((p) => {
|
||||
const name = (p.name || '').toLowerCase();
|
||||
const slug = (p.slug || '').toLowerCase();
|
||||
const isEnterprise = name.includes('enterprise') || slug === 'enterprise';
|
||||
return isAwsAdmin ? true : !isEnterprise;
|
||||
});
|
||||
|
||||
// Ensure the user's assigned plan is included even if subscriptions list is empty
|
||||
const accountPlan = user?.account?.plan;
|
||||
const isAccountEnterprise = (() => {
|
||||
if (!accountPlan) return false;
|
||||
const name = (accountPlan.name || '').toLowerCase();
|
||||
const slug = (accountPlan.slug || '').toLowerCase();
|
||||
return name.includes('enterprise') || slug === 'enterprise';
|
||||
})();
|
||||
|
||||
const shouldIncludeAccountPlan = accountPlan && (!isAccountEnterprise || isAwsAdmin);
|
||||
if (shouldIncludeAccountPlan && !filteredPlans.find((p) => p.id === accountPlan.id)) {
|
||||
filteredPlans.push(accountPlan as any);
|
||||
}
|
||||
setPlans(filteredPlans);
|
||||
const subs = subsData.results || [];
|
||||
if (subs.length === 0 && shouldIncludeAccountPlan && accountPlan) {
|
||||
subs.push({
|
||||
id: accountPlan.id || 0,
|
||||
plan: accountPlan,
|
||||
status: 'active',
|
||||
} as any);
|
||||
}
|
||||
setSubscriptions(subs);
|
||||
} catch (err: any) {
|
||||
// Handle throttling gracefully: don't block the page on subscriptions throttle
|
||||
if (err?.status === 429 && allowRetry) {
|
||||
setError('Request was throttled. Retrying...');
|
||||
setTimeout(() => loadData(false), 2500);
|
||||
} else if (err?.status === 429) {
|
||||
setError(''); // suppress lingering banner
|
||||
} else {
|
||||
setError(err.message || 'Failed to load billing data');
|
||||
console.error('Billing load error:', err);
|
||||
}
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleSelectPlan = async (planId: number) => {
|
||||
try {
|
||||
if (!selectedPaymentMethod && paymentMethods.length > 0) {
|
||||
setError('Select a payment method to continue');
|
||||
return;
|
||||
}
|
||||
setPlanLoadingId(planId);
|
||||
await createSubscription({ plan_id: planId, payment_method: selectedPaymentMethod });
|
||||
toast?.success?.('Subscription updated');
|
||||
await loadData();
|
||||
} catch (err: any) {
|
||||
handleBillingError(err, 'Failed to update subscription');
|
||||
} finally {
|
||||
setPlanLoadingId(null);
|
||||
}
|
||||
};
|
||||
|
||||
const handleCancelSubscription = async () => {
|
||||
if (!currentSubscription?.id) {
|
||||
setError('No active subscription to cancel');
|
||||
return;
|
||||
}
|
||||
try {
|
||||
setPlanLoadingId(currentSubscription.id);
|
||||
await cancelSubscription(currentSubscription.id);
|
||||
toast?.success?.('Subscription cancellation requested');
|
||||
await loadData();
|
||||
} catch (err: any) {
|
||||
handleBillingError(err, 'Failed to cancel subscription');
|
||||
} finally {
|
||||
setPlanLoadingId(null);
|
||||
}
|
||||
};
|
||||
|
||||
const handlePurchase = async (packageId: number) => {
|
||||
try {
|
||||
if (!selectedPaymentMethod && paymentMethods.length > 0) {
|
||||
setError('Select a payment method to continue');
|
||||
return;
|
||||
}
|
||||
setPurchaseLoadingId(packageId);
|
||||
await purchaseCreditPackage({
|
||||
package_id: packageId,
|
||||
payment_method: (selectedPaymentMethod as any) || 'stripe',
|
||||
});
|
||||
await loadData();
|
||||
} catch (err: any) {
|
||||
handleBillingError(err, 'Failed to purchase credits');
|
||||
} finally {
|
||||
setPurchaseLoadingId(null);
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleDownloadInvoice = async (invoiceId: number) => {
|
||||
try {
|
||||
const blob = await downloadInvoicePDF(invoiceId);
|
||||
const url = window.URL.createObjectURL(blob);
|
||||
const link = document.createElement('a');
|
||||
link.href = url;
|
||||
link.download = `invoice-${invoiceId}.pdf`;
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
link.remove();
|
||||
window.URL.revokeObjectURL(url);
|
||||
} catch (err: any) {
|
||||
handleBillingError(err, 'Failed to download invoice');
|
||||
}
|
||||
};
|
||||
|
||||
const handleSubmitManualPayment = async () => {
|
||||
try {
|
||||
const payload = {
|
||||
invoice_id: manualPayment.invoice_id ? Number(manualPayment.invoice_id) : undefined,
|
||||
amount: manualPayment.amount,
|
||||
payment_method: manualPayment.payment_method || (selectedPaymentMethod as any) || 'manual',
|
||||
reference: manualPayment.reference,
|
||||
notes: manualPayment.notes,
|
||||
};
|
||||
await submitManualPayment(payload as any);
|
||||
toast?.success?.('Manual payment submitted');
|
||||
setManualPayment({ invoice_id: '', amount: '', payment_method: '', reference: '', notes: '' });
|
||||
await loadData();
|
||||
} catch (err: any) {
|
||||
handleBillingError(err, 'Failed to submit payment');
|
||||
}
|
||||
};
|
||||
|
||||
const handleAddPaymentMethod = async () => {
|
||||
if (!newPaymentMethod.display_name.trim()) {
|
||||
setError('Payment method name is required');
|
||||
return;
|
||||
}
|
||||
try {
|
||||
await createPaymentMethod(newPaymentMethod as any);
|
||||
toast?.success?.('Payment method added');
|
||||
setNewPaymentMethod({ type: 'bank_transfer', display_name: '', instructions: '' });
|
||||
await loadData();
|
||||
} catch (err: any) {
|
||||
handleBillingError(err, 'Failed to add payment method');
|
||||
}
|
||||
};
|
||||
|
||||
const handleRemovePaymentMethod = async (id: string) => {
|
||||
try {
|
||||
await deletePaymentMethod(id);
|
||||
toast?.success?.('Payment method removed');
|
||||
await loadData();
|
||||
} catch (err: any) {
|
||||
handleBillingError(err, 'Failed to remove payment method');
|
||||
}
|
||||
};
|
||||
|
||||
const handleSetDefaultPaymentMethod = async (id: string) => {
|
||||
try {
|
||||
await setDefaultPaymentMethod(id);
|
||||
toast?.success?.('Default payment method updated');
|
||||
await loadData();
|
||||
} catch (err: any) {
|
||||
handleBillingError(err, 'Failed to set default');
|
||||
}
|
||||
};
|
||||
|
||||
if (loading) {
|
||||
return (
|
||||
<div className="flex items-center justify-center min-h-screen">
|
||||
<Loader2 className="w-8 h-8 animate-spin text-blue-600" />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
const currentSubscription = subscriptions.find((sub) => sub.status === 'active') || subscriptions[0];
|
||||
const currentPlanId = typeof currentSubscription?.plan === 'object' ? currentSubscription.plan.id : currentSubscription?.plan;
|
||||
// Fallback to account plan if subscription is missing
|
||||
const accountPlanId = user?.account?.plan?.id;
|
||||
const effectivePlanId = currentPlanId || accountPlanId;
|
||||
const currentPlan = plans.find((p) => p.id === effectivePlanId) || user?.account?.plan;
|
||||
const hasActivePlan = Boolean(effectivePlanId);
|
||||
const hasPaymentMethods = paymentMethods.length > 0;
|
||||
const subscriptionStatus = currentSubscription?.status || (hasActivePlan ? 'active' : 'none');
|
||||
const hasPendingManualPayment = payments.some((p) => p.status === 'pending_approval');
|
||||
|
||||
const tabs = [
|
||||
{ id: 'plan' as TabType, label: 'Current Plan', icon: <Package className="w-4 h-4" /> },
|
||||
{ id: 'credits' as TabType, label: 'Credits Overview', icon: <TrendingUp className="w-4 h-4" /> },
|
||||
{ id: 'billing-history' as TabType, label: 'Billing History', icon: <FileText className="w-4 h-4" /> },
|
||||
];
|
||||
|
||||
return (
|
||||
<div className="p-6">
|
||||
<div className="mb-6">
|
||||
<h1 className="text-2xl font-bold text-gray-900 dark:text-white">Plans & Billing</h1>
|
||||
<p className="text-gray-600 dark:text-gray-400 mt-1">
|
||||
Manage your subscription, credits, and billing information
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{/* Activation / pending payment notice */}
|
||||
{!hasActivePlan && (
|
||||
<div className="mb-4 p-4 rounded-lg border border-amber-200 bg-amber-50 text-amber-800 dark:border-amber-800 dark:bg-amber-900/20 dark:text-amber-200">
|
||||
No active plan. Choose a plan below to activate your account.
|
||||
</div>
|
||||
)}
|
||||
{hasPendingManualPayment && (
|
||||
<div className="mb-4 p-4 rounded-lg border border-blue-200 bg-blue-50 text-blue-800 dark:border-blue-800 dark:bg-blue-900/20 dark:text-blue-100">
|
||||
We received your manual payment. It’s pending admin approval; activation will complete once approved.
|
||||
</div>
|
||||
)}
|
||||
|
||||
{error && (
|
||||
<div className="mb-6 p-4 bg-red-50 dark:bg-red-900/20 border border-red-200 dark:border-red-800 rounded-lg flex items-center gap-3">
|
||||
<AlertCircle className="w-5 h-5 text-red-600" />
|
||||
<p className="text-red-800 dark:text-red-200">{error}</p>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Tabs */}
|
||||
<div className="mb-6 border-b border-gray-200 dark:border-gray-700">
|
||||
<nav className="-mb-px flex space-x-8 overflow-x-auto">
|
||||
{tabs.map((tab) => (
|
||||
<button
|
||||
type="button"
|
||||
key={tab.id}
|
||||
onClick={() => setActiveTab(tab.id)}
|
||||
className={`
|
||||
flex items-center gap-2 py-4 px-1 border-b-2 font-medium text-sm whitespace-nowrap
|
||||
${activeTab === tab.id
|
||||
? 'border-blue-500 text-blue-600 dark:text-blue-400'
|
||||
: 'border-transparent text-gray-500 hover:text-gray-700 hover:border-gray-300 dark:text-gray-400 dark:hover:text-gray-300'
|
||||
}
|
||||
`}
|
||||
>
|
||||
{tab.icon}
|
||||
{tab.label}
|
||||
</button>
|
||||
))}
|
||||
</nav>
|
||||
</div>
|
||||
|
||||
{/* Tab Content */}
|
||||
<div className="mt-6">
|
||||
{/* Current Plan Tab */}
|
||||
{activeTab === 'plan' && (
|
||||
<div className="space-y-6">
|
||||
<Card className="p-6">
|
||||
<h2 className="text-lg font-semibold mb-4">Your Current Plan</h2>
|
||||
{!hasActivePlan && (
|
||||
<div className="p-4 mb-4 rounded-lg border border-amber-200 bg-amber-50 text-amber-800 dark:border-amber-800 dark:bg-amber-900/20 dark:text-amber-200">
|
||||
No active plan found. Please choose a plan to activate your account.
|
||||
</div>
|
||||
)}
|
||||
<div className="space-y-4">
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<div className="text-2xl font-bold text-gray-900 dark:text-white">
|
||||
{currentPlan?.name || 'No Plan Selected'}
|
||||
</div>
|
||||
<div className="text-gray-600 dark:text-gray-400">
|
||||
{currentPlan?.description || 'Select a plan to unlock full access.'}
|
||||
</div>
|
||||
</div>
|
||||
<Badge variant="light" color={hasActivePlan ? 'success' : 'warning'}>
|
||||
{hasActivePlan ? subscriptionStatus : 'plan required'}
|
||||
</Badge>
|
||||
</div>
|
||||
<div className="grid grid-cols-1 md:grid-cols-3 gap-4 mt-6">
|
||||
<div className="p-4 bg-gray-50 dark:bg-gray-800 rounded-lg">
|
||||
<div className="text-sm text-gray-600 dark:text-gray-400">Monthly Credits</div>
|
||||
<div className="text-2xl font-bold text-gray-900 dark:text-white">
|
||||
{creditBalance?.plan_credits_per_month?.toLocaleString?.() || 0}
|
||||
</div>
|
||||
</div>
|
||||
<div className="p-4 bg-gray-50 dark:bg-gray-800 rounded-lg">
|
||||
<div className="text-sm text-gray-600 dark:text-gray-400">Current Balance</div>
|
||||
<div className="text-2xl font-bold text-gray-900 dark:text-white">
|
||||
{creditBalance?.credits?.toLocaleString?.() || 0}
|
||||
</div>
|
||||
</div>
|
||||
<div className="p-4 bg-gray-50 dark:bg-gray-800 rounded-lg">
|
||||
<div className="text-sm text-gray-600 dark:text-gray-400">Period Ends</div>
|
||||
<div className="text-2xl font-bold text-gray-900 dark:text-white text-base">
|
||||
{currentSubscription?.current_period_end
|
||||
? new Date(currentSubscription.current_period_end).toLocaleDateString()
|
||||
: '—'}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div className="mt-6 flex gap-3">
|
||||
<Button variant="primary" tone="brand" onClick={() => setActiveTab('upgrade')}>
|
||||
{hasActivePlan ? 'Change Plan' : 'Choose a Plan'}
|
||||
</Button>
|
||||
<Button variant="outline" tone="neutral" onClick={() => setActiveTab('purchase')}>
|
||||
Purchase Credits
|
||||
</Button>
|
||||
{hasActivePlan && (
|
||||
<Button
|
||||
variant="outline"
|
||||
tone="neutral"
|
||||
disabled={planLoadingId === currentSubscription?.id}
|
||||
onClick={handleCancelSubscription}
|
||||
>
|
||||
{planLoadingId === currentSubscription?.id ? 'Cancelling...' : 'Cancel Subscription'}
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</Card>
|
||||
|
||||
<Card className="p-6">
|
||||
<h2 className="text-lg font-semibold mb-4">Plan Features</h2>
|
||||
<ul className="space-y-3">
|
||||
{(currentPlan?.features && currentPlan.features.length > 0
|
||||
? currentPlan.features
|
||||
: ['Credits included each month', 'Module access per plan limits', 'Email support'])
|
||||
.map((feature) => (
|
||||
<li key={feature} className="flex items-center gap-2 text-gray-700 dark:text-gray-300">
|
||||
<CheckCircle className="w-5 h-5 text-green-600" />
|
||||
{feature}
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
</Card>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Upgrade/Downgrade Tab */}
|
||||
{activeTab === 'upgrade' && (
|
||||
<div className="space-y-6">
|
||||
<div className="mb-4">
|
||||
<h2 className="text-xl font-semibold mb-2">Available Plans</h2>
|
||||
<p className="text-gray-600 dark:text-gray-400">Choose the plan that best fits your needs</p>
|
||||
</div>
|
||||
|
||||
{hasPaymentMethods ? (
|
||||
<div className="p-4 border border-gray-200 dark:border-gray-700 rounded-lg">
|
||||
<div className="text-sm font-medium text-gray-700 dark:text-gray-200 mb-2">Select payment method</div>
|
||||
<div className="flex flex-wrap gap-3">
|
||||
{paymentMethods.map((method) => (
|
||||
<label
|
||||
key={method.id}
|
||||
className={`px-3 py-2 rounded-lg border cursor-pointer text-sm ${
|
||||
selectedPaymentMethod === (method.type || method.id)
|
||||
? 'border-blue-500 bg-blue-50 dark:bg-blue-900/30'
|
||||
: 'border-gray-200 dark:border-gray-700'
|
||||
}`}
|
||||
>
|
||||
<input
|
||||
type="radio"
|
||||
className="sr-only"
|
||||
checked={selectedPaymentMethod === (method.type || method.id)}
|
||||
onChange={() => setSelectedPaymentMethod(method.type || method.id)}
|
||||
/>
|
||||
<div className="font-semibold text-gray-900 dark:text-white">{method.display_name}</div>
|
||||
<div className="text-xs text-gray-500 dark:text-gray-400">{method.type}</div>
|
||||
</label>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
) : (
|
||||
<div className="p-4 rounded-lg border border-amber-200 bg-amber-50 text-amber-800 dark:border-amber-800 dark:bg-amber-900/20 dark:text-amber-200">
|
||||
No payment methods available. Please contact support or add one from the Payment Methods tab.
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
|
||||
{plans.map((plan) => {
|
||||
const isCurrent = plan.id === currentPlanId;
|
||||
const price = plan.price ? `$${plan.price}/${plan.interval || 'month'}` : 'Custom';
|
||||
return (
|
||||
<Card key={plan.id} className="p-6 relative border border-gray-200 dark:border-gray-700">
|
||||
<div className="mb-4">
|
||||
<h3 className="text-lg font-semibold">{plan.name}</h3>
|
||||
<div className="text-3xl font-bold text-gray-900 dark:text-white mt-2">{price}</div>
|
||||
<div className="text-sm text-gray-500">{plan.description || 'Standard plan'}</div>
|
||||
</div>
|
||||
<div className="space-y-3 mb-6">
|
||||
{(plan.features && plan.features.length > 0 ? plan.features : ['Monthly credits included', 'Module access per plan', 'Email support']).map((feature) => (
|
||||
<div key={feature} className="flex items-center gap-2 text-sm">
|
||||
<CheckCircle className="w-4 h-4 text-green-600" />
|
||||
<span>{feature}</span>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
<Button
|
||||
variant={isCurrent ? 'outline' : 'primary'}
|
||||
tone="brand"
|
||||
fullWidth
|
||||
disabled={isCurrent || planLoadingId === plan.id}
|
||||
onClick={() => handleSelectPlan(plan.id)}
|
||||
>
|
||||
{planLoadingId === plan.id
|
||||
? 'Updating...'
|
||||
: isCurrent
|
||||
? 'Current Plan'
|
||||
: 'Select Plan'}
|
||||
</Button>
|
||||
</Card>
|
||||
);
|
||||
})}
|
||||
{plans.length === 0 && (
|
||||
<div className="col-span-3 text-center py-12 text-gray-500">
|
||||
No plans available. Please contact support.
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<Card className="p-6 bg-blue-50 dark:bg-blue-900/20 border-blue-200 dark:border-blue-800">
|
||||
<h3 className="font-semibold text-blue-900 dark:text-blue-100 mb-2">Plan Change Policy</h3>
|
||||
<ul className="space-y-2 text-sm text-blue-800 dark:text-blue-200">
|
||||
<li>• Upgrades take effect immediately and you'll be charged a prorated amount</li>
|
||||
<li>• Downgrades take effect at the end of your current billing period</li>
|
||||
<li>• Unused credits from your current plan will carry over</li>
|
||||
<li>• You can cancel your subscription at any time</li>
|
||||
</ul>
|
||||
</Card>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Credits Overview Tab */}
|
||||
{activeTab === 'credits' && (
|
||||
<div className="space-y-6">
|
||||
<div className="grid grid-cols-1 md:grid-cols-3 gap-6">
|
||||
<Card className="p-6">
|
||||
<div className="text-sm text-gray-600 dark:text-gray-400 mb-1">Current Balance</div>
|
||||
<div className="text-3xl font-bold text-blue-600 dark:text-blue-400">
|
||||
{creditBalance?.credits.toLocaleString() || 0}
|
||||
</div>
|
||||
<div className="text-sm text-gray-500 mt-2">credits available</div>
|
||||
</Card>
|
||||
<Card className="p-6">
|
||||
<div className="text-sm text-gray-600 dark:text-gray-400 mb-1">Used This Month</div>
|
||||
<div className="text-3xl font-bold text-red-600 dark:text-red-400">
|
||||
{creditBalance?.credits_used_this_month.toLocaleString() || 0}
|
||||
</div>
|
||||
<div className="text-sm text-gray-500 mt-2">credits consumed</div>
|
||||
</Card>
|
||||
<Card className="p-6">
|
||||
<div className="text-sm text-gray-600 dark:text-gray-400 mb-1">Monthly Included</div>
|
||||
<div className="text-3xl font-bold text-green-600 dark:text-green-400">
|
||||
{creditBalance?.plan_credits_per_month.toLocaleString() || 0}
|
||||
</div>
|
||||
<div className="text-sm text-gray-500 mt-2">from your plan</div>
|
||||
</Card>
|
||||
</div>
|
||||
|
||||
<Card className="p-6">
|
||||
<h2 className="text-lg font-semibold mb-4">Credit Usage Summary</h2>
|
||||
<div className="space-y-2">
|
||||
<div className="flex justify-between items-center">
|
||||
<span className="text-gray-700 dark:text-gray-300">Remaining Credits</span>
|
||||
<span className="font-semibold">{creditBalance?.credits_remaining.toLocaleString() || 0}</span>
|
||||
</div>
|
||||
<div className="w-full bg-gray-200 dark:bg-gray-700 rounded-full h-2">
|
||||
<div
|
||||
className="bg-blue-600 h-2 rounded-full"
|
||||
style={{
|
||||
width: creditBalance?.credits
|
||||
? `${Math.min((creditBalance.credits / (creditBalance.plan_credits_per_month || 1)) * 100, 100)}%`
|
||||
: '0%'
|
||||
}}
|
||||
></div>
|
||||
</div>
|
||||
</div>
|
||||
</Card>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Purchase Credits Tab */}
|
||||
{activeTab === 'purchase' && (
|
||||
<div className="space-y-6">
|
||||
{hasPaymentMethods ? (
|
||||
<div className="p-4 border border-gray-200 dark:border-gray-700 rounded-lg">
|
||||
<div className="text-sm font-medium text-gray-700 dark:text-gray-200 mb-2">Select payment method</div>
|
||||
<div className="flex flex-wrap gap-3">
|
||||
{paymentMethods.map((method) => (
|
||||
<label
|
||||
key={method.id}
|
||||
className={`px-3 py-2 rounded-lg border cursor-pointer text-sm ${
|
||||
selectedPaymentMethod === (method.type || method.id)
|
||||
? 'border-blue-500 bg-blue-50 dark:bg-blue-900/30'
|
||||
: 'border-gray-200 dark:border-gray-700'
|
||||
}`}
|
||||
>
|
||||
<input
|
||||
type="radio"
|
||||
className="sr-only"
|
||||
checked={selectedPaymentMethod === (method.type || method.id)}
|
||||
onChange={() => setSelectedPaymentMethod(method.type || method.id)}
|
||||
/>
|
||||
<div className="font-semibold text-gray-900 dark:text-white">{method.display_name}</div>
|
||||
<div className="text-xs text-gray-500 dark:text-gray-400">{method.type}</div>
|
||||
</label>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
) : (
|
||||
<div className="p-4 rounded-lg border border-amber-200 bg-amber-50 text-amber-800 dark:border-amber-800 dark:bg-amber-900/20 dark:text-amber-200">
|
||||
No payment methods available. Please contact support or add one from the Payment Methods tab.
|
||||
</div>
|
||||
)}
|
||||
|
||||
<Card className="p-6">
|
||||
<h2 className="text-lg font-semibold mb-4">Credit Packages</h2>
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
|
||||
{packages.map((pkg) => (
|
||||
<div key={pkg.id} className="border border-gray-200 dark:border-gray-700 rounded-lg p-6 hover:border-blue-500 transition-colors">
|
||||
<div className="text-lg font-semibold text-gray-900 dark:text-white">{pkg.name}</div>
|
||||
<div className="text-3xl font-bold text-blue-600 dark:text-blue-400 mt-2">
|
||||
{pkg.credits.toLocaleString()} <span className="text-sm text-gray-500">credits</span>
|
||||
</div>
|
||||
<div className="text-2xl font-semibold text-gray-900 dark:text-white mt-4">
|
||||
${pkg.price}
|
||||
</div>
|
||||
{pkg.description && (
|
||||
<div className="text-sm text-gray-600 dark:text-gray-400 mt-2">{pkg.description}</div>
|
||||
)}
|
||||
<Button
|
||||
variant="primary"
|
||||
tone="brand"
|
||||
onClick={() => handlePurchase(pkg.id)}
|
||||
fullWidth
|
||||
className="mt-6"
|
||||
disabled={purchaseLoadingId === pkg.id || (!hasPaymentMethods && paymentMethods.length > 0)}
|
||||
>
|
||||
{purchaseLoadingId === pkg.id ? 'Processing...' : 'Purchase'}
|
||||
</Button>
|
||||
</div>
|
||||
))}
|
||||
{packages.length === 0 && (
|
||||
<div className="col-span-3 text-center py-12 text-gray-500">
|
||||
No credit packages available at this time
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</Card>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Billing History Tab */}
|
||||
{activeTab === 'invoices' && (
|
||||
<Card className="overflow-hidden">
|
||||
<div className="overflow-x-auto">
|
||||
<table className="w-full">
|
||||
<thead className="bg-gray-50 dark:bg-gray-800 border-b border-gray-200 dark:border-gray-700">
|
||||
<tr>
|
||||
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
|
||||
Invoice
|
||||
</th>
|
||||
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
|
||||
Date
|
||||
</th>
|
||||
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
|
||||
Amount
|
||||
</th>
|
||||
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
|
||||
Status
|
||||
</th>
|
||||
<th className="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase tracking-wider">
|
||||
Actions
|
||||
</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody className="divide-y divide-gray-200 dark:divide-gray-700">
|
||||
{invoices.length === 0 ? (
|
||||
<tr>
|
||||
<td colSpan={5} className="px-6 py-8 text-center text-gray-500">
|
||||
<FileText className="w-12 h-12 mx-auto mb-2 text-gray-400" />
|
||||
No invoices yet
|
||||
</td>
|
||||
</tr>
|
||||
) : (
|
||||
invoices.map((invoice) => (
|
||||
<tr key={invoice.id} className="hover:bg-gray-50 dark:hover:bg-gray-800">
|
||||
<td className="px-6 py-4 font-medium">{invoice.invoice_number}</td>
|
||||
<td className="px-6 py-4 text-sm text-gray-600 dark:text-gray-400">
|
||||
{new Date(invoice.created_at).toLocaleDateString()}
|
||||
</td>
|
||||
<td className="px-6 py-4 font-semibold">${invoice.total_amount}</td>
|
||||
<td className="px-6 py-4">
|
||||
<Badge
|
||||
variant="light"
|
||||
color={invoice.status === 'paid' ? 'success' : 'warning'}
|
||||
>
|
||||
{invoice.status}
|
||||
</Badge>
|
||||
</td>
|
||||
<td className="px-6 py-4 text-right">
|
||||
<Button
|
||||
variant="ghost"
|
||||
tone="brand"
|
||||
size="sm"
|
||||
startIcon={<Download className="w-4 h-4" />}
|
||||
className="ml-auto"
|
||||
onClick={() => handleDownloadInvoice(invoice.id)}
|
||||
>
|
||||
Download
|
||||
</Button>
|
||||
</td>
|
||||
</tr>
|
||||
))
|
||||
)}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</Card>
|
||||
)}
|
||||
|
||||
{/* Payments Tab */}
|
||||
{activeTab === 'payments' && (
|
||||
<div className="space-y-6">
|
||||
<Card className="p-6">
|
||||
<div className="flex items-center justify-between mb-4">
|
||||
<div>
|
||||
<h2 className="text-lg font-semibold">Payments</h2>
|
||||
<p className="text-sm text-gray-600 dark:text-gray-400">Recent payments and manual submissions</p>
|
||||
</div>
|
||||
</div>
|
||||
<div className="overflow-x-auto">
|
||||
<table className="w-full">
|
||||
<thead className="bg-gray-50 dark:bg-gray-800 border-b border-gray-200 dark:border-gray-700">
|
||||
<tr>
|
||||
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Invoice</th>
|
||||
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Amount</th>
|
||||
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Method</th>
|
||||
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Status</th>
|
||||
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Date</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody className="divide-y divide-gray-200 dark:divide-gray-700">
|
||||
{payments.length === 0 ? (
|
||||
<tr>
|
||||
<td colSpan={5} className="px-6 py-8 text-center text-gray-500">
|
||||
No payments yet
|
||||
</td>
|
||||
</tr>
|
||||
) : (
|
||||
payments.map((payment) => (
|
||||
<tr key={payment.id} className="hover:bg-gray-50 dark:hover:bg-gray-800">
|
||||
<td className="px-6 py-4 text-sm font-medium text-gray-900 dark:text-white">
|
||||
{payment.invoice_number || payment.invoice_id || '-'}
|
||||
</td>
|
||||
<td className="px-6 py-4 text-sm text-gray-900 dark:text-white">
|
||||
${payment.amount}
|
||||
</td>
|
||||
<td className="px-6 py-4 text-sm text-gray-600 dark:text-gray-400">
|
||||
{payment.payment_method}
|
||||
</td>
|
||||
<td className="px-6 py-4">
|
||||
<Badge
|
||||
variant="light"
|
||||
color={
|
||||
payment.status === 'succeeded' || payment.status === 'completed'
|
||||
? 'success'
|
||||
: payment.status === 'pending' || payment.status === 'processing'
|
||||
? 'warning'
|
||||
: 'error'
|
||||
}
|
||||
>
|
||||
{payment.status}
|
||||
</Badge>
|
||||
</td>
|
||||
<td className="px-6 py-4 text-sm text-gray-600 dark:text-gray-400">
|
||||
{new Date(payment.created_at).toLocaleDateString()}
|
||||
</td>
|
||||
</tr>
|
||||
))
|
||||
)}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</Card>
|
||||
|
||||
<Card className="p-6">
|
||||
<h3 className="text-lg font-semibold mb-4">Submit Manual Payment</h3>
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">Invoice ID (optional)</label>
|
||||
<input
|
||||
type="number"
|
||||
value={manualPayment.invoice_id}
|
||||
onChange={(e) => setManualPayment((p) => ({ ...p, invoice_id: e.target.value }))}
|
||||
className="w-full px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-800 text-gray-900 dark:text-white"
|
||||
placeholder="Invoice ID"
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">Amount</label>
|
||||
<input
|
||||
type="text"
|
||||
value={manualPayment.amount}
|
||||
onChange={(e) => setManualPayment((p) => ({ ...p, amount: e.target.value }))}
|
||||
className="w-full px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-800 text-gray-900 dark:text-white"
|
||||
placeholder="e.g., 99.00"
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">Payment Method</label>
|
||||
<input
|
||||
type="text"
|
||||
value={manualPayment.payment_method}
|
||||
onChange={(e) => setManualPayment((p) => ({ ...p, payment_method: e.target.value }))}
|
||||
className="w-full px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-800 text-gray-900 dark:text-white"
|
||||
placeholder="bank_transfer / local_wallet / manual"
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">Reference</label>
|
||||
<input
|
||||
type="text"
|
||||
value={manualPayment.reference}
|
||||
onChange={(e) => setManualPayment((p) => ({ ...p, reference: e.target.value }))}
|
||||
className="w-full px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-800 text-gray-900 dark:text-white"
|
||||
placeholder="Reference or transaction id"
|
||||
/>
|
||||
</div>
|
||||
<div className="md:col-span-2">
|
||||
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">Notes</label>
|
||||
<textarea
|
||||
value={manualPayment.notes}
|
||||
onChange={(e) => setManualPayment((p) => ({ ...p, notes: e.target.value }))}
|
||||
className="w-full px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-800 text-gray-900 dark:text-white"
|
||||
placeholder="Optional notes"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<div className="mt-4 flex justify-end">
|
||||
<Button variant="primary" tone="brand" onClick={handleSubmitManualPayment}>
|
||||
Submit Manual Payment
|
||||
</Button>
|
||||
</div>
|
||||
</Card>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Payment Methods Tab */}
|
||||
{activeTab === 'payment-methods' && (
|
||||
<div className="space-y-6">
|
||||
<Card className="p-6">
|
||||
<div className="flex items-center justify-between mb-4">
|
||||
<h2 className="text-lg font-semibold">Payment Methods</h2>
|
||||
</div>
|
||||
<div className="grid grid-cols-1 md:grid-cols-3 gap-4 mb-4">
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">Type</label>
|
||||
<select
|
||||
value={newPaymentMethod.type}
|
||||
onChange={(e) => setNewPaymentMethod((p) => ({ ...p, type: e.target.value }))}
|
||||
className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-800 text-gray-900 dark:text-white"
|
||||
>
|
||||
<option value="bank_transfer">Bank Transfer</option>
|
||||
<option value="local_wallet">Local Wallet</option>
|
||||
<option value="manual">Manual</option>
|
||||
</select>
|
||||
</div>
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">Display Name</label>
|
||||
<input
|
||||
type="text"
|
||||
value={newPaymentMethod.display_name}
|
||||
onChange={(e) => setNewPaymentMethod((p) => ({ ...p, display_name: e.target.value }))}
|
||||
className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-800 text-gray-900 dark:text-white"
|
||||
placeholder="e.g., Bank Transfer (USD)"
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">Instructions (optional)</label>
|
||||
<input
|
||||
type="text"
|
||||
value={newPaymentMethod.instructions}
|
||||
onChange={(e) => setNewPaymentMethod((p) => ({ ...p, instructions: e.target.value }))}
|
||||
className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-800 text-gray-900 dark:text-white"
|
||||
placeholder="Where to send payment"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<div className="mb-4">
|
||||
<Button variant="primary" tone="brand" onClick={handleAddPaymentMethod}>
|
||||
Add Payment Method
|
||||
</Button>
|
||||
</div>
|
||||
<div className="space-y-4">
|
||||
{paymentMethods.map((method) => (
|
||||
<div key={method.id} className="border border-gray-200 dark:border-gray-700 rounded-lg p-4 flex items-center justify-between">
|
||||
<div className="flex items-center gap-4">
|
||||
<CreditCard className="w-8 h-8 text-gray-400" />
|
||||
<div>
|
||||
<div className="font-medium text-gray-900 dark:text-white">{method.display_name}</div>
|
||||
<div className="text-sm text-gray-600 dark:text-gray-400">{method.type}</div>
|
||||
{method.instructions && (
|
||||
<div className="text-xs text-gray-500 dark:text-gray-400 mt-1">{method.instructions}</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
{method.is_enabled && (
|
||||
<Badge variant="light" color="success">Active</Badge>
|
||||
)}
|
||||
{method.is_default ? (
|
||||
<Badge variant="light" color="info">Default</Badge>
|
||||
) : (
|
||||
<Button variant="outline" size="sm" onClick={() => handleSetDefaultPaymentMethod(method.id)}>
|
||||
Make Default
|
||||
</Button>
|
||||
)}
|
||||
<Button variant="outline" size="sm" tone="neutral" onClick={() => handleRemovePaymentMethod(method.id)}>
|
||||
Remove
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
{paymentMethods.length === 0 && (
|
||||
<div className="text-center py-12 text-gray-500">
|
||||
No payment methods configured
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</Card>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -655,7 +655,8 @@ export async function getAvailablePaymentMethods(): Promise<{
|
||||
const response = await fetchAPI('/v1/billing/payment-methods/');
|
||||
// Frontend guard: only allow the simplified set we currently support
|
||||
const allowed = new Set(['bank_transfer', 'manual']);
|
||||
const filtered = (response.results || []).filter((m: PaymentMethod) => allowed.has(m.type));
|
||||
const results = Array.isArray(response.results) ? response.results : [];
|
||||
const filtered = results.filter((m: PaymentMethod) => allowed.has(m.type));
|
||||
return { results: filtered, count: filtered.length };
|
||||
}
|
||||
|
||||
@@ -830,6 +831,8 @@ export interface Plan {
|
||||
interval?: 'month' | 'year';
|
||||
description?: string;
|
||||
is_active?: boolean;
|
||||
is_featured?: boolean;
|
||||
annual_discount_percent?: number;
|
||||
features?: string[];
|
||||
limits?: Record<string, any>;
|
||||
display_order?: number;
|
||||
|
||||
@@ -114,11 +114,22 @@ export const useAuthStore = create<AuthState>()(
|
||||
},
|
||||
|
||||
logout: () => {
|
||||
// Clear cookies (session contamination protection)
|
||||
document.cookie.split(";").forEach((c) => {
|
||||
document.cookie = `${c.split("=")[0].trim()}=;expires=Thu, 01 Jan 1970 00:00:00 UTC;path=/`;
|
||||
});
|
||||
// CRITICAL: Properly clear ALL cookies to prevent session contamination
|
||||
const cookies = document.cookie.split(";");
|
||||
for (let i = 0; i < cookies.length; i++) {
|
||||
const cookie = cookies[i];
|
||||
const eqPos = cookie.indexOf("=");
|
||||
const name = eqPos > -1 ? cookie.substr(0, eqPos).trim() : cookie.trim();
|
||||
// Clear cookie for all possible domains and paths
|
||||
document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/";
|
||||
document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/;domain=" + window.location.hostname;
|
||||
document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/;domain=." + window.location.hostname;
|
||||
}
|
||||
// Clear all localStorage to prevent state contamination
|
||||
localStorage.clear();
|
||||
// Clear sessionStorage as well
|
||||
sessionStorage.clear();
|
||||
// Reset auth state
|
||||
set({ user: null, token: null, refreshToken: null, isAuthenticated: false, loading: false });
|
||||
},
|
||||
|
||||
|
||||
Reference in New Issue
Block a user