feat(billing): proveedores intercambiables (Mercado Pago en AR, Stripe en el resto)
- 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>
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
import type { PrismaClient } from '@generated/prisma/client';
|
||||
import type { BillingStatusDto, ProblemDetails, Result } from '@gruperly/shared';
|
||||
import { COUNTRY_CURRENCY, err, ok, PRO_PRICE_BY_COUNTRY } from '@gruperly/shared';
|
||||
import { billingProviderForCountry, COUNTRY_CURRENCY, err, ok, PRO_PRICE_BY_COUNTRY } from '@gruperly/shared';
|
||||
import { notFoundResourceProblem } from '@/http/problem-builders';
|
||||
import prisma from '@/lib/prisma';
|
||||
import { countryOf, getPlatformCredentials, PlanGuard, SyncSubscription } from '../../lib';
|
||||
import { countryOf, PlanGuard, SyncSubscription } from '../../lib';
|
||||
import { type BillingProviderResolver, getBillingProvider } from '../../providers';
|
||||
|
||||
/** A PENDING subscription is re-read from Mercado Pago at most this often. */
|
||||
const PENDING_SYNC_INTERVAL_MS = 30 * 1000;
|
||||
@@ -12,7 +13,7 @@ type GetBillingStatusDeps = {
|
||||
db?: Pick<PrismaClient, 'user' | 'billingSubscription'>;
|
||||
guard?: PlanGuard;
|
||||
sync?: Pick<SyncSubscription, 'execute'>;
|
||||
credentials?: typeof getPlatformCredentials;
|
||||
providers?: BillingProviderResolver;
|
||||
now?: () => Date;
|
||||
};
|
||||
|
||||
@@ -29,11 +30,11 @@ export class GetBillingStatus {
|
||||
|
||||
let subscription = await db.billingSubscription.findUnique({ where: { userId } });
|
||||
|
||||
// Coming back from Mercado Pago, the webhook may not have arrived yet (or
|
||||
// may never arrive in local dev): ask Mercado Pago directly, throttled.
|
||||
// Coming back from the provider's checkout, the webhook may not have
|
||||
// arrived yet (or may never arrive in local dev): ask directly, throttled.
|
||||
if (
|
||||
subscription?.status === 'PENDING'
|
||||
&& subscription.mpPreapprovalId
|
||||
&& (subscription.externalId || subscription.checkoutRef)
|
||||
&& (!subscription.lastSyncedAt
|
||||
|| now.getTime() - subscription.lastSyncedAt.getTime() > PENDING_SYNC_INTERVAL_MS)
|
||||
) {
|
||||
@@ -47,6 +48,11 @@ export class GetBillingStatus {
|
||||
// A live subscription keeps the price it was created with; otherwise the
|
||||
// current price of the country is what subscribing would cost.
|
||||
const live = subscription && subscription.status !== 'CANCELLED' ? subscription : null;
|
||||
// An authorized subscription stays with its provider; a new attempt (or
|
||||
// retrying a PENDING one) goes to the country's current provider.
|
||||
const provider = (this.deps.providers ?? getBillingProvider)(
|
||||
live && live.status !== 'PENDING' ? live.provider : billingProviderForCountry(country),
|
||||
);
|
||||
|
||||
return ok({
|
||||
plan: entitlements.plan,
|
||||
@@ -62,7 +68,8 @@ export class GetBillingStatus {
|
||||
priceAmount: live ? Number(live.amount.toString()) : PRO_PRICE_BY_COUNTRY[country],
|
||||
currency: live ? live.currency : COUNTRY_CURRENCY[country],
|
||||
country,
|
||||
availableInCountry: (this.deps.credentials ?? getPlatformCredentials)(country).ok,
|
||||
provider: provider.id,
|
||||
availableInCountry: provider.isAvailable(country),
|
||||
enforced: entitlements.enforced,
|
||||
usage: entitlements.usage,
|
||||
limits: entitlements.limits,
|
||||
|
||||
Reference in New Issue
Block a user