fix: auto refresh simulated customer id when real asaas key is set and change 500 status to 400
Build and Deploy (Gitea) / build-and-deploy (push) Successful in 44s Details

This commit is contained in:
Sidney Gomes 2026-07-21 18:36:52 -03:00
parent fb84a2ccb6
commit ba998abbc7
1 changed files with 11 additions and 2 deletions

View File

@ -331,6 +331,10 @@ app.post('/api/courses/:id/unlock', authenticateToken, checkoutRateLimiter, asyn
try { try {
let customerId = user.asaasCustomerId; let customerId = user.asaasCustomerId;
const apiKey = AsaasService.getApiKey();
if (apiKey && (customerId?.startsWith('cus_sim_') || customerId?.startsWith('cus_fallback_'))) {
customerId = null;
}
if (!customerId) { if (!customerId) {
customerId = await AsaasService.createCustomer(user.name, user.email); customerId = await AsaasService.createCustomer(user.name, user.email);
user.asaasCustomerId = customerId; user.asaasCustomerId = customerId;
@ -388,7 +392,7 @@ app.post('/api/courses/:id/unlock', authenticateToken, checkoutRateLimiter, asyn
res.json({ success: true, payment: newPayment, pixData }); res.json({ success: true, payment: newPayment, pixData });
} catch (err: any) { } catch (err: any) {
console.error('Course unlock error:', err); console.error('Course unlock error:', err);
res.status(500).json({ error: err.message || 'Erro ao processar pagamento do curso' }); res.status(400).json({ error: err.message || 'Erro ao processar pagamento do curso' });
} }
}); });
@ -511,6 +515,10 @@ app.post('/api/subscription/subscribe', authenticateToken, async (req: Authentic
try { try {
let customerId = user.asaasCustomerId; let customerId = user.asaasCustomerId;
const apiKey = AsaasService.getApiKey();
if (apiKey && (customerId?.startsWith('cus_sim_') || customerId?.startsWith('cus_fallback_'))) {
customerId = null;
}
if (!customerId) { if (!customerId) {
customerId = await AsaasService.createCustomer(user.name, user.email); customerId = await AsaasService.createCustomer(user.name, user.email);
user.asaasCustomerId = customerId; user.asaasCustomerId = customerId;
@ -554,7 +562,8 @@ app.post('/api/subscription/subscribe', authenticateToken, async (req: Authentic
payment: newPayment payment: newPayment
}); });
} catch (err: any) { } catch (err: any) {
res.status(500).json({ error: err.message || 'Erro ao processar assinatura' }); console.error('Subscription error:', err);
res.status(400).json({ error: err.message || 'Erro ao processar assinatura' });
} }
}); });