Add NOK pricing catalog, credit ledger, success-based charging, and tier-gated model routing
- PricingCatalog.php: single source of truth for plans (free/plus/pro), top-ups, Stripe price env keys, tool costs (0–6 credits), STT variable billing, feature limits - FreeTier.php: monthly-first credit deduction, ledger (user_tool_credit_ledger), STT reservation/settle/release, monthly reset, trial logic - StripeClient.php: canonical SKUs (plus/pro/topup_100/300/1000), legacy aliases kept - stripe-checkout.php: subscription vs payment mode, trial gating, catalog metadata - stripe-webhook.php: idempotent via stripe_events, handles subscription lifecycle + invoice.paid renewal + one-time topup credit grants - All API tools: success-based credit deduction (check before, charge after) - transcribe.php: file-size heuristic reservation, settle from actual provider duration - ask.php + LegalTools.php: ToolModels engine resolution — Pro gets gpt-4o - KorrespondAgent.php + korrespond.php: tier-gated draft deployment — Free/Plus gets gpt-4o-mini, Pro gets gpt-4o - pricing.php: NOK-only, plan cards, top-up packs, Organisation contact card, tool cost table, separate monthly/prepaid balance display - 003_pricing_credit_catalog.sql: ledger and STT reservation tables Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
+44
-4
@@ -2,12 +2,14 @@
|
||||
declare(strict_types=1);
|
||||
|
||||
require_once __DIR__ . '/../includes/LegalTools.php';
|
||||
require_once __DIR__ . '/../includes/FreeTier.php';
|
||||
|
||||
dbnToolsRequireMethod('POST');
|
||||
dbnToolsRequireAuth();
|
||||
$ftUid = dbnToolsFreeTierCheck('transcribe');
|
||||
$ftRemaining = dbnToolsFreeTierDeduct($ftUid, 'transcribe');
|
||||
if ($ftRemaining >= 0) { header('X-Credits-Remaining: ' . $ftRemaining); }
|
||||
$ftUid = 0;
|
||||
$ftRemaining = -1;
|
||||
$sttReservationId = 0;
|
||||
$sttSettled = false;
|
||||
|
||||
set_time_limit(0);
|
||||
ignore_user_abort(true);
|
||||
@@ -92,6 +94,23 @@ $detectedMime = mime_content_type($file['tmp_name']) ?: 'application/octet-strea
|
||||
$timeOffset = max(0.0, (float)($_POST['time_offset'] ?? 0));
|
||||
$t0 = microtime(true);
|
||||
|
||||
$estimatedCredits = FreeTier::estimateTranscribeCreditsFromFile((string)($file['name'] ?? ''), (int)($file['size'] ?? 0));
|
||||
$ftUid = dbnToolsFreeTierCheckAmount('transcribe', $estimatedCredits);
|
||||
if ($ftUid > 0) {
|
||||
$sttReservationId = FreeTier::createReservation($ftUid, 'transcribe', $estimatedCredits, [
|
||||
'filename' => (string)($file['name'] ?? ''),
|
||||
'bytes' => (int)($file['size'] ?? 0),
|
||||
'mime' => $detectedMime,
|
||||
'language' => $language,
|
||||
'diarize' => $diarize,
|
||||
]);
|
||||
register_shutdown_function(static function () use (&$sttSettled, $sttReservationId): void {
|
||||
if (!$sttSettled && $sttReservationId > 0) {
|
||||
FreeTier::releaseReservation($sttReservationId, ['reason' => 'request_ended_before_settlement']);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// ── Auto-cascade: Azure → GCP → Whisper GPU ───────────────────────────────────
|
||||
|
||||
$result = null;
|
||||
@@ -186,6 +205,25 @@ $engineLabel = match($engineUsed) {
|
||||
|
||||
// ── Log + respond ─────────────────────────────────────────────────────────────
|
||||
|
||||
$durationSec = round((float)($result['duration_seconds'] ?? $result['duration'] ?? 0), 2);
|
||||
$creditsUsed = FreeTier::transcribeCreditsForSeconds($durationSec);
|
||||
$ftRemaining = dbnToolsFreeTierDeductAmount($ftUid, 'transcribe', $creditsUsed, [
|
||||
'engine' => $engineUsed,
|
||||
'duration_sec' => $durationSec,
|
||||
'estimated_credits' => $estimatedCredits,
|
||||
'reservation_id' => $sttReservationId,
|
||||
]);
|
||||
if ($ftRemaining >= 0) {
|
||||
header('X-Credits-Remaining: ' . $ftRemaining);
|
||||
}
|
||||
if ($sttReservationId > 0) {
|
||||
FreeTier::settleReservation($sttReservationId, $creditsUsed, $engineUsed, $durationSec, [
|
||||
'estimated_credits' => $estimatedCredits,
|
||||
'balance_after' => $ftRemaining,
|
||||
]);
|
||||
$sttSettled = true;
|
||||
}
|
||||
|
||||
dbnToolsLogMetadata([
|
||||
'tool' => 'transcribe',
|
||||
'engine' => $engineUsed,
|
||||
@@ -203,12 +241,14 @@ dbnToolsRespond([
|
||||
'speaker_roles' => $speakerRoles,
|
||||
'num_speakers' => $numDetected,
|
||||
'language' => (string)($result['language'] ?? $language),
|
||||
'duration_sec' => round((float)($result['duration_seconds'] ?? $result['duration'] ?? 0), 2),
|
||||
'duration_sec' => $durationSec,
|
||||
'processing_sec'=> round((float)($result['processing_seconds'] ?? 0), 2),
|
||||
'model' => $engineLabel,
|
||||
'engine' => $engineUsed,
|
||||
'latency_ms' => $latencyMs,
|
||||
'cleaned_by' => $cleanedBy,
|
||||
'credits_used' => $creditsUsed,
|
||||
'balance' => $ftRemaining,
|
||||
]);
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user