Fixing PLans page

This commit is contained in:
IGNY8 VPS (Salman)
2025-12-08 14:12:08 +00:00
parent da3b45d1c7
commit 144e955b92
24 changed files with 1992 additions and 1105 deletions

View File

@@ -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 />

View File

@@ -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 />;

View File

@@ -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 });
}

View File

@@ -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 };
}

View File

@@ -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" },
],
},
],

View File

@@ -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>
);
}

View File

@@ -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) => (

View 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. Its 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>
);
}

View File

@@ -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;

View File

@@ -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 });
},