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:
@@ -27,6 +27,7 @@ import prisma from '@/lib/prisma';
|
||||
import { getMpAccessToken, mpRoutes } from '@/modules/mp';
|
||||
import { RefreshExpiringMpTokens } from '@/modules/mp/jobs/refresh-expiring-mp-tokens';
|
||||
import { RefreshMpAccount } from '@/modules/mp/jobs/refresh-mp-account';
|
||||
import { mpCallbackBounceUrl } from '@/modules/mp/lib/callback-origin';
|
||||
import { codeChallengeFromVerifier, openToken, sealToken } from '@/modules/mp/lib/crypto';
|
||||
import { MpApiError } from '@/modules/mp/lib/mp-oauth';
|
||||
|
||||
@@ -579,6 +580,17 @@ describe('modules/mp', () => {
|
||||
expect(location.href).not.toContain(storedState.codeVerifier);
|
||||
});
|
||||
|
||||
it('refuses a state started by another teacher, without spending it', async () => {
|
||||
prisma.mpOAuthState.findUnique.mockResolvedValue({ ...storedState, ownerId: otherOwnerId });
|
||||
|
||||
const res = await makeApp({ id: ownerId }).request(callbackUrl('?code=auth-code-123&state=state-abc'));
|
||||
|
||||
expect(res.status).toBe(302);
|
||||
expect(locationOf(res).searchParams.get('reason')).toBe('invalid_state');
|
||||
expect(prisma.mpOAuthState.deleteMany).not.toHaveBeenCalled();
|
||||
expect(prisma.mpAccount.upsert).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('sends a teacher without a session to the login screen', async () => {
|
||||
const res = await makeApp(null).request(callbackUrl('?code=code&state=state-abc'));
|
||||
|
||||
@@ -1040,3 +1052,30 @@ describe('modules/mp RefreshExpiringMpTokens', () => {
|
||||
expect(totals).toEqual({ refreshed: 1, requiresReauth: 1, failed: 1 });
|
||||
});
|
||||
});
|
||||
|
||||
describe('mpCallbackBounceUrl', () => {
|
||||
const env = {
|
||||
MP_REDIRECT_URI: 'https://tunnel.ngrok-free.app/api/v1/mp/callback',
|
||||
BETTER_AUTH_URL: 'http://localhost:4000',
|
||||
};
|
||||
const url = 'https://tunnel.ngrok-free.app/api/v1/mp/callback?code=c&state=s';
|
||||
|
||||
it('replays a callback that came through the tunnel on the origin with the session', () => {
|
||||
expect(mpCallbackBounceUrl(url, 'tunnel.ngrok-free.app', env)).toBe(
|
||||
'http://localhost:4000/api/v1/mp/callback?code=c&state=s',
|
||||
);
|
||||
});
|
||||
|
||||
it('leaves it alone once it is on the session origin', () => {
|
||||
expect(mpCallbackBounceUrl('http://localhost:4000/api/v1/mp/callback?code=c', 'localhost:4000', env)).toBeNull();
|
||||
});
|
||||
|
||||
it('never fires when the redirect and the session share a host (production)', () => {
|
||||
const prod = {
|
||||
MP_REDIRECT_URI: 'https://app.gruperly.com/api/v1/mp/callback',
|
||||
BETTER_AUTH_URL: 'https://app.gruperly.com',
|
||||
};
|
||||
expect(mpCallbackBounceUrl('http://api:4000/api/v1/mp/callback?code=c', 'app.gruperly.com', prod)).toBeNull();
|
||||
expect(mpCallbackBounceUrl('http://api:4000/api/v1/mp/callback?code=c', 'api:4000', prod)).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user