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
Build and Deploy (Gitea) / build-and-deploy (push) Successful in 45s
Details
This commit is contained in:
parent
65a245f972
commit
fb84a2ccb6
|
|
@ -80,29 +80,34 @@ export class AsaasService {
|
||||||
return `cus_sim_${Math.random().toString(36).substring(2, 9)}`;
|
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 {
|
try {
|
||||||
const response = await fetch(`${this.getApiUrl()}/customers`, {
|
const response = await fetch(`${this.getApiUrl()}/customers`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: this.getHeaders(),
|
headers: this.getHeaders(),
|
||||||
body: JSON.stringify({
|
body: JSON.stringify(payload)
|
||||||
name,
|
|
||||||
email,
|
|
||||||
cpfCnpj: cpfCnpj || '000.000.000-00', // Default mock CPF if not provided
|
|
||||||
notificationDisabled: true
|
|
||||||
})
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
const errorData = await response.json().catch(() => ({}));
|
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();
|
const data = await response.json();
|
||||||
return data.id;
|
return data.id;
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
console.error('Asaas createCustomer error:', err);
|
console.error('Asaas createCustomer error:', err);
|
||||||
// Fallback in case of networking issues, so it doesn't block local experience completely
|
throw err;
|
||||||
return `cus_fallback_${Math.random().toString(36).substring(2, 9)}`;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -342,16 +347,7 @@ export class AsaasService {
|
||||||
return data;
|
return data;
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
console.error('Asaas createPayment error:', err);
|
console.error('Asaas createPayment error:', err);
|
||||||
// Return a simulated structure if networking fails so local experience doesn't crash 500
|
throw err;
|
||||||
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,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue