feat(credits): solde IA unique (option M), packs Stripe et polish billing
Un pot de crédits global (BASIC 100 / PRO 1 000 / BUSINESS 4 000) remplace les seaux par fonction côté débit. Packs one-shot S/M/L, admin sans seaux legacy, usage multi-features, hints de coût, anti-flash thème et hydratation admin. Inclut aussi le pipeline slides renforcé et la page publique polishée.
This commit is contained in:
@@ -6,11 +6,79 @@ import {
|
||||
handleSubscriptionDeleted,
|
||||
resolveUserIdFromStripeEvent,
|
||||
} from '@/lib/billing/sync-subscription-from-stripe';
|
||||
import { prisma } from '@/lib/prisma';
|
||||
import { addPurchasedCredits } from '@/lib/credits';
|
||||
import {
|
||||
getCreditPack,
|
||||
isCreditPackId,
|
||||
resolvePackFromPriceId,
|
||||
} from '@/lib/billing/credit-packs';
|
||||
import type Stripe from 'stripe';
|
||||
|
||||
export const runtime = 'nodejs';
|
||||
|
||||
async function fulfillCreditPackPurchase(session: Stripe.Checkout.Session) {
|
||||
if (session.mode !== 'payment') return;
|
||||
if (session.payment_status && session.payment_status !== 'paid' && session.payment_status !== 'no_payment_required') {
|
||||
console.warn('[billing/webhook] pack checkout not paid yet', session.id, session.payment_status);
|
||||
return;
|
||||
}
|
||||
|
||||
const userId = session.metadata?.userId as string | undefined;
|
||||
if (!userId) {
|
||||
console.warn('[billing/webhook] credit pack: no userId', session.id);
|
||||
return;
|
||||
}
|
||||
|
||||
let packId = session.metadata?.packId as string | undefined;
|
||||
let credits = Number(session.metadata?.credits ?? 0);
|
||||
|
||||
if ((!packId || !Number.isFinite(credits) || credits <= 0) && session.line_items == null) {
|
||||
try {
|
||||
const full = await stripe.checkout.sessions.retrieve(session.id, {
|
||||
expand: ['line_items.data.price'],
|
||||
});
|
||||
const priceId = full.line_items?.data?.[0]?.price?.id;
|
||||
if (priceId) {
|
||||
const resolved = await resolvePackFromPriceId(priceId);
|
||||
if (resolved) {
|
||||
packId = resolved.packId;
|
||||
credits = resolved.credits;
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('[billing/webhook] expand line_items failed', err);
|
||||
}
|
||||
}
|
||||
|
||||
if (packId && isCreditPackId(packId) && (!Number.isFinite(credits) || credits <= 0)) {
|
||||
credits = getCreditPack(packId).credits;
|
||||
}
|
||||
|
||||
if (!Number.isFinite(credits) || credits <= 0) {
|
||||
console.warn('[billing/webhook] credit pack: invalid credits', session.id, { packId, credits });
|
||||
return;
|
||||
}
|
||||
|
||||
const result = await addPurchasedCredits(userId, credits, {
|
||||
stripeSessionId: session.id,
|
||||
packId: packId ?? null,
|
||||
type: 'credit_pack',
|
||||
paymentIntent:
|
||||
typeof session.payment_intent === 'string'
|
||||
? session.payment_intent
|
||||
: session.payment_intent?.id ?? null,
|
||||
});
|
||||
|
||||
if (result.applied) {
|
||||
console.info('[billing/webhook] credited pack', {
|
||||
userId,
|
||||
credits: result.units,
|
||||
packId,
|
||||
sessionId: session.id,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
const body = await req.text();
|
||||
const headersList = await headers();
|
||||
@@ -49,6 +117,11 @@ export async function POST(req: NextRequest) {
|
||||
} else {
|
||||
console.warn('[billing/webhook] checkout.session.completed: no userId in metadata', session.id);
|
||||
}
|
||||
} else if (
|
||||
session.mode === 'payment' &&
|
||||
(session.metadata?.type === 'credit_pack' || session.metadata?.packId)
|
||||
) {
|
||||
await fulfillCreditPackPurchase(session);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user