diff --git a/server.ts b/server.ts index 4ce4161..614f330 100644 --- a/server.ts +++ b/server.ts @@ -639,9 +639,10 @@ app.post('/api/admin/settings', authenticateToken, requireAdmin, (req: Authentic res.json({ success: true, settings: db.settings }); }); -app.get('/api/admin/test-asaas', authenticateToken, requireAdmin, async (req: AuthenticatedRequest, res: Response) => { +app.post('/api/admin/test-asaas', authenticateToken, requireAdmin, async (req: AuthenticatedRequest, res: Response) => { try { - const result = await AsaasService.testConnection(); + const { asaasApiKey, asaasEnvironment } = req.body || {}; + const result = await AsaasService.testConnection(asaasApiKey, asaasEnvironment); res.json(result); } catch (err: any) { res.status(500).json({ success: false, message: err.message || 'Erro ao testar conexão Asaas' }); diff --git a/src/components/AdminDashboard.tsx b/src/components/AdminDashboard.tsx index c41e7f3..63f9104 100644 --- a/src/components/AdminDashboard.tsx +++ b/src/components/AdminDashboard.tsx @@ -128,7 +128,15 @@ export default function AdminDashboard({ token }: AdminDashboardProps) { setAsaasTestResult({ loading: true, success: null, message: 'Testando conexão com Asaas...' }); try { const res = await fetch('/api/admin/test-asaas', { - headers: { 'Authorization': `Bearer ${token}` } + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'Authorization': `Bearer ${token}` + }, + body: JSON.stringify({ + asaasApiKey: settings?.asaasApiKey, + asaasEnvironment: settings?.asaasEnvironment + }) }); const data = await res.json(); setAsaasTestResult({ diff --git a/src/services/asaas.ts b/src/services/asaas.ts index a98e458..b6cfffe 100644 --- a/src/services/asaas.ts +++ b/src/services/asaas.ts @@ -24,22 +24,26 @@ export class AsaasService { /** * Test the connection to Asaas API */ - public static async testConnection(): Promise<{ success: boolean; message: string; environment: string }> { - const apiKey = this.getApiKey(); - const env = loadDb().settings?.asaasEnvironment || 'sandbox'; + public static async testConnection(customApiKey?: string, customEnv?: string): Promise<{ success: boolean; message: string; environment: string }> { + const apiKey = customApiKey || this.getApiKey(); + const env = customEnv || loadDb().settings?.asaasEnvironment || 'sandbox'; + const apiUrl = env === 'production' ? 'https://api.asaas.com/v3' : 'https://sandbox.asaas.com/v3'; - if (!apiKey) { + if (!apiKey || apiKey.trim() === '') { return { success: false, - message: 'Chave de API (API Key) não foi configurada.', + message: 'A chave de API (API Key) não foi definida ou está em branco.', environment: env }; } try { - const response = await fetch(`${this.getApiUrl()}/customers?limit=1`, { + const response = await fetch(`${apiUrl}/customers?limit=1`, { method: 'GET', - headers: this.getHeaders() + headers: { + 'Content-Type': 'application/json', + 'access_token': apiKey.trim() + } }); if (response.ok) {