From ba998abbc7ff5b40aee9f702f950bbd3c0165263 Mon Sep 17 00:00:00 2001 From: Sidney Gomes Date: Tue, 21 Jul 2026 18:36:52 -0300 Subject: [PATCH] fix: auto refresh simulated customer id when real asaas key is set and change 500 status to 400 --- server.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/server.ts b/server.ts index c2ca8cd..542f48e 100644 --- a/server.ts +++ b/server.ts @@ -331,6 +331,10 @@ app.post('/api/courses/:id/unlock', authenticateToken, checkoutRateLimiter, asyn try { let customerId = user.asaasCustomerId; + const apiKey = AsaasService.getApiKey(); + if (apiKey && (customerId?.startsWith('cus_sim_') || customerId?.startsWith('cus_fallback_'))) { + customerId = null; + } if (!customerId) { customerId = await AsaasService.createCustomer(user.name, user.email); user.asaasCustomerId = customerId; @@ -388,7 +392,7 @@ app.post('/api/courses/:id/unlock', authenticateToken, checkoutRateLimiter, asyn res.json({ success: true, payment: newPayment, pixData }); } catch (err: any) { 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 { let customerId = user.asaasCustomerId; + const apiKey = AsaasService.getApiKey(); + if (apiKey && (customerId?.startsWith('cus_sim_') || customerId?.startsWith('cus_fallback_'))) { + customerId = null; + } if (!customerId) { customerId = await AsaasService.createCustomer(user.name, user.email); user.asaasCustomerId = customerId; @@ -554,7 +562,8 @@ app.post('/api/subscription/subscribe', authenticateToken, async (req: Authentic payment: newPayment }); } 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' }); } });