- Puerto BillingProvider con adaptadores de Mercado Pago (/preapproval) y Stripe (Checkout en modo suscripción, fetch plano). El país elige proveedor solo para suscripciones nuevas; sync, cancel y webhook usan row.provider. - Migración add_billing_providers (provider, checkout_ref, ids genéricos). - fix: /authorized_payments/search respondía 400 "Invalid value for limit"; se pagina por offset sin limit. - fix: si falla la consulta de cobros, la sync guarda igual el estado de la suscripción (ya no queda PENDING tras autorizar) y reintenta los cobros. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
83 lines
3.8 KiB
Plaintext
83 lines
3.8 KiB
Plaintext
// Gruperly's own billing: the professor pays a monthly Pro subscription,
|
|
// collected by Mercado Pago Subscriptions (preapproval, Argentina) or Stripe
|
|
// (every other country), see `modules/billing/providers/`. Not to be confused
|
|
// with `Payment`, which is what students pay the professor.
|
|
|
|
enum BillingProviderId {
|
|
MERCADO_PAGO
|
|
STRIPE
|
|
}
|
|
|
|
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")
|
|
// Who collects it. Sync and cancel always follow this, never the current country.
|
|
provider BillingProviderId @default(MERCADO_PAGO)
|
|
// The provider's subscription id (MP preapproval, Stripe subscription). Null
|
|
// until the provider creates it: right after MP answers, or once the
|
|
// professor completes Stripe Checkout.
|
|
externalId String? @unique(map: "billing_subscriptions_externalId_key") @map("external_id")
|
|
// Stripe Checkout Session of the current attempt; null for Mercado Pago.
|
|
checkoutRef String? @unique(map: "billing_subscriptions_checkoutRef_key") @map("checkout_ref")
|
|
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 monthly invoice of a subscription (MP authorized payment, Stripe invoice).
|
|
model BillingCharge {
|
|
id String @id @default(cuid())
|
|
subscriptionId String @map("subscription_id")
|
|
externalId String @unique(map: "billing_charges_externalId_key") @map("external_id")
|
|
amount Decimal @db.Decimal(12, 2)
|
|
currency String
|
|
// Normalized across providers: approved, pending or 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 billing webhook deliveries, per sending account: `mp:<CC>`
|
|
// (one Mercado Pago account per country) or `stripe`.
|
|
model BillingWebhookEvent {
|
|
source String @map("source")
|
|
dedupeKey String @map("dedupe_key")
|
|
receivedAt DateTime @default(now()) @map("received_at")
|
|
|
|
@@id([source, dedupeKey], map: "billing_webhook_events_pkey")
|
|
@@index([receivedAt], map: "billing_webhook_events_receivedAt_idx")
|
|
@@map("billing_webhook_events")
|
|
}
|