billing adn account
This commit is contained in:
253
frontend/src/services/billing.api.ts
Normal file
253
frontend/src/services/billing.api.ts
Normal file
@@ -0,0 +1,253 @@
|
||||
/**
|
||||
* Billing API Service
|
||||
* Handles all billing-related API calls
|
||||
*/
|
||||
|
||||
import { fetchAPI } from './api';
|
||||
|
||||
// ============================================================================
|
||||
// TYPES
|
||||
// ============================================================================
|
||||
|
||||
export interface CreditPackage {
|
||||
id: number;
|
||||
name: string;
|
||||
slug: string;
|
||||
credits: number;
|
||||
price: string;
|
||||
discount_percentage: number;
|
||||
is_featured: boolean;
|
||||
description: string;
|
||||
display_order: number;
|
||||
}
|
||||
|
||||
export interface Invoice {
|
||||
id: number;
|
||||
invoice_number: string;
|
||||
status: 'draft' | 'pending' | 'paid' | 'void';
|
||||
total_amount: string;
|
||||
subtotal: string;
|
||||
tax_amount: string;
|
||||
currency: string;
|
||||
created_at: string;
|
||||
paid_at: string | null;
|
||||
due_date: string | null;
|
||||
line_items: Array<{
|
||||
description: string;
|
||||
amount: string;
|
||||
quantity: number;
|
||||
}>;
|
||||
billing_period_start: string | null;
|
||||
billing_period_end: string | null;
|
||||
}
|
||||
|
||||
export interface Payment {
|
||||
id: number;
|
||||
amount: string;
|
||||
currency: string;
|
||||
payment_method: 'stripe' | 'paypal' | 'bank_transfer' | 'local_wallet' | 'manual';
|
||||
status: 'pending' | 'completed' | 'failed' | 'pending_approval';
|
||||
created_at: string;
|
||||
processed_at: string | null;
|
||||
invoice_id: number;
|
||||
invoice_number: string | null;
|
||||
transaction_reference: string;
|
||||
failure_reason: string | null;
|
||||
}
|
||||
|
||||
export interface PaymentMethod {
|
||||
type: string;
|
||||
name: string;
|
||||
instructions: string;
|
||||
bank_details?: {
|
||||
bank_name: string;
|
||||
account_number: string;
|
||||
routing_number: string;
|
||||
swift_code: string;
|
||||
};
|
||||
wallet_details?: {
|
||||
wallet_type: string;
|
||||
wallet_id: string;
|
||||
};
|
||||
}
|
||||
|
||||
export interface CreditTransaction {
|
||||
id: number;
|
||||
amount: number;
|
||||
transaction_type: string;
|
||||
description: string;
|
||||
created_at: string;
|
||||
reference_id: string;
|
||||
metadata: Record<string, any>;
|
||||
}
|
||||
|
||||
export interface CreditBalance {
|
||||
balance: number;
|
||||
subscription_plan: string;
|
||||
monthly_credits: number;
|
||||
subscription_status: string | null;
|
||||
}
|
||||
|
||||
export interface PendingPayment {
|
||||
id: number;
|
||||
account_name: string;
|
||||
amount: string;
|
||||
currency: string;
|
||||
payment_method: string;
|
||||
transaction_reference: string;
|
||||
created_at: string;
|
||||
invoice_number: string | null;
|
||||
admin_notes: string;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// CREDIT PACKAGES
|
||||
// ============================================================================
|
||||
|
||||
export async function getCreditPackages(): Promise<{ results: CreditPackage[]; count: number }> {
|
||||
return fetchAPI('/billing/v2/credit-packages/');
|
||||
}
|
||||
|
||||
export async function purchaseCreditPackage(
|
||||
packageId: number,
|
||||
paymentMethod: string
|
||||
): Promise<{
|
||||
invoice_id: number;
|
||||
invoice_number: string;
|
||||
total_amount: string;
|
||||
message: string;
|
||||
next_action: string;
|
||||
}> {
|
||||
return fetchAPI(`/billing/v2/credit-packages/${packageId}/purchase/`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ payment_method: paymentMethod }),
|
||||
});
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// INVOICES
|
||||
// ============================================================================
|
||||
|
||||
export async function getInvoices(status?: string): Promise<{ results: Invoice[]; count: number }> {
|
||||
const params = status ? `?status=${status}` : '';
|
||||
return fetchAPI(`/billing/v2/invoices/${params}`);
|
||||
}
|
||||
|
||||
export async function getInvoice(invoiceId: number): Promise<Invoice> {
|
||||
return fetchAPI(`/billing/v2/invoices/${invoiceId}/`);
|
||||
}
|
||||
|
||||
export async function downloadInvoicePDF(invoiceId: number): Promise<Blob> {
|
||||
const response = await fetch(`/api/v1/billing/v2/invoices/${invoiceId}/download_pdf/`, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${localStorage.getItem('access_token')}`,
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to download invoice');
|
||||
}
|
||||
|
||||
return response.blob();
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// PAYMENTS
|
||||
// ============================================================================
|
||||
|
||||
export async function getPayments(status?: string): Promise<{ results: Payment[]; count: number }> {
|
||||
const params = status ? `?status=${status}` : '';
|
||||
return fetchAPI(`/billing/v2/payments/${params}`);
|
||||
}
|
||||
|
||||
export async function getAvailablePaymentMethods(): Promise<{
|
||||
methods: PaymentMethod[];
|
||||
stripe: boolean;
|
||||
paypal: boolean;
|
||||
bank_transfer: boolean;
|
||||
local_wallet: boolean;
|
||||
}> {
|
||||
return fetchAPI('/billing/v2/payments/available_methods/');
|
||||
}
|
||||
|
||||
export async function submitManualPayment(data: {
|
||||
invoice_id: number;
|
||||
payment_method: 'bank_transfer' | 'local_wallet';
|
||||
transaction_reference: string;
|
||||
notes?: string;
|
||||
}): Promise<{
|
||||
id: number;
|
||||
status: string;
|
||||
message: string;
|
||||
}> {
|
||||
return fetchAPI('/billing/v2/payments/create_manual_payment/', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// CREDIT TRANSACTIONS
|
||||
// ============================================================================
|
||||
|
||||
export async function getCreditTransactions(): Promise<{
|
||||
results: CreditTransaction[];
|
||||
count: number;
|
||||
current_balance: number;
|
||||
}> {
|
||||
return fetchAPI('/billing/v2/transactions/');
|
||||
}
|
||||
|
||||
export async function getCreditBalance(): Promise<CreditBalance> {
|
||||
return fetchAPI('/billing/v2/transactions/balance/');
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// ADMIN - PAYMENT APPROVALS
|
||||
// ============================================================================
|
||||
|
||||
export async function getPendingPayments(): Promise<{
|
||||
results: PendingPayment[];
|
||||
count: number;
|
||||
}> {
|
||||
return fetchAPI('/billing/v2/admin/pending_payments/');
|
||||
}
|
||||
|
||||
export async function approvePayment(
|
||||
paymentId: number,
|
||||
notes?: string
|
||||
): Promise<{
|
||||
id: number;
|
||||
status: string;
|
||||
message: string;
|
||||
}> {
|
||||
return fetchAPI(`/billing/v2/admin/${paymentId}/approve_payment/`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ notes }),
|
||||
});
|
||||
}
|
||||
|
||||
export async function rejectPayment(
|
||||
paymentId: number,
|
||||
reason: string
|
||||
): Promise<{
|
||||
id: number;
|
||||
status: string;
|
||||
message: string;
|
||||
}> {
|
||||
return fetchAPI(`/billing/v2/admin/${paymentId}/reject_payment/`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ reason }),
|
||||
});
|
||||
}
|
||||
|
||||
export async function getAdminBillingStats(): Promise<{
|
||||
total_accounts: number;
|
||||
active_subscriptions: number;
|
||||
total_revenue: string;
|
||||
pending_approvals: number;
|
||||
invoices_pending: number;
|
||||
invoices_paid: number;
|
||||
}> {
|
||||
return fetchAPI('/billing/v2/admin/stats/');
|
||||
}
|
||||
Reference in New Issue
Block a user