396 lines
14 KiB
PHP
396 lines
14 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
* Credit + tier system for users of tools.dobetternorge.no.
|
|
*
|
|
* Three tiers: free, plus, pro (NOK pricing, see pricing.php).
|
|
*
|
|
* Tables:
|
|
* user_tool_credits — balance (monthly, resets), bonus_balance (never expires), tier,
|
|
* Stripe links, trial_started_at / trial_expires_at / trial_downgraded_at
|
|
* 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 tier has the largest monthly allowance (still subject to hourly cap).
|
|
*
|
|
* Trial is Stripe-driven: when subscription.status='trialing', tier='plus' and
|
|
* trial_expires_at mirrors Stripe's trial_end. No homegrown trial cron — the
|
|
* Stripe webhook flips tier='free' on subscription.deleted.
|
|
*
|
|
* CaveauAI client sessions bypass all credit checks.
|
|
* Only SSO sessions are subject to limits.
|
|
*/
|
|
final class FreeTier
|
|
{
|
|
private const COSTS = [
|
|
'corpus-search' => 0,
|
|
'search' => 0,
|
|
'ask' => 1,
|
|
'extract' => 1,
|
|
'timeline' => 2,
|
|
'redact' => 2,
|
|
'barnevernet' => 3,
|
|
'advocate' => 3,
|
|
'deep-research' => 5,
|
|
'transcribe' => 2,
|
|
'discrepancy' => 4,
|
|
'korrespond' => 3,
|
|
];
|
|
|
|
/** Monthly credit allowance per tier. */
|
|
private const MONTHLY_ALLOWANCE = [
|
|
'free' => 30,
|
|
'plus' => 250,
|
|
'pro' => 1000,
|
|
];
|
|
|
|
/** Hourly rate-limit per tier (number of paid tool calls per rolling hour). */
|
|
private const HOURLY_CAP = [
|
|
'free' => 10,
|
|
'plus' => 20,
|
|
'pro' => 40,
|
|
];
|
|
|
|
/** Per-user case-storage quota in bytes. */
|
|
private const STORAGE_QUOTA = [
|
|
'free' => 0,
|
|
'plus' => 524288000, // 500 MB
|
|
'pro' => 5368709120, // 5 GB
|
|
];
|
|
|
|
/** Credit cost for a given tool slug. Returns 1 for unknown tools. */
|
|
public static function cost(string $tool): int
|
|
{
|
|
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';
|
|
}
|
|
|
|
/** Tiers that have access to "My Case" features (upload, save analyses, use case context). */
|
|
public static function isPaidTier(string $tier): bool
|
|
{
|
|
return in_array($tier, ['plus', 'pro'], true);
|
|
}
|
|
|
|
/** True when the user is currently in a Stripe trial (tier='plus', trial_expires_at in future). */
|
|
public static function isTrialActive(int $userId): bool
|
|
{
|
|
$row = self::row($userId);
|
|
if (!$row || ($row['tier'] ?? '') !== 'plus') {
|
|
return false;
|
|
}
|
|
$expires = $row['trial_expires_at'] ?? null;
|
|
if (!$expires) {
|
|
return false;
|
|
}
|
|
return strtotime((string)$expires) > time();
|
|
}
|
|
|
|
/** Days remaining in the active trial (0 if no trial / expired). */
|
|
public static function trialDaysRemaining(int $userId): int
|
|
{
|
|
if (!self::isTrialActive($userId)) {
|
|
return 0;
|
|
}
|
|
$row = self::row($userId);
|
|
$expires = strtotime((string)$row['trial_expires_at']);
|
|
return max(0, (int)ceil(($expires - time()) / 86400));
|
|
}
|
|
|
|
/** 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 'plus' THEN " . self::MONTHLY_ALLOWANCE['plus'] . "
|
|
WHEN 'pro' THEN " . self::MONTHLY_ALLOWANCE['pro'] . "
|
|
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.
|
|
*
|
|
* 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);
|
|
|
|
if ($row === null) {
|
|
return [
|
|
'ok' => false, 'balance' => 0, 'bonus_balance' => 0,
|
|
'tier' => 'free', 'reason' => 'no_credits',
|
|
];
|
|
}
|
|
|
|
$balance = (int)$row['balance'];
|
|
$bonus = (int)$row['bonus_balance'];
|
|
$tier = (string)$row['tier'];
|
|
|
|
$base = [
|
|
'balance' => $balance,
|
|
'bonus_balance' => $bonus,
|
|
'tier' => $tier,
|
|
];
|
|
|
|
// Hourly rate limit (always applies)
|
|
$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'
|
|
);
|
|
$stmt->execute([$userId]);
|
|
$hourly = (int)$stmt->fetchColumn();
|
|
if ($hourly >= self::hourlyCap($tier)) {
|
|
return $base + ['ok' => false, 'reason' => 'rate_limit'];
|
|
}
|
|
|
|
// Free tool (cost=0) always passes credit check
|
|
if ($cost === 0) {
|
|
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.
|
|
* Spends from `balance` first, then `bonus_balance`.
|
|
*
|
|
* 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);
|
|
|
|
if ($cost > 0 && $row !== null) {
|
|
$balance = (int)$row['balance'];
|
|
$bonus = (int)$row['bonus_balance'];
|
|
|
|
$fromBalance = min($cost, $balance);
|
|
$fromBonus = $cost - $fromBalance;
|
|
|
|
$db->prepare(
|
|
'UPDATE user_tool_credits
|
|
SET balance = GREATEST(0, balance - ?),
|
|
bonus_balance = GREATEST(0, bonus_balance - ?)
|
|
WHERE user_id = ?'
|
|
)->execute([$fromBalance, $fromBonus, $userId]);
|
|
}
|
|
|
|
$db->prepare(
|
|
'INSERT INTO user_tool_usage_log (user_id, tool, credits_used) VALUES (?, ?, ?)'
|
|
)->execute([$userId, $tool, $cost]);
|
|
|
|
$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,
|
|
'trial_started_at' => $row['trial_started_at'] ?? null,
|
|
'trial_expires_at' => $row['trial_expires_at'] ?? null,
|
|
'trial_active' => self::isTrialActive($userId),
|
|
'trial_days_remaining' => self::trialDaysRemaining($userId),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 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 awardBonus(int $userId, int $credits, string $source): int
|
|
{
|
|
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.
|
|
*
|
|
* When $trialEndIso is non-null, also writes trial_started_at (preserving original on updates)
|
|
* and trial_expires_at — used when subscription.status='trialing'.
|
|
*/
|
|
public static function setTier(
|
|
int $userId,
|
|
string $tier,
|
|
?string $stripeCustomerId,
|
|
?string $subscriptionId,
|
|
?string $periodEndIso,
|
|
?string $trialEndIso = null
|
|
): void {
|
|
$db = dbnmDb();
|
|
self::ensureRow($userId);
|
|
$allowance = self::monthlyAllowance($tier);
|
|
|
|
if ($trialEndIso !== null) {
|
|
$db->prepare(
|
|
'UPDATE user_tool_credits
|
|
SET tier = ?, balance = ?, allowance = ?,
|
|
stripe_customer_id = COALESCE(?, stripe_customer_id),
|
|
subscription_id = ?,
|
|
subscription_period_end = ?,
|
|
trial_started_at = COALESCE(trial_started_at, NOW()),
|
|
trial_expires_at = ?,
|
|
trial_downgraded_at = NULL,
|
|
last_reset = CURDATE()
|
|
WHERE user_id = ?'
|
|
)->execute([$tier, $allowance, $allowance, $stripeCustomerId, $subscriptionId, $periodEndIso, $trialEndIso, $userId]);
|
|
} else {
|
|
$db->prepare(
|
|
'UPDATE user_tool_credits
|
|
SET tier = ?, balance = ?, allowance = ?,
|
|
stripe_customer_id = COALESCE(?, stripe_customer_id),
|
|
subscription_id = ?,
|
|
subscription_period_end = ?,
|
|
trial_expires_at = NULL,
|
|
trial_downgraded_at = NULL,
|
|
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, trial ended without conversion).
|
|
* Preserves bonus_balance and case_documents (handled by 60-day cron).
|
|
* Stamps trial_downgraded_at if a trial was active.
|
|
*/
|
|
public static function clearTier(int $userId): void
|
|
{
|
|
$db = dbnmDb();
|
|
$db->prepare(
|
|
"UPDATE user_tool_credits
|
|
SET tier = 'free',
|
|
allowance = ?,
|
|
balance = ?,
|
|
subscription_id = NULL,
|
|
subscription_period_end = NULL,
|
|
trial_downgraded_at = CASE
|
|
WHEN trial_expires_at IS NOT NULL AND trial_downgraded_at IS NULL
|
|
THEN NOW()
|
|
ELSE trial_downgraded_at
|
|
END,
|
|
trial_expires_at = NULL
|
|
WHERE user_id = ?"
|
|
)->execute([self::monthlyAllowance('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']);
|
|
}
|
|
}
|