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:
@@ -2,7 +2,6 @@ import { NextRequest, NextResponse } from 'next/server';
|
||||
import { auth } from '@/auth';
|
||||
import { getUserInfo, getEffectiveTier } from '@/lib/entitlements';
|
||||
import { stripe } from '@/lib/stripe';
|
||||
import type Stripe from 'stripe';
|
||||
import { priceIdToTier, getDynamicPrices, isBillingEnabled } from '@/lib/billing/stripe-prices';
|
||||
|
||||
export const dynamic = 'force-dynamic';
|
||||
@@ -76,6 +75,42 @@ export async function GET(req: NextRequest) {
|
||||
const subscription = await prisma.subscription.findUnique({ where: { userId } });
|
||||
const prices = await getDynamicPrices();
|
||||
const billingEnabled = await isBillingEnabled();
|
||||
const { getPackPublicPrices } = await import('@/lib/billing/credit-packs');
|
||||
const creditPacks = await getPackPublicPrices();
|
||||
|
||||
// Si retour checkout pack : créditer au cas où le webhook n'a pas encore tourné
|
||||
if (sessionId && sessionId.startsWith('cs_')) {
|
||||
try {
|
||||
const checkoutSession = await stripe.checkout.sessions.retrieve(sessionId);
|
||||
if (
|
||||
checkoutSession.mode === 'payment' &&
|
||||
checkoutSession.status === 'complete' &&
|
||||
(checkoutSession.metadata?.type === 'credit_pack' || checkoutSession.metadata?.packId) &&
|
||||
checkoutSession.metadata?.userId === userId
|
||||
) {
|
||||
const { addPurchasedCredits } = await import('@/lib/credits');
|
||||
const {
|
||||
getCreditPack,
|
||||
isCreditPackId,
|
||||
} = await import('@/lib/billing/credit-packs');
|
||||
const packId = checkoutSession.metadata?.packId;
|
||||
let credits = Number(checkoutSession.metadata?.credits ?? 0);
|
||||
if (packId && isCreditPackId(packId) && (!Number.isFinite(credits) || credits <= 0)) {
|
||||
credits = getCreditPack(packId).credits;
|
||||
}
|
||||
if (Number.isFinite(credits) && credits > 0) {
|
||||
await addPurchasedCredits(userId, credits, {
|
||||
stripeSessionId: checkoutSession.id,
|
||||
packId: packId ?? null,
|
||||
type: 'credit_pack',
|
||||
source: 'billing_status_sync',
|
||||
});
|
||||
}
|
||||
}
|
||||
} catch (packSyncErr) {
|
||||
console.error('[billing/status] pack sync failed:', packSyncErr);
|
||||
}
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
tier,
|
||||
@@ -86,6 +121,7 @@ export async function GET(req: NextRequest) {
|
||||
cancelAtPeriodEnd: subscription?.cancelAtPeriodEnd ?? false,
|
||||
hasStripeSubscription: !!subscription?.stripeSubscriptionId,
|
||||
prices,
|
||||
creditPacks,
|
||||
billingEnabled,
|
||||
});
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user