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:
@@ -0,0 +1,74 @@
|
||||
-- CreateEnum
|
||||
CREATE TYPE "SubscriptionStatus" AS ENUM ('PENDING', 'AUTHORIZED', 'PAUSED', 'CANCELLED');
|
||||
|
||||
-- AlterTable
|
||||
ALTER TABLE "users" ADD COLUMN "plan_selected_at" TIMESTAMP(3);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "billing_subscriptions" (
|
||||
"id" TEXT NOT NULL,
|
||||
"user_id" TEXT NOT NULL,
|
||||
"country_code" TEXT NOT NULL,
|
||||
"currency" TEXT NOT NULL,
|
||||
"amount" DECIMAL(12,2) NOT NULL,
|
||||
"payer_email" TEXT NOT NULL,
|
||||
"mp_preapproval_id" TEXT,
|
||||
"status" "SubscriptionStatus" NOT NULL DEFAULT 'PENDING',
|
||||
"requested_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"authorized_at" TIMESTAMP(3),
|
||||
"next_payment_date" TIMESTAMP(3),
|
||||
"paid_through" TIMESTAMP(3),
|
||||
"last_synced_at" TIMESTAMP(3),
|
||||
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updated_at" TIMESTAMP(3) NOT NULL,
|
||||
|
||||
CONSTRAINT "billing_subscriptions_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "billing_charges" (
|
||||
"id" TEXT NOT NULL,
|
||||
"subscription_id" TEXT NOT NULL,
|
||||
"mp_authorized_payment_id" TEXT NOT NULL,
|
||||
"amount" DECIMAL(12,2) NOT NULL,
|
||||
"currency" TEXT NOT NULL,
|
||||
"status" TEXT NOT NULL,
|
||||
"paid_at" TIMESTAMP(3),
|
||||
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updated_at" TIMESTAMP(3) NOT NULL,
|
||||
|
||||
CONSTRAINT "billing_charges_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "billing_webhook_events" (
|
||||
"country_code" TEXT NOT NULL,
|
||||
"dedupe_key" TEXT NOT NULL,
|
||||
"received_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
CONSTRAINT "billing_webhook_events_pkey" PRIMARY KEY ("country_code","dedupe_key")
|
||||
);
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "billing_subscriptions_userId_key" ON "billing_subscriptions"("user_id");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "billing_subscriptions_mpPreapprovalId_key" ON "billing_subscriptions"("mp_preapproval_id");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "billing_subscriptions_status_lastSyncedAt_idx" ON "billing_subscriptions"("status", "last_synced_at");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "billing_charges_mpAuthorizedPaymentId_key" ON "billing_charges"("mp_authorized_payment_id");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "billing_charges_subscriptionId_createdAt_idx" ON "billing_charges"("subscription_id", "created_at");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "billing_webhook_events_receivedAt_idx" ON "billing_webhook_events"("received_at");
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "billing_subscriptions" ADD CONSTRAINT "billing_subscriptions_userId_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "billing_charges" ADD CONSTRAINT "billing_charges_subscriptionId_fkey" FOREIGN KEY ("subscription_id") REFERENCES "billing_subscriptions"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
@@ -1,26 +1,29 @@
|
||||
// Auth (Better Auth) - required tables
|
||||
|
||||
model User {
|
||||
id String @id @default(cuid())
|
||||
id String @id @default(cuid())
|
||||
name String
|
||||
email String @unique
|
||||
emailVerified Boolean @default(false) @map("email_verified")
|
||||
email String @unique
|
||||
emailVerified Boolean @default(false) @map("email_verified")
|
||||
image String?
|
||||
onboardingCompleted Boolean @default(false) @map("onboarding_completed")
|
||||
country String? @map("country_code")
|
||||
createdAt DateTime @default(now()) @map("created_at")
|
||||
updatedAt DateTime @updatedAt @map("updated_at")
|
||||
onboardingCompleted Boolean @default(false) @map("onboarding_completed")
|
||||
country String? @map("country_code")
|
||||
// When the professor chose Free or Pro in the onboarding (mandatory step).
|
||||
planSelectedAt DateTime? @map("plan_selected_at")
|
||||
createdAt DateTime @default(now()) @map("created_at")
|
||||
updatedAt DateTime @updatedAt @map("updated_at")
|
||||
|
||||
accounts Account[]
|
||||
sessions Session[]
|
||||
passkeys Passkey[]
|
||||
groupsMember GroupMember[]
|
||||
groupsOwner Group[] @relation("OwnerGroups")
|
||||
groupsOwner Group[] @relation("OwnerGroups")
|
||||
orgMemberships Member[]
|
||||
orgInvitations Invitation[] @relation("InvitedBy")
|
||||
orgInvitations Invitation[] @relation("InvitedBy")
|
||||
waitlist WaitlistEntry[]
|
||||
mpAccount MpAccount?
|
||||
whatsappAccount WhatsappAccount?
|
||||
subscription BillingSubscription?
|
||||
|
||||
@@map("users")
|
||||
}
|
||||
|
||||
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