feat(billing): configurable plans from the backoffice and MP payment notifications
Plans: - Plan limits and Pro prices per country live in the database (plan_configs, plan_prices, seeded with the previous constants) and are read through PlanCatalog (30 s cache, defaults as fallback). - /admin/plans edits limits (with the count of accounts that would become read-only) and prices, optionally pushing the new price to live subscriptions from their next charge (MP preapproval amount, Stripe item without proration) via the ApplyPriceChanges job, with an email and a notice on /billing. - GET /billing/plans feeds the onboarding and the billing screen. Mercado Pago: - The Pro checkout returns through GET /billing/return on API_PUBLIC_URL, since MP refuses a back_url on localhost. - The OAuth callback bounces from the tunnel to BETTER_AUTH_URL to find the session, requires the session to own the state, and redirects instead of answering JSON on unexpected errors. - Checkout Pro links skip auto_return off public HTTPS and carry a notification_url to POST /payments/webhooks/mp/:paymentId, which confirms the payment with the owner's token (same rule as the polling job, which stays as fallback). The search now prefers the approved attempt. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,11 @@
|
||||
import { z } from 'zod';
|
||||
import { BillingChargeDtoSchema, billingProviderIdSchema, planSchema, subscriptionStatusSchema } from './billing.js';
|
||||
import {
|
||||
BillingChargeDtoSchema,
|
||||
billingProviderIdSchema,
|
||||
PlanLimitsSchema,
|
||||
planSchema,
|
||||
subscriptionStatusSchema,
|
||||
} from './billing.js';
|
||||
import { countryCodeSchema } from './country.js';
|
||||
import { createPageSchema, createPageSizeSchema, createPaginationSchema } from './pagination.js';
|
||||
|
||||
@@ -69,6 +75,8 @@ export const AdminAccountDetailSchema = AdminAccountListItemSchema.extend({
|
||||
graceUntil: isoDateTimeSchema.nullable(),
|
||||
/** Free with more usage than its limits (only with BILLING_ENFORCEMENT on). */
|
||||
readOnly: z.boolean(),
|
||||
/** The current limits of the account's plan (Free while none was chosen). */
|
||||
limits: PlanLimitsSchema,
|
||||
subscription: AdminSubscriptionSummarySchema.extend({
|
||||
payerEmail: z.string(),
|
||||
authorizedAt: isoDateTimeSchema.nullable(),
|
||||
@@ -87,3 +95,75 @@ export const AdminAccountDetailSchema = AdminAccountListItemSchema.extend({
|
||||
charges: z.array(BillingChargeDtoSchema),
|
||||
});
|
||||
export type AdminAccountDetail = z.output<typeof AdminAccountDetailSchema>;
|
||||
|
||||
// Plans: limits of Free and Pro and the price of Pro per country, editable from
|
||||
// the backoffice. Limits apply right away; a new price applies to new
|
||||
// subscriptions and, if asked, to live ones from their next charge.
|
||||
|
||||
const limitValueSchema = z.number().int().min(0).max(1_000_000).nullable();
|
||||
|
||||
export const UpdatePlanLimitsInputSchema = z.strictObject({
|
||||
/** `null` means no limit. */
|
||||
maxGroups: limitValueSchema,
|
||||
maxAttendees: limitValueSchema,
|
||||
whatsappAutomations: z.boolean(),
|
||||
mpCharges: z.boolean(),
|
||||
});
|
||||
export type UpdatePlanLimitsInput = z.output<typeof UpdatePlanLimitsInputSchema>;
|
||||
|
||||
/** Query values are strings: an empty or missing one means no limit. */
|
||||
const limitQuerySchema = z
|
||||
.string()
|
||||
.optional()
|
||||
.transform((value) => (value === undefined || value === '' ? null : Number(value)))
|
||||
.pipe(limitValueSchema);
|
||||
|
||||
export const PlanLimitsImpactQuerySchema = z.object({
|
||||
maxGroups: limitQuerySchema,
|
||||
maxAttendees: limitQuerySchema,
|
||||
});
|
||||
export type PlanLimitsImpactQuery = z.output<typeof PlanLimitsImpactQuerySchema>;
|
||||
|
||||
export const PlanLimitsImpactDtoSchema = z.object({
|
||||
/** Accounts on that plan whose usage would exceed the new limits. */
|
||||
affectedAccounts: z.number().int(),
|
||||
});
|
||||
export type PlanLimitsImpactDto = z.output<typeof PlanLimitsImpactDtoSchema>;
|
||||
|
||||
export const UpdatePlanPriceInputSchema = z.strictObject({
|
||||
amount: z.number().positive('billing.priceInvalid').max(100_000_000),
|
||||
/** Also move live subscriptions of that country to the new price, from their next charge. */
|
||||
applyToExisting: z.boolean(),
|
||||
});
|
||||
export type UpdatePlanPriceInput = z.output<typeof UpdatePlanPriceInputSchema>;
|
||||
|
||||
export const UpdatePlanPriceResultDtoSchema = z.object({
|
||||
country: countryCodeSchema,
|
||||
amount: z.number(),
|
||||
/** Live subscriptions queued to move to the new price. */
|
||||
queued: z.number().int(),
|
||||
});
|
||||
export type UpdatePlanPriceResultDto = z.output<typeof UpdatePlanPriceResultDtoSchema>;
|
||||
|
||||
export const AdminPlanPriceSchema = z.object({
|
||||
country: countryCodeSchema,
|
||||
amount: z.number(),
|
||||
currency: z.string(),
|
||||
provider: billingProviderIdSchema,
|
||||
updatedAt: isoDateTimeSchema.nullable(),
|
||||
/** Authorized or paused subscriptions of the country: what "apply to existing" moves. */
|
||||
liveSubscriptions: z.number().int(),
|
||||
/** Live subscriptions still at a different price. */
|
||||
atOtherPrice: z.number().int(),
|
||||
/** Price changes waiting to reach the provider. */
|
||||
pendingChanges: z.number().int(),
|
||||
/** Price changes the provider refused after every retry. */
|
||||
failedChanges: z.number().int(),
|
||||
});
|
||||
export type AdminPlanPrice = z.output<typeof AdminPlanPriceSchema>;
|
||||
|
||||
export const AdminPlansDtoSchema = z.object({
|
||||
limits: z.object({ FREE: PlanLimitsSchema, PRO: PlanLimitsSchema }),
|
||||
prices: z.array(AdminPlanPriceSchema),
|
||||
});
|
||||
export type AdminPlansDto = z.output<typeof AdminPlansDtoSchema>;
|
||||
|
||||
@@ -20,6 +20,9 @@ export type PlanLimits = {
|
||||
mpCharges: boolean;
|
||||
};
|
||||
|
||||
// Defaults only: the live limits and prices are edited from the backoffice and
|
||||
// live in `plan_configs` / `plan_prices` (seeded with these values). The backend
|
||||
// falls back to them when a row is missing; the web asks `GET /billing/plans`.
|
||||
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 },
|
||||
@@ -66,13 +69,20 @@ export type SubscriptionStatus = z.output<typeof subscriptionStatusSchema>;
|
||||
export const billingReturnToSchema = z.enum(['onboarding', 'billing']);
|
||||
export type BillingReturnTo = z.output<typeof billingReturnToSchema>;
|
||||
|
||||
const PlanLimitsSchema = z.object({
|
||||
export const PlanLimitsSchema = z.object({
|
||||
maxGroups: z.number().int().nullable(),
|
||||
maxAttendees: z.number().int().nullable(),
|
||||
whatsappAutomations: z.boolean(),
|
||||
mpCharges: z.boolean(),
|
||||
});
|
||||
|
||||
/** What each plan allows and what Pro costs in each country, as configured now. */
|
||||
export const PlanCatalogDtoSchema = z.object({
|
||||
limits: z.object({ FREE: PlanLimitsSchema, PRO: PlanLimitsSchema }),
|
||||
proPrices: z.record(countryCodeSchema, z.object({ amount: z.number(), currency: z.string() })),
|
||||
});
|
||||
export type PlanCatalogDto = z.output<typeof PlanCatalogDtoSchema>;
|
||||
|
||||
export const BillingStatusDtoSchema = z.object({
|
||||
plan: planSchema,
|
||||
/** `null` when the professor never started a subscription. */
|
||||
@@ -85,6 +95,8 @@ export const BillingStatusDtoSchema = z.object({
|
||||
nextPaymentDate: isoDateTimeSchema.nullable(),
|
||||
payerEmail: z.string().nullable(),
|
||||
priceAmount: z.number(),
|
||||
/** A new price that applies from the next charge on; `null` when unchanged. */
|
||||
nextPriceAmount: z.number().nullable(),
|
||||
currency: z.string(),
|
||||
country: countryCodeSchema,
|
||||
/** Who collects (or would collect) the subscription. */
|
||||
|
||||
Reference in New Issue
Block a user