* feat(peppol): Qvalia access-point adapter, send flow and delivery webhook Qvalia is the contracted Peppol Access Point (signed 2026-08-21). This fills the provider-neutral PeppolTransport seam from #1595 with a real adapter and turns the disabled "Skicka via Peppol" menu item into a working send flow. Adapter (lib/invoices/transports/qvalia.ts): partner-scoped recipient lookup, XML submission to /invoices/outgoing with integrationId correlation, 409 recovery only when the stored copy carries the same seller endpoint, tolerant mapping of Qvalia's free-text webhook statuses onto the 11-state lifecycle, constant-time shared-secret webhook verification (Qvalia does not sign webhooks), and evidence retrieval of the message-log status plus Qvalia's stored XML copy. Registered from the environment in lib/init.ts; switched on per deployment with PEPPOL_TRANSPORT_PROVIDER=qvalia. POST /api/invoices/[id]/peppol/send: stage the exact XML, look up the recipient, record recipient_verified and submitting, submit, record submission_accepted, then issue a draft with the mark-sent semantics (issueAndBookInvoice) only after the network accepted it. A sync rejection is a terminal failed event so the identical document is never re-sent; an operational failure is retryable; an already-submitted XML replays idempotently. POST /api/webhooks/peppol/qvalia resolves the delivery by integrationId, persists the verified event via the service-role RPC and stores evidence best-effort; unknown submissions answer 200, our own persistence failures 500. UI: the send item is availability-driven with a confirm dialog, the invoice page shows the latest Peppol status, and drafts can be sent (the number is assigned server-side). Probe script for the first sandbox contact under scripts/peppol/qvalia-probe.ts. Refs #546 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TqFpxeWqbpR7bcwUJLRERQ * fix(peppol): Qvalia sandbox facts from first live contact: bare-key auth, api-test host, SMP-URL document types The onboarding mail and a live probe against the sandbox (partner SE5595386219) corrected three assumptions from the public docs: the key is accepted bare in the Authorization header (the ApiKey prefix answers 401), the sandbox host is api-test.qvalia.com, and the recipient lookup returns document types as SMP service URLs, so capabilities are now normalized to bare Peppol document type ids before comparison. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TqFpxeWqbpR7bcwUJLRERQ * feat(peppol): probe commands to inspect and configure the Qvalia webhook subscription Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TqFpxeWqbpR7bcwUJLRERQ * fix(peppol): decode UBL entities in one pass (CodeQL js/double-escaping) Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TqFpxeWqbpR7bcwUJLRERQ --------- Co-authored-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com> Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
169 lines
4.9 KiB
TypeScript
169 lines
4.9 KiB
TypeScript
/**
|
|
* Provider-neutral boundary between Accounted's Peppol lifecycle and a
|
|
* contracted Access Point. Core code must not infer network delivery from an
|
|
* HTTP response: submissions, asynchronous events, and evidence are distinct.
|
|
*/
|
|
|
|
export type PeppolDeliveryStatus =
|
|
| 'staged'
|
|
| 'recipient_verified'
|
|
| 'submitting'
|
|
| 'retryable_failure'
|
|
| 'submission_accepted'
|
|
| 'transport_succeeded'
|
|
| 'recipient_acknowledged'
|
|
| 'business_accepted'
|
|
| 'business_rejected'
|
|
| 'no_route'
|
|
| 'failed'
|
|
|
|
export interface PeppolParticipant {
|
|
scheme: string
|
|
identifier: string
|
|
}
|
|
export interface PeppolRecipientCapability {
|
|
documentTypeId: string
|
|
processId: string
|
|
}
|
|
|
|
export type PeppolRecipientLookup =
|
|
| {
|
|
reachable: true
|
|
participant: PeppolParticipant
|
|
capabilities: PeppolRecipientCapability[]
|
|
checkedAt: string
|
|
}
|
|
| {
|
|
reachable: false
|
|
participant: PeppolParticipant
|
|
reasonCode: string
|
|
checkedAt: string
|
|
}
|
|
|
|
export interface PeppolSubmission {
|
|
idempotencyKey: string
|
|
tenantReference: string
|
|
sender: PeppolParticipant
|
|
recipient: PeppolParticipant
|
|
documentTypeId: string
|
|
processId: string
|
|
filename: string
|
|
contentType: 'application/xml'
|
|
document: string
|
|
documentSha256: string
|
|
}
|
|
|
|
export interface PeppolSubmissionReceipt {
|
|
provider: string
|
|
providerSubmissionId: string
|
|
idempotencyKey: string
|
|
tenantReference: string
|
|
acceptedAt: string
|
|
}
|
|
|
|
export interface PeppolVerifiedEvent {
|
|
provider: string
|
|
providerTenantId: string | null
|
|
providerSubmissionId: string | null
|
|
providerEventId: string | null
|
|
/**
|
|
* Accounted's delivery idempotency key. A provider webhook only knows its
|
|
* own submission id, so an adapter returns `null` here and the webhook route
|
|
* resolves the key from `providerSubmissionId` before persisting.
|
|
*/
|
|
idempotencyKey: string | null
|
|
eventCode: string
|
|
normalizedStatus: PeppolDeliveryStatus
|
|
isTerminal: boolean
|
|
detail: string | null
|
|
occurredAt: string
|
|
rawPayload: Record<string, unknown>
|
|
eventSha256: string
|
|
verificationMethod: string
|
|
}
|
|
|
|
export interface PeppolDeliveryEvidence {
|
|
provider: string
|
|
evidenceType: string
|
|
payload: Record<string, unknown>
|
|
exactDocument: string | null
|
|
exactDocumentSha256: string | null
|
|
evidenceSha256: string
|
|
retrievedAt: string
|
|
}
|
|
|
|
export interface PeppolWebhookRequest {
|
|
headers: Headers
|
|
rawBody: Uint8Array
|
|
}
|
|
|
|
/**
|
|
* Provider-neutral failure raised by an adapter. `retryable` separates an
|
|
* operational problem (network, rate limit, credentials) from a verdict on the
|
|
* document itself (rejected, duplicate); the send route records them as
|
|
* different lifecycle events.
|
|
*/
|
|
export class PeppolTransportError extends Error {
|
|
readonly retryable: boolean
|
|
readonly detail: string | null
|
|
|
|
constructor(message: string, options: { retryable: boolean; detail?: string | null; cause?: unknown }) {
|
|
super(message, options.cause !== undefined ? { cause: options.cause } : undefined)
|
|
this.name = 'PeppolTransportError'
|
|
this.retryable = options.retryable
|
|
this.detail = options.detail ?? null
|
|
}
|
|
}
|
|
|
|
export function isPeppolTransportError(error: unknown): error is PeppolTransportError {
|
|
return error instanceof PeppolTransportError
|
|
}
|
|
|
|
export interface PeppolTransport {
|
|
readonly provider: string
|
|
lookupRecipient(participant: PeppolParticipant): Promise<PeppolRecipientLookup>
|
|
submit(submission: PeppolSubmission): Promise<PeppolSubmissionReceipt>
|
|
verifyWebhook(request: PeppolWebhookRequest): Promise<PeppolVerifiedEvent[]>
|
|
retrieveEvidence(providerSubmissionId: string): Promise<PeppolDeliveryEvidence[]>
|
|
}
|
|
|
|
const transports = new Map<string, PeppolTransport>()
|
|
|
|
export function registerPeppolTransport(transport: PeppolTransport): () => void {
|
|
const provider = transport.provider.trim().toLowerCase()
|
|
if (!provider) throw new Error('Peppol transport provider is required')
|
|
if (transports.has(provider)) {
|
|
throw new Error(`Peppol transport already registered: ${provider}`)
|
|
}
|
|
|
|
transports.set(provider, transport)
|
|
return () => {
|
|
if (transports.get(provider) === transport) transports.delete(provider)
|
|
}
|
|
}
|
|
|
|
export function getPeppolTransport(provider: string): PeppolTransport | null {
|
|
return transports.get(provider.trim().toLowerCase()) ?? null
|
|
}
|
|
|
|
export type PeppolTransportAvailability =
|
|
| { available: true; provider: string }
|
|
| {
|
|
available: false
|
|
provider: null
|
|
reason: 'provider_selection_required' | 'provider_adapter_unavailable'
|
|
}
|
|
|
|
export function getPeppolTransportAvailability(): PeppolTransportAvailability {
|
|
const configuredProvider = process.env.PEPPOL_TRANSPORT_PROVIDER?.trim().toLowerCase()
|
|
if (!configuredProvider) {
|
|
return { available: false, provider: null, reason: 'provider_selection_required' }
|
|
}
|
|
|
|
if (!transports.has(configuredProvider)) {
|
|
return { available: false, provider: null, reason: 'provider_adapter_unavailable' }
|
|
}
|
|
|
|
return { available: true, provider: configuredProvider }
|
|
}
|