diff --git a/src/services/asaas.ts b/src/services/asaas.ts index 6d2a58b..c37372b 100644 --- a/src/services/asaas.ts +++ b/src/services/asaas.ts @@ -80,29 +80,34 @@ export class AsaasService { return `cus_sim_${Math.random().toString(36).substring(2, 9)}`; } + const payload: any = { + name, + email, + notificationDisabled: true + }; + + if (cpfCnpj && cpfCnpj !== '000.000.000-00') { + payload.cpfCnpj = cpfCnpj.replace(/\D/g, ''); + } + try { const response = await fetch(`${this.getApiUrl()}/customers`, { method: 'POST', headers: this.getHeaders(), - body: JSON.stringify({ - name, - email, - cpfCnpj: cpfCnpj || '000.000.000-00', // Default mock CPF if not provided - notificationDisabled: true - }) + body: JSON.stringify(payload) }); if (!response.ok) { const errorData = await response.json().catch(() => ({})); - throw new Error(errorData.errors?.[0]?.description || 'Failed to create customer in Asaas'); + const desc = errorData.errors?.[0]?.description || `HTTP ${response.status}`; + throw new Error(`Falha ao criar cliente no Asaas: ${desc}`); } const data = await response.json(); return data.id; } catch (err: any) { console.error('Asaas createCustomer error:', err); - // Fallback in case of networking issues, so it doesn't block local experience completely - return `cus_fallback_${Math.random().toString(36).substring(2, 9)}`; + throw err; } } @@ -342,16 +347,7 @@ export class AsaasService { return data; } catch (err: any) { console.error('Asaas createPayment error:', err); - // Return a simulated structure if networking fails so local experience doesn't crash 500 - const payId = `pay_fb_${Math.random().toString(36).substring(2, 9)}`; - return { - id: payId, - value, - status: 'PENDING', - billingType, - invoiceUrl: `https://sandbox.asaas.com/i/fallback_${payId}`, - dueDate: futureDueDate, - }; + throw err; } } }