@asteby/storefront-react 0.26.0 → 0.28.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -2
- package/dist/account-server.cjs +533 -83
- package/dist/account-server.cjs.map +1 -1
- package/dist/account-server.d.cts +13 -2
- package/dist/account-server.d.ts +13 -2
- package/dist/account-server.js +23 -6
- package/dist/account-server.js.map +1 -1
- package/dist/account.cjs +572 -96
- package/dist/account.cjs.map +1 -1
- package/dist/account.d.cts +82 -45
- package/dist/account.d.ts +82 -45
- package/dist/account.js +53 -14
- package/dist/account.js.map +1 -1
- package/dist/checkout.cjs +114 -38
- package/dist/checkout.cjs.map +1 -1
- package/dist/checkout.d.cts +24 -1
- package/dist/checkout.d.ts +24 -1
- package/dist/checkout.js +114 -38
- package/dist/checkout.js.map +1 -1
- package/dist/chunk-BXKV2KWZ.js +291 -0
- package/dist/chunk-BXKV2KWZ.js.map +1 -0
- package/dist/chunk-LIUEQENV.js +156 -0
- package/dist/chunk-LIUEQENV.js.map +1 -0
- package/dist/fiscal-server.cjs +27 -2
- package/dist/fiscal-server.cjs.map +1 -1
- package/dist/fiscal-server.d.cts +10 -1
- package/dist/fiscal-server.d.ts +10 -1
- package/dist/fiscal-server.js +26 -3
- package/dist/fiscal-server.js.map +1 -1
- package/dist/voucher-document-FWH4DQ5N.js +72 -0
- package/dist/voucher-document-FWH4DQ5N.js.map +1 -0
- package/dist/voucher-pdf-LQC333AB.js +213 -0
- package/dist/voucher-pdf-LQC333AB.js.map +1 -0
- package/package.json +1 -1
- package/dist/chunk-TXEYBDFG.js +0 -182
- package/dist/chunk-TXEYBDFG.js.map +0 -1
- package/dist/voucher-document-SFWJCLTN.js +0 -124
- package/dist/voucher-document-SFWJCLTN.js.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/fiscal/format.ts","../src/fiscal/extraction.ts","../src/fiscal/server-extract.ts","../src/fiscal/server.ts"],"names":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D,YAAA,MAAM,QAA2B,CAAC,SAAA,EAAW,WAAA,EAAa,SAAA,EAAW,YAAY,YAAY,CAAA;AAE7F,YAAA,OAAO,KAAK,MAAA,EAAQ,KAAA,CAAM,SAAS,IAAuB,CAAA,GAAK,OAA2B,YAAY,CAAA;AAAA,UACxG;AAAA,QACF;AAAA,QACA;AACE,UAAA,OAAO,IAAA,CAAK,QAAW,YAAY,CAAA;AAAA;AACvC,IACF,SAAS,GAAA,EAAK;AACZ,MAAA,OAAO,UAAU,GAAA,EAAK,MAAA,EAAQ,QAAQ,MAAA,KAAW,eAAA,GAAkB,SAAY,IAAI,CAAA;AAAA,IACrF;AAAA,EACF,CAAA;AACF","file":"fiscal-server.cjs","sourcesContent":["/**\n * Pure helpers over the region's fiscal catalog (`Region.fiscal_id`). They know no country: the tax id format, the\n * regimes, the invoice uses and the postal code format are all data.\n */\nimport type { InvoiceRequest, Region, RegionFiscalEntry, RegionFiscalId } from '@asteby/storefront'\nimport type { FiscalFieldName, FiscalFieldValues } from './types.js'\n\nexport type FiscalRegion = Pick<Region, 'address'> & { fiscal_id?: RegionFiscalId }\n\n/** Uppercase, no spaces, hyphens or dots: how a tax id is compared and sent. */\nexport function normalizeTaxId(raw: string): string {\n return raw.replace(/[\\s\\-.]/g, '').toUpperCase()\n}\n\n/** Masks a tax id for display and logs: first 2 and last 3 characters stay (`PE********ABC`). */\nexport function maskTaxId(taxId: string): string {\n const v = taxId.trim()\n return v.length <= 5 ? '*'.repeat(v.length) : `${v.slice(0, 2)}${'*'.repeat(v.length - 5)}${v.slice(-3)}`\n}\n\nconst compile = (pattern: string | undefined): RegExp | null => {\n if (!pattern) return null\n try {\n return new RegExp(pattern)\n } catch {\n return null\n }\n}\n\n/** `null` when the tax id is valid for the region (its pattern or one of its generic ids); otherwise why not. */\nexport function taxIdProblem(fid: RegionFiscalId | undefined, raw: string): 'required' | 'format' | null {\n const v = normalizeTaxId(raw)\n if (!v) return 'required'\n if (!fid) return null\n if (fid.generic_ids.includes(v)) return null\n const re = compile(fid.pattern)\n return re && !re.test(v) ? 'format' : null\n}\n\nexport function postalCodeProblem(region: FiscalRegion, raw: string): 'required' | 'format' | null {\n const v = raw.trim()\n if (!v) return 'required'\n const re = compile(region.address?.postal_code?.pattern)\n return re && !re.test(v) ? 'format' : null\n}\n\n/**\n * Builds the function that tells a person type from a tax id when the region does not publish one. Pass the length\n * of the tax id per type, e.g. `personTypeByLength({ individual: 13, company: 12 })`. Without a function the regimes\n * are not narrowed by person type (the server still checks the combination).\n */\nexport function personTypeByLength(lengths: Record<string, number>): (taxId: string) => string | null {\n return (taxId) => {\n const n = normalizeTaxId(taxId).length\n return Object.keys(lengths).find((t) => lengths[t] === n) ?? null\n }\n}\n\n/**\n * Tells a person type from a tax id with the region's own patterns (`fiscal_id.person_patterns`, DATA of the region\n * pack). `null` when the region publishes none or none matches. A pattern that does not compile is ignored.\n */\nexport function personTypeFromPatterns(fid: RegionFiscalId | undefined, taxId: string): string | null {\n const patterns = fid?.person_patterns\n if (!patterns) return null\n const v = normalizeTaxId(taxId)\n for (const type of Object.keys(patterns).sort()) {\n const re = compile(patterns[type])\n if (re?.test(v)) return type\n }\n return null\n}\n\n/** The function to use for the person type: the store's override, else the region's patterns. */\nexport function personTypeResolver(fid: RegionFiscalId | undefined, override?: (taxId: string) => string | null): (taxId: string) => string | null {\n return override ?? ((taxId) => personTypeFromPatterns(fid, taxId))\n}\n\n/**\n * The invoice use to suggest for a regime, from region data only: the organization's `default_invoice_use` when the\n * regime admits it, else the first use whose `suggested_for_regimes` lists the regime (and whose `allowed_regimes`\n * admits it). `null` when the region suggests nothing: then nothing is preselected. A suggestion, never a rule.\n */\nexport function suggestInvoiceUse(fid: RegionFiscalId | undefined, regime: string | null | undefined): string | null {\n if (!fid || !regime) return null\n const allowed = usesFor(fid, regime)\n const preferred = fid.default_invoice_use\n if (preferred && allowed.some((u) => u.code === preferred)) return preferred\n return allowed.find((u) => u.suggested_for_regimes?.includes(regime))?.code ?? null\n}\n\n/** Regimes that fit a person type (all when the type is unknown). */\nexport function regimesFor(fid: RegionFiscalId | undefined, personType: string | null): RegionFiscalEntry[] {\n const all = fid?.regimes ?? []\n if (!personType) return all\n return all.filter((r) => r.person_types.length === 0 || r.person_types.includes(personType))\n}\n\n/** Invoice uses a regime admits (all when no regime is chosen yet). */\nexport function usesFor(fid: RegionFiscalId | undefined, regime: string | null | undefined): RegionFiscalEntry[] {\n const all = fid?.invoice_uses ?? []\n if (!regime) return all\n return all.filter((u) => !u.allowed_regimes || u.allowed_regimes.length === 0 || u.allowed_regimes.includes(regime))\n}\n\nexport type FiscalProblem = 'required' | 'format' | 'unknown' | 'person_type' | 'use_not_allowed'\n\n/** Client-side check of the whole form against the region; the server stays the authority. */\nexport function validateFiscalValues(\n region: FiscalRegion,\n v: Partial<FiscalFieldValues>,\n personTypeOverride?: (taxId: string) => string | null,\n): Partial<Record<FiscalFieldName, FiscalProblem>> {\n const fid = region.fiscal_id\n const personTypeOf = personTypeResolver(fid, personTypeOverride)\n const out: Partial<Record<FiscalFieldName, FiscalProblem>> = {}\n const tax = taxIdProblem(fid, v.tax_id ?? '')\n if (tax) out.tax_id = tax\n if (!(v.legal_name ?? '').trim()) out.legal_name = 'required'\n if (postalCodeProblem(region, v.postal_code ?? '')) out.postal_code = postalCodeProblem(region, v.postal_code ?? '')!\n const regime = v.tax_regime ?? ''\n if (!regime) out.tax_regime = 'required'\n else if (fid) {\n const entry = fid.regimes.find((r) => r.code === regime)\n if (!entry) out.tax_regime = 'unknown'\n else if (!tax && !fid.generic_ids.includes(normalizeTaxId(v.tax_id ?? ''))) {\n const person = personTypeOf?.(v.tax_id ?? '') ?? null\n if (person && entry.person_types.length > 0 && !entry.person_types.includes(person)) out.tax_regime = 'person_type'\n }\n }\n const use = v.use ?? ''\n if (!use) out.use = 'required'\n else if (fid) {\n const entry = fid.invoice_uses.find((u) => u.code === use)\n if (!entry) out.use = 'unknown'\n else if (regime && entry.allowed_regimes && entry.allowed_regimes.length > 0 && !entry.allowed_regimes.includes(regime)) out.use = 'use_not_allowed'\n }\n return out\n}\n\n/** Form values -> the API's `InvoiceRequest` (without `document`). */\nexport function toInvoiceRequest(v: Partial<FiscalFieldValues>, countryCode?: string): InvoiceRequest {\n return {\n requested: true,\n tax_id: normalizeTaxId(v.tax_id ?? ''),\n legal_name: (v.legal_name ?? '').trim(),\n tax_regime: v.tax_regime ?? '',\n use: v.use ?? '',\n postal_code: (v.postal_code ?? '').trim(),\n ...(countryCode ? { country_code: countryCode } : {}),\n }\n}\n\n/** `label_en` for English locales when the region has it. */\nexport function entryLabel(e: RegionFiscalEntry, locale?: string | null): string {\n return (locale ?? 'es-MX').toLowerCase().startsWith('en') && e.label_en ? e.label_en : e.label\n}\n","/** Turns what the engine read into what the UI shows. Shared by the browser reader and the server upload. */\nimport type { FiscalDocumentEvidence } from '@asteby/storefront'\nimport type { IdentityExtraction } from '@asteby/storefront/fiscal-document'\nimport { normalizeTaxId } from './format.js'\nimport type { FiscalExtractionSummary, FiscalFieldName } from './types.js'\n\nconst first = (v: string | string[] | undefined): string => (Array.isArray(v) ? (v[0] ?? '') : (v ?? ''))\n\n/** `evidence` comes from `buildDocumentEvidence`. Only the conventional fields are kept: nothing else can leak. */\nexport function summarizeExtraction(x: IdentityExtraction, evidence: FiscalDocumentEvidence, validRegimes?: Iterable<string>): FiscalExtractionSummary {\n const f = x.fields\n const valid = validRegimes ? new Set(validRegimes) : null\n const raw = f.tax_regime\n const candidates = (Array.isArray(raw) ? raw : raw ? [raw] : []).filter((c) => !valid || valid.has(c))\n const values: FiscalExtractionSummary['values'] = {}\n const from: FiscalFieldName[] = []\n const put = (name: FiscalFieldName, v: string) => {\n if (!v) return\n values[name] = v\n from.push(name)\n }\n put('tax_id', normalizeTaxId(first(f.tax_id)))\n put('legal_name', first(f.legal_name).trim())\n put('postal_code', first(f.postal_code).trim())\n if (candidates.length === 1) put('tax_regime', candidates[0]!)\n else if (candidates.length > 1) from.push('tax_regime')\n return {\n kind: x.kind,\n values,\n regime_candidates: candidates,\n from_document: from,\n qr_match: x.qr.match,\n stale: x.stale,\n age_days: x.age_days,\n missing: [...x.missing],\n evidence,\n }\n}\n","/**\n * Reads a tax document uploaded without JavaScript, on the server, with the same engine the browser uses. In memory\n * only: the bytes are never written to disk, never logged and are dropped when this returns.\n */\nimport type { IdentityDocumentSpec, Region } from '@asteby/storefront'\nimport { IdentityDocumentError, buildDocumentEvidence, catalogsFromRegion, extractIdentityDocument, readPdfText } from '@asteby/storefront/fiscal-document'\nimport { summarizeExtraction } from './extraction.js'\nimport type { FiscalExtractionSummary } from './types.js'\n\nexport type UploadLimits = { maxBytes: number; maxPages: number; timeoutMs: number }\n\n/** 5 MiB, 6 pages, 3 s: the same as the browser reader. */\nexport const UPLOAD_LIMITS: Readonly<UploadLimits> = Object.freeze({ maxBytes: 5 * 1024 * 1024, maxPages: 6, timeoutMs: 3000 })\n\nconst PDF_MAGIC = [0x25, 0x50, 0x44, 0x46, 0x2d] // %PDF-\n\nexport async function extractUploadedDocument(\n file: Blob,\n spec: IdentityDocumentSpec,\n region: Pick<Region, 'fiscal_id'>,\n options: { limits?: Partial<UploadLimits>; now?: Date } = {},\n): Promise<FiscalExtractionSummary> {\n const lim = { ...UPLOAD_LIMITS, ...options.limits }\n // The region can only lower the limits.\n const maxBytes = Math.min(lim.maxBytes, spec.max_bytes || lim.maxBytes)\n const maxPages = Math.min(lim.maxPages, spec.max_pages || lim.maxPages)\n if (file.size > maxBytes) throw new IdentityDocumentError('too_large', 'file over the size limit', { reason: 'bytes' })\n const bytes = new Uint8Array(await file.arrayBuffer())\n if (!PDF_MAGIC.every((b, i) => bytes[i] === b)) throw new IdentityDocumentError('not_pdf', 'not a PDF')\n // The engine is the only reader. QR cross-check needs a canvas, which a server does not have: `qr_match` stays null.\n const { text, sha256 } = await readPdfText(bytes, { maxBytes, maxPages, timeoutMs: lim.timeoutMs })\n const x = extractIdentityDocument(text, spec, {\n catalogs: region.fiscal_id ? catalogsFromRegion(region.fiscal_id) : {},\n ...(options.now ? { now: options.now } : {}),\n })\n return summarizeExtraction(x, buildDocumentEvidence(x, { sha256, source: 'pdf_server' }), region.fiscal_id?.regimes.map((r) => r.code))\n}\n","/**\n * Server side of the fiscal components (`@asteby/storefront-react/fiscal/server`): `loadFiscalContext` for the loader and\n * `createInvoiceAction` for the action. No React, no router: Web `Request`/`Response` only, so any framework mounts it.\n *\n * The browser never talks to OPS. The action uses the store's `sk_` client and the buyer's session token (read from the\n * store's cookie). A tax document uploaded WITHOUT JavaScript is read in memory here with the same engine the browser\n * uses, and discarded: no disk, no logs, no copy.\n */\nimport {\n isStorefrontError,\n repairPath,\n type FiscalDocumentEvidence,\n type FiscalIdentityInput,\n type InvoiceRequest,\n type InvoicingCapabilities,\n type Region,\n type StorefrontClient,\n} from '@asteby/storefront'\nimport { normalizeTaxId } from './format.js'\nimport { extractUploadedDocument, UPLOAD_LIMITS, type UploadLimits } from './server-extract.js'\nimport type { FiscalActionData, FiscalErrorCode, FiscalIntent, FiscalReason } from './types.js'\n\nexport type { FiscalActionData, FiscalErrorCode, FiscalIntent, FiscalReason } from './types.js'\nexport { UPLOAD_LIMITS, extractUploadedDocument } from './server-extract.js'\nexport type { UploadLimits } from './server-extract.js'\n\n// ---- context ---------------------------------------------------------------------------------------------------------\n\nexport type FiscalContext = {\n /** The store can invoice: it has a region with a fiscal catalog and `capabilities.invoicing.available`. */\n available: boolean\n /** Why `available` is what it is. Absent on contexts built by older code: read it as `unknown`. */\n reason?: FiscalReason\n region: Region | null\n invoicing: InvoicingCapabilities | null\n}\n\n/** What is assumed when the store cannot be asked: nothing is known (`unreachable`), nothing breaks. */\nexport const NO_FISCAL_CONTEXT: Readonly<FiscalContext> = Object.freeze({ available: false, reason: 'unreachable' as const, region: null, invoicing: null })\n\nconst FAILURE_TTL_MS = 5_000\nconst cache = new WeakMap<object, { at: number; ok: boolean; value: FiscalContext }>()\n\nexport type LoadFiscalContextOptions = {\n /** How long a good answer is reused per client (default 60 s). `0` disables the cache. */\n ttlMs?: number\n signal?: AbortSignal\n now?: () => number\n}\n\n/**\n * Asks the store for its region and what invoicing it offers, with a short cache per client. It NEVER throws: when OPS\n * does not answer (older server, outage, token without scope) it returns `NO_FISCAL_CONTEXT` and retries after a few\n * seconds, so a checkout without invoicing keeps working. `reason` says which of the cases it is. Use it as the loader:\n * `loader = () => loadFiscalContext(sf)`.\n */\nexport async function loadFiscalContext(sf: StorefrontClient, options: LoadFiscalContextOptions = {}): Promise<FiscalContext> {\n const now = options.now ?? Date.now\n const ttl = options.ttlMs ?? 60_000\n const hit = cache.get(sf)\n if (hit && now() - hit.at < (hit.ok ? ttl : FAILURE_TTL_MS)) return hit.value\n const opt = options.signal ? { signal: options.signal } : undefined\n const [region, caps] = await Promise.allSettled([sf.region(opt), sf.customer.capabilities(opt)])\n const reg = region.status === 'fulfilled' ? region.value : null\n const noRegion = region.status === 'rejected' && isStorefrontError(region.reason) && region.reason.status === 404\n const invoicing = caps.status === 'fulfilled' ? (caps.value?.invoicing ?? null) : null\n const ok = (region.status === 'fulfilled' || noRegion) && caps.status === 'fulfilled'\n const available = !!reg?.fiscal_id && !!invoicing?.available\n const reason: FiscalReason = available\n ? 'available'\n : !ok\n ? 'unreachable'\n : noRegion || !reg?.fiscal_id\n ? invoicing && !invoicing.available\n ? 'off'\n : 'no_region'\n : !invoicing\n ? 'unknown'\n : 'off'\n const value: FiscalContext = { available, reason, region: reg, invoicing: invoicing?.available ? invoicing : null }\n cache.set(sf, { at: now(), ok, value: ok || available ? value : NO_FISCAL_CONTEXT })\n return ok || available ? value : NO_FISCAL_CONTEXT\n}\n\n// ---- action ----------------------------------------------------------------------------------------------------------\n\n/** What the action needs of a cookie to read the buyer's session. React Router's `createCookie(...)` fits as it is. */\nexport type SessionCookie = { parse(header: string | null): unknown }\n\nexport type CreateInvoiceActionOptions = {\n storefront: StorefrontClient\n /** The store's cookie for the buyer session token (`createCookie('sf_customer', ...)`), to know who is asking. */\n customerCookie?: SessionCookie\n /** Alternative to `customerCookie`: returns the buyer's session token, or `null` for a guest. */\n readCustomerToken?: (request: Request) => string | null | undefined | Promise<string | null | undefined>\n /** Shopper IP for the per-shopper rate limit (see `clientIp` of the client). */\n clientIp?: (request: Request) => string | null | undefined\n /** Region + capabilities source, for tests or a shared cache (default `loadFiscalContext`). */\n context?: (sf: StorefrontClient) => Promise<FiscalContext>\n /** Limits of the no-JS upload (defaults: 5 MiB, 6 pages, 3 s). The region's own limits can only lower them. */\n limits?: Partial<UploadLimits>\n /** Clock, for the freshness check. */\n now?: () => Date\n /** Rejects cross-site posts (default `true`): `Origin` and `Sec-Fetch-Site` must match the site. */\n sameSiteOnly?: boolean\n /** Prefix of the form fields (default `invoice`). */\n namePrefix?: string\n}\n\nconst EVIDENCE_SOURCES = new Set(['pdf_client', 'pdf_server', 'qr', 'manual'])\n\n/** Reads the evidence the reader put in the form, keeping only the known keys (a form is untrusted input). */\nexport function parseEvidence(raw: unknown): FiscalDocumentEvidence | undefined {\n if (typeof raw !== 'string' || !raw || raw.length > 4096) return undefined\n let o: unknown\n try {\n o = JSON.parse(raw)\n } catch {\n return undefined\n }\n if (typeof o !== 'object' || o === null) return undefined\n const e = o as Record<string, unknown>\n if (typeof e.kind !== 'string' || !e.kind || typeof e.source !== 'string' || !EVIDENCE_SOURCES.has(e.source)) return undefined\n const str = (v: unknown, max: number) => (typeof v === 'string' && v.length <= max ? v : null)\n return {\n kind: e.kind.slice(0, 64),\n document_id: str(e.document_id, 64),\n issued_on: str(e.issued_on, 32),\n sha256: typeof e.sha256 === 'string' && /^[0-9a-f]{64}$/.test(e.sha256) ? e.sha256 : null,\n ...(typeof e.extractor === 'string' ? { extractor: e.extractor.slice(0, 64) } : {}),\n source: e.source as FiscalDocumentEvidence['source'],\n qr_match: typeof e.qr_match === 'boolean' ? e.qr_match : null,\n }\n}\n\n/** Reads the `invoice.*` fields of a form into the API's `InvoiceRequest`. */\nexport function invoiceFromForm(form: FormData, prefix = 'invoice'): InvoiceRequest {\n const text = (k: string) => {\n const v = form.get(`${prefix}.${k}`)\n return typeof v === 'string' ? v.trim() : ''\n }\n const evidence = parseEvidence(form.get(`${prefix}.document`))\n const out: InvoiceRequest = {\n requested: true,\n tax_id: normalizeTaxId(text('tax_id')),\n legal_name: text('legal_name'),\n tax_regime: text('tax_regime'),\n use: text('use'),\n postal_code: text('postal_code'),\n }\n if (evidence) out.document = evidence\n if (form.get(`${prefix}.save`)) out.save = true\n return out\n}\n\nconst fail = (intent: FiscalIntent | undefined, code: FiscalErrorCode, extra: Partial<FiscalActionData> = {}): FiscalActionData => ({\n ok: false,\n ...(intent ? { intent } : {}),\n ...extra,\n error: { code, ...(extra.error?.field ? { field: extra.error.field } : {}), ...(extra.error?.blocked ? { blocked: extra.error.blocked } : {}) },\n})\n\nconst LOG_EVERY_MS = 60_000\nlet lastLogAt = -Infinity\nfunction logMisconfigured(scopes: readonly string[]): void {\n const t = Date.now()\n if (t - lastLogAt < LOG_EVERY_MS) return\n lastLogAt = t\n console.error(\n `[@asteby/storefront-react] La credencial de la tienda no tiene permisos${scopes.length ? ` (${scopes.join(', ')})` : ''}; ` +\n `la facturación no funciona hasta repararlo en ${repairPath()}.`,\n )\n}\n\n/** `fiscal.tax_regime` (profile) and `invoice.tax_regime` (cart, order) both name the same form control. */\nconst formField = (field: string | undefined, prefix: string) => (field ? field.replace(/^(fiscal|invoice)\\./, `${prefix}.`) : undefined)\n\nfunction toFailure(err: unknown, intent: FiscalIntent, prefix: string, values?: Partial<FiscalActionData['values']>): FiscalActionData {\n const carry: Partial<FiscalActionData> = values ? { values } : {}\n if (!isStorefrontError(err)) return fail(intent, 'unavailable', carry)\n if (err.code === 'insufficient_scope') {\n logMisconfigured(err.requiredScopes)\n return fail(intent, 'store_misconfigured', carry)\n }\n const details = (err.details ?? {}) as { reason?: unknown; blocked_reason?: unknown }\n switch (err.status) {\n case 401:\n return fail(intent, 'unauthorized', carry)\n case 404:\n return fail(intent, 'not_found', carry)\n case 409: {\n const why = typeof details.reason === 'string' ? details.reason : typeof details.blocked_reason === 'string' ? details.blocked_reason : ''\n return fail(intent, why === 'already_checked_out' ? 'not_found' : 'blocked', { ...carry, ...(why ? { error: { code: 'blocked', blocked: why } } : {}) })\n }\n case 422:\n case 400: {\n const field = formField(err.field, prefix)\n return fail(intent, 'validation', {\n ...carry,\n ...(field ? { fieldErrors: { [field]: err.message || 'invalid' }, error: { code: 'validation', field } } : {}),\n })\n }\n case 429:\n return fail(intent, 'rate_limited', carry)\n case 403:\n return fail(intent, 'unsupported', carry)\n default:\n return fail(intent, 'unavailable', carry)\n }\n}\n\nfunction sameSite(request: Request): boolean {\n const site = request.headers.get('sec-fetch-site')\n if (site && site !== 'same-origin' && site !== 'same-site' && site !== 'none') return false\n const origin = request.headers.get('origin')\n if (!origin) return true\n try {\n const host = request.headers.get('x-forwarded-host') ?? request.headers.get('host') ?? new URL(request.url).host\n return new URL(origin).host === host.split(',')[0]!.trim()\n } catch {\n return false\n }\n}\n\n/**\n * The invoicing action: one `POST` form with an `intent` field.\n *\n * | intent | fields | does |\n * | ------------------ | --------------------------------------------------- | ---------------------------------------------------------- |\n * | `set-invoice` | cart_id, invoice.* (`invoice.want` unset withdraws) | `carts.setInvoice`: validates against the region BEFORE paying |\n * | `save-profile` | invoice.*, consent | `customer.update({ fiscal })`, needs a session and consent |\n * | `request-invoice` | order_id, invoice.* | `customer.requestInvoice` (invoice after the purchase) |\n * | `clear-profile` | - | `customer.update({ fiscal: null })` (the buyer's right to delete) |\n * | `extract-document` | document (file), document_kind? | reads the file in memory, discards it, `step: 'review'` |\n *\n * It never redirects: it returns a `FiscalActionData` for the page to render (`<InvoiceToggle data={...}>`), with 422s\n * mapped to the field (`fieldErrors['invoice.tax_regime']`). The tax id of the buyer is never logged.\n */\nexport function createInvoiceAction(options: CreateInvoiceActionOptions) {\n const { storefront: sf } = options\n const prefix = options.namePrefix ?? 'invoice'\n const loadContext = options.context ?? ((c: StorefrontClient) => loadFiscalContext(c))\n const limits: UploadLimits = { ...UPLOAD_LIMITS, ...options.limits }\n\n async function sessionToken(request: Request): Promise<string | null> {\n if (options.readCustomerToken) return (await options.readCustomerToken(request)) || null\n if (!options.customerCookie) return null\n const v = await options.customerCookie.parse(request.headers.get('cookie'))\n return typeof v === 'string' && v ? v : null\n }\n\n return async function invoiceAction({ request }: { request: Request }): Promise<FiscalActionData> {\n if (options.sameSiteOnly !== false && !sameSite(request)) return fail(undefined, 'unsupported')\n // A file bigger than the limit is refused before it is buffered.\n const declared = Number(request.headers.get('content-length') ?? 0)\n if (declared > limits.maxBytes + 256 * 1024) return fail('extract-document', 'too_large')\n const form = await request.formData().catch(() => null)\n if (!form) return fail(undefined, 'validation')\n const intent = form.get('intent') as FiscalIntent\n const ip = options.clientIp?.(request)\n const token = await sessionToken(request)\n const w = { ...(ip ? { clientIp: ip } : {}), ...(token ? { customerToken: token } : {}) }\n const invoice = invoiceFromForm(form, prefix)\n const echo = { tax_id: invoice.tax_id ?? '', legal_name: invoice.legal_name ?? '', postal_code: invoice.postal_code ?? '', tax_regime: invoice.tax_regime ?? '', use: invoice.use ?? '' }\n const text = (name: string) => {\n const v = form.get(name)\n return typeof v === 'string' ? v.trim() : ''\n }\n\n try {\n switch (intent) {\n case 'set-invoice': {\n const cartId = text('cart_id')\n if (!cartId) return fail(intent, 'validation')\n if (!form.get(`${prefix}.want`) && !form.get(`${prefix}.requested`)) {\n await sf.carts.setInvoice(cartId, null, w)\n return { ok: true, intent, step: 'cleared' }\n }\n await sf.carts.setInvoice(cartId, invoice, w)\n return { ok: true, intent, step: 'saved', values: echo }\n }\n case 'save-profile': {\n if (!token) return fail(intent, 'unauthorized', { values: echo })\n if (!form.get('consent')) return fail(intent, 'consent_required', { values: echo })\n const fiscal: FiscalIdentityInput = {\n tax_id: invoice.tax_id ?? '',\n legal_name: invoice.legal_name ?? '',\n tax_regime: invoice.tax_regime ?? '',\n postal_code: invoice.postal_code ?? '',\n invoice_use: invoice.use ?? '',\n ...(invoice.document ? { document: invoice.document } : {}),\n }\n await sf.customer.update({ fiscal }, w)\n return { ok: true, intent, step: 'saved', values: echo }\n }\n case 'clear-profile': {\n if (!token) return fail(intent, 'unauthorized')\n await sf.customer.update({ fiscal: null }, w)\n return { ok: true, intent, step: 'cleared' }\n }\n case 'request-invoice': {\n if (!token) return fail(intent, 'unauthorized', { values: echo })\n const orderId = text('order_id')\n if (!orderId) return fail(intent, 'validation')\n const result = await sf.customer.requestInvoice(orderId, invoice, w)\n return { ok: true, intent, step: result.status === 'issued' ? 'issued' : 'pending', result, ...(result.invoice ? { invoice: result.invoice } : {}) }\n }\n case 'extract-document': {\n const ctx = await loadContext(sf)\n const region = ctx.region\n const kind = text('document_kind')\n const specs = region?.fiscal_id?.documents ?? []\n const spec = (kind ? specs.find((d) => d.kind === kind) : specs[0]) ?? undefined\n if (!ctx.available || !region || !spec) return fail(intent, 'unsupported')\n const file = form.get('document')\n if (!file || typeof file === 'string' || file.size === 0) return fail(intent, 'not_pdf')\n try {\n const extraction = await extractUploadedDocument(file, spec, region, { limits, ...(options.now ? { now: options.now() } : {}) })\n return { ok: true, intent, step: 'review', extraction }\n } catch (err) {\n const code = (err as { code?: string } | null)?.code\n const known: FiscalErrorCode[] = ['not_pdf', 'too_large', 'no_text', 'password', 'unreadable']\n // Never log the error: it can carry text from the document.\n return fail(intent, known.includes(code as FiscalErrorCode) ? (code as FiscalErrorCode) : 'unreadable')\n }\n }\n default:\n return fail(undefined, 'validation')\n }\n } catch (err) {\n return toFailure(err, intent, prefix, intent === 'clear-profile' ? undefined : echo)\n }\n }\n}\n\nexport type InvoiceAction = ReturnType<typeof createInvoiceAction>\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/fiscal/format.ts","../src/fiscal/extraction.ts","../src/fiscal/server-extract.ts","../src/fiscal/server.ts"],"names":["IdentityDocumentError","readPdfText","extractIdentityDocument","catalogsFromRegion","buildDocumentEvidence","isStorefrontError","repairPath"],"mappings":";;;;;;;;AAUO,SAAS,eAA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AAA,CAAA;AACvE,UAAA,IAAI,CAAC,GAAA,CAAI,SAAA,IAAa,CAAC,MAAA,IAAU,CAAC,IAAA,EAAM,OAAO,IAAA,CAAK,MAAA,EAAQ,aAAa,CAAA;AACzE,UAAA,MAAM,IAAA,GAAO,IAAA,CAAK,GAAA,CAAI,UAAU,CAAA;AAChC,UAAA,IAAI,CAAC,IAAA,IAAQ,OAAO,IAAA,KAAS,QAAA,IAAY,IAAA,CAAK,IAAA,KAAS,CAAA,EAAG,OAAO,IAAA,CAAK,MAAA,EAAQ,SAAS,CAAA;AACvF,UAAA,IAAI;AACF,YAAA,MAAM,aAAa,MAAM,uBAAA,CAAwB,MAAM,IAAA,EAAM,MAAA,EAAQ,EAAE,MAAA,EAAQ,GAAI,QAAQ,GAAA,GAAM,EAAE,KAAK,OAAA,CAAQ,GAAA,IAAM,GAAI,IAAK,CAAA;AAC/H,YAAA,OAAO,EAAE,EAAA,EAAI,IAAA,EAAM,MAAA,EAAQ,IAAA,EAAM,UAAU,UAAA,EAAW;AAAA,UACxD,SAAS,GAAA,EAAK;AACZ,YAAA,MAAM,OAAQ,GAAA,EAAkC,IAAA;AAChD,YAAA,MAAM,QAA2B,CAAC,SAAA,EAAW,WAAA,EAAa,SAAA,EAAW,YAAY,YAAY,CAAA;AAE7F,YAAA,OAAO,KAAK,MAAA,EAAQ,KAAA,CAAM,SAAS,IAAuB,CAAA,GAAK,OAA2B,YAAY,CAAA;AAAA,UACxG;AAAA,QACF;AAAA,QACA;AACE,UAAA,OAAO,IAAA,CAAK,QAAW,YAAY,CAAA;AAAA;AACvC,IACF,SAAS,GAAA,EAAK;AACZ,MAAA,OAAO,UAAU,GAAA,EAAK,MAAA,EAAQ,QAAQ,MAAA,KAAW,eAAA,GAAkB,SAAY,IAAI,CAAA;AAAA,IACrF;AAAA,EACF,CAAA;AACF","file":"fiscal-server.cjs","sourcesContent":["/**\n * Pure helpers over the region's fiscal catalog (`Region.fiscal_id`). They know no country: the tax id format, the\n * regimes, the invoice uses and the postal code format are all data.\n */\nimport type { InvoiceRequest, Region, RegionFiscalEntry, RegionFiscalId } from '@asteby/storefront'\nimport type { FiscalFieldName, FiscalFieldValues } from './types.js'\n\nexport type FiscalRegion = Pick<Region, 'address'> & { fiscal_id?: RegionFiscalId }\n\n/** Uppercase, no spaces, hyphens or dots: how a tax id is compared and sent. */\nexport function normalizeTaxId(raw: string): string {\n return raw.replace(/[\\s\\-.]/g, '').toUpperCase()\n}\n\n/** Masks a tax id for display and logs: first 2 and last 3 characters stay (`PE********ABC`). */\nexport function maskTaxId(taxId: string): string {\n const v = taxId.trim()\n return v.length <= 5 ? '*'.repeat(v.length) : `${v.slice(0, 2)}${'*'.repeat(v.length - 5)}${v.slice(-3)}`\n}\n\nconst compile = (pattern: string | undefined): RegExp | null => {\n if (!pattern) return null\n try {\n return new RegExp(pattern)\n } catch {\n return null\n }\n}\n\n/** `null` when the tax id is valid for the region (its pattern or one of its generic ids); otherwise why not. */\nexport function taxIdProblem(fid: RegionFiscalId | undefined, raw: string): 'required' | 'format' | null {\n const v = normalizeTaxId(raw)\n if (!v) return 'required'\n if (!fid) return null\n if (fid.generic_ids.includes(v)) return null\n const re = compile(fid.pattern)\n return re && !re.test(v) ? 'format' : null\n}\n\nexport function postalCodeProblem(region: FiscalRegion, raw: string): 'required' | 'format' | null {\n const v = raw.trim()\n if (!v) return 'required'\n const re = compile(region.address?.postal_code?.pattern)\n return re && !re.test(v) ? 'format' : null\n}\n\n/**\n * Builds the function that tells a person type from a tax id when the region does not publish one. Pass the length\n * of the tax id per type, e.g. `personTypeByLength({ individual: 13, company: 12 })`. Without a function the regimes\n * are not narrowed by person type (the server still checks the combination).\n */\nexport function personTypeByLength(lengths: Record<string, number>): (taxId: string) => string | null {\n return (taxId) => {\n const n = normalizeTaxId(taxId).length\n return Object.keys(lengths).find((t) => lengths[t] === n) ?? null\n }\n}\n\n/**\n * Tells a person type from a tax id with the region's own patterns (`fiscal_id.person_patterns`, DATA of the region\n * pack). `null` when the region publishes none or none matches. A pattern that does not compile is ignored.\n */\nexport function personTypeFromPatterns(fid: RegionFiscalId | undefined, taxId: string): string | null {\n const patterns = fid?.person_patterns\n if (!patterns) return null\n const v = normalizeTaxId(taxId)\n for (const type of Object.keys(patterns).sort()) {\n const re = compile(patterns[type])\n if (re?.test(v)) return type\n }\n return null\n}\n\n/** The function to use for the person type: the store's override, else the region's patterns. */\nexport function personTypeResolver(fid: RegionFiscalId | undefined, override?: (taxId: string) => string | null): (taxId: string) => string | null {\n return override ?? ((taxId) => personTypeFromPatterns(fid, taxId))\n}\n\n/**\n * The invoice use to suggest for a regime, from region data only: the organization's `default_invoice_use` when the\n * regime admits it, else the first use whose `suggested_for_regimes` lists the regime (and whose `allowed_regimes`\n * admits it). `null` when the region suggests nothing: then nothing is preselected. A suggestion, never a rule.\n */\nexport function suggestInvoiceUse(fid: RegionFiscalId | undefined, regime: string | null | undefined): string | null {\n if (!fid || !regime) return null\n const allowed = usesFor(fid, regime)\n const preferred = fid.default_invoice_use\n if (preferred && allowed.some((u) => u.code === preferred)) return preferred\n return allowed.find((u) => u.suggested_for_regimes?.includes(regime))?.code ?? null\n}\n\n/** Regimes that fit a person type (all when the type is unknown). */\nexport function regimesFor(fid: RegionFiscalId | undefined, personType: string | null): RegionFiscalEntry[] {\n const all = fid?.regimes ?? []\n if (!personType) return all\n return all.filter((r) => r.person_types.length === 0 || r.person_types.includes(personType))\n}\n\n/** Invoice uses a regime admits (all when no regime is chosen yet). */\nexport function usesFor(fid: RegionFiscalId | undefined, regime: string | null | undefined): RegionFiscalEntry[] {\n const all = fid?.invoice_uses ?? []\n if (!regime) return all\n return all.filter((u) => !u.allowed_regimes || u.allowed_regimes.length === 0 || u.allowed_regimes.includes(regime))\n}\n\nexport type FiscalProblem = 'required' | 'format' | 'unknown' | 'person_type' | 'use_not_allowed'\n\n/** Client-side check of the whole form against the region; the server stays the authority. */\nexport function validateFiscalValues(\n region: FiscalRegion,\n v: Partial<FiscalFieldValues>,\n personTypeOverride?: (taxId: string) => string | null,\n): Partial<Record<FiscalFieldName, FiscalProblem>> {\n const fid = region.fiscal_id\n const personTypeOf = personTypeResolver(fid, personTypeOverride)\n const out: Partial<Record<FiscalFieldName, FiscalProblem>> = {}\n const tax = taxIdProblem(fid, v.tax_id ?? '')\n if (tax) out.tax_id = tax\n if (!(v.legal_name ?? '').trim()) out.legal_name = 'required'\n if (postalCodeProblem(region, v.postal_code ?? '')) out.postal_code = postalCodeProblem(region, v.postal_code ?? '')!\n const regime = v.tax_regime ?? ''\n if (!regime) out.tax_regime = 'required'\n else if (fid) {\n const entry = fid.regimes.find((r) => r.code === regime)\n if (!entry) out.tax_regime = 'unknown'\n else if (!tax && !fid.generic_ids.includes(normalizeTaxId(v.tax_id ?? ''))) {\n const person = personTypeOf?.(v.tax_id ?? '') ?? null\n if (person && entry.person_types.length > 0 && !entry.person_types.includes(person)) out.tax_regime = 'person_type'\n }\n }\n const use = v.use ?? ''\n if (!use) out.use = 'required'\n else if (fid) {\n const entry = fid.invoice_uses.find((u) => u.code === use)\n if (!entry) out.use = 'unknown'\n else if (regime && entry.allowed_regimes && entry.allowed_regimes.length > 0 && !entry.allowed_regimes.includes(regime)) out.use = 'use_not_allowed'\n }\n return out\n}\n\n/** Form values -> the API's `InvoiceRequest` (without `document`). */\nexport function toInvoiceRequest(v: Partial<FiscalFieldValues>, countryCode?: string): InvoiceRequest {\n return {\n requested: true,\n tax_id: normalizeTaxId(v.tax_id ?? ''),\n legal_name: (v.legal_name ?? '').trim(),\n tax_regime: v.tax_regime ?? '',\n use: v.use ?? '',\n postal_code: (v.postal_code ?? '').trim(),\n ...(countryCode ? { country_code: countryCode } : {}),\n }\n}\n\n/** `label_en` for English locales when the region has it. */\nexport function entryLabel(e: RegionFiscalEntry, locale?: string | null): string {\n return (locale ?? 'es-MX').toLowerCase().startsWith('en') && e.label_en ? e.label_en : e.label\n}\n","/** Turns what the engine read into what the UI shows. Shared by the browser reader and the server upload. */\nimport type { FiscalDocumentEvidence } from '@asteby/storefront'\nimport type { IdentityExtraction } from '@asteby/storefront/fiscal-document'\nimport { normalizeTaxId } from './format.js'\nimport type { FiscalExtractionSummary, FiscalFieldName } from './types.js'\n\nconst first = (v: string | string[] | undefined): string => (Array.isArray(v) ? (v[0] ?? '') : (v ?? ''))\n\n/** `evidence` comes from `buildDocumentEvidence`. Only the conventional fields are kept: nothing else can leak. */\nexport function summarizeExtraction(x: IdentityExtraction, evidence: FiscalDocumentEvidence, validRegimes?: Iterable<string>): FiscalExtractionSummary {\n const f = x.fields\n const valid = validRegimes ? new Set(validRegimes) : null\n const raw = f.tax_regime\n const candidates = (Array.isArray(raw) ? raw : raw ? [raw] : []).filter((c) => !valid || valid.has(c))\n const values: FiscalExtractionSummary['values'] = {}\n const from: FiscalFieldName[] = []\n const put = (name: FiscalFieldName, v: string) => {\n if (!v) return\n values[name] = v\n from.push(name)\n }\n put('tax_id', normalizeTaxId(first(f.tax_id)))\n put('legal_name', first(f.legal_name).trim())\n put('postal_code', first(f.postal_code).trim())\n if (candidates.length === 1) put('tax_regime', candidates[0]!)\n else if (candidates.length > 1) from.push('tax_regime')\n return {\n kind: x.kind,\n values,\n regime_candidates: candidates,\n from_document: from,\n qr_match: x.qr.match,\n stale: x.stale,\n age_days: x.age_days,\n missing: [...x.missing],\n evidence,\n }\n}\n","/**\n * Reads a tax document uploaded without JavaScript, on the server, with the same engine the browser uses. In memory\n * only: the bytes are never written to disk, never logged and are dropped when this returns.\n */\nimport type { IdentityDocumentSpec, Region } from '@asteby/storefront'\nimport { IdentityDocumentError, buildDocumentEvidence, catalogsFromRegion, extractIdentityDocument, readPdfText } from '@asteby/storefront/fiscal-document'\nimport { summarizeExtraction } from './extraction.js'\nimport type { FiscalExtractionSummary } from './types.js'\n\nexport type UploadLimits = { maxBytes: number; maxPages: number; timeoutMs: number }\n\n/** 5 MiB, 6 pages, 3 s: the same as the browser reader. */\nexport const UPLOAD_LIMITS: Readonly<UploadLimits> = Object.freeze({ maxBytes: 5 * 1024 * 1024, maxPages: 6, timeoutMs: 3000 })\n\nconst PDF_MAGIC = [0x25, 0x50, 0x44, 0x46, 0x2d] // %PDF-\n\nexport async function extractUploadedDocument(\n file: Blob,\n spec: IdentityDocumentSpec,\n region: Pick<Region, 'fiscal_id'>,\n options: { limits?: Partial<UploadLimits>; now?: Date } = {},\n): Promise<FiscalExtractionSummary> {\n const lim = { ...UPLOAD_LIMITS, ...options.limits }\n // The region can only lower the limits.\n const maxBytes = Math.min(lim.maxBytes, spec.max_bytes || lim.maxBytes)\n const maxPages = Math.min(lim.maxPages, spec.max_pages || lim.maxPages)\n if (file.size > maxBytes) throw new IdentityDocumentError('too_large', 'file over the size limit', { reason: 'bytes' })\n const bytes = new Uint8Array(await file.arrayBuffer())\n if (!PDF_MAGIC.every((b, i) => bytes[i] === b)) throw new IdentityDocumentError('not_pdf', 'not a PDF')\n // The engine is the only reader. QR cross-check needs a canvas, which a server does not have: `qr_match` stays null.\n const { text, sha256 } = await readPdfText(bytes, { maxBytes, maxPages, timeoutMs: lim.timeoutMs })\n const x = extractIdentityDocument(text, spec, {\n catalogs: region.fiscal_id ? catalogsFromRegion(region.fiscal_id) : {},\n ...(options.now ? { now: options.now } : {}),\n })\n return summarizeExtraction(x, buildDocumentEvidence(x, { sha256, source: 'pdf_server' }), region.fiscal_id?.regimes.map((r) => r.code))\n}\n","/**\n * Server side of the fiscal components (`@asteby/storefront-react/fiscal/server`): `loadFiscalContext` for the loader and\n * `createInvoiceAction` for the action. No React, no router: Web `Request`/`Response` only, so any framework mounts it.\n *\n * The browser never talks to OPS. The action uses the store's `sk_` client and the buyer's session token (read from the\n * store's cookie). A tax document uploaded WITHOUT JavaScript is read in memory here with the same engine the browser\n * uses, and discarded: no disk, no logs, no copy.\n */\nimport {\n isStorefrontError,\n repairPath,\n type FiscalDocumentEvidence,\n type FiscalIdentityInput,\n type InvoiceRequest,\n type InvoicingCapabilities,\n type Region,\n type StorefrontClient,\n} from '@asteby/storefront'\nimport { normalizeTaxId } from './format.js'\nimport { extractUploadedDocument, UPLOAD_LIMITS, type UploadLimits } from './server-extract.js'\nimport type { FiscalActionData, FiscalErrorCode, FiscalIntent, FiscalReason } from './types.js'\n\nexport type { FiscalActionData, FiscalErrorCode, FiscalIntent, FiscalReason } from './types.js'\nexport { UPLOAD_LIMITS, extractUploadedDocument } from './server-extract.js'\nexport type { UploadLimits } from './server-extract.js'\n\n// ---- context ---------------------------------------------------------------------------------------------------------\n\nexport type FiscalContext = {\n /** The store can invoice: it has a region with a fiscal catalog and `capabilities.invoicing.available`. */\n available: boolean\n /** Why `available` is what it is. Absent on contexts built by older code: read it as `unknown`. */\n reason?: FiscalReason\n region: Region | null\n invoicing: InvoicingCapabilities | null\n}\n\n/** What is assumed when the store cannot be asked: nothing is known (`unreachable`), nothing breaks. */\nexport const NO_FISCAL_CONTEXT: Readonly<FiscalContext> = Object.freeze({ available: false, reason: 'unreachable' as const, region: null, invoicing: null })\n\nconst FAILURE_TTL_MS = 5_000\nconst cache = new WeakMap<object, { at: number; ok: boolean; value: FiscalContext }>()\n\nexport type LoadFiscalContextOptions = {\n /** How long a good answer is reused per client (default 60 s). `0` disables the cache. */\n ttlMs?: number\n signal?: AbortSignal\n now?: () => number\n}\n\n/**\n * Asks the store for its region and what invoicing it offers, with a short cache per client. It NEVER throws: when OPS\n * does not answer (older server, outage, token without scope) it returns `NO_FISCAL_CONTEXT` and retries after a few\n * seconds, so a checkout without invoicing keeps working. `reason` says which of the cases it is. Use it as the loader:\n * `loader = () => loadFiscalContext(sf)`.\n */\nexport async function loadFiscalContext(sf: StorefrontClient, options: LoadFiscalContextOptions = {}): Promise<FiscalContext> {\n const now = options.now ?? Date.now\n const ttl = options.ttlMs ?? 60_000\n const hit = cache.get(sf)\n if (hit && now() - hit.at < (hit.ok ? ttl : FAILURE_TTL_MS)) return hit.value\n const opt = options.signal ? { signal: options.signal } : undefined\n const [region, caps] = await Promise.allSettled([sf.region(opt), sf.customer.capabilities(opt)])\n const reg = region.status === 'fulfilled' ? region.value : null\n const noRegion = region.status === 'rejected' && isStorefrontError(region.reason) && region.reason.status === 404\n const invoicing = caps.status === 'fulfilled' ? (caps.value?.invoicing ?? null) : null\n const ok = (region.status === 'fulfilled' || noRegion) && caps.status === 'fulfilled'\n const available = !!reg?.fiscal_id && !!invoicing?.available\n const reason: FiscalReason = available\n ? 'available'\n : !ok\n ? 'unreachable'\n : noRegion || !reg?.fiscal_id\n ? invoicing && !invoicing.available\n ? 'off'\n : 'no_region'\n : !invoicing\n ? 'unknown'\n : 'off'\n const value: FiscalContext = { available, reason, region: reg, invoicing: invoicing?.available ? invoicing : null }\n cache.set(sf, { at: now(), ok, value: ok || available ? value : NO_FISCAL_CONTEXT })\n return ok || available ? value : NO_FISCAL_CONTEXT\n}\n\n// ---- action ----------------------------------------------------------------------------------------------------------\n\n/** What the action needs of a cookie to read the buyer's session. React Router's `createCookie(...)` fits as it is. */\nexport type SessionCookie = { parse(header: string | null): unknown }\n\nexport type CreateInvoiceActionOptions = {\n storefront: StorefrontClient\n /** The store's cookie for the buyer session token (`createCookie('sf_customer', ...)`), to know who is asking. */\n customerCookie?: SessionCookie\n /** Alternative to `customerCookie`: returns the buyer's session token, or `null` for a guest. */\n readCustomerToken?: (request: Request) => string | null | undefined | Promise<string | null | undefined>\n /** Shopper IP for the per-shopper rate limit (see `clientIp` of the client). */\n clientIp?: (request: Request) => string | null | undefined\n /** Region + capabilities source, for tests or a shared cache (default `loadFiscalContext`). */\n context?: (sf: StorefrontClient) => Promise<FiscalContext>\n /** Limits of the no-JS upload (defaults: 5 MiB, 6 pages, 3 s). The region's own limits can only lower them. */\n limits?: Partial<UploadLimits>\n /** Clock, for the freshness check. */\n now?: () => Date\n /** Rejects cross-site posts (default `true`): `Origin` and `Sec-Fetch-Site` must match the site. */\n sameSiteOnly?: boolean\n /** Prefix of the form fields (default `invoice`). */\n namePrefix?: string\n}\n\nconst EVIDENCE_SOURCES = new Set(['pdf_client', 'pdf_server', 'qr', 'manual'])\n\n/** Reads the evidence the reader put in the form, keeping only the known keys (a form is untrusted input). */\nexport function parseEvidence(raw: unknown): FiscalDocumentEvidence | undefined {\n if (typeof raw !== 'string' || !raw || raw.length > 4096) return undefined\n let o: unknown\n try {\n o = JSON.parse(raw)\n } catch {\n return undefined\n }\n if (typeof o !== 'object' || o === null) return undefined\n const e = o as Record<string, unknown>\n if (typeof e.kind !== 'string' || !e.kind || typeof e.source !== 'string' || !EVIDENCE_SOURCES.has(e.source)) return undefined\n const str = (v: unknown, max: number) => (typeof v === 'string' && v.length <= max ? v : null)\n return {\n kind: e.kind.slice(0, 64),\n document_id: str(e.document_id, 64),\n issued_on: str(e.issued_on, 32),\n sha256: typeof e.sha256 === 'string' && /^[0-9a-f]{64}$/.test(e.sha256) ? e.sha256 : null,\n ...(typeof e.extractor === 'string' ? { extractor: e.extractor.slice(0, 64) } : {}),\n source: e.source as FiscalDocumentEvidence['source'],\n qr_match: typeof e.qr_match === 'boolean' ? e.qr_match : null,\n }\n}\n\n/** Reads the `invoice.*` fields of a form into the API's `InvoiceRequest`. */\nexport function invoiceFromForm(form: FormData, prefix = 'invoice'): InvoiceRequest {\n const text = (k: string) => {\n const v = form.get(`${prefix}.${k}`)\n return typeof v === 'string' ? v.trim() : ''\n }\n const evidence = parseEvidence(form.get(`${prefix}.document`))\n const out: InvoiceRequest = {\n requested: true,\n tax_id: normalizeTaxId(text('tax_id')),\n legal_name: text('legal_name'),\n tax_regime: text('tax_regime'),\n use: text('use'),\n postal_code: text('postal_code'),\n }\n if (evidence) out.document = evidence\n if (form.get(`${prefix}.save`)) out.save = true\n return out\n}\n\nconst fail = (intent: FiscalIntent | undefined, code: FiscalErrorCode, extra: Partial<FiscalActionData> = {}): FiscalActionData => ({\n ok: false,\n ...(intent ? { intent } : {}),\n ...extra,\n error: { code, ...(extra.error?.field ? { field: extra.error.field } : {}), ...(extra.error?.blocked ? { blocked: extra.error.blocked } : {}) },\n})\n\nconst LOG_EVERY_MS = 60_000\nlet lastLogAt = -Infinity\nfunction logMisconfigured(scopes: readonly string[]): void {\n const t = Date.now()\n if (t - lastLogAt < LOG_EVERY_MS) return\n lastLogAt = t\n console.error(\n `[@asteby/storefront-react] La credencial de la tienda no tiene permisos${scopes.length ? ` (${scopes.join(', ')})` : ''}; ` +\n `la facturación no funciona hasta repararlo en ${repairPath()}.`,\n )\n}\n\n/**\n * The form control a server field belongs to. The server names a field by what it validated (`fiscal.invoice_use`,\n * `invoice.requested`, an alias like `razon_social`), the form names the control (`invoice.use`): a field that maps to no\n * control returns `undefined` so it is NOT reported as a marked field (the buyer would see \"check the marked fields\" with\n * nothing marked).\n */\nconst CONTROL_OF: Record<string, string> = {\n tax_id: 'tax_id',\n rfc: 'tax_id',\n legal_name: 'legal_name',\n razon_social: 'legal_name',\n postal_code: 'postal_code',\n zip: 'postal_code',\n cp: 'postal_code',\n tax_regime: 'tax_regime',\n regimen: 'tax_regime',\n use: 'use',\n invoice_use: 'use',\n cfdi_use: 'use',\n uso_cfdi: 'use',\n}\n\n/** `fiscal.tax_regime` (profile) and `invoice.tax_regime` (cart, order) both name the same form control. */\nexport const formField = (field: string | undefined, prefix: string) => {\n if (!field) return undefined\n const control = CONTROL_OF[field.replace(/^(fiscal|invoice)\\./, '')]\n return control ? `${prefix}.${control}` : undefined\n}\n\n/**\n * The body of `request-invoice`: only the fiscal values, with the names every server version accepts (`invoice_use`, not\n * `use`; no `requested` flag, an order that is being invoiced is requested by definition; no evidence object, the order\n * keeps the data it was validated with). An OPS that predates contract 1.9 answered 400 `requested: unknown field` to the\n * full `InvoiceRequest` the checkout sends.\n */\nexport function orderInvoiceBody(invoice: InvoiceRequest): InvoiceRequest {\n const { requested: _requested, document: _document, use, ...rest } = invoice\n return { ...rest, ...(use ? { invoice_use: use } : {}) }\n}\n\nfunction toFailure(err: unknown, intent: FiscalIntent, prefix: string, values?: Partial<FiscalActionData['values']>): FiscalActionData {\n const carry: Partial<FiscalActionData> = values ? { values } : {}\n if (!isStorefrontError(err)) return fail(intent, 'unavailable', carry)\n if (err.code === 'insufficient_scope') {\n logMisconfigured(err.requiredScopes)\n return fail(intent, 'store_misconfigured', carry)\n }\n const details = (err.details ?? {}) as { reason?: unknown; blocked_reason?: unknown }\n switch (err.status) {\n case 401:\n return fail(intent, 'unauthorized', carry)\n case 404:\n return fail(intent, 'not_found', carry)\n case 409: {\n const why = typeof details.reason === 'string' ? details.reason : typeof details.blocked_reason === 'string' ? details.blocked_reason : ''\n return fail(intent, why === 'already_checked_out' ? 'not_found' : 'blocked', { ...carry, ...(why ? { error: { code: 'blocked', blocked: why } } : {}) })\n }\n case 422:\n case 400: {\n const field = formField(err.field, prefix)\n return fail(intent, 'validation', {\n ...carry,\n ...(field ? { fieldErrors: { [field]: err.message || 'invalid' }, error: { code: 'validation', field } } : {}),\n })\n }\n case 429:\n return fail(intent, 'rate_limited', carry)\n case 403:\n return fail(intent, 'unsupported', carry)\n default:\n return fail(intent, 'unavailable', carry)\n }\n}\n\nfunction sameSite(request: Request): boolean {\n const site = request.headers.get('sec-fetch-site')\n if (site && site !== 'same-origin' && site !== 'same-site' && site !== 'none') return false\n const origin = request.headers.get('origin')\n if (!origin) return true\n try {\n const host = request.headers.get('x-forwarded-host') ?? request.headers.get('host') ?? new URL(request.url).host\n return new URL(origin).host === host.split(',')[0]!.trim()\n } catch {\n return false\n }\n}\n\n/**\n * The invoicing action: one `POST` form with an `intent` field.\n *\n * | intent | fields | does |\n * | ------------------ | --------------------------------------------------- | ---------------------------------------------------------- |\n * | `set-invoice` | cart_id, invoice.* (`invoice.want` unset withdraws) | `carts.setInvoice`: validates against the region BEFORE paying |\n * | `save-profile` | invoice.*, consent | `customer.update({ fiscal })`, needs a session and consent |\n * | `request-invoice` | order_id, invoice.* | `customer.requestInvoice` (invoice after the purchase) |\n * | `clear-profile` | - | `customer.update({ fiscal: null })` (the buyer's right to delete) |\n * | `extract-document` | document (file), document_kind? | reads the file in memory, discards it, `step: 'review'` |\n *\n * It never redirects: it returns a `FiscalActionData` for the page to render (`<InvoiceToggle data={...}>`), with 422s\n * mapped to the field (`fieldErrors['invoice.tax_regime']`). The tax id of the buyer is never logged.\n */\nexport function createInvoiceAction(options: CreateInvoiceActionOptions) {\n const { storefront: sf } = options\n const prefix = options.namePrefix ?? 'invoice'\n const loadContext = options.context ?? ((c: StorefrontClient) => loadFiscalContext(c))\n const limits: UploadLimits = { ...UPLOAD_LIMITS, ...options.limits }\n\n async function sessionToken(request: Request): Promise<string | null> {\n if (options.readCustomerToken) return (await options.readCustomerToken(request)) || null\n if (!options.customerCookie) return null\n const v = await options.customerCookie.parse(request.headers.get('cookie'))\n return typeof v === 'string' && v ? v : null\n }\n\n return async function invoiceAction({ request }: { request: Request }): Promise<FiscalActionData> {\n if (options.sameSiteOnly !== false && !sameSite(request)) return fail(undefined, 'unsupported')\n // A file bigger than the limit is refused before it is buffered.\n const declared = Number(request.headers.get('content-length') ?? 0)\n if (declared > limits.maxBytes + 256 * 1024) return fail('extract-document', 'too_large')\n const form = await request.formData().catch(() => null)\n if (!form) return fail(undefined, 'validation')\n const intent = form.get('intent') as FiscalIntent\n const ip = options.clientIp?.(request)\n const token = await sessionToken(request)\n const w = { ...(ip ? { clientIp: ip } : {}), ...(token ? { customerToken: token } : {}) }\n const invoice = invoiceFromForm(form, prefix)\n const echo = { tax_id: invoice.tax_id ?? '', legal_name: invoice.legal_name ?? '', postal_code: invoice.postal_code ?? '', tax_regime: invoice.tax_regime ?? '', use: invoice.use ?? '' }\n const text = (name: string) => {\n const v = form.get(name)\n return typeof v === 'string' ? v.trim() : ''\n }\n\n try {\n switch (intent) {\n case 'set-invoice': {\n const cartId = text('cart_id')\n if (!cartId) return fail(intent, 'validation')\n if (!form.get(`${prefix}.want`) && !form.get(`${prefix}.requested`)) {\n await sf.carts.setInvoice(cartId, null, w)\n return { ok: true, intent, step: 'cleared' }\n }\n await sf.carts.setInvoice(cartId, invoice, w)\n return { ok: true, intent, step: 'saved', values: echo }\n }\n case 'save-profile': {\n if (!token) return fail(intent, 'unauthorized', { values: echo })\n if (!form.get('consent')) return fail(intent, 'consent_required', { values: echo })\n const fiscal: FiscalIdentityInput = {\n tax_id: invoice.tax_id ?? '',\n legal_name: invoice.legal_name ?? '',\n tax_regime: invoice.tax_regime ?? '',\n postal_code: invoice.postal_code ?? '',\n invoice_use: invoice.use ?? '',\n ...(invoice.document ? { document: invoice.document } : {}),\n }\n await sf.customer.update({ fiscal }, w)\n return { ok: true, intent, step: 'saved', values: echo }\n }\n case 'clear-profile': {\n if (!token) return fail(intent, 'unauthorized')\n await sf.customer.update({ fiscal: null }, w)\n return { ok: true, intent, step: 'cleared' }\n }\n case 'request-invoice': {\n if (!token) return fail(intent, 'unauthorized', { values: echo })\n const orderId = text('order_id')\n if (!orderId) return fail(intent, 'validation')\n const result = await sf.customer.requestInvoice(orderId, orderInvoiceBody(invoice), w)\n return { ok: true, intent, step: result.status === 'issued' ? 'issued' : 'pending', result, ...(result.invoice ? { invoice: result.invoice } : {}) }\n }\n case 'extract-document': {\n const ctx = await loadContext(sf)\n const region = ctx.region\n const kind = text('document_kind')\n const specs = region?.fiscal_id?.documents ?? []\n const spec = (kind ? specs.find((d) => d.kind === kind) : specs[0]) ?? undefined\n if (!ctx.available || !region || !spec) return fail(intent, 'unsupported')\n const file = form.get('document')\n if (!file || typeof file === 'string' || file.size === 0) return fail(intent, 'not_pdf')\n try {\n const extraction = await extractUploadedDocument(file, spec, region, { limits, ...(options.now ? { now: options.now() } : {}) })\n return { ok: true, intent, step: 'review', extraction }\n } catch (err) {\n const code = (err as { code?: string } | null)?.code\n const known: FiscalErrorCode[] = ['not_pdf', 'too_large', 'no_text', 'password', 'unreadable']\n // Never log the error: it can carry text from the document.\n return fail(intent, known.includes(code as FiscalErrorCode) ? (code as FiscalErrorCode) : 'unreadable')\n }\n }\n default:\n return fail(undefined, 'validation')\n }\n } catch (err) {\n return toFailure(err, intent, prefix, intent === 'clear-profile' ? undefined : echo)\n }\n }\n}\n\nexport type InvoiceAction = ReturnType<typeof createInvoiceAction>\n"]}
|
package/dist/fiscal-server.d.cts
CHANGED
|
@@ -78,6 +78,15 @@ type CreateInvoiceActionOptions = {
|
|
|
78
78
|
declare function parseEvidence(raw: unknown): FiscalDocumentEvidence | undefined;
|
|
79
79
|
/** Reads the `invoice.*` fields of a form into the API's `InvoiceRequest`. */
|
|
80
80
|
declare function invoiceFromForm(form: FormData, prefix?: string): InvoiceRequest;
|
|
81
|
+
/** `fiscal.tax_regime` (profile) and `invoice.tax_regime` (cart, order) both name the same form control. */
|
|
82
|
+
declare const formField: (field: string | undefined, prefix: string) => string | undefined;
|
|
83
|
+
/**
|
|
84
|
+
* The body of `request-invoice`: only the fiscal values, with the names every server version accepts (`invoice_use`, not
|
|
85
|
+
* `use`; no `requested` flag, an order that is being invoiced is requested by definition; no evidence object, the order
|
|
86
|
+
* keeps the data it was validated with). An OPS that predates contract 1.9 answered 400 `requested: unknown field` to the
|
|
87
|
+
* full `InvoiceRequest` the checkout sends.
|
|
88
|
+
*/
|
|
89
|
+
declare function orderInvoiceBody(invoice: InvoiceRequest): InvoiceRequest;
|
|
81
90
|
/**
|
|
82
91
|
* The invoicing action: one `POST` form with an `intent` field.
|
|
83
92
|
*
|
|
@@ -97,4 +106,4 @@ declare function createInvoiceAction(options: CreateInvoiceActionOptions): ({ re
|
|
|
97
106
|
}) => Promise<FiscalActionData>;
|
|
98
107
|
type InvoiceAction = ReturnType<typeof createInvoiceAction>;
|
|
99
108
|
|
|
100
|
-
export { type CreateInvoiceActionOptions, FiscalActionData, type FiscalContext, FiscalReason, type InvoiceAction, type LoadFiscalContextOptions, NO_FISCAL_CONTEXT, type SessionCookie, UPLOAD_LIMITS, type UploadLimits, createInvoiceAction, extractUploadedDocument, invoiceFromForm, loadFiscalContext, parseEvidence };
|
|
109
|
+
export { type CreateInvoiceActionOptions, FiscalActionData, type FiscalContext, FiscalReason, type InvoiceAction, type LoadFiscalContextOptions, NO_FISCAL_CONTEXT, type SessionCookie, UPLOAD_LIMITS, type UploadLimits, createInvoiceAction, extractUploadedDocument, formField, invoiceFromForm, loadFiscalContext, orderInvoiceBody, parseEvidence };
|
package/dist/fiscal-server.d.ts
CHANGED
|
@@ -78,6 +78,15 @@ type CreateInvoiceActionOptions = {
|
|
|
78
78
|
declare function parseEvidence(raw: unknown): FiscalDocumentEvidence | undefined;
|
|
79
79
|
/** Reads the `invoice.*` fields of a form into the API's `InvoiceRequest`. */
|
|
80
80
|
declare function invoiceFromForm(form: FormData, prefix?: string): InvoiceRequest;
|
|
81
|
+
/** `fiscal.tax_regime` (profile) and `invoice.tax_regime` (cart, order) both name the same form control. */
|
|
82
|
+
declare const formField: (field: string | undefined, prefix: string) => string | undefined;
|
|
83
|
+
/**
|
|
84
|
+
* The body of `request-invoice`: only the fiscal values, with the names every server version accepts (`invoice_use`, not
|
|
85
|
+
* `use`; no `requested` flag, an order that is being invoiced is requested by definition; no evidence object, the order
|
|
86
|
+
* keeps the data it was validated with). An OPS that predates contract 1.9 answered 400 `requested: unknown field` to the
|
|
87
|
+
* full `InvoiceRequest` the checkout sends.
|
|
88
|
+
*/
|
|
89
|
+
declare function orderInvoiceBody(invoice: InvoiceRequest): InvoiceRequest;
|
|
81
90
|
/**
|
|
82
91
|
* The invoicing action: one `POST` form with an `intent` field.
|
|
83
92
|
*
|
|
@@ -97,4 +106,4 @@ declare function createInvoiceAction(options: CreateInvoiceActionOptions): ({ re
|
|
|
97
106
|
}) => Promise<FiscalActionData>;
|
|
98
107
|
type InvoiceAction = ReturnType<typeof createInvoiceAction>;
|
|
99
108
|
|
|
100
|
-
export { type CreateInvoiceActionOptions, FiscalActionData, type FiscalContext, FiscalReason, type InvoiceAction, type LoadFiscalContextOptions, NO_FISCAL_CONTEXT, type SessionCookie, UPLOAD_LIMITS, type UploadLimits, createInvoiceAction, extractUploadedDocument, invoiceFromForm, loadFiscalContext, parseEvidence };
|
|
109
|
+
export { type CreateInvoiceActionOptions, FiscalActionData, type FiscalContext, FiscalReason, type InvoiceAction, type LoadFiscalContextOptions, NO_FISCAL_CONTEXT, type SessionCookie, UPLOAD_LIMITS, type UploadLimits, createInvoiceAction, extractUploadedDocument, formField, invoiceFromForm, loadFiscalContext, orderInvoiceBody, parseEvidence };
|
package/dist/fiscal-server.js
CHANGED
|
@@ -97,7 +97,30 @@ function logMisconfigured(scopes) {
|
|
|
97
97
|
`[@asteby/storefront-react] La credencial de la tienda no tiene permisos${scopes.length ? ` (${scopes.join(", ")})` : ""}; la facturaci\xF3n no funciona hasta repararlo en ${repairPath()}.`
|
|
98
98
|
);
|
|
99
99
|
}
|
|
100
|
-
var
|
|
100
|
+
var CONTROL_OF = {
|
|
101
|
+
tax_id: "tax_id",
|
|
102
|
+
rfc: "tax_id",
|
|
103
|
+
legal_name: "legal_name",
|
|
104
|
+
razon_social: "legal_name",
|
|
105
|
+
postal_code: "postal_code",
|
|
106
|
+
zip: "postal_code",
|
|
107
|
+
cp: "postal_code",
|
|
108
|
+
tax_regime: "tax_regime",
|
|
109
|
+
regimen: "tax_regime",
|
|
110
|
+
use: "use",
|
|
111
|
+
invoice_use: "use",
|
|
112
|
+
cfdi_use: "use",
|
|
113
|
+
uso_cfdi: "use"
|
|
114
|
+
};
|
|
115
|
+
var formField = (field, prefix) => {
|
|
116
|
+
if (!field) return void 0;
|
|
117
|
+
const control = CONTROL_OF[field.replace(/^(fiscal|invoice)\./, "")];
|
|
118
|
+
return control ? `${prefix}.${control}` : void 0;
|
|
119
|
+
};
|
|
120
|
+
function orderInvoiceBody(invoice) {
|
|
121
|
+
const { requested: _requested, document: _document, use, ...rest } = invoice;
|
|
122
|
+
return { ...rest, ...use ? { invoice_use: use } : {} };
|
|
123
|
+
}
|
|
101
124
|
function toFailure(err, intent, prefix, values) {
|
|
102
125
|
const carry = values ? { values } : {};
|
|
103
126
|
if (!isStorefrontError(err)) return fail(intent, "unavailable", carry);
|
|
@@ -205,7 +228,7 @@ function createInvoiceAction(options) {
|
|
|
205
228
|
if (!token) return fail(intent, "unauthorized", { values: echo });
|
|
206
229
|
const orderId = text("order_id");
|
|
207
230
|
if (!orderId) return fail(intent, "validation");
|
|
208
|
-
const result = await sf.customer.requestInvoice(orderId, invoice, w);
|
|
231
|
+
const result = await sf.customer.requestInvoice(orderId, orderInvoiceBody(invoice), w);
|
|
209
232
|
return { ok: true, intent, step: result.status === "issued" ? "issued" : "pending", result, ...result.invoice ? { invoice: result.invoice } : {} };
|
|
210
233
|
}
|
|
211
234
|
case "extract-document": {
|
|
@@ -235,6 +258,6 @@ function createInvoiceAction(options) {
|
|
|
235
258
|
};
|
|
236
259
|
}
|
|
237
260
|
|
|
238
|
-
export { NO_FISCAL_CONTEXT, UPLOAD_LIMITS, createInvoiceAction, extractUploadedDocument, invoiceFromForm, loadFiscalContext, parseEvidence };
|
|
261
|
+
export { NO_FISCAL_CONTEXT, UPLOAD_LIMITS, createInvoiceAction, extractUploadedDocument, formField, invoiceFromForm, loadFiscalContext, orderInvoiceBody, parseEvidence };
|
|
239
262
|
//# sourceMappingURL=fiscal-server.js.map
|
|
240
263
|
//# sourceMappingURL=fiscal-server.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/fiscal/server-extract.ts","../src/fiscal/server.ts"],"names":[],"mappings":";;;;AAYO,IAAM,aAAA,GAAwC,MAAA,CAAO,MAAA,CAAO,EAAE,QAAA,EAAU,CAAA,GAAI,IAAA,GAAO,IAAA,EAAM,QAAA,EAAU,CAAA,EAAG,SAAA,EAAW,GAAA,EAAM;AAE9H,IAAM,YAAY,CAAC,EAAA,EAAM,EAAA,EAAM,EAAA,EAAM,IAAM,EAAI,CAAA;AAE/C,eAAsB,wBACpB,IAAA,EACA,IAAA,EACA,MAAA,EACA,OAAA,GAA0D,EAAC,EACzB;AAClC,EAAA,MAAM,MAAM,EAAE,GAAG,aAAA,EAAe,GAAG,QAAQ,MAAA,EAAO;AAElD,EAAA,MAAM,QAAA,GAAW,KAAK,GAAA,CAAI,GAAA,CAAI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AAA,OAAO,EAAE,EAAA,EAAI,IAAA,EAAM,MAAA,EAAQ,IAAA,EAAM,UAAU,UAAA,EAAW;AAAA,UACxD,SAAS,GAAA,EAAK;AACZ,YAAA,MAAM,OAAQ,GAAA,EAAkC,IAAA;AAChD,YAAA,MAAM,QAA2B,CAAC,SAAA,EAAW,WAAA,EAAa,SAAA,EAAW,YAAY,YAAY,CAAA;AAE7F,YAAA,OAAO,KAAK,MAAA,EAAQ,KAAA,CAAM,SAAS,IAAuB,CAAA,GAAK,OAA2B,YAAY,CAAA;AAAA,UACxG;AAAA,QACF;AAAA,QACA;AACE,UAAA,OAAO,IAAA,CAAK,QAAW,YAAY,CAAA;AAAA;AACvC,IACF,SAAS,GAAA,EAAK;AACZ,MAAA,OAAO,UAAU,GAAA,EAAK,MAAA,EAAQ,QAAQ,MAAA,KAAW,eAAA,GAAkB,SAAY,IAAI,CAAA;AAAA,IACrF;AAAA,EACF,CAAA;AACF","file":"fiscal-server.js","sourcesContent":["/**\n * Reads a tax document uploaded without JavaScript, on the server, with the same engine the browser uses. In memory\n * only: the bytes are never written to disk, never logged and are dropped when this returns.\n */\nimport type { IdentityDocumentSpec, Region } from '@asteby/storefront'\nimport { IdentityDocumentError, buildDocumentEvidence, catalogsFromRegion, extractIdentityDocument, readPdfText } from '@asteby/storefront/fiscal-document'\nimport { summarizeExtraction } from './extraction.js'\nimport type { FiscalExtractionSummary } from './types.js'\n\nexport type UploadLimits = { maxBytes: number; maxPages: number; timeoutMs: number }\n\n/** 5 MiB, 6 pages, 3 s: the same as the browser reader. */\nexport const UPLOAD_LIMITS: Readonly<UploadLimits> = Object.freeze({ maxBytes: 5 * 1024 * 1024, maxPages: 6, timeoutMs: 3000 })\n\nconst PDF_MAGIC = [0x25, 0x50, 0x44, 0x46, 0x2d] // %PDF-\n\nexport async function extractUploadedDocument(\n file: Blob,\n spec: IdentityDocumentSpec,\n region: Pick<Region, 'fiscal_id'>,\n options: { limits?: Partial<UploadLimits>; now?: Date } = {},\n): Promise<FiscalExtractionSummary> {\n const lim = { ...UPLOAD_LIMITS, ...options.limits }\n // The region can only lower the limits.\n const maxBytes = Math.min(lim.maxBytes, spec.max_bytes || lim.maxBytes)\n const maxPages = Math.min(lim.maxPages, spec.max_pages || lim.maxPages)\n if (file.size > maxBytes) throw new IdentityDocumentError('too_large', 'file over the size limit', { reason: 'bytes' })\n const bytes = new Uint8Array(await file.arrayBuffer())\n if (!PDF_MAGIC.every((b, i) => bytes[i] === b)) throw new IdentityDocumentError('not_pdf', 'not a PDF')\n // The engine is the only reader. QR cross-check needs a canvas, which a server does not have: `qr_match` stays null.\n const { text, sha256 } = await readPdfText(bytes, { maxBytes, maxPages, timeoutMs: lim.timeoutMs })\n const x = extractIdentityDocument(text, spec, {\n catalogs: region.fiscal_id ? catalogsFromRegion(region.fiscal_id) : {},\n ...(options.now ? { now: options.now } : {}),\n })\n return summarizeExtraction(x, buildDocumentEvidence(x, { sha256, source: 'pdf_server' }), region.fiscal_id?.regimes.map((r) => r.code))\n}\n","/**\n * Server side of the fiscal components (`@asteby/storefront-react/fiscal/server`): `loadFiscalContext` for the loader and\n * `createInvoiceAction` for the action. No React, no router: Web `Request`/`Response` only, so any framework mounts it.\n *\n * The browser never talks to OPS. The action uses the store's `sk_` client and the buyer's session token (read from the\n * store's cookie). A tax document uploaded WITHOUT JavaScript is read in memory here with the same engine the browser\n * uses, and discarded: no disk, no logs, no copy.\n */\nimport {\n isStorefrontError,\n repairPath,\n type FiscalDocumentEvidence,\n type FiscalIdentityInput,\n type InvoiceRequest,\n type InvoicingCapabilities,\n type Region,\n type StorefrontClient,\n} from '@asteby/storefront'\nimport { normalizeTaxId } from './format.js'\nimport { extractUploadedDocument, UPLOAD_LIMITS, type UploadLimits } from './server-extract.js'\nimport type { FiscalActionData, FiscalErrorCode, FiscalIntent, FiscalReason } from './types.js'\n\nexport type { FiscalActionData, FiscalErrorCode, FiscalIntent, FiscalReason } from './types.js'\nexport { UPLOAD_LIMITS, extractUploadedDocument } from './server-extract.js'\nexport type { UploadLimits } from './server-extract.js'\n\n// ---- context ---------------------------------------------------------------------------------------------------------\n\nexport type FiscalContext = {\n /** The store can invoice: it has a region with a fiscal catalog and `capabilities.invoicing.available`. */\n available: boolean\n /** Why `available` is what it is. Absent on contexts built by older code: read it as `unknown`. */\n reason?: FiscalReason\n region: Region | null\n invoicing: InvoicingCapabilities | null\n}\n\n/** What is assumed when the store cannot be asked: nothing is known (`unreachable`), nothing breaks. */\nexport const NO_FISCAL_CONTEXT: Readonly<FiscalContext> = Object.freeze({ available: false, reason: 'unreachable' as const, region: null, invoicing: null })\n\nconst FAILURE_TTL_MS = 5_000\nconst cache = new WeakMap<object, { at: number; ok: boolean; value: FiscalContext }>()\n\nexport type LoadFiscalContextOptions = {\n /** How long a good answer is reused per client (default 60 s). `0` disables the cache. */\n ttlMs?: number\n signal?: AbortSignal\n now?: () => number\n}\n\n/**\n * Asks the store for its region and what invoicing it offers, with a short cache per client. It NEVER throws: when OPS\n * does not answer (older server, outage, token without scope) it returns `NO_FISCAL_CONTEXT` and retries after a few\n * seconds, so a checkout without invoicing keeps working. `reason` says which of the cases it is. Use it as the loader:\n * `loader = () => loadFiscalContext(sf)`.\n */\nexport async function loadFiscalContext(sf: StorefrontClient, options: LoadFiscalContextOptions = {}): Promise<FiscalContext> {\n const now = options.now ?? Date.now\n const ttl = options.ttlMs ?? 60_000\n const hit = cache.get(sf)\n if (hit && now() - hit.at < (hit.ok ? ttl : FAILURE_TTL_MS)) return hit.value\n const opt = options.signal ? { signal: options.signal } : undefined\n const [region, caps] = await Promise.allSettled([sf.region(opt), sf.customer.capabilities(opt)])\n const reg = region.status === 'fulfilled' ? region.value : null\n const noRegion = region.status === 'rejected' && isStorefrontError(region.reason) && region.reason.status === 404\n const invoicing = caps.status === 'fulfilled' ? (caps.value?.invoicing ?? null) : null\n const ok = (region.status === 'fulfilled' || noRegion) && caps.status === 'fulfilled'\n const available = !!reg?.fiscal_id && !!invoicing?.available\n const reason: FiscalReason = available\n ? 'available'\n : !ok\n ? 'unreachable'\n : noRegion || !reg?.fiscal_id\n ? invoicing && !invoicing.available\n ? 'off'\n : 'no_region'\n : !invoicing\n ? 'unknown'\n : 'off'\n const value: FiscalContext = { available, reason, region: reg, invoicing: invoicing?.available ? invoicing : null }\n cache.set(sf, { at: now(), ok, value: ok || available ? value : NO_FISCAL_CONTEXT })\n return ok || available ? value : NO_FISCAL_CONTEXT\n}\n\n// ---- action ----------------------------------------------------------------------------------------------------------\n\n/** What the action needs of a cookie to read the buyer's session. React Router's `createCookie(...)` fits as it is. */\nexport type SessionCookie = { parse(header: string | null): unknown }\n\nexport type CreateInvoiceActionOptions = {\n storefront: StorefrontClient\n /** The store's cookie for the buyer session token (`createCookie('sf_customer', ...)`), to know who is asking. */\n customerCookie?: SessionCookie\n /** Alternative to `customerCookie`: returns the buyer's session token, or `null` for a guest. */\n readCustomerToken?: (request: Request) => string | null | undefined | Promise<string | null | undefined>\n /** Shopper IP for the per-shopper rate limit (see `clientIp` of the client). */\n clientIp?: (request: Request) => string | null | undefined\n /** Region + capabilities source, for tests or a shared cache (default `loadFiscalContext`). */\n context?: (sf: StorefrontClient) => Promise<FiscalContext>\n /** Limits of the no-JS upload (defaults: 5 MiB, 6 pages, 3 s). The region's own limits can only lower them. */\n limits?: Partial<UploadLimits>\n /** Clock, for the freshness check. */\n now?: () => Date\n /** Rejects cross-site posts (default `true`): `Origin` and `Sec-Fetch-Site` must match the site. */\n sameSiteOnly?: boolean\n /** Prefix of the form fields (default `invoice`). */\n namePrefix?: string\n}\n\nconst EVIDENCE_SOURCES = new Set(['pdf_client', 'pdf_server', 'qr', 'manual'])\n\n/** Reads the evidence the reader put in the form, keeping only the known keys (a form is untrusted input). */\nexport function parseEvidence(raw: unknown): FiscalDocumentEvidence | undefined {\n if (typeof raw !== 'string' || !raw || raw.length > 4096) return undefined\n let o: unknown\n try {\n o = JSON.parse(raw)\n } catch {\n return undefined\n }\n if (typeof o !== 'object' || o === null) return undefined\n const e = o as Record<string, unknown>\n if (typeof e.kind !== 'string' || !e.kind || typeof e.source !== 'string' || !EVIDENCE_SOURCES.has(e.source)) return undefined\n const str = (v: unknown, max: number) => (typeof v === 'string' && v.length <= max ? v : null)\n return {\n kind: e.kind.slice(0, 64),\n document_id: str(e.document_id, 64),\n issued_on: str(e.issued_on, 32),\n sha256: typeof e.sha256 === 'string' && /^[0-9a-f]{64}$/.test(e.sha256) ? e.sha256 : null,\n ...(typeof e.extractor === 'string' ? { extractor: e.extractor.slice(0, 64) } : {}),\n source: e.source as FiscalDocumentEvidence['source'],\n qr_match: typeof e.qr_match === 'boolean' ? e.qr_match : null,\n }\n}\n\n/** Reads the `invoice.*` fields of a form into the API's `InvoiceRequest`. */\nexport function invoiceFromForm(form: FormData, prefix = 'invoice'): InvoiceRequest {\n const text = (k: string) => {\n const v = form.get(`${prefix}.${k}`)\n return typeof v === 'string' ? v.trim() : ''\n }\n const evidence = parseEvidence(form.get(`${prefix}.document`))\n const out: InvoiceRequest = {\n requested: true,\n tax_id: normalizeTaxId(text('tax_id')),\n legal_name: text('legal_name'),\n tax_regime: text('tax_regime'),\n use: text('use'),\n postal_code: text('postal_code'),\n }\n if (evidence) out.document = evidence\n if (form.get(`${prefix}.save`)) out.save = true\n return out\n}\n\nconst fail = (intent: FiscalIntent | undefined, code: FiscalErrorCode, extra: Partial<FiscalActionData> = {}): FiscalActionData => ({\n ok: false,\n ...(intent ? { intent } : {}),\n ...extra,\n error: { code, ...(extra.error?.field ? { field: extra.error.field } : {}), ...(extra.error?.blocked ? { blocked: extra.error.blocked } : {}) },\n})\n\nconst LOG_EVERY_MS = 60_000\nlet lastLogAt = -Infinity\nfunction logMisconfigured(scopes: readonly string[]): void {\n const t = Date.now()\n if (t - lastLogAt < LOG_EVERY_MS) return\n lastLogAt = t\n console.error(\n `[@asteby/storefront-react] La credencial de la tienda no tiene permisos${scopes.length ? ` (${scopes.join(', ')})` : ''}; ` +\n `la facturación no funciona hasta repararlo en ${repairPath()}.`,\n )\n}\n\n/** `fiscal.tax_regime` (profile) and `invoice.tax_regime` (cart, order) both name the same form control. */\nconst formField = (field: string | undefined, prefix: string) => (field ? field.replace(/^(fiscal|invoice)\\./, `${prefix}.`) : undefined)\n\nfunction toFailure(err: unknown, intent: FiscalIntent, prefix: string, values?: Partial<FiscalActionData['values']>): FiscalActionData {\n const carry: Partial<FiscalActionData> = values ? { values } : {}\n if (!isStorefrontError(err)) return fail(intent, 'unavailable', carry)\n if (err.code === 'insufficient_scope') {\n logMisconfigured(err.requiredScopes)\n return fail(intent, 'store_misconfigured', carry)\n }\n const details = (err.details ?? {}) as { reason?: unknown; blocked_reason?: unknown }\n switch (err.status) {\n case 401:\n return fail(intent, 'unauthorized', carry)\n case 404:\n return fail(intent, 'not_found', carry)\n case 409: {\n const why = typeof details.reason === 'string' ? details.reason : typeof details.blocked_reason === 'string' ? details.blocked_reason : ''\n return fail(intent, why === 'already_checked_out' ? 'not_found' : 'blocked', { ...carry, ...(why ? { error: { code: 'blocked', blocked: why } } : {}) })\n }\n case 422:\n case 400: {\n const field = formField(err.field, prefix)\n return fail(intent, 'validation', {\n ...carry,\n ...(field ? { fieldErrors: { [field]: err.message || 'invalid' }, error: { code: 'validation', field } } : {}),\n })\n }\n case 429:\n return fail(intent, 'rate_limited', carry)\n case 403:\n return fail(intent, 'unsupported', carry)\n default:\n return fail(intent, 'unavailable', carry)\n }\n}\n\nfunction sameSite(request: Request): boolean {\n const site = request.headers.get('sec-fetch-site')\n if (site && site !== 'same-origin' && site !== 'same-site' && site !== 'none') return false\n const origin = request.headers.get('origin')\n if (!origin) return true\n try {\n const host = request.headers.get('x-forwarded-host') ?? request.headers.get('host') ?? new URL(request.url).host\n return new URL(origin).host === host.split(',')[0]!.trim()\n } catch {\n return false\n }\n}\n\n/**\n * The invoicing action: one `POST` form with an `intent` field.\n *\n * | intent | fields | does |\n * | ------------------ | --------------------------------------------------- | ---------------------------------------------------------- |\n * | `set-invoice` | cart_id, invoice.* (`invoice.want` unset withdraws) | `carts.setInvoice`: validates against the region BEFORE paying |\n * | `save-profile` | invoice.*, consent | `customer.update({ fiscal })`, needs a session and consent |\n * | `request-invoice` | order_id, invoice.* | `customer.requestInvoice` (invoice after the purchase) |\n * | `clear-profile` | - | `customer.update({ fiscal: null })` (the buyer's right to delete) |\n * | `extract-document` | document (file), document_kind? | reads the file in memory, discards it, `step: 'review'` |\n *\n * It never redirects: it returns a `FiscalActionData` for the page to render (`<InvoiceToggle data={...}>`), with 422s\n * mapped to the field (`fieldErrors['invoice.tax_regime']`). The tax id of the buyer is never logged.\n */\nexport function createInvoiceAction(options: CreateInvoiceActionOptions) {\n const { storefront: sf } = options\n const prefix = options.namePrefix ?? 'invoice'\n const loadContext = options.context ?? ((c: StorefrontClient) => loadFiscalContext(c))\n const limits: UploadLimits = { ...UPLOAD_LIMITS, ...options.limits }\n\n async function sessionToken(request: Request): Promise<string | null> {\n if (options.readCustomerToken) return (await options.readCustomerToken(request)) || null\n if (!options.customerCookie) return null\n const v = await options.customerCookie.parse(request.headers.get('cookie'))\n return typeof v === 'string' && v ? v : null\n }\n\n return async function invoiceAction({ request }: { request: Request }): Promise<FiscalActionData> {\n if (options.sameSiteOnly !== false && !sameSite(request)) return fail(undefined, 'unsupported')\n // A file bigger than the limit is refused before it is buffered.\n const declared = Number(request.headers.get('content-length') ?? 0)\n if (declared > limits.maxBytes + 256 * 1024) return fail('extract-document', 'too_large')\n const form = await request.formData().catch(() => null)\n if (!form) return fail(undefined, 'validation')\n const intent = form.get('intent') as FiscalIntent\n const ip = options.clientIp?.(request)\n const token = await sessionToken(request)\n const w = { ...(ip ? { clientIp: ip } : {}), ...(token ? { customerToken: token } : {}) }\n const invoice = invoiceFromForm(form, prefix)\n const echo = { tax_id: invoice.tax_id ?? '', legal_name: invoice.legal_name ?? '', postal_code: invoice.postal_code ?? '', tax_regime: invoice.tax_regime ?? '', use: invoice.use ?? '' }\n const text = (name: string) => {\n const v = form.get(name)\n return typeof v === 'string' ? v.trim() : ''\n }\n\n try {\n switch (intent) {\n case 'set-invoice': {\n const cartId = text('cart_id')\n if (!cartId) return fail(intent, 'validation')\n if (!form.get(`${prefix}.want`) && !form.get(`${prefix}.requested`)) {\n await sf.carts.setInvoice(cartId, null, w)\n return { ok: true, intent, step: 'cleared' }\n }\n await sf.carts.setInvoice(cartId, invoice, w)\n return { ok: true, intent, step: 'saved', values: echo }\n }\n case 'save-profile': {\n if (!token) return fail(intent, 'unauthorized', { values: echo })\n if (!form.get('consent')) return fail(intent, 'consent_required', { values: echo })\n const fiscal: FiscalIdentityInput = {\n tax_id: invoice.tax_id ?? '',\n legal_name: invoice.legal_name ?? '',\n tax_regime: invoice.tax_regime ?? '',\n postal_code: invoice.postal_code ?? '',\n invoice_use: invoice.use ?? '',\n ...(invoice.document ? { document: invoice.document } : {}),\n }\n await sf.customer.update({ fiscal }, w)\n return { ok: true, intent, step: 'saved', values: echo }\n }\n case 'clear-profile': {\n if (!token) return fail(intent, 'unauthorized')\n await sf.customer.update({ fiscal: null }, w)\n return { ok: true, intent, step: 'cleared' }\n }\n case 'request-invoice': {\n if (!token) return fail(intent, 'unauthorized', { values: echo })\n const orderId = text('order_id')\n if (!orderId) return fail(intent, 'validation')\n const result = await sf.customer.requestInvoice(orderId, invoice, w)\n return { ok: true, intent, step: result.status === 'issued' ? 'issued' : 'pending', result, ...(result.invoice ? { invoice: result.invoice } : {}) }\n }\n case 'extract-document': {\n const ctx = await loadContext(sf)\n const region = ctx.region\n const kind = text('document_kind')\n const specs = region?.fiscal_id?.documents ?? []\n const spec = (kind ? specs.find((d) => d.kind === kind) : specs[0]) ?? undefined\n if (!ctx.available || !region || !spec) return fail(intent, 'unsupported')\n const file = form.get('document')\n if (!file || typeof file === 'string' || file.size === 0) return fail(intent, 'not_pdf')\n try {\n const extraction = await extractUploadedDocument(file, spec, region, { limits, ...(options.now ? { now: options.now() } : {}) })\n return { ok: true, intent, step: 'review', extraction }\n } catch (err) {\n const code = (err as { code?: string } | null)?.code\n const known: FiscalErrorCode[] = ['not_pdf', 'too_large', 'no_text', 'password', 'unreadable']\n // Never log the error: it can carry text from the document.\n return fail(intent, known.includes(code as FiscalErrorCode) ? (code as FiscalErrorCode) : 'unreadable')\n }\n }\n default:\n return fail(undefined, 'validation')\n }\n } catch (err) {\n return toFailure(err, intent, prefix, intent === 'clear-profile' ? undefined : echo)\n }\n }\n}\n\nexport type InvoiceAction = ReturnType<typeof createInvoiceAction>\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/fiscal/server-extract.ts","../src/fiscal/server.ts"],"names":[],"mappings":";;;;AAYO,IAAM,aAAA,GAAwC,MAAA,CAAO,MAAA,CAAO,EAAE,QAAA,EAAU,CAAA,GAAI,IAAA,GAAO,IAAA,EAAM,QAAA,EAAU,CAAA,EAAG,SAAA,EAAW,GAAA,EAAM;AAE9H,IAAM,YAAY,CAAC,EAAA,EAAM,EAAA,EAAM,EAAA,EAAM,IAAM,EAAI,CAAA;AAE/C,eAAsB,wBACpB,IAAA,EACA,IAAA,EACA,MAAA,EACA,OAAA,GAA0D,EAAC,EACzB;AAClC,EAAA,MAAM,MAAM,EAAE,GAAG,aAAA,EAAe,GAAG,QAAQ,MAAA,EAAO;AAElD,EAAA,MAAM,QAAA,GAAW,KAAK,GAAA,CAAI,GAAA,CAAI,UAAU,IAAA,CAAK,SAAA,IAAa,IAAI,QAAQ,CAAA;AACtE,EAAA,MAAM,QAAA,GAAW,KAAK,GAAA,CAAI,GAAA,CAAI,UAAU,IAAA,CAAK,SAAA,IAAa,IAAI,QAAQ,CAAA;AACtE,EAAA,IAAI,IAAA,CAAK,IAAA,GAAO,QAAA,EAAU,MAAM,IAAI,qBAAA,CAAsB,WAAA,EAAa,0BAAA,EAA4B,EAAE,MAAA,EAAQ,OAAA,EAAS,CAAA;AACtH,EAAA,MAAM,QAAQ,IAAI,UAAA,CAAW,MAAM,IAAA,CAAK,aAAa,CAAA;AACrD,EAAA,IAAI,CAAC,SAAA,CAAU,KAA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,MAAM,IAAA,GAAO,IAAA,CAAK,GAAA,CAAI,UAAU,CAAA;AAChC,UAAA,IAAI,CAAC,IAAA,IAAQ,OAAO,IAAA,KAAS,QAAA,IAAY,IAAA,CAAK,IAAA,KAAS,CAAA,EAAG,OAAO,IAAA,CAAK,MAAA,EAAQ,SAAS,CAAA;AACvF,UAAA,IAAI;AACF,YAAA,MAAM,aAAa,MAAM,uBAAA,CAAwB,MAAM,IAAA,EAAM,MAAA,EAAQ,EAAE,MAAA,EAAQ,GAAI,QAAQ,GAAA,GAAM,EAAE,KAAK,OAAA,CAAQ,GAAA,IAAM,GAAI,IAAK,CAAA;AAC/H,YAAA,OAAO,EAAE,EAAA,EAAI,IAAA,EAAM,MAAA,EAAQ,IAAA,EAAM,UAAU,UAAA,EAAW;AAAA,UACxD,SAAS,GAAA,EAAK;AACZ,YAAA,MAAM,OAAQ,GAAA,EAAkC,IAAA;AAChD,YAAA,MAAM,QAA2B,CAAC,SAAA,EAAW,WAAA,EAAa,SAAA,EAAW,YAAY,YAAY,CAAA;AAE7F,YAAA,OAAO,KAAK,MAAA,EAAQ,KAAA,CAAM,SAAS,IAAuB,CAAA,GAAK,OAA2B,YAAY,CAAA;AAAA,UACxG;AAAA,QACF;AAAA,QACA;AACE,UAAA,OAAO,IAAA,CAAK,QAAW,YAAY,CAAA;AAAA;AACvC,IACF,SAAS,GAAA,EAAK;AACZ,MAAA,OAAO,UAAU,GAAA,EAAK,MAAA,EAAQ,QAAQ,MAAA,KAAW,eAAA,GAAkB,SAAY,IAAI,CAAA;AAAA,IACrF;AAAA,EACF,CAAA;AACF","file":"fiscal-server.js","sourcesContent":["/**\n * Reads a tax document uploaded without JavaScript, on the server, with the same engine the browser uses. In memory\n * only: the bytes are never written to disk, never logged and are dropped when this returns.\n */\nimport type { IdentityDocumentSpec, Region } from '@asteby/storefront'\nimport { IdentityDocumentError, buildDocumentEvidence, catalogsFromRegion, extractIdentityDocument, readPdfText } from '@asteby/storefront/fiscal-document'\nimport { summarizeExtraction } from './extraction.js'\nimport type { FiscalExtractionSummary } from './types.js'\n\nexport type UploadLimits = { maxBytes: number; maxPages: number; timeoutMs: number }\n\n/** 5 MiB, 6 pages, 3 s: the same as the browser reader. */\nexport const UPLOAD_LIMITS: Readonly<UploadLimits> = Object.freeze({ maxBytes: 5 * 1024 * 1024, maxPages: 6, timeoutMs: 3000 })\n\nconst PDF_MAGIC = [0x25, 0x50, 0x44, 0x46, 0x2d] // %PDF-\n\nexport async function extractUploadedDocument(\n file: Blob,\n spec: IdentityDocumentSpec,\n region: Pick<Region, 'fiscal_id'>,\n options: { limits?: Partial<UploadLimits>; now?: Date } = {},\n): Promise<FiscalExtractionSummary> {\n const lim = { ...UPLOAD_LIMITS, ...options.limits }\n // The region can only lower the limits.\n const maxBytes = Math.min(lim.maxBytes, spec.max_bytes || lim.maxBytes)\n const maxPages = Math.min(lim.maxPages, spec.max_pages || lim.maxPages)\n if (file.size > maxBytes) throw new IdentityDocumentError('too_large', 'file over the size limit', { reason: 'bytes' })\n const bytes = new Uint8Array(await file.arrayBuffer())\n if (!PDF_MAGIC.every((b, i) => bytes[i] === b)) throw new IdentityDocumentError('not_pdf', 'not a PDF')\n // The engine is the only reader. QR cross-check needs a canvas, which a server does not have: `qr_match` stays null.\n const { text, sha256 } = await readPdfText(bytes, { maxBytes, maxPages, timeoutMs: lim.timeoutMs })\n const x = extractIdentityDocument(text, spec, {\n catalogs: region.fiscal_id ? catalogsFromRegion(region.fiscal_id) : {},\n ...(options.now ? { now: options.now } : {}),\n })\n return summarizeExtraction(x, buildDocumentEvidence(x, { sha256, source: 'pdf_server' }), region.fiscal_id?.regimes.map((r) => r.code))\n}\n","/**\n * Server side of the fiscal components (`@asteby/storefront-react/fiscal/server`): `loadFiscalContext` for the loader and\n * `createInvoiceAction` for the action. No React, no router: Web `Request`/`Response` only, so any framework mounts it.\n *\n * The browser never talks to OPS. The action uses the store's `sk_` client and the buyer's session token (read from the\n * store's cookie). A tax document uploaded WITHOUT JavaScript is read in memory here with the same engine the browser\n * uses, and discarded: no disk, no logs, no copy.\n */\nimport {\n isStorefrontError,\n repairPath,\n type FiscalDocumentEvidence,\n type FiscalIdentityInput,\n type InvoiceRequest,\n type InvoicingCapabilities,\n type Region,\n type StorefrontClient,\n} from '@asteby/storefront'\nimport { normalizeTaxId } from './format.js'\nimport { extractUploadedDocument, UPLOAD_LIMITS, type UploadLimits } from './server-extract.js'\nimport type { FiscalActionData, FiscalErrorCode, FiscalIntent, FiscalReason } from './types.js'\n\nexport type { FiscalActionData, FiscalErrorCode, FiscalIntent, FiscalReason } from './types.js'\nexport { UPLOAD_LIMITS, extractUploadedDocument } from './server-extract.js'\nexport type { UploadLimits } from './server-extract.js'\n\n// ---- context ---------------------------------------------------------------------------------------------------------\n\nexport type FiscalContext = {\n /** The store can invoice: it has a region with a fiscal catalog and `capabilities.invoicing.available`. */\n available: boolean\n /** Why `available` is what it is. Absent on contexts built by older code: read it as `unknown`. */\n reason?: FiscalReason\n region: Region | null\n invoicing: InvoicingCapabilities | null\n}\n\n/** What is assumed when the store cannot be asked: nothing is known (`unreachable`), nothing breaks. */\nexport const NO_FISCAL_CONTEXT: Readonly<FiscalContext> = Object.freeze({ available: false, reason: 'unreachable' as const, region: null, invoicing: null })\n\nconst FAILURE_TTL_MS = 5_000\nconst cache = new WeakMap<object, { at: number; ok: boolean; value: FiscalContext }>()\n\nexport type LoadFiscalContextOptions = {\n /** How long a good answer is reused per client (default 60 s). `0` disables the cache. */\n ttlMs?: number\n signal?: AbortSignal\n now?: () => number\n}\n\n/**\n * Asks the store for its region and what invoicing it offers, with a short cache per client. It NEVER throws: when OPS\n * does not answer (older server, outage, token without scope) it returns `NO_FISCAL_CONTEXT` and retries after a few\n * seconds, so a checkout without invoicing keeps working. `reason` says which of the cases it is. Use it as the loader:\n * `loader = () => loadFiscalContext(sf)`.\n */\nexport async function loadFiscalContext(sf: StorefrontClient, options: LoadFiscalContextOptions = {}): Promise<FiscalContext> {\n const now = options.now ?? Date.now\n const ttl = options.ttlMs ?? 60_000\n const hit = cache.get(sf)\n if (hit && now() - hit.at < (hit.ok ? ttl : FAILURE_TTL_MS)) return hit.value\n const opt = options.signal ? { signal: options.signal } : undefined\n const [region, caps] = await Promise.allSettled([sf.region(opt), sf.customer.capabilities(opt)])\n const reg = region.status === 'fulfilled' ? region.value : null\n const noRegion = region.status === 'rejected' && isStorefrontError(region.reason) && region.reason.status === 404\n const invoicing = caps.status === 'fulfilled' ? (caps.value?.invoicing ?? null) : null\n const ok = (region.status === 'fulfilled' || noRegion) && caps.status === 'fulfilled'\n const available = !!reg?.fiscal_id && !!invoicing?.available\n const reason: FiscalReason = available\n ? 'available'\n : !ok\n ? 'unreachable'\n : noRegion || !reg?.fiscal_id\n ? invoicing && !invoicing.available\n ? 'off'\n : 'no_region'\n : !invoicing\n ? 'unknown'\n : 'off'\n const value: FiscalContext = { available, reason, region: reg, invoicing: invoicing?.available ? invoicing : null }\n cache.set(sf, { at: now(), ok, value: ok || available ? value : NO_FISCAL_CONTEXT })\n return ok || available ? value : NO_FISCAL_CONTEXT\n}\n\n// ---- action ----------------------------------------------------------------------------------------------------------\n\n/** What the action needs of a cookie to read the buyer's session. React Router's `createCookie(...)` fits as it is. */\nexport type SessionCookie = { parse(header: string | null): unknown }\n\nexport type CreateInvoiceActionOptions = {\n storefront: StorefrontClient\n /** The store's cookie for the buyer session token (`createCookie('sf_customer', ...)`), to know who is asking. */\n customerCookie?: SessionCookie\n /** Alternative to `customerCookie`: returns the buyer's session token, or `null` for a guest. */\n readCustomerToken?: (request: Request) => string | null | undefined | Promise<string | null | undefined>\n /** Shopper IP for the per-shopper rate limit (see `clientIp` of the client). */\n clientIp?: (request: Request) => string | null | undefined\n /** Region + capabilities source, for tests or a shared cache (default `loadFiscalContext`). */\n context?: (sf: StorefrontClient) => Promise<FiscalContext>\n /** Limits of the no-JS upload (defaults: 5 MiB, 6 pages, 3 s). The region's own limits can only lower them. */\n limits?: Partial<UploadLimits>\n /** Clock, for the freshness check. */\n now?: () => Date\n /** Rejects cross-site posts (default `true`): `Origin` and `Sec-Fetch-Site` must match the site. */\n sameSiteOnly?: boolean\n /** Prefix of the form fields (default `invoice`). */\n namePrefix?: string\n}\n\nconst EVIDENCE_SOURCES = new Set(['pdf_client', 'pdf_server', 'qr', 'manual'])\n\n/** Reads the evidence the reader put in the form, keeping only the known keys (a form is untrusted input). */\nexport function parseEvidence(raw: unknown): FiscalDocumentEvidence | undefined {\n if (typeof raw !== 'string' || !raw || raw.length > 4096) return undefined\n let o: unknown\n try {\n o = JSON.parse(raw)\n } catch {\n return undefined\n }\n if (typeof o !== 'object' || o === null) return undefined\n const e = o as Record<string, unknown>\n if (typeof e.kind !== 'string' || !e.kind || typeof e.source !== 'string' || !EVIDENCE_SOURCES.has(e.source)) return undefined\n const str = (v: unknown, max: number) => (typeof v === 'string' && v.length <= max ? v : null)\n return {\n kind: e.kind.slice(0, 64),\n document_id: str(e.document_id, 64),\n issued_on: str(e.issued_on, 32),\n sha256: typeof e.sha256 === 'string' && /^[0-9a-f]{64}$/.test(e.sha256) ? e.sha256 : null,\n ...(typeof e.extractor === 'string' ? { extractor: e.extractor.slice(0, 64) } : {}),\n source: e.source as FiscalDocumentEvidence['source'],\n qr_match: typeof e.qr_match === 'boolean' ? e.qr_match : null,\n }\n}\n\n/** Reads the `invoice.*` fields of a form into the API's `InvoiceRequest`. */\nexport function invoiceFromForm(form: FormData, prefix = 'invoice'): InvoiceRequest {\n const text = (k: string) => {\n const v = form.get(`${prefix}.${k}`)\n return typeof v === 'string' ? v.trim() : ''\n }\n const evidence = parseEvidence(form.get(`${prefix}.document`))\n const out: InvoiceRequest = {\n requested: true,\n tax_id: normalizeTaxId(text('tax_id')),\n legal_name: text('legal_name'),\n tax_regime: text('tax_regime'),\n use: text('use'),\n postal_code: text('postal_code'),\n }\n if (evidence) out.document = evidence\n if (form.get(`${prefix}.save`)) out.save = true\n return out\n}\n\nconst fail = (intent: FiscalIntent | undefined, code: FiscalErrorCode, extra: Partial<FiscalActionData> = {}): FiscalActionData => ({\n ok: false,\n ...(intent ? { intent } : {}),\n ...extra,\n error: { code, ...(extra.error?.field ? { field: extra.error.field } : {}), ...(extra.error?.blocked ? { blocked: extra.error.blocked } : {}) },\n})\n\nconst LOG_EVERY_MS = 60_000\nlet lastLogAt = -Infinity\nfunction logMisconfigured(scopes: readonly string[]): void {\n const t = Date.now()\n if (t - lastLogAt < LOG_EVERY_MS) return\n lastLogAt = t\n console.error(\n `[@asteby/storefront-react] La credencial de la tienda no tiene permisos${scopes.length ? ` (${scopes.join(', ')})` : ''}; ` +\n `la facturación no funciona hasta repararlo en ${repairPath()}.`,\n )\n}\n\n/**\n * The form control a server field belongs to. The server names a field by what it validated (`fiscal.invoice_use`,\n * `invoice.requested`, an alias like `razon_social`), the form names the control (`invoice.use`): a field that maps to no\n * control returns `undefined` so it is NOT reported as a marked field (the buyer would see \"check the marked fields\" with\n * nothing marked).\n */\nconst CONTROL_OF: Record<string, string> = {\n tax_id: 'tax_id',\n rfc: 'tax_id',\n legal_name: 'legal_name',\n razon_social: 'legal_name',\n postal_code: 'postal_code',\n zip: 'postal_code',\n cp: 'postal_code',\n tax_regime: 'tax_regime',\n regimen: 'tax_regime',\n use: 'use',\n invoice_use: 'use',\n cfdi_use: 'use',\n uso_cfdi: 'use',\n}\n\n/** `fiscal.tax_regime` (profile) and `invoice.tax_regime` (cart, order) both name the same form control. */\nexport const formField = (field: string | undefined, prefix: string) => {\n if (!field) return undefined\n const control = CONTROL_OF[field.replace(/^(fiscal|invoice)\\./, '')]\n return control ? `${prefix}.${control}` : undefined\n}\n\n/**\n * The body of `request-invoice`: only the fiscal values, with the names every server version accepts (`invoice_use`, not\n * `use`; no `requested` flag, an order that is being invoiced is requested by definition; no evidence object, the order\n * keeps the data it was validated with). An OPS that predates contract 1.9 answered 400 `requested: unknown field` to the\n * full `InvoiceRequest` the checkout sends.\n */\nexport function orderInvoiceBody(invoice: InvoiceRequest): InvoiceRequest {\n const { requested: _requested, document: _document, use, ...rest } = invoice\n return { ...rest, ...(use ? { invoice_use: use } : {}) }\n}\n\nfunction toFailure(err: unknown, intent: FiscalIntent, prefix: string, values?: Partial<FiscalActionData['values']>): FiscalActionData {\n const carry: Partial<FiscalActionData> = values ? { values } : {}\n if (!isStorefrontError(err)) return fail(intent, 'unavailable', carry)\n if (err.code === 'insufficient_scope') {\n logMisconfigured(err.requiredScopes)\n return fail(intent, 'store_misconfigured', carry)\n }\n const details = (err.details ?? {}) as { reason?: unknown; blocked_reason?: unknown }\n switch (err.status) {\n case 401:\n return fail(intent, 'unauthorized', carry)\n case 404:\n return fail(intent, 'not_found', carry)\n case 409: {\n const why = typeof details.reason === 'string' ? details.reason : typeof details.blocked_reason === 'string' ? details.blocked_reason : ''\n return fail(intent, why === 'already_checked_out' ? 'not_found' : 'blocked', { ...carry, ...(why ? { error: { code: 'blocked', blocked: why } } : {}) })\n }\n case 422:\n case 400: {\n const field = formField(err.field, prefix)\n return fail(intent, 'validation', {\n ...carry,\n ...(field ? { fieldErrors: { [field]: err.message || 'invalid' }, error: { code: 'validation', field } } : {}),\n })\n }\n case 429:\n return fail(intent, 'rate_limited', carry)\n case 403:\n return fail(intent, 'unsupported', carry)\n default:\n return fail(intent, 'unavailable', carry)\n }\n}\n\nfunction sameSite(request: Request): boolean {\n const site = request.headers.get('sec-fetch-site')\n if (site && site !== 'same-origin' && site !== 'same-site' && site !== 'none') return false\n const origin = request.headers.get('origin')\n if (!origin) return true\n try {\n const host = request.headers.get('x-forwarded-host') ?? request.headers.get('host') ?? new URL(request.url).host\n return new URL(origin).host === host.split(',')[0]!.trim()\n } catch {\n return false\n }\n}\n\n/**\n * The invoicing action: one `POST` form with an `intent` field.\n *\n * | intent | fields | does |\n * | ------------------ | --------------------------------------------------- | ---------------------------------------------------------- |\n * | `set-invoice` | cart_id, invoice.* (`invoice.want` unset withdraws) | `carts.setInvoice`: validates against the region BEFORE paying |\n * | `save-profile` | invoice.*, consent | `customer.update({ fiscal })`, needs a session and consent |\n * | `request-invoice` | order_id, invoice.* | `customer.requestInvoice` (invoice after the purchase) |\n * | `clear-profile` | - | `customer.update({ fiscal: null })` (the buyer's right to delete) |\n * | `extract-document` | document (file), document_kind? | reads the file in memory, discards it, `step: 'review'` |\n *\n * It never redirects: it returns a `FiscalActionData` for the page to render (`<InvoiceToggle data={...}>`), with 422s\n * mapped to the field (`fieldErrors['invoice.tax_regime']`). The tax id of the buyer is never logged.\n */\nexport function createInvoiceAction(options: CreateInvoiceActionOptions) {\n const { storefront: sf } = options\n const prefix = options.namePrefix ?? 'invoice'\n const loadContext = options.context ?? ((c: StorefrontClient) => loadFiscalContext(c))\n const limits: UploadLimits = { ...UPLOAD_LIMITS, ...options.limits }\n\n async function sessionToken(request: Request): Promise<string | null> {\n if (options.readCustomerToken) return (await options.readCustomerToken(request)) || null\n if (!options.customerCookie) return null\n const v = await options.customerCookie.parse(request.headers.get('cookie'))\n return typeof v === 'string' && v ? v : null\n }\n\n return async function invoiceAction({ request }: { request: Request }): Promise<FiscalActionData> {\n if (options.sameSiteOnly !== false && !sameSite(request)) return fail(undefined, 'unsupported')\n // A file bigger than the limit is refused before it is buffered.\n const declared = Number(request.headers.get('content-length') ?? 0)\n if (declared > limits.maxBytes + 256 * 1024) return fail('extract-document', 'too_large')\n const form = await request.formData().catch(() => null)\n if (!form) return fail(undefined, 'validation')\n const intent = form.get('intent') as FiscalIntent\n const ip = options.clientIp?.(request)\n const token = await sessionToken(request)\n const w = { ...(ip ? { clientIp: ip } : {}), ...(token ? { customerToken: token } : {}) }\n const invoice = invoiceFromForm(form, prefix)\n const echo = { tax_id: invoice.tax_id ?? '', legal_name: invoice.legal_name ?? '', postal_code: invoice.postal_code ?? '', tax_regime: invoice.tax_regime ?? '', use: invoice.use ?? '' }\n const text = (name: string) => {\n const v = form.get(name)\n return typeof v === 'string' ? v.trim() : ''\n }\n\n try {\n switch (intent) {\n case 'set-invoice': {\n const cartId = text('cart_id')\n if (!cartId) return fail(intent, 'validation')\n if (!form.get(`${prefix}.want`) && !form.get(`${prefix}.requested`)) {\n await sf.carts.setInvoice(cartId, null, w)\n return { ok: true, intent, step: 'cleared' }\n }\n await sf.carts.setInvoice(cartId, invoice, w)\n return { ok: true, intent, step: 'saved', values: echo }\n }\n case 'save-profile': {\n if (!token) return fail(intent, 'unauthorized', { values: echo })\n if (!form.get('consent')) return fail(intent, 'consent_required', { values: echo })\n const fiscal: FiscalIdentityInput = {\n tax_id: invoice.tax_id ?? '',\n legal_name: invoice.legal_name ?? '',\n tax_regime: invoice.tax_regime ?? '',\n postal_code: invoice.postal_code ?? '',\n invoice_use: invoice.use ?? '',\n ...(invoice.document ? { document: invoice.document } : {}),\n }\n await sf.customer.update({ fiscal }, w)\n return { ok: true, intent, step: 'saved', values: echo }\n }\n case 'clear-profile': {\n if (!token) return fail(intent, 'unauthorized')\n await sf.customer.update({ fiscal: null }, w)\n return { ok: true, intent, step: 'cleared' }\n }\n case 'request-invoice': {\n if (!token) return fail(intent, 'unauthorized', { values: echo })\n const orderId = text('order_id')\n if (!orderId) return fail(intent, 'validation')\n const result = await sf.customer.requestInvoice(orderId, orderInvoiceBody(invoice), w)\n return { ok: true, intent, step: result.status === 'issued' ? 'issued' : 'pending', result, ...(result.invoice ? { invoice: result.invoice } : {}) }\n }\n case 'extract-document': {\n const ctx = await loadContext(sf)\n const region = ctx.region\n const kind = text('document_kind')\n const specs = region?.fiscal_id?.documents ?? []\n const spec = (kind ? specs.find((d) => d.kind === kind) : specs[0]) ?? undefined\n if (!ctx.available || !region || !spec) return fail(intent, 'unsupported')\n const file = form.get('document')\n if (!file || typeof file === 'string' || file.size === 0) return fail(intent, 'not_pdf')\n try {\n const extraction = await extractUploadedDocument(file, spec, region, { limits, ...(options.now ? { now: options.now() } : {}) })\n return { ok: true, intent, step: 'review', extraction }\n } catch (err) {\n const code = (err as { code?: string } | null)?.code\n const known: FiscalErrorCode[] = ['not_pdf', 'too_large', 'no_text', 'password', 'unreadable']\n // Never log the error: it can carry text from the document.\n return fail(intent, known.includes(code as FiscalErrorCode) ? (code as FiscalErrorCode) : 'unreadable')\n }\n }\n default:\n return fail(undefined, 'validation')\n }\n } catch (err) {\n return toFailure(err, intent, prefix, intent === 'clear-profile' ? undefined : echo)\n }\n }\n}\n\nexport type InvoiceAction = ReturnType<typeof createInvoiceAction>\n"]}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { docText, methodName, formatDate, voucherFields, voucherSteps, formatMoney } from './chunk-LIUEQENV.js';
|
|
2
|
+
import { barcodeGeometry } from './chunk-BXKV2KWZ.js';
|
|
3
|
+
|
|
4
|
+
// src/account/voucher-document.ts
|
|
5
|
+
function esc(s) {
|
|
6
|
+
return s.replace(/[&<>"']/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" })[c]);
|
|
7
|
+
}
|
|
8
|
+
var CSS = `
|
|
9
|
+
@page { size: A4; margin: 18mm; }
|
|
10
|
+
* { box-sizing: border-box; }
|
|
11
|
+
body { margin: 0; font: 14px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; color: #111; background: #fff; }
|
|
12
|
+
main { max-width: 174mm; margin: 0 auto; padding: 24px 16px; }
|
|
13
|
+
h1 { font-size: 22px; margin: 0; }
|
|
14
|
+
.muted { color: #555; }
|
|
15
|
+
.head { display: flex; justify-content: space-between; align-items: flex-start; gap: 16px; border-bottom: 3px solid #111; padding-bottom: 12px; }
|
|
16
|
+
.amount { margin: 0; font-size: 28px; font-weight: 700; text-align: right; }
|
|
17
|
+
.box { border: 1px solid #111; padding: 14px 16px; margin-top: 18px; }
|
|
18
|
+
.label { margin: 0; font-weight: 400; font-size: 12px; letter-spacing: .08em; text-transform: uppercase; color: #555; }
|
|
19
|
+
.value { margin: 0; font: 700 22px/1.3 ui-monospace, "SF Mono", Menlo, Consolas, monospace; word-break: break-all; letter-spacing: .04em; }
|
|
20
|
+
.bar { text-align: center; }
|
|
21
|
+
.bar svg { max-width: 100%; height: auto; }
|
|
22
|
+
.note { margin-top: 18px; font-size: 12px; }
|
|
23
|
+
.pre { white-space: pre-line; }
|
|
24
|
+
.steps-title { font-size: 16px; margin: 18px 0 0; }
|
|
25
|
+
.steps { margin: 6px 0 0; padding-left: 20px; }
|
|
26
|
+
.bar { margin: 18px 0 0; }
|
|
27
|
+
.actions { margin-top: 24px; display: flex; flex-wrap: wrap; gap: 12px; align-items: center; }
|
|
28
|
+
.btn { display: inline-block; font: inherit; padding: 10px 18px; border: 2px solid #111; background: #111; color: #fff; cursor: pointer; text-decoration: none; }
|
|
29
|
+
.btn.alt { background: #fff; color: #111; }
|
|
30
|
+
.btn:focus-visible { outline: 3px solid #1a56db; outline-offset: 2px; }
|
|
31
|
+
@media print { .actions { display: none; } main { padding: 0; } }
|
|
32
|
+
`;
|
|
33
|
+
function barcodeSvg(value, format, label) {
|
|
34
|
+
const g = barcodeGeometry(value, { ...format ? { format } : {} });
|
|
35
|
+
if (!g) return null;
|
|
36
|
+
const rects = g.rects.map((r) => `<rect x="${r.x}" y="0" width="${r.w}" height="${g.height}"/>`).join("");
|
|
37
|
+
return `<svg viewBox="0 0 ${g.width} ${g.height}" width="${g.width}" height="${g.height}" role="img" aria-label="${esc(label)} ${esc(value)}" shape-rendering="crispEdges" fill="#000">${rects}</svg>`;
|
|
38
|
+
}
|
|
39
|
+
function renderVoucherDocument(v, opts = {}) {
|
|
40
|
+
const locale = opts.locale ?? "es-MX";
|
|
41
|
+
const t = docText(locale);
|
|
42
|
+
const shop = opts.shopName || "";
|
|
43
|
+
const method = methodName(v, t);
|
|
44
|
+
const code = v.barcode;
|
|
45
|
+
const svg = code ? barcodeSvg(code, v.barcode_format, t.barcode) : null;
|
|
46
|
+
const expires = formatDate(v.expires_at, locale, opts.timeZone);
|
|
47
|
+
const fields = voucherFields(v, t);
|
|
48
|
+
const steps = voucherSteps(v, t);
|
|
49
|
+
const subtitle = [shop, method, opts.number ? `${t.order} ${opts.number}` : ""].filter(Boolean).map(esc).join(" \xB7 ");
|
|
50
|
+
const actions = opts.printButton || opts.pdfHref;
|
|
51
|
+
return `<!doctype html>
|
|
52
|
+
<html lang="${esc(locale)}"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1">
|
|
53
|
+
<meta name="robots" content="noindex"><title>${esc(t.title)}${shop ? ` - ${esc(shop)}` : ""}</title><style>${CSS}</style></head>
|
|
54
|
+
<body><main aria-labelledby="t">
|
|
55
|
+
<header class="head"><div><h1 id="t">${esc(t.title)}</h1><p class="muted" data-voucher-method="${esc(v.method ?? "")}">${subtitle}</p></div>
|
|
56
|
+
${v.amount ? `<div><p class="label" style="text-align:right">${esc(t.total)}</p><p class="amount">${esc(formatMoney(v.amount, locale))}</p></div>` : ""}</header>
|
|
57
|
+
${fields.map((f) => `<section class="box" data-field="${f.key}"><h2 class="label">${esc(f.label)}</h2><p class="value">${esc(f.value)}</p></section>`).join("")}
|
|
58
|
+
${svg ? `<figure class="bar">${svg}<figcaption class="muted" style="font-family:ui-monospace,monospace">${esc(code)}</figcaption></figure>` : ""}
|
|
59
|
+
${expires ? `<p><strong>${esc(t.expires)}:</strong> ${esc(expires.replace(/\.$/, ""))}. ${esc(t.expiresNote)}</p>` : ""}
|
|
60
|
+
<h2 class="steps-title">${esc(t.stepsTitle)}</h2>
|
|
61
|
+
<ol class="steps">${steps.map((s) => `<li>${esc(s)}</li>`).join("")}</ol>
|
|
62
|
+
${v.instructions ? `<p class="note pre muted">${esc(v.instructions)}</p>` : ""}
|
|
63
|
+
<p class="note muted">${esc(t.footer)}</p>
|
|
64
|
+
${actions ? `<div class="actions" role="group" aria-label="${esc(t.title)}">${opts.pdfHref ? `<a class="btn" href="${esc(opts.pdfHref)}" download="ficha-de-pago.pdf" data-voucher-pdf>${esc(t.downloadPdf)}</a>` : ""}${opts.printButton ? `<button type="button" class="btn alt" id="print" hidden>${esc(t.print)}</button><noscript><p class="note muted">${esc(t.noScript)}</p></noscript>` : ""}</div>` : ""}
|
|
65
|
+
</main>
|
|
66
|
+
${opts.printButton || opts.autoPrint ? `<script${opts.nonce ? ` nonce="${esc(opts.nonce)}"` : ""}>(function(){var b=document.getElementById("print");if(b){b.hidden=false;b.addEventListener("click",function(){window.print()})}${opts.autoPrint ? 'window.addEventListener("load",function(){setTimeout(function(){window.print()},300)});' : ""}})();</script>` : ""}
|
|
67
|
+
</body></html>`;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export { esc, renderVoucherDocument };
|
|
71
|
+
//# sourceMappingURL=voucher-document-FWH4DQ5N.js.map
|
|
72
|
+
//# sourceMappingURL=voucher-document-FWH4DQ5N.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/account/voucher-document.ts"],"names":[],"mappings":";;;;AAQO,SAAS,IAAI,CAAA,EAAmB;AACrC,EAAA,OAAO,EAAE,OAAA,CAAQ,UAAA,EAAY,CAAC,CAAA,KAAA,CAAO,EAAE,KAAK,OAAA,EAAS,GAAA,EAAK,MAAA,EAAQ,GAAA,EAAK,QAAQ,GAAA,EAAK,QAAA,EAAU,KAAK,OAAA,EAAQ,EAAG,CAAC,CAAW,CAAA;AAC5H;AAmBA,IAAM,GAAA,GAAM;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,CAAA;AA0BZ,SAAS,UAAA,CAAW,KAAA,EAAe,MAAA,EAAuC,KAAA,EAA8B;AACtG,EAAA,MAAM,CAAA,GAAI,eAAA,CAAgB,KAAA,EAAO,EAAE,GAAI,MAAA,GAAS,EAAE,MAAA,EAAO,GAAI,EAAC,EAAI,CAAA;AAClE,EAAA,IAAI,CAAC,GAAG,OAAO,IAAA;AACf,EAAA,MAAM,QAAQ,CAAA,CAAE,KAAA,CAAM,IAAI,CAAC,CAAA,KAAM,YAAY,CAAA,CAAE,CAAC,CAAA,eAAA,E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AAC,SAAS,EAAE;AAAA,wBAAA,EAC7F,GAAA,CAAI,CAAA,CAAE,UAAU,CAAC,CAAA;AAAA,kBAAA,EACvB,KAAA,CAAM,GAAA,CAAI,CAAC,CAAA,KAAM,CAAA,IAAA,EAAO,GAAA,CAAI,CAAC,CAAC,CAAA,KAAA,CAAO,CAAA,CAAE,IAAA,CAAK,EAAE,CAAC,CAAA;AAAA,EACjE,CAAA,CAAE,eAAe,CAAA,0BAAA,EAA6B,GAAA,CAAI,EAAE,YAAY,CAAC,SAAS,EAAE;AAAA,sBAAA,EACtD,GAAA,CAAI,CAAA,CAAE,MAAM,CAAC,CAAA;AAAA,EAEnC,UACI,CAAA,8CAAA,EAAiD,GAAA,CAAI,CAAA,CAAE,KAAK,CAAC,CAAA,EAAA,EAAK,IAAA,CAAK,OAAA,GAAU,CAAA,qBAAA,EAAwB,IAAI,IAAA,CAAK,OAAO,CAAC,CAAA,gDAAA,EAAmD,IAAI,CAAA,CAAE,WAAW,CAAC,CAAA,IAAA,CAAA,GAAS,EAAE,CAAA,EAAG,IAAA,CAAK,WAAA,GAAc,CAAA,wDAAA,EAA2D,IAAI,CAAA,CAAE,KAAK,CAAC,CAAA,yCAAA,EAA4C,IAAI,CAAA,CAAE,QAAQ,CAAC,CAAA,eAAA,CAAA,GAAoB,EAAE,WACxX,EACN;AAAA;AAAA,EAEE,IAAA,CAAK,eAAe,IAAA,CAAK,SAAA,GAAY,UAAU,IAAA,CAAK,KAAA,GAAQ,WAAW,GAAA,CAAI,IAAA,CAAK,KAAK,CAAC,CAAA,CAAA,CAAA,GAAM,EAAE,CAAA,gIAAA,EAAmI,IAAA,CAAK,YAAY,yFAAA,GAA4F,EAAE,mBAAmB,EAAE;AAAA,cAAA,CAAA;AAEvW","file":"voucher-document-FWH4DQ5N.js","sourcesContent":["/**\n * Printable voucher (A4 HTML) that does not depend on any payment gateway: built only from the voucher data OPS keeps.\n * Loaded on demand (the buyer taps print/download) or by `pendingVoucherResponse` on the server; plain text, no React.\n * Everything external passes through `esc` before it enters the HTML.\n */\nimport { barcodeGeometry, type VoucherView } from './voucher.js'\nimport { docText, formatDate, formatMoney, methodName, voucherFields, voucherSteps } from './voucher-text.js'\n\nexport function esc(s: string): string {\n return s.replace(/[&<>\"']/g, (c) => ({ '&': '&', '<': '<', '>': '>', '\"': '"', \"'\": ''' })[c] as string)\n}\n\nexport type VoucherDocumentOptions = {\n shopName?: string\n /** Order number, if there is one yet. */\n number?: string | null\n locale?: string\n /** IANA zone the expiry is printed in (default: the runtime's). */\n timeZone?: string\n /** Print button on screen (hidden when printing). Needs JS; without it the page says to use the browser's print. */\n printButton?: boolean\n /** Link of the real PDF of this voucher (a plain `<a download>`, so it works without JS). */\n pdfHref?: string\n /** Opens the print dialog on load. */\n autoPrint?: boolean\n /** `<script>` nonce when the one serving the voucher sends a CSP with `script-src 'nonce-…'`. */\n nonce?: string\n}\n\nconst CSS = `\n@page { size: A4; margin: 18mm; }\n* { box-sizing: border-box; }\nbody { margin: 0; font: 14px/1.5 system-ui, -apple-system, \"Segoe UI\", Roboto, sans-serif; color: #111; background: #fff; }\nmain { max-width: 174mm; margin: 0 auto; padding: 24px 16px; }\nh1 { font-size: 22px; margin: 0; }\n.muted { color: #555; }\n.head { display: flex; justify-content: space-between; align-items: flex-start; gap: 16px; border-bottom: 3px solid #111; padding-bottom: 12px; }\n.amount { margin: 0; font-size: 28px; font-weight: 700; text-align: right; }\n.box { border: 1px solid #111; padding: 14px 16px; margin-top: 18px; }\n.label { margin: 0; font-weight: 400; font-size: 12px; letter-spacing: .08em; text-transform: uppercase; color: #555; }\n.value { margin: 0; font: 700 22px/1.3 ui-monospace, \"SF Mono\", Menlo, Consolas, monospace; word-break: break-all; letter-spacing: .04em; }\n.bar { text-align: center; }\n.bar svg { max-width: 100%; height: auto; }\n.note { margin-top: 18px; font-size: 12px; }\n.pre { white-space: pre-line; }\n.steps-title { font-size: 16px; margin: 18px 0 0; }\n.steps { margin: 6px 0 0; padding-left: 20px; }\n.bar { margin: 18px 0 0; }\n.actions { margin-top: 24px; display: flex; flex-wrap: wrap; gap: 12px; align-items: center; }\n.btn { display: inline-block; font: inherit; padding: 10px 18px; border: 2px solid #111; background: #111; color: #fff; cursor: pointer; text-decoration: none; }\n.btn.alt { background: #fff; color: #111; }\n.btn:focus-visible { outline: 3px solid #1a56db; outline-offset: 2px; }\n@media print { .actions { display: none; } main { padding: 0; } }\n`\n\nfunction barcodeSvg(value: string, format: VoucherView['barcode_format'], label: string): string | null {\n const g = barcodeGeometry(value, { ...(format ? { format } : {}) })\n if (!g) return null\n const rects = g.rects.map((r) => `<rect x=\"${r.x}\" y=\"0\" width=\"${r.w}\" height=\"${g.height}\"/>`).join('')\n return `<svg viewBox=\"0 0 ${g.width} ${g.height}\" width=\"${g.width}\" height=\"${g.height}\" role=\"img\" aria-label=\"${esc(label)} ${esc(value)}\" shape-rendering=\"crispEdges\" fill=\"#000\">${rects}</svg>`\n}\n\n/** Complete HTML document of the voucher. Missing data is simply not drawn. */\nexport function renderVoucherDocument(v: VoucherView, opts: VoucherDocumentOptions = {}): string {\n const locale = opts.locale ?? 'es-MX'\n const t = docText(locale)\n const shop = opts.shopName || ''\n const method = methodName(v, t)\n const code = v.barcode\n const svg = code ? barcodeSvg(code, v.barcode_format, t.barcode) : null\n const expires = formatDate(v.expires_at, locale, opts.timeZone)\n const fields = voucherFields(v, t)\n const steps = voucherSteps(v, t)\n const subtitle = [shop, method, opts.number ? `${t.order} ${opts.number}` : ''].filter(Boolean).map(esc).join(' · ')\n const actions = opts.printButton || opts.pdfHref\n return `<!doctype html>\n<html lang=\"${esc(locale)}\"><head><meta charset=\"utf-8\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n<meta name=\"robots\" content=\"noindex\"><title>${esc(t.title)}${shop ? ` - ${esc(shop)}` : ''}</title><style>${CSS}</style></head>\n<body><main aria-labelledby=\"t\">\n<header class=\"head\"><div><h1 id=\"t\">${esc(t.title)}</h1><p class=\"muted\" data-voucher-method=\"${esc(v.method ?? '')}\">${subtitle}</p></div>\n${v.amount ? `<div><p class=\"label\" style=\"text-align:right\">${esc(t.total)}</p><p class=\"amount\">${esc(formatMoney(v.amount, locale))}</p></div>` : ''}</header>\n${fields.map((f) => `<section class=\"box\" data-field=\"${f.key}\"><h2 class=\"label\">${esc(f.label)}</h2><p class=\"value\">${esc(f.value)}</p></section>`).join('')}\n${svg ? `<figure class=\"bar\">${svg}<figcaption class=\"muted\" style=\"font-family:ui-monospace,monospace\">${esc(code as string)}</figcaption></figure>` : ''}\n${expires ? `<p><strong>${esc(t.expires)}:</strong> ${esc(expires.replace(/\\.$/, ''))}. ${esc(t.expiresNote)}</p>` : ''}\n<h2 class=\"steps-title\">${esc(t.stepsTitle)}</h2>\n<ol class=\"steps\">${steps.map((s) => `<li>${esc(s)}</li>`).join('')}</ol>\n${v.instructions ? `<p class=\"note pre muted\">${esc(v.instructions)}</p>` : ''}\n<p class=\"note muted\">${esc(t.footer)}</p>\n${\n actions\n ? `<div class=\"actions\" role=\"group\" aria-label=\"${esc(t.title)}\">${opts.pdfHref ? `<a class=\"btn\" href=\"${esc(opts.pdfHref)}\" download=\"ficha-de-pago.pdf\" data-voucher-pdf>${esc(t.downloadPdf)}</a>` : ''}${opts.printButton ? `<button type=\"button\" class=\"btn alt\" id=\"print\" hidden>${esc(t.print)}</button><noscript><p class=\"note muted\">${esc(t.noScript)}</p></noscript>` : ''}</div>`\n : ''\n}\n</main>\n${opts.printButton || opts.autoPrint ? `<script${opts.nonce ? ` nonce=\"${esc(opts.nonce)}\"` : ''}>(function(){var b=document.getElementById(\"print\");if(b){b.hidden=false;b.addEventListener(\"click\",function(){window.print()})}${opts.autoPrint ? 'window.addEventListener(\"load\",function(){setTimeout(function(){window.print()},300)});' : ''}})();</script>` : ''}\n</body></html>`\n}\n"]}
|