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,19 +1,20 @@
|
||||
import type { PrismaClient, SubscriptionStatus } from '@generated/prisma/client';
|
||||
import type { CountryCode, ProblemDetails, Result } from '@gruperly/shared';
|
||||
import type { ProblemDetails, Result } from '@gruperly/shared';
|
||||
import { err, ok } from '@gruperly/shared';
|
||||
import { billingProviderErrorProblem, noActiveSubscriptionProblem } from '@/http/problem-builders';
|
||||
import { noActiveSubscriptionProblem } from '@/http/problem-builders';
|
||||
import prisma from '@/lib/prisma';
|
||||
import { getPlatformCredentials, mpPreapprovalClient, type PreapprovalClient } from '../../lib';
|
||||
import { subscriptionRefOf } from '../../lib';
|
||||
import { type BillingProviderResolver, getBillingProvider, toBillingProblem } from '../../providers';
|
||||
|
||||
type CancelSubscriptionDeps = {
|
||||
db?: Pick<PrismaClient, 'billingSubscription'>;
|
||||
client?: PreapprovalClient;
|
||||
credentials?: typeof getPlatformCredentials;
|
||||
providers?: BillingProviderResolver;
|
||||
};
|
||||
|
||||
/**
|
||||
* Stops the monthly charge. `paidThrough` stays: the professor keeps Pro until
|
||||
* the end of what they already paid (plus the grace period).
|
||||
* Stops the monthly charge with the provider that collects it. `paidThrough`
|
||||
* stays: the professor keeps Pro until the end of what they already paid
|
||||
* (plus the grace period).
|
||||
*/
|
||||
export class CancelSubscription {
|
||||
constructor(private readonly deps: CancelSubscriptionDeps = {}) {}
|
||||
@@ -22,22 +23,20 @@ export class CancelSubscription {
|
||||
const db = this.deps.db ?? prisma;
|
||||
|
||||
const subscription = await db.billingSubscription.findUnique({ where: { userId } });
|
||||
if (!subscription?.mpPreapprovalId || subscription.status === 'CANCELLED') {
|
||||
if (
|
||||
!subscription
|
||||
|| (!subscription.externalId && !subscription.checkoutRef)
|
||||
|| subscription.status === 'CANCELLED'
|
||||
) {
|
||||
return err(noActiveSubscriptionProblem());
|
||||
}
|
||||
|
||||
const credentials = (this.deps.credentials ?? getPlatformCredentials)(
|
||||
subscription.country as CountryCode,
|
||||
);
|
||||
if (!credentials.ok) return credentials;
|
||||
|
||||
try {
|
||||
await (this.deps.client ?? mpPreapprovalClient).cancelPreapproval(
|
||||
credentials.value.accessToken,
|
||||
subscription.mpPreapprovalId,
|
||||
await (this.deps.providers ?? getBillingProvider)(subscription.provider).cancel(
|
||||
subscriptionRefOf(subscription),
|
||||
);
|
||||
} catch {
|
||||
return err(billingProviderErrorProblem());
|
||||
} catch (error) {
|
||||
return err(toBillingProblem(error));
|
||||
}
|
||||
|
||||
await db.billingSubscription.update({
|
||||
|
||||
Reference in New Issue
Block a user