Refactor: Integrate Stripe and complete configuration
This commit is contained in:
@@ -45,6 +45,24 @@ export interface AuditLog {
|
||||
status: 'success' | 'failed';
|
||||
}
|
||||
|
||||
export interface PaymentConfig {
|
||||
id: string;
|
||||
provider: 'stripe' | 'paypal' | 'mercadopago' | 'bank_transfer';
|
||||
name: string;
|
||||
enabled: boolean;
|
||||
credentials: {
|
||||
publishableKey?: string;
|
||||
secretKey?: string;
|
||||
clientId?: string;
|
||||
clientSecret?: string;
|
||||
webhookSecret?: string;
|
||||
[key: string]: string | undefined;
|
||||
};
|
||||
status: 'active' | 'inactive' | 'testing';
|
||||
testMode: boolean;
|
||||
lastTested?: string;
|
||||
}
|
||||
|
||||
export const configApi = {
|
||||
// API Configuration
|
||||
async getApiConfigs(): Promise<ApiConfig[]> {
|
||||
@@ -159,4 +177,45 @@ export const configApi = {
|
||||
return { logs: [], total: 0 };
|
||||
}
|
||||
},
|
||||
|
||||
// Payment Configuration
|
||||
async getPaymentConfigs(): Promise<PaymentConfig[]> {
|
||||
try {
|
||||
const response = await fetch(`${API_BASE_URL}/config/payments`, {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${localStorage.getItem('auth_token')}`,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
if (!response.ok) throw new Error('Failed to fetch payment configs');
|
||||
return await response.json();
|
||||
} catch (error) {
|
||||
console.error('Error fetching payment configs:', error);
|
||||
return [];
|
||||
}
|
||||
},
|
||||
|
||||
async updatePaymentConfig(config: Partial<PaymentConfig>): Promise<PaymentConfig> {
|
||||
const response = await fetch(`${API_BASE_URL}/config/payments/${config.id}`, {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${localStorage.getItem('auth_token')}`,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(config),
|
||||
});
|
||||
if (!response.ok) throw new Error('Failed to update payment config');
|
||||
return await response.json();
|
||||
},
|
||||
|
||||
async testPaymentConnection(configId: string): Promise<boolean> {
|
||||
const response = await fetch(`${API_BASE_URL}/config/payments/${configId}/test`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${localStorage.getItem('auth_token')}`,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
return response.ok;
|
||||
},
|
||||
};
|
||||
101
src/services/paymentService.ts
Normal file
101
src/services/paymentService.ts
Normal file
@@ -0,0 +1,101 @@
|
||||
import { API_BASE_URL } from './config';
|
||||
|
||||
export interface StripeCredentials {
|
||||
publishableKey: string;
|
||||
enabled: boolean;
|
||||
testMode: boolean;
|
||||
}
|
||||
|
||||
export interface PaymentIntent {
|
||||
id: string;
|
||||
amount: number;
|
||||
currency: string;
|
||||
status: string;
|
||||
clientSecret: string;
|
||||
}
|
||||
|
||||
class PaymentService {
|
||||
private async getAuthToken(): Promise<string | null> {
|
||||
return localStorage.getItem('auth_token');
|
||||
}
|
||||
|
||||
async getStripeCredentials(): Promise<StripeCredentials | null> {
|
||||
try {
|
||||
const token = await this.getAuthToken();
|
||||
const response = await fetch(`${API_BASE_URL}/config/payments/stripe/credentials`, {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${token}`,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to fetch Stripe credentials');
|
||||
}
|
||||
|
||||
return await response.json();
|
||||
} catch (error) {
|
||||
console.error('Error fetching Stripe credentials:', error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
async createPaymentIntent(data: {
|
||||
amount: number;
|
||||
currency: string;
|
||||
description?: string;
|
||||
metadata?: Record<string, string>;
|
||||
}): Promise<PaymentIntent> {
|
||||
const token = await this.getAuthToken();
|
||||
|
||||
const response = await fetch(`${API_BASE_URL}/payments/create-intent`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${token}`,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to create payment intent');
|
||||
}
|
||||
|
||||
return await response.json();
|
||||
}
|
||||
|
||||
async confirmPayment(paymentIntentId: string): Promise<boolean> {
|
||||
const token = await this.getAuthToken();
|
||||
|
||||
const response = await fetch(`${API_BASE_URL}/payments/confirm`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${token}`,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({ paymentIntentId }),
|
||||
});
|
||||
|
||||
return response.ok;
|
||||
}
|
||||
|
||||
async getPaymentStatus(paymentIntentId: string): Promise<string> {
|
||||
const token = await this.getAuthToken();
|
||||
|
||||
const response = await fetch(`${API_BASE_URL}/payments/status/${paymentIntentId}`, {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${token}`,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to get payment status');
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
return data.status;
|
||||
}
|
||||
}
|
||||
|
||||
export const paymentService = new PaymentService();
|
||||
Reference in New Issue
Block a user