Files
dobetternorge-tools/api/stripe-portal.php
T
daveadmin ba9cddf9a1 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>
2026-05-20 20:52:54 +02:00

37 lines
1.3 KiB
PHP

<?php
declare(strict_types=1);
require_once __DIR__ . '/../includes/bootstrap.php';
require_once __DIR__ . '/../includes/StripeClient.php';
require_once __DIR__ . '/../includes/FreeTier.php';
dbnToolsRequireMethod('POST');
dbnToolsRequireAuth();
$user = dbnToolsAuthenticatedUser();
$userId = (int)($user['user_id'] ?? 0);
if ($userId <= 0) {
dbnToolsError('User session missing user_id.', 401, 'bad_session');
}
$row = FreeTier::row($userId);
$customerId = (string)($row['stripe_customer_id'] ?? '');
if ($customerId === '') {
// No Stripe customer yet — happens if user never checked out.
dbnToolsError('No Stripe customer on record. Subscribe first.', 400, 'no_customer');
}
try {
$stripe = new StripeClient();
$baseUrl = (dbnToolsIsHttps() ? 'https://' : 'http://') . ($_SERVER['HTTP_HOST'] ?? 'tools.dobetternorge.no');
$session = $stripe->createPortalSession($customerId, $baseUrl . '/billing.php');
$url = (string)($session['url'] ?? '');
if ($url === '') {
dbnToolsError('Stripe did not return a portal URL.', 502, 'stripe_no_url');
}
dbnToolsRespond(['ok' => true, 'url' => $url]);
} catch (Throwable $e) {
error_log('[stripe-portal] ' . $e->getMessage());
dbnToolsError('Could not open billing portal: ' . $e->getMessage(), 500, 'stripe_failed');
}