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:
Jose Selesan
2026-11-11 07:50:14 -03:00
parent abb18f1815
commit aac142d324
90 changed files with 2642 additions and 104 deletions

View File

@@ -0,0 +1,55 @@
import { afterEach, describe, expect, it } from 'bun:test';
import { Hono } from 'hono';
import { isPublicApiRequest } from '@/http/session-auth';
import returnRoute from '@/modules/billing/features/return/route';
import { getApiPublicOrigin } from '@/modules/billing/lib';
const previousWebUrl = process.env.WEB_URL;
function makeApp() {
return new Hono().basePath('/api/v1').route('/billing', returnRoute);
}
describe('GET /billing/return', () => {
afterEach(() => {
process.env.WEB_URL = previousWebUrl;
});
it('bounces the browser to the billing screen of the web', async () => {
process.env.WEB_URL = 'https://app.example.com';
const res = await makeApp().request('/api/v1/billing/return?to=billing&preapproval_id=pre-1');
expect(res.status).toBe(302);
expect(res.headers.get('location')).toBe('https://app.example.com/billing?sub=return');
});
it('goes back to the onboarding step when it started there', async () => {
process.env.WEB_URL = 'https://app.example.com';
const res = await makeApp().request('/api/v1/billing/return?to=onboarding');
expect(res.headers.get('location')).toBe('https://app.example.com/onboarding?sub=return');
});
it('never redirects anywhere but the web', async () => {
process.env.WEB_URL = 'https://app.example.com';
const res = await makeApp().request('/api/v1/billing/return?to=https://evil.example');
expect(res.headers.get('location')).toBe('https://app.example.com/billing?sub=return');
});
it('is reachable without a session', () => {
expect(isPublicApiRequest('GET', '/api/v1/billing/return')).toBe(true);
expect(isPublicApiRequest('POST', '/api/v1/billing/return')).toBe(false);
});
});
describe('getApiPublicOrigin', () => {
it('prefers API_PUBLIC_URL, then BETTER_AUTH_URL, then localhost', () => {
expect(
getApiPublicOrigin({ API_PUBLIC_URL: 'https://x.ngrok-free.app/', BETTER_AUTH_URL: 'http://localhost:4000' }),
).toBe('https://x.ngrok-free.app');
expect(getApiPublicOrigin({ BETTER_AUTH_URL: 'https://app.example.com' })).toBe('https://app.example.com');
expect(getApiPublicOrigin({})).toBe('http://localhost:4000');
expect(getApiPublicOrigin({ API_PUBLIC_URL: 'not a url' })).toBe('http://localhost:4000');
});
});