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>
159 lines
4.8 KiB
TypeScript
159 lines
4.8 KiB
TypeScript
import { beforeEach, describe, expect, it, mock, vi } from 'bun:test';
|
|
import { Hono } from 'hono';
|
|
|
|
const db = {
|
|
payment: {
|
|
findUnique: mock(),
|
|
update: mock(),
|
|
},
|
|
user: {
|
|
findUnique: mock(),
|
|
},
|
|
};
|
|
|
|
mock.module('@/lib/prisma', () => ({
|
|
default: db,
|
|
getPrismaClient: mock(),
|
|
UnitOfWork: class {},
|
|
}));
|
|
|
|
import prisma from '@/lib/prisma';
|
|
import { paymentsRoutes } from '@/modules/payments';
|
|
import { RenewPaymentLink } from '@/modules/payments/features/renew/use-case';
|
|
|
|
const userId = 'user-1';
|
|
const webOrigin = 'http://localhost:6173';
|
|
|
|
function makeApp(userValue: unknown) {
|
|
const app = new Hono();
|
|
app.use('*', async (c, next) => {
|
|
c.set('user', userValue as never);
|
|
await next();
|
|
});
|
|
app.route('/payments', paymentsRoutes);
|
|
return app;
|
|
}
|
|
|
|
function paymentWithoutLink(overrides: Record<string, unknown> = {}) {
|
|
return {
|
|
id: 'pay-1',
|
|
groupId: 'group-1',
|
|
attendeeId: 'att-1',
|
|
amount: { toString: () => '120.00' } as { toString(): string },
|
|
currency: 'ARS',
|
|
status: 'PENDING',
|
|
description: null,
|
|
checkoutUrl: null,
|
|
mpPreferenceId: null,
|
|
dueDate: new Date('2026-10-05T12:00:00.000Z'),
|
|
paidAt: null,
|
|
createdAt: new Date('2026-10-01T10:00:00.000Z'),
|
|
updatedAt: new Date('2026-10-01T10:00:00.000Z'),
|
|
attendee: { person: { fullName: 'Juana Pérez' } },
|
|
group: {
|
|
name: 'Vinyasa',
|
|
price: { toString: () => '120.00' },
|
|
createdById: userId,
|
|
members: [],
|
|
},
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
describe('renew payment link', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
it('creates a new link for a payment that lost it and returns the payment', async () => {
|
|
prisma.payment.findUnique.mockResolvedValue(paymentWithoutLink());
|
|
prisma.user.findUnique.mockResolvedValue({ id: userId, country: 'AR' });
|
|
prisma.payment.update.mockResolvedValue({
|
|
...paymentWithoutLink(),
|
|
checkoutUrl: 'https://mp.link/pay-1',
|
|
group: { name: 'Vinyasa', price: { toString: () => '120.00' } },
|
|
});
|
|
const getToken = mock(async () => ({
|
|
ok: true,
|
|
value: { accountId: 'acc-1', accessToken: 'token-1', expiresAt: new Date() },
|
|
}));
|
|
const createPreference = mock(async () => ({
|
|
id: 'pref-new',
|
|
initPoint: 'https://mp.link/pay-1',
|
|
sandboxInitPoint: null,
|
|
}));
|
|
|
|
const result = await new RenewPaymentLink({ getToken, createPreference, webOrigin, apiOrigin: 'https://api' }).execute(
|
|
'pay-1',
|
|
userId,
|
|
);
|
|
|
|
expect(result.ok).toBe(true);
|
|
if (!result.ok) throw new Error('expected ok');
|
|
expect(result.value).toMatchObject({
|
|
id: 'pay-1',
|
|
groupName: 'Vinyasa',
|
|
attendeeName: 'Juana Pérez',
|
|
checkoutUrl: 'https://mp.link/pay-1',
|
|
});
|
|
expect(createPreference).toHaveBeenCalledWith('token-1', {
|
|
title: 'Vinyasa',
|
|
unitPrice: 120,
|
|
currencyId: 'ARS',
|
|
externalReference: 'pay-1',
|
|
backUrl: `${webOrigin}/student/payments`,
|
|
notificationUrl: 'https://api/api/v1/payments/webhooks/mp/pay-1?source_news=webhooks',
|
|
});
|
|
expect(prisma.payment.update).toHaveBeenCalledWith({
|
|
where: { id: 'pay-1' },
|
|
data: { checkoutUrl: 'https://mp.link/pay-1', mpPreferenceId: 'pref-new' },
|
|
include: {
|
|
attendee: { select: { person: { select: { fullName: true } } } },
|
|
group: { select: { name: true, price: true } },
|
|
},
|
|
});
|
|
});
|
|
|
|
it('keeps an existing link: idempotent, no Mercado Pago call', async () => {
|
|
prisma.payment.findUnique.mockResolvedValue(
|
|
paymentWithoutLink({ checkoutUrl: 'https://mp.link/pay-1' }),
|
|
);
|
|
|
|
const res = await makeApp({ id: userId }).request('/payments/pay-1/renew', { method: 'POST' });
|
|
|
|
expect(res.status).toBe(200);
|
|
expect(prisma.payment.update).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('answers 409 when the owner has no linked account', async () => {
|
|
prisma.payment.findUnique.mockResolvedValue(paymentWithoutLink());
|
|
prisma.user.findUnique.mockResolvedValue({ id: userId, country: 'AR' });
|
|
const getToken = mock(async () => ({
|
|
ok: false,
|
|
error: { type: 'x', title: 'Conflict', status: 409, detail: 'x', code: 'mp_not_connected' },
|
|
}));
|
|
|
|
const result = await new RenewPaymentLink({ getToken }).execute('pay-1', userId);
|
|
|
|
expect(result.ok).toBe(false);
|
|
expect(prisma.payment.update).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('rejects a payment of a group the user has no access to', async () => {
|
|
prisma.payment.findUnique.mockResolvedValue(
|
|
paymentWithoutLink({
|
|
group: {
|
|
name: 'Vinyasa',
|
|
price: { toString: () => '120.00' },
|
|
createdById: 'someone-else',
|
|
members: [],
|
|
},
|
|
}),
|
|
);
|
|
|
|
const res = await makeApp({ id: userId }).request('/payments/pay-1/renew', { method: 'POST' });
|
|
|
|
expect(res.status).toBe(403);
|
|
expect(prisma.payment.update).not.toHaveBeenCalled();
|
|
});
|
|
}); |