feat(billing): configurable plans from the backoffice and MP payment notifications
Plans: - Plan limits and Pro prices per country live in the database (plan_configs, plan_prices, seeded with the previous constants) and are read through PlanCatalog (30 s cache, defaults as fallback). - /admin/plans edits limits (with the count of accounts that would become read-only) and prices, optionally pushing the new price to live subscriptions from their next charge (MP preapproval amount, Stripe item without proration) via the ApplyPriceChanges job, with an email and a notice on /billing. - GET /billing/plans feeds the onboarding and the billing screen. Mercado Pago: - The Pro checkout returns through GET /billing/return on API_PUBLIC_URL, since MP refuses a back_url on localhost. - The OAuth callback bounces from the tunnel to BETTER_AUTH_URL to find the session, requires the session to own the state, and redirects instead of answering JSON on unexpected errors. - Checkout Pro links skip auto_return off public HTTPS and carry a notification_url to POST /payments/webhooks/mp/:paymentId, which confirms the payment with the owner's token (same rule as the polling job, which stays as fallback). The search now prefers the approved attempt. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { beforeEach, describe, expect, it, mock } from 'bun:test';
|
||||
import { CancelSubscription } from '@/modules/billing/features/cancel/use-case';
|
||||
import { Subscribe } from '@/modules/billing/features/subscribe/use-case';
|
||||
import { staticPlanCatalog } from '@/modules/billing/lib/catalog';
|
||||
import type { BillingProvider } from '@/modules/billing/providers';
|
||||
import { MercadoPagoBillingProvider, type PreapprovalClient } from '@/modules/billing/providers/mercado-pago';
|
||||
|
||||
@@ -53,7 +54,7 @@ function subscribe(db: ReturnType<typeof makeDb>, client = makeClient()) {
|
||||
return {
|
||||
client,
|
||||
run: (returnTo: 'billing' | 'onboarding' = 'billing') =>
|
||||
new Subscribe({ db: db as never, providers: () => provider, webOrigin: 'http://web', now: () => now }).execute(
|
||||
new Subscribe({ catalog: staticPlanCatalog(), db: db as never, providers: () => provider, apiOrigin: 'https://api', now: () => now }).execute(
|
||||
userId,
|
||||
{ payerEmail: 'profe@mail.com', returnTo },
|
||||
),
|
||||
@@ -79,7 +80,7 @@ describe('Subscribe', () => {
|
||||
payerEmail: 'profe@mail.com',
|
||||
amount: 15000,
|
||||
currencyId: 'ARS',
|
||||
backUrl: 'http://web/billing?sub=return',
|
||||
backUrl: 'https://api/api/v1/billing/return?to=billing',
|
||||
startDate: undefined,
|
||||
});
|
||||
expect(db.billingSubscription.update).toHaveBeenCalledWith({
|
||||
@@ -93,7 +94,7 @@ describe('Subscribe', () => {
|
||||
await run('onboarding');
|
||||
|
||||
expect((client.createPreapproval as ReturnType<typeof mock>).mock.calls[0]?.[1]).toMatchObject({
|
||||
backUrl: 'http://web/onboarding?sub=return',
|
||||
backUrl: 'https://api/api/v1/billing/return?to=onboarding',
|
||||
});
|
||||
});
|
||||
|
||||
@@ -131,7 +132,7 @@ describe('Subscribe', () => {
|
||||
const db = makeDb(null, 'AR');
|
||||
const client = makeClient();
|
||||
const provider = new MercadoPagoBillingProvider({ client, credentials: () => null });
|
||||
const result = await new Subscribe({ db: db as never, providers: () => provider }).execute(userId, {
|
||||
const result = await new Subscribe({ catalog: staticPlanCatalog(), db: db as never, providers: () => provider }).execute(userId, {
|
||||
payerEmail: 'profe@mail.com',
|
||||
returnTo: 'billing',
|
||||
});
|
||||
@@ -224,13 +225,13 @@ describe('Subscribe outside Argentina (Stripe)', () => {
|
||||
const stripe = fakeStripe();
|
||||
const resolved: string[] = [];
|
||||
|
||||
const result = await new Subscribe({
|
||||
const result = await new Subscribe({ catalog: staticPlanCatalog(),
|
||||
db: db as never,
|
||||
providers: (id) => {
|
||||
resolved.push(id);
|
||||
return stripe;
|
||||
},
|
||||
webOrigin: 'http://web',
|
||||
apiOrigin: 'https://api',
|
||||
now: () => now,
|
||||
}).execute(userId, { returnTo: 'billing' });
|
||||
|
||||
@@ -242,7 +243,7 @@ describe('Subscribe outside Argentina (Stripe)', () => {
|
||||
amount: 199,
|
||||
currency: 'MXN',
|
||||
payerEmail: null,
|
||||
backUrl: 'http://web/billing?sub=return',
|
||||
backUrl: 'https://api/api/v1/billing/return?to=billing',
|
||||
startDate: undefined,
|
||||
});
|
||||
expect((db.billingSubscription.upsert.mock.calls[0] as unknown as [{ create: Record<string, unknown> }])[0].create).toMatchObject({
|
||||
@@ -261,10 +262,10 @@ describe('Subscribe outside Argentina (Stripe)', () => {
|
||||
const mp = mpProvider(client);
|
||||
const stripe = fakeStripe();
|
||||
|
||||
await new Subscribe({
|
||||
await new Subscribe({ catalog: staticPlanCatalog(),
|
||||
db: db as never,
|
||||
providers: (id) => (id === 'MERCADO_PAGO' ? mp : stripe),
|
||||
webOrigin: 'http://web',
|
||||
apiOrigin: 'https://api',
|
||||
now: () => now,
|
||||
}).execute(userId, { returnTo: 'billing' });
|
||||
|
||||
@@ -275,7 +276,7 @@ describe('Subscribe outside Argentina (Stripe)', () => {
|
||||
const db = makeDb(null, 'CL');
|
||||
const stripe = fakeStripe({ isAvailable: () => false });
|
||||
|
||||
const result = await new Subscribe({ db: db as never, providers: () => stripe }).execute(userId, {
|
||||
const result = await new Subscribe({ catalog: staticPlanCatalog(), db: db as never, providers: () => stripe }).execute(userId, {
|
||||
returnTo: 'billing',
|
||||
});
|
||||
|
||||
@@ -288,7 +289,7 @@ describe('Subscribe outside Argentina (Stripe)', () => {
|
||||
const db = makeDb(null, 'AR');
|
||||
const provider = mpProvider(makeClient());
|
||||
|
||||
const result = await new Subscribe({ db: db as never, providers: () => provider }).execute(userId, {
|
||||
const result = await new Subscribe({ catalog: staticPlanCatalog(), db: db as never, providers: () => provider }).execute(userId, {
|
||||
returnTo: 'billing',
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user