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:
47
apps/backend/src/modules/billing/lib/config.ts
Normal file
47
apps/backend/src/modules/billing/lib/config.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import type { CountryCode, ProblemDetails, Result } from '@gruperly/shared';
|
||||
import { err, ok } from '@gruperly/shared';
|
||||
import { billingNotAvailableProblem } from '@/http/problem-builders';
|
||||
|
||||
export type PlatformCredentials = {
|
||||
/** Access token of Gruperly's own Mercado Pago account in that country. */
|
||||
accessToken: string;
|
||||
/** Secret of the webhook configured in that account's panel. */
|
||||
webhookSecret: string;
|
||||
};
|
||||
|
||||
const DEFAULT_GRACE_DAYS = 7;
|
||||
|
||||
/**
|
||||
* Gruperly's Mercado Pago credentials for one country. Mercado Pago does not
|
||||
* charge across countries, so each country has its own account; a country
|
||||
* without both variables cannot subscribe to Pro (503, not a deployment crash:
|
||||
* rolling out country by country is expected).
|
||||
*
|
||||
* These are platform credentials, never the professor's OAuth token.
|
||||
*/
|
||||
export function getPlatformCredentials(
|
||||
country: CountryCode,
|
||||
env: Record<string, string | undefined> = process.env,
|
||||
): Result<PlatformCredentials, ProblemDetails> {
|
||||
const accessToken = env[`MP_PLATFORM_ACCESS_TOKEN_${country}`]?.trim();
|
||||
const webhookSecret = env[`MP_PLATFORM_WEBHOOK_SECRET_${country}`]?.trim();
|
||||
if (!accessToken || !webhookSecret) {
|
||||
return err(billingNotAvailableProblem());
|
||||
}
|
||||
return ok({ accessToken, webhookSecret });
|
||||
}
|
||||
|
||||
/**
|
||||
* Plan limits only apply with `BILLING_ENFORCEMENT=on`. Anything else (unset
|
||||
* included) treats everyone as Pro, so the module can ship before the existing
|
||||
* professors are moved to a plan.
|
||||
*/
|
||||
export function isBillingEnforced(env: Record<string, string | undefined> = process.env): boolean {
|
||||
return env.BILLING_ENFORCEMENT?.trim().toLowerCase() === 'on';
|
||||
}
|
||||
|
||||
/** Days a lapsed Pro keeps working after its last paid period. */
|
||||
export function getGraceDays(env: Record<string, string | undefined> = process.env): number {
|
||||
const value = Number(env.BILLING_GRACE_DAYS);
|
||||
return Number.isInteger(value) && value >= 0 ? value : DEFAULT_GRACE_DAYS;
|
||||
}
|
||||
Reference in New Issue
Block a user