payments with stripe
Signed-off-by: Omar Sánchez Pizarro <omar.sanchez@pistacero.net>
This commit is contained in:
@@ -7,6 +7,98 @@ import { combineFingerprint } from '../utils/fingerprint.js';
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
// Endpoint de registro (público)
|
||||
router.post('/register', async (req, res) => {
|
||||
try {
|
||||
const db = getDB();
|
||||
if (!db) {
|
||||
return res.status(500).json({ error: 'MongoDB no está disponible' });
|
||||
}
|
||||
|
||||
const { username, password, email, planId } = req.body;
|
||||
|
||||
if (!username || !password) {
|
||||
return res.status(400).json({ error: 'username y password son requeridos' });
|
||||
}
|
||||
|
||||
if (username.length < 3) {
|
||||
return res.status(400).json({ error: 'El nombre de usuario debe tener al menos 3 caracteres' });
|
||||
}
|
||||
|
||||
if (password.length < 6) {
|
||||
return res.status(400).json({ error: 'La contraseña debe tener al menos 6 caracteres' });
|
||||
}
|
||||
|
||||
// Validar email si se proporciona
|
||||
if (email) {
|
||||
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
||||
if (!emailRegex.test(email)) {
|
||||
return res.status(400).json({ error: 'Email no válido' });
|
||||
}
|
||||
}
|
||||
|
||||
// Verificar si el usuario ya existe
|
||||
const existingUser = await getUser(username);
|
||||
if (existingUser) {
|
||||
return res.status(409).json({ error: 'El usuario ya existe' });
|
||||
}
|
||||
|
||||
// Verificar si es plan de pago y si Stripe está disponible
|
||||
const selectedPlanId = planId || 'free';
|
||||
const isPaidPlan = selectedPlanId !== 'free';
|
||||
|
||||
if (isPaidPlan) {
|
||||
// Para planes de pago, verificar que Stripe esté configurado
|
||||
const { getStripeClient } = await import('../services/stripe.js');
|
||||
const stripeClient = getStripeClient();
|
||||
|
||||
if (!stripeClient) {
|
||||
return res.status(503).json({
|
||||
error: 'El sistema de pagos no está disponible actualmente',
|
||||
message: 'No se pueden procesar planes de pago en este momento. Por favor, intenta con el plan gratuito o contacta con soporte.'
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Hashear contraseña y crear usuario
|
||||
const passwordHash = await bcrypt.hash(password, 10);
|
||||
await createUser({
|
||||
username,
|
||||
passwordHash,
|
||||
email: email || null,
|
||||
role: 'user',
|
||||
// Si es plan de pago, marcar como pendiente hasta que se complete el pago
|
||||
status: isPaidPlan ? 'pending_payment' : 'active',
|
||||
});
|
||||
|
||||
// Crear suscripción inicial
|
||||
const { updateUserSubscription } = await import('../services/mongodb.js');
|
||||
await updateUserSubscription(username, {
|
||||
planId: selectedPlanId,
|
||||
status: isPaidPlan ? 'pending' : 'active',
|
||||
currentPeriodStart: new Date(),
|
||||
currentPeriodEnd: null,
|
||||
cancelAtPeriodEnd: false,
|
||||
});
|
||||
|
||||
console.log(`✅ Usuario registrado: ${username} (${selectedPlanId}) - Estado: ${isPaidPlan ? 'pending_payment' : 'active'}`);
|
||||
res.json({
|
||||
success: true,
|
||||
message: 'Usuario creado correctamente',
|
||||
username,
|
||||
planId: selectedPlanId,
|
||||
requiresPayment: isPaidPlan,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error registrando usuario:', error);
|
||||
// Manejar error de duplicado
|
||||
if (error.code === 11000) {
|
||||
return res.status(409).json({ error: 'El usuario ya existe' });
|
||||
}
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// Endpoint de login (público)
|
||||
router.post('/login', async (req, res) => {
|
||||
try {
|
||||
@@ -42,6 +134,23 @@ router.post('/login', async (req, res) => {
|
||||
return res.status(401).json({ error: 'Invalid credentials', message: 'Usuario o contraseña incorrectos' });
|
||||
}
|
||||
|
||||
// Verificar estado del usuario
|
||||
if (user.status === 'pending_payment') {
|
||||
return res.status(403).json({
|
||||
error: 'Payment pending',
|
||||
message: 'Tu cuenta está pendiente de pago. Por favor, completa el proceso de pago para activar tu cuenta.',
|
||||
status: 'pending_payment'
|
||||
});
|
||||
}
|
||||
|
||||
if (user.status === 'suspended' || user.status === 'disabled') {
|
||||
return res.status(403).json({
|
||||
error: 'Account suspended',
|
||||
message: 'Tu cuenta ha sido suspendida. Contacta con soporte.',
|
||||
status: user.status
|
||||
});
|
||||
}
|
||||
|
||||
// Generar fingerprint del dispositivo
|
||||
const { fingerprint, deviceInfo } = combineFingerprint(clientFingerprint, clientDeviceInfo, req);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user