Add monetization spine + Build Your Own Case (Min Sak)

- Stripe: StripeClient.php, checkout/portal/webhook endpoints, idempotent event handling
- FreeTier: tier-aware credits (free/light/pro/pro_plus), bonus_balance, hourly caps per tier
- pricing.php + billing.php: 4-tier cards, 3 topups, Customer Portal, balance breakdown
- Min Sak: CaseStore.php, AzureDocIntelligence.php, AzureSearchAdmin.php — per-user hybrid RAG
- api/case/: upload, list, delete, ingest-callback (HMAC-auth'd from n8n)
- award-survey-credits: inter-site HMAC endpoint for dobetternorge.no survey bonus
- dashboard.php: tier badge, balance breakdown card, Min Sak CTA, survey CTA
- KorrespondAgent + all 3 other agents: use_my_case toggle wired to dbnToolsCaseContext()
- bootstrap.php: dbnToolsCaseContext(), dbnToolsIntersiteSecret(), dbnToolsCurrentTier()

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-05-20 20:52:54 +02:00
parent ed5489d174
commit ba9cddf9a1
30 changed files with 2804 additions and 133 deletions
+259 -55
View File
@@ -2,18 +2,22 @@
declare(strict_types=1);
/**
* Credit system for free-tier (Google-authenticated) users of tools.dobetternorge.no.
* Credit + tier system for users of tools.dobetternorge.no.
*
* Credits are stored in dobetternorge_maindb.user_tool_credits.
* Usage is logged in user_tool_usage_log.
* Tables:
* user_tool_credits — balance (monthly, resets), bonus_balance (never expires), tier, Stripe links
* user_tool_usage_log — every tool call with credits_used
* user_subscriptions — Stripe subscription ledger
*
* Effective balance = balance + bonus_balance.
* Spend order: deduct from balance first, overflow to bonus_balance.
* pro_plus tier bypasses balance checks (still subject to hourly cap).
*
* CaveauAI client sessions (dbn_tools_user_id + client_id) bypass all checks.
* Only SSO sessions with tier='free' are subject to limits.
* Only SSO sessions are subject to limits.
*/
final class FreeTier
{
private const HOURLY_LIMIT = 10;
private const COSTS = [
'corpus-search' => 0,
'search' => 0,
@@ -24,8 +28,33 @@ final class FreeTier
'barnevernet' => 3,
'advocate' => 3,
'deep-research' => 5,
'transcribe' => 2, // flat rate; actual duration unknown upfront
'discrepancy' => 4, // 2 docs × 4 extraction steps + cross-ref + synthesis
'transcribe' => 2,
'discrepancy' => 4,
'korrespond' => 3,
];
/** Monthly credit allowance per tier. pro_plus is "effectively unlimited" but hourly-capped. */
private const MONTHLY_ALLOWANCE = [
'free' => 30,
'light' => 120,
'pro' => 500,
'pro_plus' => 999999,
];
/** Hourly rate-limit per tier (number of paid tool calls per rolling hour). */
private const HOURLY_CAP = [
'free' => 10,
'light' => 15,
'pro' => 30,
'pro_plus' => 50,
];
/** Per-user case-storage quota in bytes. */
private const STORAGE_QUOTA = [
'free' => 0,
'light' => 104857600, // 100 MB
'pro' => 1073741824, // 1 GB
'pro_plus' => 10737418240, // 10 GB
];
/** Credit cost for a given tool slug. Returns 1 for unknown tools. */
@@ -34,94 +63,269 @@ final class FreeTier
return self::COSTS[$tool] ?? 1;
}
public static function monthlyAllowance(string $tier): int
{
return self::MONTHLY_ALLOWANCE[$tier] ?? self::MONTHLY_ALLOWANCE['free'];
}
public static function hourlyCap(string $tier): int
{
return self::HOURLY_CAP[$tier] ?? self::HOURLY_CAP['free'];
}
public static function storageQuota(string $tier): int
{
return self::STORAGE_QUOTA[$tier] ?? 0;
}
/** Fetch a user's tier (defaults to 'free' if no row). */
public static function tier(int $userId): string
{
$row = self::row($userId);
return $row['tier'] ?? 'free';
}
/** Fetch the full credits row, applying lazy monthly reset. */
public static function row(int $userId): ?array
{
$db = dbnmDb();
// Auto-refill monthly balance based on tier-specific allowance, only if a new calendar month has begun.
$db->prepare(
"UPDATE user_tool_credits
SET balance = CASE tier
WHEN 'free' THEN " . self::MONTHLY_ALLOWANCE['free'] . "
WHEN 'light' THEN " . self::MONTHLY_ALLOWANCE['light'] . "
WHEN 'pro' THEN " . self::MONTHLY_ALLOWANCE['pro'] . "
WHEN 'pro_plus' THEN " . self::MONTHLY_ALLOWANCE['pro_plus'] . "
ELSE balance END,
last_reset = CURDATE()
WHERE user_id = ?
AND (YEAR(last_reset) < YEAR(CURDATE()) OR MONTH(last_reset) < MONTH(CURDATE()))"
)->execute([$userId]);
$stmt = $db->prepare('SELECT * FROM user_tool_credits WHERE user_id = ? LIMIT 1');
$stmt->execute([$userId]);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
return is_array($row) ? $row : null;
}
/**
* Check whether the user may proceed with a tool call.
* Handles monthly reset automatically.
*
* Returns ['ok' => true, 'balance' => int]
* or ['ok' => false, 'balance' => int, 'reason' => 'no_credits'|'rate_limit']
* Returns:
* ['ok' => true, 'balance' => int, 'bonus_balance' => int, 'tier' => string]
* ['ok' => false, 'balance' => int, 'bonus_balance' => int, 'tier' => string,
* 'reason' => 'no_credits'|'rate_limit']
*/
public static function check(int $userId, string $tool): array
{
$db = dbnmDb();
$cost = self::cost($tool);
$row = self::row($userId);
// Auto-reset balance if a new month has begun since last reset
$db->prepare(
'UPDATE user_tool_credits
SET balance = allowance, last_reset = CURDATE()
WHERE user_id = ?
AND (YEAR(last_reset) < YEAR(CURDATE()) OR MONTH(last_reset) < MONTH(CURDATE()))'
)->execute([$userId]);
$row = $db->prepare(
'SELECT balance FROM user_tool_credits WHERE user_id = ? LIMIT 1'
);
$row->execute([$userId]);
$credits = $row->fetch(PDO::FETCH_ASSOC);
if ($credits === false) {
// No credits row — treat as 0 balance (shouldn't happen after ensureFreeTierCredits)
return ['ok' => false, 'balance' => 0, 'reason' => 'no_credits'];
if ($row === null) {
return [
'ok' => false, 'balance' => 0, 'bonus_balance' => 0,
'tier' => 'free', 'reason' => 'no_credits',
];
}
$balance = (int)$credits['balance'];
$balance = (int)$row['balance'];
$bonus = (int)$row['bonus_balance'];
$tier = (string)$row['tier'];
// Free tools always pass
if ($cost === 0) {
return ['ok' => true, 'balance' => $balance];
}
$base = [
'balance' => $balance,
'bonus_balance' => $bonus,
'tier' => $tier,
];
if ($balance < $cost) {
return ['ok' => false, 'balance' => $balance, 'reason' => 'no_credits'];
}
// Hourly rate limit check (counts any tool that costs > 0)
$hourlyCount = $db->prepare(
// Hourly rate limit (always applies, even to pro_plus)
$stmt = $db->prepare(
'SELECT COUNT(*) FROM user_tool_usage_log
WHERE user_id = ? AND created_at > DATE_SUB(NOW(), INTERVAL 1 HOUR) AND credits_used > 0'
);
$hourlyCount->execute([$userId]);
if ((int)$hourlyCount->fetchColumn() >= self::HOURLY_LIMIT) {
return ['ok' => false, 'balance' => $balance, 'reason' => 'rate_limit'];
$stmt->execute([$userId]);
$hourly = (int)$stmt->fetchColumn();
if ($hourly >= self::hourlyCap($tier)) {
return $base + ['ok' => false, 'reason' => 'rate_limit'];
}
return ['ok' => true, 'balance' => $balance];
// Free tool (cost=0) always passes credit check
if ($cost === 0) {
return $base + ['ok' => true];
}
// pro_plus bypasses credit check
if ($tier === 'pro_plus') {
return $base + ['ok' => true];
}
if (($balance + $bonus) < $cost) {
return $base + ['ok' => false, 'reason' => 'no_credits'];
}
return $base + ['ok' => true];
}
/**
* Deduct credits for a completed tool call and log the usage.
* Safe to call even when cost is 0 (logs the call but deducts nothing).
* Spends from `balance` first, then `bonus_balance`.
* pro_plus tier logs the call but does not deduct.
*
* Returns the new effective balance (balance + bonus_balance).
*/
public static function deduct(int $userId, string $tool): int
{
$db = dbnmDb();
$cost = self::cost($tool);
$row = self::row($userId);
$tier = $row['tier'] ?? 'free';
if ($cost > 0 && $tier !== 'pro_plus' && $row !== null) {
$balance = (int)$row['balance'];
$bonus = (int)$row['bonus_balance'];
$fromBalance = min($cost, $balance);
$fromBonus = $cost - $fromBalance;
if ($cost > 0) {
$db->prepare(
'UPDATE user_tool_credits
SET balance = GREATEST(0, balance - ?)
SET balance = GREATEST(0, balance - ?),
bonus_balance = GREATEST(0, bonus_balance - ?)
WHERE user_id = ?'
)->execute([$cost, $userId]);
)->execute([$fromBalance, $fromBonus, $userId]);
}
$db->prepare(
'INSERT INTO user_tool_usage_log (user_id, tool, credits_used) VALUES (?, ?, ?)'
)->execute([$userId, $tool, $cost]);
$row = $db->prepare('SELECT balance FROM user_tool_credits WHERE user_id = ? LIMIT 1');
$row->execute([$userId]);
$r = $row->fetch(PDO::FETCH_ASSOC);
return $r ? (int)$r['balance'] : 0;
$latest = self::row($userId);
return $latest ? ((int)$latest['balance'] + (int)$latest['bonus_balance']) : 0;
}
/** Effective balance (monthly + bonus). */
public static function balance(int $userId): int
{
$row = self::row($userId);
return $row ? ((int)$row['balance'] + (int)$row['bonus_balance']) : 0;
}
/** Detailed balance breakdown for UI rendering. */
public static function balanceDetail(int $userId): array
{
$row = self::row($userId);
if (!$row) {
return ['balance' => 0, 'bonus_balance' => 0, 'tier' => 'free'];
}
return [
'balance' => (int)$row['balance'],
'bonus_balance' => (int)$row['bonus_balance'],
'tier' => (string)$row['tier'],
'storage_used_bytes' => (int)($row['storage_used_bytes'] ?? 0),
'storage_quota_bytes' => self::storageQuota((string)$row['tier']),
'survey_completed_at' => $row['survey_completed_at'] ?? null,
'subscription_period_end' => $row['subscription_period_end'] ?? null,
];
}
/**
* Current balance for a user (after any pending monthly reset).
* Award one-time bonus credits (survey reward, Stripe topup, manual grant).
* Source is logged via user_tool_usage_log with a negative credits_used value.
*/
public static function balance(int $userId): int
public static function awardBonus(int $userId, int $credits, string $source): int
{
$result = self::check($userId, 'corpus-search'); // cost=0, triggers reset if needed
return $result['balance'];
if ($credits <= 0) {
return self::balance($userId);
}
$db = dbnmDb();
self::ensureRow($userId);
$db->prepare('UPDATE user_tool_credits SET bonus_balance = bonus_balance + ? WHERE user_id = ?')
->execute([$credits, $userId]);
$db->prepare('INSERT INTO user_tool_usage_log (user_id, tool, credits_used) VALUES (?, ?, ?)')
->execute([$userId, 'bonus:' . substr($source, 0, 40), -$credits]);
return self::balance($userId);
}
/**
* Set or upgrade a user's tier (called by Stripe subscription webhook).
* Refills monthly balance to the new tier's allowance.
*/
public static function setTier(
int $userId,
string $tier,
?string $stripeCustomerId,
?string $subscriptionId,
?string $periodEndIso
): void {
$db = dbnmDb();
self::ensureRow($userId);
$allowance = self::monthlyAllowance($tier);
$db->prepare(
'UPDATE user_tool_credits
SET tier = ?, balance = ?, allowance = ?,
stripe_customer_id = COALESCE(?, stripe_customer_id),
subscription_id = ?,
subscription_period_end = ?,
last_reset = CURDATE()
WHERE user_id = ?'
)->execute([$tier, $allowance, $allowance, $stripeCustomerId, $subscriptionId, $periodEndIso, $userId]);
}
/**
* Refill monthly balance at subscription renewal (invoice.paid).
* Does not touch bonus_balance.
*/
public static function refillForRenewal(int $userId, string $tier, ?string $periodEndIso): void
{
$db = dbnmDb();
$allowance = self::monthlyAllowance($tier);
$db->prepare(
'UPDATE user_tool_credits
SET balance = ?,
subscription_period_end = ?,
last_reset = CURDATE()
WHERE user_id = ?'
)->execute([$allowance, $periodEndIso, $userId]);
}
/**
* Revert a user to free tier (subscription canceled or fully ended).
* Preserves bonus_balance and case_documents (handled by 90-day cron).
*/
public static function clearTier(int $userId): void
{
$db = dbnmDb();
$db->prepare(
'UPDATE user_tool_credits
SET tier = ?, allowance = ?, subscription_id = NULL, subscription_period_end = NULL
WHERE user_id = ?'
)->execute(['free', self::monthlyAllowance('free'), $userId]);
}
/** Mark survey as completed so the bonus can only be claimed once per account. */
public static function markSurveyCompleted(int $userId): void
{
$db = dbnmDb();
self::ensureRow($userId);
$db->prepare('UPDATE user_tool_credits SET survey_completed_at = NOW() WHERE user_id = ?')
->execute([$userId]);
}
public static function hasCompletedSurvey(int $userId): bool
{
$row = self::row($userId);
return !empty($row['survey_completed_at']);
}
/** Create the user_tool_credits row if missing (idempotent). */
public static function ensureRow(int $userId): void
{
$db = dbnmDb();
$db->prepare(
'INSERT IGNORE INTO user_tool_credits (user_id, balance, allowance, tier, last_reset, created_at)
VALUES (?, ?, ?, ?, CURDATE(), NOW())'
)->execute([$userId, self::monthlyAllowance('free'), self::monthlyAllowance('free'), 'free']);
}
}