3a2c57a167
* feat(billing): paywall conversion pass: deferred first charge, trial touchpoint, sell-view upgrade - checkout passes subscription_data.trial_end (trial grant expiry, 49h floor) so a mid-trial upgrade costs 0 kr today instead of double-billing days the company already has free; billing/status counts 'trialing' as paying - trial countdown pill in the sidebar (CompanyContext.trialEndsAt via getCompanyEntitlements); hidden for sandbox, dev bypass, and once any non-trial grant is active - sell view: what-happens-when timeline, free-vs-paid comparison table, risk-reversal copy + chevron CTA, post-checkout confirmation state Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(billing): review triage: fail-closed trial lookup, hourly countdown refresh, BFL retention note - checkout returns 500 (no Stripe session) when the trial-grant lookup errors, instead of silently charging immediately after the UI promised 0 kr idag - sidebar trial countdown recomputes hourly so a long-lived tab stays honest - sell-view retention copy states BFL 7-year retention explicitly (compliance-bot suggestion) Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * chore: retrigger CI (pull_request event delivery stuck) --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
60 lines
2.3 KiB
TypeScript
60 lines
2.3 KiB
TypeScript
import { NextResponse } from 'next/server'
|
|
import { requireAuth } from '@/lib/auth/require-auth'
|
|
import { requireCompanyId } from '@/lib/company/context'
|
|
import { isStripeConfigured } from '@/lib/stripe/client'
|
|
import { isSandboxCompany } from '@/lib/sandbox/guard'
|
|
|
|
/**
|
|
* Billing status for the client-rendered billing section (which lives inside the
|
|
* settings modal Dialog and can't read the DB server-side). Returns whether the
|
|
* company is paying, whether Stripe checkout is configured, and the trial expiry
|
|
* (for the days-left urgency banner). Read-only.
|
|
*/
|
|
export async function GET() {
|
|
const { user, supabase, error } = await requireAuth()
|
|
if (error) return error
|
|
|
|
let companyId: string | null = null
|
|
try {
|
|
companyId = await requireCompanyId(supabase, user.id)
|
|
} catch {
|
|
companyId = null
|
|
}
|
|
|
|
// Demo accounts (anonymous user or sandbox company) can't check out, so the
|
|
// client hides the upgrade CTA rather than showing a button that only errors.
|
|
let isDemo = user.is_anonymous === true
|
|
if (companyId && !isDemo) {
|
|
isDemo = await isSandboxCompany(supabase, companyId)
|
|
}
|
|
|
|
let isPaying = false
|
|
let trialEndsAt: string | null = null
|
|
if (companyId) {
|
|
const { data: sub } = await supabase
|
|
.from('company_subscriptions')
|
|
.select('status')
|
|
.eq('company_id', companyId)
|
|
.maybeSingle()
|
|
const status = (sub as { status: string | null } | null)?.status ?? null
|
|
// Paying = a real subscription. Includes 'trialing': checkout defers the
|
|
// first charge to the product-trial end, so a Stripe-trialing subscription
|
|
// means the card is already committed and the user should see the manage
|
|
// view, not the upgrade pitch. Companies without a subscription (product
|
|
// trial only, no card) stay on the upgrade path.
|
|
isPaying = status === 'active' || status === 'past_due' || status === 'trialing'
|
|
|
|
const { data: trial } = await supabase
|
|
.from('capability_grants')
|
|
.select('expires_at')
|
|
.eq('company_id', companyId)
|
|
.eq('source', 'trial')
|
|
.order('expires_at', { ascending: false })
|
|
.limit(1)
|
|
.maybeSingle()
|
|
trialEndsAt = (trial as { expires_at: string | null } | null)?.expires_at ?? null
|
|
}
|
|
|
|
return NextResponse.json({ isPaying, configured: isStripeConfigured(), trialEndsAt, isDemo })
|
|
}
|