feat(billing): planes Free/Pro con Suscripciones de Mercado Pago

- Módulo billing: suscripción Pro mensual vía /preapproval con credenciales
  de plataforma por país, webhook firmado y reconciliación por polling.
- PlanGuard en los use-cases que crean grupos/alumnos, cobran con MP o
  activan automatizaciones (402 plan_limit_reached / plan_feature_required /
  plan_read_only), detrás de BILLING_ENFORCEMENT.
- Onboarding: paso Plan obligatorio (POST /onboarding/plan); con Free se
  saltean Cobros y WhatsApp.
- Web: pantalla /billing, aviso de Pro desde el MutationCache.
- Migración billing_subscriptions y docs/billing.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Jose Selesan
2026-10-23 15:49:27 -03:00
parent f4d2c26e29
commit f5e77c99f6
93 changed files with 4402 additions and 117 deletions

View File

@@ -215,4 +215,89 @@ export function noAttendeesToBillProblem(): ProblemDetails {
detail: 'No hay miembros activos con un cobro pendiente para este grupo.',
code: 'no_attendees_to_bill',
});
}
}
function paymentRequiredProblem(params: { detail: string; code: string }): ProblemDetails {
return {
type: `${PROBLEM_DOMAIN}/problems/plan`,
title: 'Payment Required',
status: 402,
detail: params.detail,
code: params.code,
};
}
export type PlanLimitKind = 'groups' | 'attendees';
/** The Free plan does not allow one more group or attendee. */
export function planLimitReachedProblem(limit: PlanLimitKind, max: number): ProblemDetails {
return paymentRequiredProblem({
detail:
limit === 'groups'
? `El plan Free permite hasta ${max} ${max === 1 ? 'grupo' : 'grupos'}. Pasate a Pro para crear más.`
: `El plan Free permite hasta ${max} alumnos activos. Pasate a Pro para sumar más.`,
code: 'plan_limit_reached',
});
}
/** A Pro-only feature: charging students with Mercado Pago or WhatsApp automations. */
export function planFeatureRequiredProblem(feature: 'mpCharges' | 'whatsappAutomations'): ProblemDetails {
return paymentRequiredProblem({
detail:
feature === 'mpCharges'
? 'Cobrar con Mercado Pago está disponible en el plan Pro.'
: 'Los mensajes automáticos de WhatsApp están disponibles en el plan Pro.',
code: 'plan_feature_required',
});
}
/** Free plan with more usage than its limits: only reads and deletes. */
export function planReadOnlyProblem(): ProblemDetails {
return paymentRequiredProblem({
detail:
'Tu cuenta está en solo lectura porque superás los límites del plan Free. Pasate a Pro o reducí el uso para volver a editar.',
code: 'plan_read_only',
});
}
/** Gruperly has no Mercado Pago account configured for the professor's country. */
export function billingNotAvailableProblem(): ProblemDetails {
return {
type: `${PROBLEM_DOMAIN}/problems/billing-not-available`,
title: 'Service Unavailable',
status: 503,
detail: 'La suscripción a Pro todavía no está disponible en tu país.',
code: 'billing_not_available',
};
}
/** Mercado Pago refused the subscription request, or could not be reached. */
export function billingProviderErrorProblem(): ProblemDetails {
return {
type: `${PROBLEM_DOMAIN}/problems/billing-provider-error`,
title: 'Bad Gateway',
status: 502,
detail: 'No pudimos comunicarnos con Mercado Pago. Intentá de nuevo en unos minutos.',
code: 'billing_provider_error',
};
}
export function subscriptionAlreadyActiveProblem(): ProblemDetails {
return conflictProblem({
detail: 'Ya tenés una suscripción a Pro activa.',
code: 'subscription_already_active',
});
}
export function noActiveSubscriptionProblem(): ProblemDetails {
return conflictProblem({
detail: 'No tenés una suscripción para cancelar.',
code: 'no_active_subscription',
});
}
export function planNotSelectedProblem(): ProblemDetails {
return conflictProblem({
detail: 'Elegí un plan antes de crear tu primer grupo.',
code: 'plan_not_selected',
});
}

View File

@@ -42,6 +42,11 @@ export function isPublicApiRequest(method: string, path: string): boolean {
) {
return true;
}
// Mercado Pago notifications about Pro subscriptions, one URL per country:
// no session, authenticated by Mercado Pago's `x-signature`.
if (method === 'POST' && /^\/api\/v1\/billing\/webhooks\/mp\/[a-z]{2}$/i.test(normalizedPath)) {
return true;
}
return (
normalizedPath === '/api/v1/health'