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:
130
packages/shared/src/schemas/billing.ts
Normal file
130
packages/shared/src/schemas/billing.ts
Normal file
@@ -0,0 +1,130 @@
|
||||
import { z } from 'zod';
|
||||
import { type CountryCode, countryCodeSchema } from './country.js';
|
||||
import { createPageSchema, createPageSizeSchema, createPaginationSchema } from './pagination.js';
|
||||
|
||||
const isoDateTimeSchema = z.string().datetime();
|
||||
|
||||
// Gruperly's own plans: what the professor pays us, not what students pay them.
|
||||
export const planSchema = z.enum(['FREE', 'PRO']);
|
||||
export type Plan = z.output<typeof planSchema>;
|
||||
|
||||
export const planFeatureSchema = z.enum(['whatsappAutomations', 'mpCharges']);
|
||||
export type PlanFeature = z.output<typeof planFeatureSchema>;
|
||||
|
||||
export type PlanLimits = {
|
||||
/** Groups the professor owns. `null` means no limit. */
|
||||
maxGroups: number | null;
|
||||
/** ACTIVE attendees across all the groups the professor owns. */
|
||||
maxAttendees: number | null;
|
||||
whatsappAutomations: boolean;
|
||||
mpCharges: boolean;
|
||||
};
|
||||
|
||||
export const PLAN_LIMITS: Record<Plan, PlanLimits> = {
|
||||
FREE: { maxGroups: 1, maxAttendees: 15, whatsappAutomations: false, mpCharges: false },
|
||||
PRO: { maxGroups: null, maxAttendees: null, whatsappAutomations: true, mpCharges: true },
|
||||
};
|
||||
|
||||
// Monthly price of Pro, in the currency of each country (COUNTRY_CURRENCY).
|
||||
// Mercado Pago does not charge across countries, so each one is collected by
|
||||
// Gruperly's own account in that country.
|
||||
export const PRO_PRICE_BY_COUNTRY: Record<CountryCode, number> = {
|
||||
AR: 15000,
|
||||
BR: 49,
|
||||
CL: 9900,
|
||||
CO: 39900,
|
||||
MX: 199,
|
||||
PE: 39,
|
||||
UY: 390,
|
||||
};
|
||||
|
||||
// Mirror of the Mercado Pago preapproval status.
|
||||
export const subscriptionStatusSchema = z.enum(['PENDING', 'AUTHORIZED', 'PAUSED', 'CANCELLED']);
|
||||
export type SubscriptionStatus = z.output<typeof subscriptionStatusSchema>;
|
||||
|
||||
// Where the professor started the subscription, so Mercado Pago sends them back.
|
||||
export const billingReturnToSchema = z.enum(['onboarding', 'billing']);
|
||||
export type BillingReturnTo = z.output<typeof billingReturnToSchema>;
|
||||
|
||||
const PlanLimitsSchema = z.object({
|
||||
maxGroups: z.number().int().nullable(),
|
||||
maxAttendees: z.number().int().nullable(),
|
||||
whatsappAutomations: z.boolean(),
|
||||
mpCharges: z.boolean(),
|
||||
});
|
||||
|
||||
export const BillingStatusDtoSchema = z.object({
|
||||
plan: planSchema,
|
||||
/** `null` when the professor never started a subscription. */
|
||||
status: subscriptionStatusSchema.nullable(),
|
||||
/** Free plan with more usage than its limits: only reads and deletes work. */
|
||||
readOnly: z.boolean(),
|
||||
/** Set while a lapsed Pro still works: payment failed or cancelled, not yet expired. */
|
||||
graceUntil: isoDateTimeSchema.nullable(),
|
||||
paidThrough: isoDateTimeSchema.nullable(),
|
||||
nextPaymentDate: isoDateTimeSchema.nullable(),
|
||||
payerEmail: z.string().nullable(),
|
||||
priceAmount: z.number(),
|
||||
currency: z.string(),
|
||||
country: countryCodeSchema,
|
||||
/** Gruperly has Mercado Pago credentials for the professor's country. */
|
||||
availableInCountry: z.boolean(),
|
||||
/** `false` when the server runs with BILLING_ENFORCEMENT off. */
|
||||
enforced: z.boolean(),
|
||||
usage: z.object({ groups: z.number().int(), attendees: z.number().int() }),
|
||||
limits: PlanLimitsSchema,
|
||||
});
|
||||
export type BillingStatusDto = z.output<typeof BillingStatusDtoSchema>;
|
||||
|
||||
export const SubscribeInputSchema = z.strictObject({
|
||||
// Mercado Pago requires the payer to log in with this exact email.
|
||||
payerEmail: z.string().trim().email('Ingresá un email válido'),
|
||||
returnTo: billingReturnToSchema.default('billing'),
|
||||
});
|
||||
export type SubscribeInput = z.output<typeof SubscribeInputSchema>;
|
||||
|
||||
export const SubscribeResultDtoSchema = z.object({
|
||||
checkoutUrl: z.string(),
|
||||
});
|
||||
export type SubscribeResultDto = z.output<typeof SubscribeResultDtoSchema>;
|
||||
|
||||
export const BillingChargeDtoSchema = z.object({
|
||||
id: z.string(),
|
||||
amount: z.number(),
|
||||
currency: z.string(),
|
||||
status: z.string(),
|
||||
paidAt: isoDateTimeSchema.nullable(),
|
||||
createdAt: isoDateTimeSchema,
|
||||
});
|
||||
export type BillingChargeDto = z.output<typeof BillingChargeDtoSchema>;
|
||||
|
||||
export const BillingChargesQuerySchema = z.object({
|
||||
page: createPageSchema(),
|
||||
pageSize: createPageSizeSchema(10),
|
||||
});
|
||||
export type BillingChargesQuery = z.output<typeof BillingChargesQuerySchema>;
|
||||
|
||||
export const BillingChargeListSchema = z.object({
|
||||
data: z.array(BillingChargeDtoSchema),
|
||||
pagination: createPaginationSchema(),
|
||||
});
|
||||
export type BillingChargeList = z.output<typeof BillingChargeListSchema>;
|
||||
|
||||
// Onboarding step "Plan": mandatory. The country comes along because it is not
|
||||
// stored until then, and it decides the price and the Mercado Pago account.
|
||||
export const SelectPlanInputSchema = z.discriminatedUnion('plan', [
|
||||
z.strictObject({ plan: z.literal('FREE'), country: countryCodeSchema }),
|
||||
z.strictObject({
|
||||
plan: z.literal('PRO'),
|
||||
country: countryCodeSchema,
|
||||
payerEmail: z.string().trim().email('Ingresá un email válido'),
|
||||
}),
|
||||
]);
|
||||
export type SelectPlanInput = z.output<typeof SelectPlanInputSchema>;
|
||||
|
||||
export const SelectPlanResultDtoSchema = z.object({
|
||||
plan: planSchema,
|
||||
/** Only for PRO: where the browser goes to authorize the subscription. */
|
||||
checkoutUrl: z.string().nullable(),
|
||||
});
|
||||
export type SelectPlanResultDto = z.output<typeof SelectPlanResultDtoSchema>;
|
||||
@@ -1,4 +1,5 @@
|
||||
import { z } from 'zod';
|
||||
import { planSchema, subscriptionStatusSchema } from './billing.js';
|
||||
import { countryCodeSchema, DEFAULT_COUNTRY } from './country.js';
|
||||
import { billingTypeSchema, weekdaySchema } from './enums.js';
|
||||
import { GroupDtoSchema } from './groups.js';
|
||||
@@ -65,5 +66,12 @@ export const OnboardingStatusDtoSchema = z.object({
|
||||
// WhatsApp is its own skippable step and does not move `step`.
|
||||
whatsappConnected: z.boolean(),
|
||||
completed: z.boolean(),
|
||||
// The "Plan" step is mandatory: these survive the round trip to Mercado Pago
|
||||
// and a reload, so the flow resumes where it was.
|
||||
planSelected: z.boolean(),
|
||||
// PRO as soon as the professor chose it, even while the subscription is PENDING.
|
||||
selectedPlan: planSchema.nullable(),
|
||||
subscriptionStatus: subscriptionStatusSchema.nullable(),
|
||||
country: countryCodeSchema.nullable(),
|
||||
});
|
||||
export type OnboardingStatusDto = z.output<typeof OnboardingStatusDtoSchema>;
|
||||
Reference in New Issue
Block a user