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:
70
apps/backend/prisma/models/billing.prisma
Normal file
70
apps/backend/prisma/models/billing.prisma
Normal file
@@ -0,0 +1,70 @@
|
||||
// Gruperly's own billing: the professor pays a monthly Pro subscription through
|
||||
// Mercado Pago Subscriptions (preapproval), collected by Gruperly's account in
|
||||
// the professor's country. Not to be confused with `Payment`, which is what
|
||||
// students pay the professor.
|
||||
|
||||
enum SubscriptionStatus {
|
||||
PENDING
|
||||
AUTHORIZED
|
||||
PAUSED
|
||||
CANCELLED
|
||||
}
|
||||
|
||||
model BillingSubscription {
|
||||
id String @id @default(cuid())
|
||||
userId String @unique(map: "billing_subscriptions_userId_key") @map("user_id")
|
||||
country String @map("country_code")
|
||||
currency String
|
||||
amount Decimal @db.Decimal(12, 2)
|
||||
payerEmail String @map("payer_email")
|
||||
// Null between creating the row and Mercado Pago answering.
|
||||
mpPreapprovalId String? @unique(map: "billing_subscriptions_mpPreapprovalId_key") @map("mp_preapproval_id")
|
||||
status SubscriptionStatus @default(PENDING)
|
||||
// When the current attempt started: re-subscribing reuses the row.
|
||||
requestedAt DateTime @default(now()) @map("requested_at")
|
||||
authorizedAt DateTime? @map("authorized_at")
|
||||
nextPaymentDate DateTime? @map("next_payment_date")
|
||||
// Last day covered by an approved charge. Pro lasts until then plus the grace
|
||||
// period, even after a cancellation or a failed renewal.
|
||||
paidThrough DateTime? @map("paid_through")
|
||||
lastSyncedAt DateTime? @map("last_synced_at")
|
||||
createdAt DateTime @default(now()) @map("created_at")
|
||||
updatedAt DateTime @updatedAt @map("updated_at")
|
||||
|
||||
user User @relation(fields: [userId], references: [id], onDelete: Cascade, map: "billing_subscriptions_userId_fkey")
|
||||
charges BillingCharge[]
|
||||
|
||||
// The reconciliation job walks PENDING and stale AUTHORIZED rows.
|
||||
@@index([status, lastSyncedAt], map: "billing_subscriptions_status_lastSyncedAt_idx")
|
||||
@@map("billing_subscriptions")
|
||||
}
|
||||
|
||||
// One Mercado Pago "authorized payment" (a monthly invoice) of a subscription.
|
||||
model BillingCharge {
|
||||
id String @id @default(cuid())
|
||||
subscriptionId String @map("subscription_id")
|
||||
mpAuthorizedPaymentId String @unique(map: "billing_charges_mpAuthorizedPaymentId_key") @map("mp_authorized_payment_id")
|
||||
amount Decimal @db.Decimal(12, 2)
|
||||
currency String
|
||||
// Mercado Pago's own status of the charge (e.g. approved, rejected).
|
||||
status String
|
||||
paidAt DateTime? @map("paid_at")
|
||||
createdAt DateTime @default(now()) @map("created_at")
|
||||
updatedAt DateTime @updatedAt @map("updated_at")
|
||||
|
||||
subscription BillingSubscription @relation(fields: [subscriptionId], references: [id], onDelete: Cascade, map: "billing_charges_subscriptionId_fkey")
|
||||
|
||||
@@index([subscriptionId, createdAt], map: "billing_charges_subscriptionId_createdAt_idx")
|
||||
@@map("billing_charges")
|
||||
}
|
||||
|
||||
// Deduplication of Mercado Pago webhook deliveries, per country account.
|
||||
model BillingWebhookEvent {
|
||||
country String @map("country_code")
|
||||
dedupeKey String @map("dedupe_key")
|
||||
receivedAt DateTime @default(now()) @map("received_at")
|
||||
|
||||
@@id([country, dedupeKey], map: "billing_webhook_events_pkey")
|
||||
@@index([receivedAt], map: "billing_webhook_events_receivedAt_idx")
|
||||
@@map("billing_webhook_events")
|
||||
}
|
||||
Reference in New Issue
Block a user