// 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") }