Files
gruperly/apps/backend/test/mp-preference.test.ts
Jose Selesan aac142d324 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>
2026-11-11 07:50:14 -03:00

79 lines
3.0 KiB
TypeScript

import { afterEach, describe, expect, it } from 'bun:test';
import { createCheckoutPreference, isPublicHttpsUrl, searchMpPaymentByExternalReference } from '@/modules/mp/lib/mp-http';
describe('isPublicHttpsUrl', () => {
it('only asks Mercado Pago for the automatic return on public HTTPS', () => {
expect(isPublicHttpsUrl('https://app.gruperly.com/student/payments')).toBe(true);
expect(isPublicHttpsUrl('http://localhost:6173/student/payments')).toBe(false);
expect(isPublicHttpsUrl('https://localhost:6173/student/payments')).toBe(false);
expect(isPublicHttpsUrl('http://app.gruperly.com/student/payments')).toBe(false);
expect(isPublicHttpsUrl('not a url')).toBe(false);
});
});
describe('createCheckoutPreference', () => {
const realFetch = globalThis.fetch;
afterEach(() => {
globalThis.fetch = realFetch;
});
async function sentBody(input: { backUrl: string; notificationUrl?: string }) {
let body: Record<string, unknown> = {};
globalThis.fetch = (async (_url: string, init: RequestInit) => {
body = JSON.parse(String(init.body));
return new Response(JSON.stringify({ id: 'pref-1', init_point: 'https://mp/pay' }), { status: 201 });
}) as unknown as typeof fetch;
await createCheckoutPreference('token', {
title: 'Octubre',
unitPrice: 100,
currencyId: 'ARS',
externalReference: 'pay-1',
...input,
});
return body;
}
it('asks for the automatic return and the notification on public HTTPS', async () => {
const body = await sentBody({
backUrl: 'https://app.gruperly.com/student/payments',
notificationUrl: 'https://app.gruperly.com/api/v1/payments/webhooks/mp/pay-1?source_news=webhooks',
});
expect(body.auto_return).toBe('approved');
expect(body.notification_url).toBe(
'https://app.gruperly.com/api/v1/payments/webhooks/mp/pay-1?source_news=webhooks',
);
});
it('leaves both out on localhost, which Mercado Pago refuses', async () => {
const body = await sentBody({
backUrl: 'http://localhost:6173/student/payments',
notificationUrl: 'http://localhost:4000/api/v1/payments/webhooks/mp/pay-1',
});
expect(body).not.toHaveProperty('auto_return');
expect(body).not.toHaveProperty('notification_url');
});
});
describe('searchMpPaymentByExternalReference', () => {
const realFetch = globalThis.fetch;
afterEach(() => {
globalThis.fetch = realFetch;
});
it('prefers the approved attempt over an earlier rejected one', async () => {
globalThis.fetch = (async () =>
new Response(
JSON.stringify({
results: [
{ id: 1, status: 'rejected', external_reference: 'pay-1' },
{ id: 2, status: 'approved', external_reference: 'pay-1', date_approved: '2026-11-11T03:00:00.000Z' },
],
}),
{ status: 200 },
)) as unknown as typeof fetch;
const found = await searchMpPaymentByExternalReference('token', 'pay-1');
expect(found).toMatchObject({ id: '2', status: 'approved' });
});
});