Files
accounted/extensions/general/stripe/index.ts
T
Mattsson d54b43f80f Bug/resend and invoices (#1192)
* fix(invoices): anchor the PDF logo to the top-left of its header cell

The logo box is always the full 240x80pt reserved area (any larger logo is
clamped to exactly that), so objectFit: 'contain' placed the image inside it
with the default 50% 50% centering. A wide banner logo fills the width and
lands on the left margin, but a near-square logo scaled down to the 80pt
height cap is only ~117pt wide and got pushed ~60pt in from the margin, which
reads as a misaligned logo and forced companies to reshape their artwork.

Anchor the image top-left so every aspect ratio starts at the margin.

Covered by a test that renders the real PDF and reads the image placement
matrix out of the content stream, for both a wide and a near-square logo.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* feat(invoices): show the real delivery outcome in the send history

"Skickad" only meant the email provider accepted the message, so a bounced
invoice looked identical to one that arrived. Resend reports the outcome
asynchronously; that report now lands on the delivery row and drives the
history: green is reserved for a confirmed delivery, bounce/blocked reads
red, delayed and spam-marked read amber, and an accepted-but-unconfirmed
send is neutral instead of falsely green.

The report arrives on a signed webhook and may only touch the three new
provider status columns of an already sent, unredacted row: the WORM trigger
proves nothing else changed, and a lower ranked or older report can never
downgrade an observed failure. The provider reason text can quote the failing
address, so it is masked on read and cleared by the daily PII redaction job.

Timestamps also formatted in Europe/Stockholm instead of falling back to the
runtime zone, which rendered a 14:05 send as 12:05 on Vercel.

Delivery reports are per message, never per recipient: Resend sends one event
for the whole message, so splitting a send per recipient would be the only way
to get finer granularity, at the cost of CC.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* feat(stripe): make the integration feed-only

Stripe sync now only imports balance transactions into the transactions
inbox, like any bank feed; nothing auto-books. The event/settlement sync
(lib/sync.ts, lib/payouts.ts) stays in the repo but is no longer wired to
any route or cron: the 15-min sync cron is removed from vercel.json.
Payment links on invoice send are unchanged; their payments arrive as
feed rows and are matched manually.

- /sync runs only syncStripeBalanceTransactions; response is { success,
  transactions }
- connecting via OAuth enables the nightly feed by default (toggle stays
  as opt-out)
- panel: needs-review section and plumbing removed, copy rewritten to
  transactions-first (sv + en), toast reports fetched/imported/linked
  and calls out an empty result instead of silent all-zeros

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(api): return the article currency from the v1 article list

The dashboard, importer, export and MCP article surfaces all learned to
carry a non-SEK article price (#1166, #1183, #1184), but the v1
projection still omitted currency. An API or agent caller therefore read
price_excl_vat with nothing marking it as EUR and would copy the number
straight onto a SEK invoice line, at a nine-to-one error.

Adds currency to the projection, the response shape and the example, plus
a pitfall stating the price is not always SEK and that this endpoint does
no FX conversion.

Additive field only; no migration (articles.currency already exists).

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* feat(settings): replace the settings modal with a routed panel sheet

Settings now renders as a sheet that fills the main panel, sliding up over the
page the user came from and back down on close, with the sidebar and frame left
visible and usable. Behind it sits one shared master-detail surface: underline
search across every section and subsection, the grouped section rail, and the
active section as a direct-editing accordion. All 11 sections are decomposed
into subsections, and the legacy *SettingsContent components compose the same
pieces so the stacked and accordion layouts cannot drift.

The sheet is the only presentation, on every entry path. The intercepting route
handles in-app navigation and closes by popping the history entry, landing back
on the page underneath. @settingsModal/default.tsx handles cold loads (refresh,
deep link, new tab), where interception never fires; nothing is mounted
underneath there, so it closes to the dashboard. Both branch on one shared
predicate, isSheetSection, together with the settings layout, which must render
nothing for those sections or the surface would stack twice behind the sheet
and run every section's fetches twice.

Closing is deliberate rather than incidental: the X, Esc, or navigating away.
The dialog is non-modal so the sidebar's account popover and company switcher
keep working with settings up, and an outside click no longer dismisses it.
Sections land fully collapsed, and the scroll position of the page behind
survives opening and closing the sheet.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* feat: enhance article management and settings UI

- Add PATCH test for toggling article active state without other fields.
- Remove unused MessageCircle icon from DashboardContent.
- Refactor AccountingFrameworkForm to use SettingsFieldRow for better help text display.
- Update CompanyInfoForm, DimensionsToggle, and various settings forms to replace description with help text.
- Remove redundant headings and intros in several settings components to streamline UI.
- Improve help text for various settings in English and Swedish translations.
- Update structured error messages for better clarity on article deletion.

* refactor(ArticleDetailPage): remove unused imports and duplicate state variable

* fix(settings): own deep-linked settings routes by route list, not nav visibility

Review fixes from the settings panel sheet work:
* isSheetSection reads the full settings route list so a hidden-but-deep-linked
  section (assistant before BankID, banking in sandbox, api without MCP) is
  claimed by the sheet instead of rendering the legacy shell around an empty panel
* keep 503 on the Resend delivery webhook when the signing secret is unset, with
  a test pinning the behaviour
* stripe callback route test coverage

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* refactor: update salary, tax, and templates settings components

- Refactored SalarySettingsContent to use a form wrapper and improved payment settings UI.
- Enhanced TaxSettingsContent with new signals for EU sales, KU obligations, and ROT/RUT deductions.
- Updated TemplatesSettingsContent to remove legacy comments and improve readability.
- Simplified navigation items by removing unnecessary constants and directly using hrefs.
- Cleaned up translation files by removing deprecated keys and adding new descriptions for clarity.

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-07-25 22:56:17 +02:00

444 lines
16 KiB
TypeScript

import type { Extension, ExtensionContext } from '@/lib/extensions/types'
import { NextResponse } from 'next/server'
import { checkRateLimit } from '@/lib/auth/rate-limit-http'
import { requireCapability } from '@/lib/entitlements/has-capability'
import { CAPABILITY } from '@/lib/entitlements/keys'
import { guardSandbox, sandboxBlockedResponse } from '@/lib/sandbox/guard'
import {
buildAuthorizeUrl,
deauthorizeAccount,
isStripeConnectConfigured,
isLiveMode,
} from './lib/connect'
import {
createInvoicePaymentLink,
handleCreditNoteCreated,
handleInvoicePaid,
} from './lib/payment-links'
import { syncStripeBalanceTransactions } from './lib/transaction-sync'
import { createServiceClientNoCookies } from '@/lib/auth/api-keys'
import type { StripeConnection, StripeStatusResponse } from './types'
// Per-user limits: connect/disconnect are outward-facing OAuth operations,
// sync hits the Stripe API.
const RATE_LIMIT_CONNECT = { maxRequests: 10, windowMs: 60_000 }
const RATE_LIMIT_DISCONNECT = { maxRequests: 10, windowMs: 60_000 }
const RATE_LIMIT_SYNC = { maxRequests: 10, windowMs: 60_000 }
// A pending row younger than this blocks a second connect attempt so a
// double-click cannot start two OAuth round-trips (only one state would
// survive, stranding the other at the callback).
const PENDING_FRESH_MS = 60_000
const NOT_CONFIGURED_MESSAGE =
'Stripe-integrationen är inte konfigurerad på den här installationen.'
/**
* Stripe Connect extension
*
* Connects a company's Stripe account via Connect OAuth (Standard accounts).
* Auto-creates a Stripe Payment Link when an invoice is sent, and imports the
* account's balance transactions into the transactions inbox as a bank feed
* for the Stripe balance (1686). Feed-only by decision 2026-07-24: nothing is
* auto-booked; the event/settlement sync (lib/sync.ts, lib/payouts.ts) is
* retained in the repo but not wired to any cron or route.
*
* Required environment variables:
* - STRIPE_SECRET_KEY (the platform account key, shared with billing)
* - STRIPE_CONNECT_CLIENT_ID (ca_... from the platform's Connect settings)
*/
export const stripeExtension: Extension = {
id: 'stripe',
name: 'Stripe-betalningar',
version: '1.0.0',
settingsPanel: {
label: 'Betalningar (Stripe)',
path: '/import?mode=stripe',
},
// Core-callable services, resolved via the extension registry (core never
// imports extension code). The send routes use this to auto-fill
// invoices.payment_link_url before the email/PDF render.
services: {
createInvoicePaymentLink,
},
eventHandlers: [
// A settled or credited invoice must stop accepting money through its link.
{ eventType: 'invoice.paid', handler: handleInvoicePaid },
{ eventType: 'credit_note.created', handler: handleCreditNoteCreated },
],
apiRoutes: [
{
method: 'GET',
path: '/status',
handler: async (_request: Request, ctx?: ExtensionContext) => {
const supabase = ctx?.supabase ?? await (await import('@/lib/supabase/server')).createClient()
const { data: { user } } = await supabase.auth.getUser()
if (!user) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
}
if (!ctx?.companyId) {
return NextResponse.json({ error: 'Company context required' }, { status: 400 })
}
// Prefer the active connection; otherwise surface the most recent row
// so the panel can show pending/error/revoked states.
const { data: rows } = await supabase
.from('stripe_connections')
.select(
'id, status, stripe_account_id, livemode, display_name, error_message, connected_at, last_event_created_at, transaction_sync_enabled, last_balance_txn_synced_at',
)
.eq('company_id', ctx.companyId)
.order('created_at', { ascending: false })
.limit(10)
const connection =
rows?.find((r) => r.status === 'active') ?? rows?.[0] ?? null
const payload: StripeStatusResponse = {
configured: isStripeConnectConfigured(),
connection,
}
return NextResponse.json(payload)
},
},
{
method: 'POST',
path: '/sync',
handler: async (_request: Request, ctx?: ExtensionContext) => {
const log = ctx?.log ?? console
const supabase = ctx?.supabase ?? await (await import('@/lib/supabase/server')).createClient()
const { data: { user } } = await supabase.auth.getUser()
if (!user) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
}
if (!ctx?.companyId) {
return NextResponse.json({ error: 'Company context required' }, { status: 400 })
}
const companyId = ctx.companyId
const capabilityBlocked = await requireCapability(
supabase,
companyId,
CAPABILITY.stripe_payments,
)
if (capabilityBlocked) return capabilityBlocked
const rl = await checkRateLimit({
prefix: 'stripe:sync',
identifier: user.id,
...RATE_LIMIT_SYNC,
})
if (!rl.ok) return rl.response!
// Membership-scoped lookup via the user client; the sync itself runs
// on the service client because the event ledger is service-write-only.
const { data: connection } = await supabase
.from('stripe_connections')
.select('*')
.eq('company_id', companyId)
.eq('status', 'active')
.maybeSingle()
if (!connection) {
return NextResponse.json({ error: 'Inget anslutet Stripe-konto.' }, { status: 404 })
}
try {
const serviceClient = createServiceClientNoCookies()
const typedConnection = connection as StripeConnection
// Feed-only (2026-07-24): Stripe sync imports balance transactions
// into the inbox, nothing more. The event/settlement sync
// (syncStripeConnection in lib/sync.ts) stays in the repo but is
// deliberately not called: booking is a user decision in the inbox,
// like any bank feed. The manual button ignores
// transaction_sync_enabled (that flag gates the nightly cron):
// pressing it IS the opt-in.
const transactions = await syncStripeBalanceTransactions(serviceClient, typedConnection)
return NextResponse.json({ success: true, transactions })
} catch (error) {
log.error('[stripe] Manual sync failed', {
message: error instanceof Error ? error.message : String(error),
connection_id: connection.id,
})
return NextResponse.json(
{ error: 'Synkroniseringen misslyckades. Försök igen.' },
{ status: 502 },
)
}
},
},
{
method: 'POST',
path: '/transaction-sync',
handler: async (request: Request, ctx?: ExtensionContext) => {
const supabase = ctx?.supabase ?? await (await import('@/lib/supabase/server')).createClient()
const { data: { user } } = await supabase.auth.getUser()
if (!user) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
}
if (!ctx?.companyId) {
return NextResponse.json({ error: 'Company context required' }, { status: 400 })
}
const companyId = ctx.companyId
const capabilityBlocked = await requireCapability(
supabase,
companyId,
CAPABILITY.stripe_payments,
)
if (capabilityBlocked) return capabilityBlocked
const rl = await checkRateLimit({
prefix: 'stripe:transaction-sync-toggle',
identifier: user.id,
...RATE_LIMIT_SYNC,
})
if (!rl.ok) return rl.response!
const body = (await request.json().catch(() => ({}))) as { enabled?: unknown }
if (typeof body.enabled !== 'boolean') {
return NextResponse.json(
{ error: 'enabled (boolean) krävs.' },
{ status: 400 },
)
}
const { data: updated, error: updateError } = await supabase
.from('stripe_connections')
.update({ transaction_sync_enabled: body.enabled })
.eq('company_id', companyId)
.eq('status', 'active')
.select('id')
if (updateError) {
return NextResponse.json(
{ error: 'Kunde inte spara inställningen. Försök igen.' },
{ status: 500 },
)
}
if (!updated || updated.length === 0) {
return NextResponse.json({ error: 'Inget anslutet Stripe-konto.' }, { status: 404 })
}
return NextResponse.json({ success: true, enabled: body.enabled })
},
},
{
method: 'POST',
path: '/connect',
handler: async (_request: Request, ctx?: ExtensionContext) => {
const log = ctx?.log ?? console
const supabase = ctx?.supabase ?? await (await import('@/lib/supabase/server')).createClient()
const { data: { user } } = await supabase.auth.getUser()
if (!user) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
}
if (!ctx?.companyId) {
return NextResponse.json({ error: 'Company context required' }, { status: 400 })
}
const companyId = ctx.companyId
// Anonymous/sandbox users must never reach Stripe (same doctrine as
// billing checkout: the sandbox never talks to external services).
// The anon check is identity truth and runs first (no DB round trip).
if (user.is_anonymous) return sandboxBlockedResponse()
const sandboxBlocked = await guardSandbox(supabase, companyId)
if (sandboxBlocked) return sandboxBlocked
const capabilityBlocked = await requireCapability(
supabase,
companyId,
CAPABILITY.stripe_payments,
)
if (capabilityBlocked) return capabilityBlocked
const rl = await checkRateLimit({
prefix: 'stripe:connect',
identifier: user.id,
...RATE_LIMIT_CONNECT,
})
if (!rl.ok) return rl.response!
if (!isStripeConnectConfigured()) {
return NextResponse.json({ error: NOT_CONFIGURED_MESSAGE }, { status: 503 })
}
const { data: existing } = await supabase
.from('stripe_connections')
.select('id, status, created_at')
.eq('company_id', companyId)
.in('status', ['active', 'pending'])
.order('created_at', { ascending: false })
if (existing?.some((c) => c.status === 'active')) {
return NextResponse.json(
{ error: 'Företaget har redan ett anslutet Stripe-konto. Koppla från det först.' },
{ status: 409 },
)
}
const pending = existing?.filter((c) => c.status === 'pending') ?? []
const freshPending = pending.find(
(c) => Date.now() - new Date(c.created_at).getTime() < PENDING_FRESH_MS,
)
if (freshPending) {
return NextResponse.json(
{ error: 'En anslutning pågår redan. Vänta och försök igen.' },
{ status: 409 },
)
}
if (pending.length > 0) {
// Supersede stale pending attempts so their oauth_state can never
// complete a callback after this new round-trip starts.
await supabase
.from('stripe_connections')
.update({
status: 'error',
error_message: 'Superseded by new connection attempt',
oauth_state: null,
})
.eq('company_id', companyId)
.eq('status', 'pending')
}
// Persist the CSRF state BEFORE handing the user to Stripe: the
// callback locates the row by oauth_state alone, so the row must
// exist before Stripe can ever redirect back with that state.
const oauthState = crypto.randomUUID()
const { data: created, error: insertError } = await supabase
.from('stripe_connections')
.insert({
company_id: companyId,
user_id: user.id,
status: 'pending',
oauth_state: oauthState,
livemode: isLiveMode(),
})
.select('id')
.single()
if (insertError || !created) {
log.error('[stripe] Failed to stage pending connection', {
message: insertError?.message,
code: insertError?.code,
companyId,
})
return NextResponse.json(
{ error: 'Kunde inte starta anslutningen. Försök igen.' },
{ status: 500 },
)
}
log.info('[stripe] Starting Connect OAuth', {
connection_id: created.id,
company_id: companyId,
livemode: isLiveMode(),
})
return NextResponse.json({ url: buildAuthorizeUrl(oauthState) })
},
},
{
method: 'DELETE',
path: '/disconnect',
handler: async (request: Request, ctx?: ExtensionContext) => {
const log = ctx?.log ?? console
const supabase = ctx?.supabase ?? await (await import('@/lib/supabase/server')).createClient()
const { data: { user } } = await supabase.auth.getUser()
if (!user) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
}
if (!ctx?.companyId) {
return NextResponse.json({ error: 'Company context required' }, { status: 400 })
}
const companyId = ctx.companyId
const rl = await checkRateLimit({
prefix: 'stripe:disconnect',
identifier: user.id,
...RATE_LIMIT_DISCONNECT,
})
if (!rl.ok) return rl.response!
const body = (await request.json().catch(() => ({}))) as { connection_id?: string }
const base = supabase
.from('stripe_connections')
.select('id, status, stripe_account_id')
.eq('company_id', companyId)
const query = body.connection_id
? base.eq('id', body.connection_id).limit(1)
: base.neq('status', 'revoked').order('created_at', { ascending: false }).limit(1)
const { data: rows, error: findError } = await query
const connection = rows?.[0] as
| Pick<StripeConnection, 'id' | 'status' | 'stripe_account_id'>
| undefined
if (findError || !connection) {
return NextResponse.json({ error: 'Connection not found' }, { status: 404 })
}
// Best-effort revoke at Stripe: an already-severed connection throws,
// which is fine (the goal state is reached either way). Logged at WARN
// so a systematic revoke failure stays visible to monitoring.
if (connection.status === 'active' && connection.stripe_account_id) {
try {
await deauthorizeAccount(connection.stripe_account_id)
} catch (revokeError) {
log.warn('[stripe] Deauthorize skipped (likely already revoked)', {
message: revokeError instanceof Error ? revokeError.message : String(revokeError),
connection_id: connection.id,
})
}
}
const { error: updateError } = await supabase
.from('stripe_connections')
.update({
status: 'revoked',
oauth_state: null,
disconnected_at: new Date().toISOString(),
})
.eq('id', connection.id)
.eq('company_id', companyId)
if (updateError) {
log.error('[stripe] Failed to mark connection revoked', {
message: updateError.message,
connection_id: connection.id,
})
return NextResponse.json(
{ error: 'Kunde inte koppla från. Försök igen.' },
{ status: 500 },
)
}
if (ctx?.emit) {
try {
await ctx.emit({
type: 'stripe.disconnected',
payload: {
connectionId: connection.id,
stripeAccountId: connection.stripe_account_id,
reason: 'user',
userId: user.id,
companyId,
},
})
} catch {
// Audit event failure must not block the disconnect itself.
}
}
return NextResponse.json({ success: true })
},
},
],
}
export default stripeExtension