Files
gruperly/apps/backend/src/modules/billing/lib/helpers.ts
Jose Selesan 9617447e05 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>
2026-10-26 17:01:07 -03:00

28 lines
1.2 KiB
TypeScript

import type { BillingCharge, BillingSubscription } from '@generated/prisma/client';
import type { BillingChargeDto, BillingReturnTo, CountryCode } from '@gruperly/shared';
import { countryCodeSchema, DEFAULT_COUNTRY } from '@gruperly/shared';
export function toBillingChargeDto(charge: BillingCharge): BillingChargeDto {
return {
id: charge.id,
amount: Number(charge.amount.toString()),
currency: charge.currency,
status: charge.status,
paidAt: charge.paidAt?.toISOString() ?? null,
createdAt: charge.createdAt.toISOString(),
};
}
/** The professor's country as stored on the user; Argentina until they pick one. */
export function countryOf(user: { country: string | null } | null): CountryCode {
const parsed = countryCodeSchema.safeParse(user?.country);
return parsed.success ? parsed.data : DEFAULT_COUNTRY;
}
/** Where the billing provider sends the professor after authorizing (or abandoning). */
export function buildSubscriptionBackUrl(webOrigin: string, returnTo: BillingReturnTo): string {
return `${webOrigin}/${returnTo === 'onboarding' ? 'onboarding' : 'billing'}?sub=return`;
}
export type SubscriptionRecord = BillingSubscription;