many fixes
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
/**
|
||||
* Billing API Service
|
||||
* Uses STANDARD billing endpoints from /api/v1/billing and /api/v1/admin/billing
|
||||
* Uses STANDARD billing endpoints from /v1/billing and /v1/admin/billing
|
||||
*/
|
||||
|
||||
import { fetchAPI } from './api';
|
||||
@@ -166,6 +166,7 @@ export interface PaymentMethod {
|
||||
display_name: string;
|
||||
name?: string;
|
||||
is_enabled: boolean;
|
||||
is_default?: boolean;
|
||||
instructions?: string;
|
||||
bank_details?: {
|
||||
bank_name?: string;
|
||||
@@ -200,7 +201,7 @@ export async function getCreditTransactions(): Promise<{
|
||||
count: number;
|
||||
current_balance?: number;
|
||||
}> {
|
||||
return fetchAPI('/api/v1/billing/transactions/');
|
||||
return fetchAPI('/v1/billing/transactions/');
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
@@ -220,7 +221,7 @@ export async function getCreditUsage(params?: {
|
||||
if (params?.start_date) queryParams.append('start_date', params.start_date);
|
||||
if (params?.end_date) queryParams.append('end_date', params.end_date);
|
||||
|
||||
const url = `/api/v1/billing/credits/usage/${queryParams.toString() ? '?' + queryParams.toString() : ''}`;
|
||||
const url = `/v1/billing/credits/usage/${queryParams.toString() ? '?' + queryParams.toString() : ''}`;
|
||||
return fetchAPI(url);
|
||||
}
|
||||
|
||||
@@ -266,7 +267,8 @@ export async function getCreditUsageLimits(): Promise<{
|
||||
|
||||
export async function getAdminBillingStats(): Promise<AdminBillingStats> {
|
||||
// Admin billing dashboard metrics
|
||||
return fetchAPI('/v1/admin/billing/stats/');
|
||||
// Use business billing stats endpoint to include revenue, accounts, credits, and recent payments
|
||||
return fetchAPI('/v1/billing/admin/stats/');
|
||||
}
|
||||
|
||||
export async function getAdminInvoices(params?: { status?: string; account_id?: number; search?: string }): Promise<{
|
||||
@@ -377,7 +379,7 @@ export async function getInvoiceDetail(invoiceId: number): Promise<Invoice> {
|
||||
}
|
||||
|
||||
export async function downloadInvoicePDF(invoiceId: number): Promise<Blob> {
|
||||
const response = await fetch(`/api/v1/billing/invoices/${invoiceId}/download_pdf/`, {
|
||||
const response = await fetch(`/v1/billing/invoices/${invoiceId}/download_pdf/`, {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${localStorage.getItem('access_token')}`,
|
||||
},
|
||||
@@ -448,6 +450,49 @@ export async function getAdminCreditPackages(): Promise<{
|
||||
return fetchAPI('/v1/billing/credit-packages/');
|
||||
}
|
||||
|
||||
export async function createAdminCreditPackage(data: {
|
||||
name: string;
|
||||
slug?: string;
|
||||
credits: number;
|
||||
price: string | number;
|
||||
discount_percentage?: number;
|
||||
description?: string;
|
||||
is_active?: boolean;
|
||||
is_featured?: boolean;
|
||||
sort_order?: number;
|
||||
}): Promise<{ package: CreditPackage; message?: string }> {
|
||||
return fetchAPI('/v1/billing/credit-packages/', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
}
|
||||
|
||||
export async function updateAdminCreditPackage(
|
||||
id: number,
|
||||
data: Partial<{
|
||||
name: string;
|
||||
slug: string;
|
||||
credits: number;
|
||||
price: string | number;
|
||||
discount_percentage: number;
|
||||
description: string;
|
||||
is_active: boolean;
|
||||
is_featured: boolean;
|
||||
sort_order: number;
|
||||
}>
|
||||
): Promise<{ package: CreditPackage; message?: string }> {
|
||||
return fetchAPI(`/v1/billing/credit-packages/${id}/`, {
|
||||
method: 'PATCH',
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
}
|
||||
|
||||
export async function deleteAdminCreditPackage(id: number): Promise<{ message?: string }> {
|
||||
return fetchAPI(`/v1/billing/credit-packages/${id}/`, {
|
||||
method: 'DELETE',
|
||||
});
|
||||
}
|
||||
|
||||
export async function purchaseCreditPackage(data: {
|
||||
package_id: number;
|
||||
payment_method: 'stripe' | 'paypal' | 'bank_transfer' | 'local_wallet';
|
||||
@@ -461,7 +506,7 @@ export async function purchaseCreditPackage(data: {
|
||||
stripe_client_secret?: string;
|
||||
paypal_order_id?: string;
|
||||
}> {
|
||||
return fetchAPI(`/api/v1/billing/credit-packages/${data.package_id}/purchase/`, {
|
||||
return fetchAPI(`/v1/billing/credit-packages/${data.package_id}/purchase/`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ payment_method: data.payment_method }),
|
||||
});
|
||||
@@ -505,11 +550,53 @@ export async function removeTeamMember(memberId: number): Promise<{
|
||||
// PAYMENT METHODS
|
||||
// ============================================================================
|
||||
|
||||
// Account payment methods (CRUD)
|
||||
export async function getAvailablePaymentMethods(): Promise<{
|
||||
results: PaymentMethod[];
|
||||
count: number;
|
||||
}> {
|
||||
return fetchAPI('/api/v1/billing/payment-methods/');
|
||||
return fetchAPI('/v1/billing/payment-methods/');
|
||||
}
|
||||
|
||||
export async function createPaymentMethod(data: {
|
||||
type: 'stripe' | 'paypal' | 'bank_transfer' | 'local_wallet' | 'manual';
|
||||
display_name: string;
|
||||
instructions?: string;
|
||||
metadata?: Record<string, any>;
|
||||
country_code?: string;
|
||||
is_default?: boolean;
|
||||
}): Promise<{ payment_method: PaymentMethod; message?: string }> {
|
||||
return fetchAPI('/v1/billing/payment-methods/', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
}
|
||||
|
||||
export async function deletePaymentMethod(id: string): Promise<{ message?: string }> {
|
||||
return fetchAPI(`/v1/billing/payment-methods/${id}/`, {
|
||||
method: 'DELETE',
|
||||
});
|
||||
}
|
||||
|
||||
export async function updatePaymentMethod(id: string, data: Partial<PaymentMethod>): Promise<{ payment_method?: PaymentMethod; message?: string }> {
|
||||
return fetchAPI(`/v1/billing/payment-methods/${id}/`, {
|
||||
method: 'PATCH',
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
}
|
||||
|
||||
export async function setDefaultPaymentMethod(id: string): Promise<{ payment_method?: PaymentMethod; message?: string }> {
|
||||
return fetchAPI(`/v1/billing/payment-methods/${id}/set_default/`, {
|
||||
method: 'POST',
|
||||
});
|
||||
}
|
||||
|
||||
// Country/config-driven available methods (legacy)
|
||||
export async function getPaymentMethodConfigs(): Promise<{
|
||||
results: PaymentMethod[];
|
||||
count: number;
|
||||
}> {
|
||||
return fetchAPI('/v1/billing/payment-methods/available/');
|
||||
}
|
||||
|
||||
export async function createManualPayment(data: {
|
||||
@@ -523,7 +610,7 @@ export async function createManualPayment(data: {
|
||||
status: string;
|
||||
message: string;
|
||||
}> {
|
||||
return fetchAPI('/api/v1/billing/payments/manual/', {
|
||||
return fetchAPI('/v1/billing/payments/manual/', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
@@ -537,7 +624,7 @@ export async function getPendingPayments(): Promise<{
|
||||
results: PendingPayment[];
|
||||
count: number;
|
||||
}> {
|
||||
return fetchAPI('/api/v1/billing/admin/pending_payments/');
|
||||
return fetchAPI('/v1/billing/admin/pending_payments/');
|
||||
}
|
||||
|
||||
export async function approvePayment(paymentId: number, data?: {
|
||||
@@ -546,7 +633,7 @@ export async function approvePayment(paymentId: number, data?: {
|
||||
message: string;
|
||||
payment: Payment;
|
||||
}> {
|
||||
return fetchAPI(`/api/v1/billing/admin/${paymentId}/approve_payment/`, {
|
||||
return fetchAPI(`/v1/billing/admin/${paymentId}/approve_payment/`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(data || {}),
|
||||
});
|
||||
@@ -559,7 +646,7 @@ export async function rejectPayment(paymentId: number, data: {
|
||||
message: string;
|
||||
payment: Payment;
|
||||
}> {
|
||||
return fetchAPI(`/api/v1/billing/admin/${paymentId}/reject_payment/`, {
|
||||
return fetchAPI(`/v1/billing/admin/${paymentId}/reject_payment/`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
@@ -628,3 +715,55 @@ export interface UsageAnalytics {
|
||||
export async function getUsageAnalytics(days: number = 30): Promise<UsageAnalytics> {
|
||||
return fetchAPI(`/v1/account/usage/analytics/?days=${days}`);
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// PLANS & SUBSCRIPTIONS (TENANT)
|
||||
// ============================================================================
|
||||
|
||||
export interface Plan {
|
||||
id: number;
|
||||
name: string;
|
||||
slug?: string;
|
||||
price?: number | string;
|
||||
currency?: string;
|
||||
interval?: 'month' | 'year';
|
||||
description?: string;
|
||||
is_active?: boolean;
|
||||
features?: string[];
|
||||
limits?: Record<string, any>;
|
||||
display_order?: number;
|
||||
}
|
||||
|
||||
export interface Subscription {
|
||||
id: number;
|
||||
plan: Plan | number;
|
||||
status: string;
|
||||
current_period_start?: string;
|
||||
current_period_end?: string;
|
||||
cancel_at_period_end?: boolean;
|
||||
created_at?: string;
|
||||
}
|
||||
|
||||
export async function getPlans(): Promise<{ results: Plan[] }> {
|
||||
return fetchAPI('/v1/auth/plans/');
|
||||
}
|
||||
|
||||
export async function getSubscriptions(): Promise<{ results: Subscription[] }> {
|
||||
return fetchAPI('/v1/auth/subscriptions/');
|
||||
}
|
||||
|
||||
export async function createSubscription(data: {
|
||||
plan_id: number;
|
||||
payment_method?: string;
|
||||
}): Promise<{ message?: string; subscription?: Subscription }> {
|
||||
return fetchAPI('/v1/auth/subscriptions/', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
}
|
||||
|
||||
export async function cancelSubscription(subscriptionId: number): Promise<{ message?: string }> {
|
||||
return fetchAPI(`/v1/auth/subscriptions/${subscriptionId}/cancel/`, {
|
||||
method: 'POST',
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user