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:
@@ -13,6 +13,8 @@ export type MpCreatePreferenceInput = {
|
||||
externalReference: string;
|
||||
/** Where the buyer lands after paying (pending/success/failure). */
|
||||
backUrl: string;
|
||||
/** Where Mercado Pago notifies the payment (`POST /payments/webhooks/mp/:id`). */
|
||||
notificationUrl?: string;
|
||||
};
|
||||
|
||||
export type MpPreference = {
|
||||
@@ -21,6 +23,21 @@ export type MpPreference = {
|
||||
sandboxInitPoint: string | null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Mercado Pago refuses `auto_return` (`invalid_auto_return`) and notification
|
||||
* URLs unless they are public HTTPS. In local development the web is on
|
||||
* localhost: the buyer then gets MP's "back to the site" button instead of the
|
||||
* automatic redirect, and without a tunnel only the polling confirms payments.
|
||||
*/
|
||||
export function isPublicHttpsUrl(value: string): boolean {
|
||||
try {
|
||||
const url = new URL(value);
|
||||
return url.protocol === 'https:' && !['localhost', '127.0.0.1', '[::1]'].includes(url.hostname);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a Checkout Pro preference in the seller's account. This is a payment
|
||||
* *link*: nothing is charged, and creating it again after a lost connection is
|
||||
@@ -50,7 +67,10 @@ export async function createCheckoutPreference(
|
||||
},
|
||||
],
|
||||
external_reference: input.externalReference,
|
||||
auto_return: 'approved',
|
||||
...(isPublicHttpsUrl(input.backUrl) ? { auto_return: 'approved' } : {}),
|
||||
...(input.notificationUrl && isPublicHttpsUrl(input.notificationUrl)
|
||||
? { notification_url: input.notificationUrl }
|
||||
: {}),
|
||||
back_urls: {
|
||||
success: input.backUrl,
|
||||
pending: input.backUrl,
|
||||
@@ -120,9 +140,10 @@ export async function searchMpPaymentByExternalReference(
|
||||
|
||||
const payload = (await response.json()) as { results?: Record<string, unknown>[] } | null;
|
||||
const results = payload?.results ?? [];
|
||||
const match = results.find(
|
||||
(payment) => payment.external_reference === externalReference,
|
||||
);
|
||||
// A rejected attempt and a later approved one share the reference: the
|
||||
// approved one wins, whatever order Mercado Pago lists them in.
|
||||
const ours = results.filter((payment) => payment.external_reference === externalReference);
|
||||
const match = ours.find((payment) => payment.status === 'approved') ?? ours[0];
|
||||
if (!match) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user