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:
@@ -105,6 +105,8 @@ export interface BillingProvider {
|
||||
/** `null` while the professor has not completed the checkout. */
|
||||
fetch(ref: SubscriptionRef): Promise<ProviderSubscription | null>;
|
||||
listCharges(ref: SubscriptionRef & { externalId: string }): Promise<ProviderCharge[]>;
|
||||
/** New monthly amount from the next charge on; what was already charged stays. */
|
||||
updateAmount(ref: SubscriptionRef & { externalId: string }, amount: number, currency: string): Promise<void>;
|
||||
/** Stops the monthly charge, or voids a checkout never completed. */
|
||||
cancel(ref: SubscriptionRef): Promise<void>;
|
||||
parseWebhook(request: BillingWebhookRequest): Promise<Result<BillingWebhookHint, BillingWebhookFailure>>;
|
||||
|
||||
@@ -137,6 +137,13 @@ export class MercadoPagoBillingProvider implements BillingProvider {
|
||||
await this.client.cancelPreapproval(this.tokenFor(ref.country), ref.externalId);
|
||||
}
|
||||
|
||||
async updateAmount(ref: SubscriptionRef & { externalId: string }, amount: number, currency: string): Promise<void> {
|
||||
await this.client.updatePreapprovalAmount(this.tokenFor(ref.country), ref.externalId, {
|
||||
amount,
|
||||
currencyId: currency,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* One URL per country, because each country is a different Gruperly account
|
||||
* with its own secret. Mercado Pago signs a manifest of the notified id, the
|
||||
|
||||
@@ -129,6 +129,23 @@ export async function cancelPreapproval(accessToken: string, id: string): Promis
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes the monthly amount of a live preapproval. Mercado Pago applies it to
|
||||
* the debits that follow; the ones already made are untouched.
|
||||
*/
|
||||
export async function updatePreapprovalAmount(
|
||||
accessToken: string,
|
||||
id: string,
|
||||
input: { amount: number; currencyId: string },
|
||||
): Promise<MpPreapproval> {
|
||||
return toPreapproval(
|
||||
await request(accessToken, `/preapproval/${encodeURIComponent(id)}`, {
|
||||
method: 'PUT',
|
||||
body: { auto_recurring: { transaction_amount: input.amount, currency_id: input.currencyId } },
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
function toAuthorizedPayment(payload: Record<string, unknown>): MpAuthorizedPayment {
|
||||
const payment = (payload.payment ?? null) as Record<string, unknown> | null;
|
||||
return {
|
||||
@@ -181,6 +198,7 @@ export type PreapprovalClient = {
|
||||
createPreapproval: typeof createPreapproval;
|
||||
getPreapproval: typeof getPreapproval;
|
||||
cancelPreapproval: typeof cancelPreapproval;
|
||||
updatePreapprovalAmount: typeof updatePreapprovalAmount;
|
||||
getAuthorizedPayment: typeof getAuthorizedPayment;
|
||||
searchAuthorizedPayments: typeof searchAuthorizedPayments;
|
||||
};
|
||||
@@ -189,6 +207,7 @@ export const mpPreapprovalClient: PreapprovalClient = {
|
||||
createPreapproval,
|
||||
getPreapproval,
|
||||
cancelPreapproval,
|
||||
updatePreapprovalAmount,
|
||||
getAuthorizedPayment,
|
||||
searchAuthorizedPayments,
|
||||
};
|
||||
|
||||
@@ -214,6 +214,39 @@ export class StripeBillingProvider implements BillingProvider {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Moves the subscription to a new monthly amount from its next invoice on:
|
||||
* the item gets a new inline price on the same product, without proration,
|
||||
* so the period already paid is not touched.
|
||||
*/
|
||||
async updateAmount(ref: SubscriptionRef & { externalId: string }, amount: number, currency: string): Promise<void> {
|
||||
const request = this.request;
|
||||
const path = `/subscriptions/${encodeURIComponent(ref.externalId)}`;
|
||||
const subscription = await request(path);
|
||||
const item = objectOf((objectOf(subscription.items)?.data as unknown[] | undefined)?.[0]);
|
||||
const itemId = idOf(item?.id);
|
||||
const product = idOf(objectOf(item?.price)?.product);
|
||||
if (!itemId || !product) throw new Error('Stripe subscription came back without its item');
|
||||
|
||||
await request(path, {
|
||||
method: 'POST',
|
||||
form: {
|
||||
proration_behavior: 'none',
|
||||
items: [
|
||||
{
|
||||
id: itemId,
|
||||
price_data: {
|
||||
currency: currency.toLowerCase(),
|
||||
unit_amount: toStripeUnitAmount(amount, currency),
|
||||
recurring: { interval: 'month' },
|
||||
product,
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
/** A single endpoint for the whole account; the event id is the dedupe key. */
|
||||
async parseWebhook(
|
||||
request: BillingWebhookRequest,
|
||||
|
||||
Reference in New Issue
Block a user