fix: throw explicit Asaas API errors and prevent dummy fallback when API Key is configured
Build and Deploy (Gitea) / build-and-deploy (push) Successful in 45s Details

This commit is contained in:
Sidney Gomes 2026-07-21 18:26:35 -03:00
parent 65a245f972
commit fb84a2ccb6
1 changed files with 15 additions and 19 deletions

View File

@ -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;
}
}
}