veryfi-lens-wasm 3.5.8 → 3.5.9

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 CHANGED
@@ -111,7 +111,8 @@ VeryfiLens.onFailure((error) => { /* error occurred */ });
111
111
  | `lensFlavor` | `string` | — | **Required.** Capture mode (see [Supported Flavors](#supported-flavors)) |
112
112
  | `container` | `string` | — | ID of an HTML element to render into. If omitted, creates a fullscreen overlay |
113
113
  | `theme` | `"light"\|"dark"\|"system"` | `"system"` | UI color theme. `"system"` follows OS changes while the lens is open |
114
- | `enableSubmit` | `boolean` | `true` | Show submit button after capture. With `false` (and `packageMode: false`) the review gallery's Submit uploads nothing: it fires the `showCamera` setter (checks: `{ frontImage, backImage, ...per-side LCD / front-back results }`; other flavors: `{ image }`), or calls `customSubmitHandler(image, null)` when set, then closes |
114
+ | `enableSubmit` | `boolean` | `true` | Show submit button after capture. With `false` (and `packageMode: false`) the review gallery's Submit uploads nothing: it fires the `showCamera` setter (checks: `{ frontImage, backImage, ...per-side LCD / front-back results }`; other flavors: `{ image, images }` — `image` is the last page, `images` every page in order), or calls `customSubmitHandler(image, null)` when set, then closes |
115
+ | `sendCroppedImage` | `boolean` | `true` | Include the captured image in the per-capture `/rest/send-cropped` request (`packageMode: false` only). With `false` only metadata is sent; the image is still sent to `/api/document` on submit |
115
116
  | `enableFullScreen` | `boolean` | `false` | Enable fullscreen mode |
116
117
  | `debug_mode` | `boolean` | `false` | Enable console logging |
117
118
  | `useDevApi` | `boolean` | `false` | Route API requests to `lens-dev.veryfi.com` instead of production |
@@ -145,6 +146,7 @@ VeryfiLens.onFailure((error) => { /* error occurred */ });
145
146
  | `isDocumentModal` | `boolean` | `true` | Show modal when no document detected |
146
147
  | `isLcdModal` | `boolean` | `true` | Show modal when LCD/screen capture detected |
147
148
  | `isDesktopModeModal` | `boolean` | `true` | Show the modal asking mobile users to leave "Desktop site" mode |
149
+ | `isReceiptGroupingModal` | `boolean` | `false` | When a receipt (`document` flavor) frame contains several receipts, ask whether they are one transaction (submitted as one multi-page document) or separate receipts (one document each). With `false` they are always separate. Only applies when multiple documents are allowed (`multiSubmission` or `maxDocuments` other than `1`) |
148
150
  | `enforceDocumentDetection` | `boolean` | `false` | Block submission if no document detected |
149
151
  | `enforceBlurDetection` | `boolean` | `false` | Block submission if image is blurry |
150
152
  | `enforceLcdDetection` | `boolean` | `false` | Block submission if screen capture detected |
@@ -302,6 +304,9 @@ When `multiSubmission` is set or `maxDocuments` is not `1`, `init()` asks the ba
302
304
  | `documentModalMessage` | `string` | `"Oops, There appears to be no document on your image"` |
303
305
  | `blurModalMessage` | `string` | `"Oops, The image is too blurry to process"` |
304
306
  | `lcdModalMessage` | `string` | `"LCD screen detected. Please capture a printed document instead"` |
307
+ | `receiptGroupingModalMessage` | `string` | `"Several receipts were found. Are they part of one transaction?"` |
308
+ | `receiptGroupingOneTransactionText` | `string` | `"One transaction"` |
309
+ | `receiptGroupingSeparateText` | `string` | `"Separate receipts"` |
305
310
  | `submitButtonText` | `string` | `"Submit Anyway"` |
306
311
  | `retakeButtonText` | `string` | `"Retake a picture"` |
307
312
  | `cameraPermissionDeniedMessage` | `string` | `"Camera access was blocked. Please allow camera access in your browser settings and try again."` |
@@ -0,0 +1,44 @@
1
+ import { createModal } from './modalBase.js';
2
+
3
+ export const createReceiptGroupingModal = (container, croppedImage, options = {}) => {
4
+ const defaults = {
5
+ message: 'Several receipts were found. Are they part of one transaction?',
6
+ oneTransactionText: 'One transaction',
7
+ separateText: 'Separate receipts',
8
+ };
9
+ const o = { ...defaults, ...options };
10
+
11
+ let pendingResolve = null;
12
+ const settle = (grouping) => {
13
+ const resolve = pendingResolve;
14
+ pendingResolve = null;
15
+ resolve?.(grouping);
16
+ };
17
+
18
+ const modal = createModal(container, {
19
+ id: 'veryfi-receipt-grouping-modal',
20
+ cssPrefix: 'receipt-grouping-modal',
21
+ message: o.message,
22
+ primaryButtonText: o.oneTransactionText,
23
+ primaryButtonId: 'veryfi-receipt-grouping-one',
24
+ secondaryButtonText: o.separateText,
25
+ secondaryButtonId: 'veryfi-receipt-grouping-separate',
26
+ onPrimaryClick: () => settle('one'),
27
+ onSecondaryClick: () => settle('separate'),
28
+ croppedImage,
29
+ useBackdrop: true,
30
+ primaryClearsCanvas: false,
31
+ showCropBorder: false,
32
+ });
33
+
34
+ return {
35
+ ...modal,
36
+ // Resolves with 'one' (all receipts are pages of one document) or
37
+ // 'separate' (each receipt is its own document).
38
+ ask: () => new Promise((resolve) => {
39
+ settle('separate');
40
+ pendingResolve = resolve;
41
+ modal.show();
42
+ }),
43
+ };
44
+ };
package/core/api.js CHANGED
@@ -112,10 +112,12 @@ export const buildJsonData = async () => {
112
112
  //
113
113
  // - isBase64 = true => JSON body { image: <base64>, json_data, client_id }
114
114
  // - isBase64 = false => multipart body { file: <Blob>, json_data, client_id }
115
+ // With sendCroppedImage = false the image/file is omitted and only metadata is sent.
115
116
  const sendCroppedLegacy = async ({ data, fileName, isBase64 }) => {
116
117
  const jsonData = await buildJsonData();
117
118
  const clientId = state.clientId;
118
119
  const sessionId = state.clientData?.session;
120
+ const includeImage = state.sendCroppedImage;
119
121
 
120
122
  const spinner = createSubmissionSpinner(MESSAGES.REVIEW_PREPARING);
121
123
  spinner.show();
@@ -129,13 +131,15 @@ const sendCroppedLegacy = async ({ data, fileName, isBase64 }) => {
129
131
  if (isBase64) {
130
132
  headers['Content-Type'] = 'application/json';
131
133
  body = JSON.stringify({
132
- image: data,
134
+ ...(includeImage ? { image: data } : {}),
133
135
  json_data: jsonData,
134
136
  client_id: clientId,
135
137
  });
136
138
  } else {
137
139
  const formData = new FormData();
138
- formData.append('file', data, fileName);
140
+ if (includeImage) {
141
+ formData.append('file', data, fileName);
142
+ }
139
143
  formData.append('json_data', JSON.stringify(jsonData));
140
144
  formData.append('client_id', clientId);
141
145
  body = formData;
@@ -304,7 +308,9 @@ export const fireResultSetter = async (payload, { autoUpload = true } = {}) => {
304
308
  // is passed explicitly because a session can hold several documents, each with
305
309
  // its own result; state.fieldDetectionResult is only the last one computed and
306
310
  // serves as the fallback for the legacy single-document path.
307
- export const handleSubmit = async (image, { fieldDetection } = {}) => {
311
+ // `fieldDetectionPages` holds one result per page (null where none) and is
312
+ // returned as field_detection_pages for multi-page documents.
313
+ export const handleSubmit = async (image, { fieldDetection, fieldDetectionPages } = {}) => {
308
314
  // Snapshot state into locals before any await — the submit button click handler
309
315
  // fires removeOverlay()/resetState() concurrently with this flow.
310
316
  const lensFlavor = state.lensFlavor;
@@ -327,6 +333,10 @@ export const handleSubmit = async (image, { fieldDetection } = {}) => {
327
333
  const localFieldDetection = resolvedFieldDetection
328
334
  ? structuredClone(resolvedFieldDetection)
329
335
  : null;
336
+ const localFieldDetectionPages =
337
+ Array.isArray(fieldDetectionPages) && fieldDetectionPages.length > 1 && fieldDetectionPages.some(Boolean)
338
+ ? structuredClone(fieldDetectionPages)
339
+ : null;
330
340
 
331
341
  state.submitInProgress = true;
332
342
  state.updateHandler?.({ status: 'submission_started' });
@@ -396,8 +406,9 @@ export const handleSubmit = async (image, { fieldDetection } = {}) => {
396
406
  state.updateHandler?.({ status: 'submission_completed' });
397
407
  const result = await response.json();
398
408
 
399
- if (shouldRunFieldDetection(lensFlavor) && documentFieldsToDetect.length && localFieldDetection) {
400
- result.field_detection = localFieldDetection;
409
+ if (shouldRunFieldDetection(lensFlavor) && documentFieldsToDetect.length) {
410
+ if (localFieldDetection) result.field_detection = localFieldDetection;
411
+ if (localFieldDetectionPages) result.field_detection_pages = localFieldDetectionPages;
401
412
  }
402
413
 
403
414
  if (successHandler && typeof successHandler === 'function') {
@@ -55,27 +55,48 @@ export const captureUploaded = async (imageFile) => {
55
55
  });
56
56
  };
57
57
 
58
+ // Ask whether the receipts found in one frame belong to one transaction.
59
+ // Without the modal (disabled, or a non-receipt flavor) they stay separate.
60
+ const askReceiptGrouping = async (count) => {
61
+ if (!state.receiptGroupingModal) return 'separate';
62
+ state.updateHandler?.({ status: 'receipt_grouping_prompted', count });
63
+ const grouping = await state.receiptGroupingModal.ask();
64
+ state.updateHandler?.({ status: 'receipt_grouping_selected', grouping, count });
65
+ return grouping;
66
+ };
67
+
58
68
  // Commit the capture plus any additional documents the segmenter found in the
59
69
  // same frame, tagging each page with its own quality issues so the gallery can
60
- // flag them individually.
70
+ // flag them individually. One transaction commits every crop as a page of a
71
+ // single document; separate commits each crop as its own document.
61
72
  const commitCaptureAndExtras = async () => {
62
73
  const { commitCaptureToReview } = await import('../review-flow.js');
63
- await commitCaptureToReview({
74
+ const extras = state.extraCropImages;
75
+ const primaryIssues = state.primaryCropIssues;
76
+ state.extraCropImages = [];
77
+ state.primaryCropIssues = [];
78
+
79
+ const grouping = extras.length ? await askReceiptGrouping(extras.length + 1) : 'separate';
80
+
81
+ const primaryPage = await commitCaptureToReview({
64
82
  kind: 'image',
65
83
  data: state.finalImage,
66
- issues: state.primaryCropIssues,
84
+ issues: primaryIssues,
67
85
  });
68
- for (const extra of state.extraCropImages) {
69
- if (!store.canAddDocument()) break;
70
- state.startNewDocumentOnCapture = true;
86
+ const isOneTransaction = grouping === 'one' && !!primaryPage;
87
+ if (isOneTransaction) store.extendPageLimit(extras.length);
88
+
89
+ for (const extra of extras) {
90
+ if (!isOneTransaction) {
91
+ if (!store.canAddDocument()) break;
92
+ state.startNewDocumentOnCapture = true;
93
+ }
71
94
  await commitCaptureToReview({
72
95
  kind: 'image',
73
96
  data: extra.data,
74
97
  issues: extra.issues,
75
98
  });
76
99
  }
77
- state.extraCropImages = [];
78
- state.primaryCropIssues = [];
79
100
  };
80
101
 
81
102
  export const handleDocumentAutoCaptureDone = async () => {
@@ -100,7 +100,17 @@ export const canAddDocument = () =>
100
100
  export const canAddPage = (docIndex = state.activeDocIndex) => {
101
101
  const doc = state.capturedDocs[docIndex];
102
102
  if (!doc) return false;
103
- return !state.maxPagesPerDocument || doc.pages.length < state.maxPagesPerDocument;
103
+ const limit = doc.maxPages ?? state.maxPagesPerDocument;
104
+ return !limit || doc.pages.length < limit;
105
+ };
106
+
107
+ // Let one document hold `count` more pages than it has now, regardless of
108
+ // maxPagesPerDocument. Used when several receipts in one frame are grouped
109
+ // into a single transaction.
110
+ export const extendPageLimit = (count, docIndex = state.activeDocIndex) => {
111
+ const doc = state.capturedDocs[docIndex];
112
+ if (!doc) return;
113
+ doc.maxPages = doc.pages.length + count;
104
114
  };
105
115
 
106
116
  // Append a new document and make it active. When the session is already at
@@ -218,6 +228,12 @@ export const documentFieldDetection = (docIndex) =>
218
228
  state.capturedDocs[docIndex]?.pages?.find((page) => page.fieldDetection)
219
229
  ?.fieldDetection ?? null;
220
230
 
231
+ // Per-page field-detection results for a document, in page order (null for a
232
+ // page without a result). Multi-page receipt documents (one transaction) carry
233
+ // one result per receipt.
234
+ export const documentPageFieldDetections = (docIndex) =>
235
+ (state.capturedDocs[docIndex]?.pages || []).map((page) => page.fieldDetection ?? null);
236
+
221
237
  // Produce the per-scan records consumed by api.uploadPackage()/sendCropped for
222
238
  // a single document: { data, fileName, isBase64, jsonData }.
223
239
  export const buildPackageScans = (docIndex) => {
package/core/constants.js CHANGED
@@ -39,7 +39,7 @@ export const setApiBaseUrl = (base) => {
39
39
  };
40
40
 
41
41
  export const DEFAULTS = {
42
- SDK_VERSION: '3.5.8',
42
+ SDK_VERSION: '3.5.9',
43
43
  BLUR_THRESHOLD: 0.7,
44
44
  LCD_THRESHOLD: 0.5,
45
45
  BOX_COLOR: 'rgba(84, 192, 139, 0.6)',
@@ -107,6 +107,9 @@ export const MESSAGES = {
107
107
  MULTI_CAPTURE_PROMPT: 'Do you need to capture another document?',
108
108
  MULTI_CAPTURE_YES: 'Capture another',
109
109
  MULTI_CAPTURE_NO: "No, I'm done",
110
+ RECEIPT_GROUPING_PROMPT: 'Several receipts were found. Are they part of one transaction?',
111
+ RECEIPT_GROUPING_ONE: 'One transaction',
112
+ RECEIPT_GROUPING_SEPARATE: 'Separate receipts',
110
113
  REVIEW_TITLE: 'Review',
111
114
  REVIEW_ADD_PAGE: 'Add Page',
112
115
  REVIEW_ADD_DOCUMENT: 'Add Document',
@@ -262,7 +262,7 @@ const submitOneDocument = async (docIndex) => {
262
262
  if (isChecks) {
263
263
  state.resultSetter?.(checksExtra);
264
264
  } else if (!hasHandler) {
265
- state.resultSetter?.({ image: originalData });
265
+ state.resultSetter?.({ image: originalData, images: doc.pages.map((page) => page.data) });
266
266
  }
267
267
  if (hasHandler) state.customSubmitHandler(setterImage, null);
268
268
  return null;
@@ -302,6 +302,7 @@ const submitOneDocument = async (docIndex) => {
302
302
  if (isChecks) state.resultSetter?.(checksExtra);
303
303
  return await handleSubmit(image, {
304
304
  fieldDetection: store.documentFieldDetection(docIndex),
305
+ fieldDetectionPages: store.documentPageFieldDetections(docIndex),
305
306
  });
306
307
  };
307
308
 
package/core/state.js CHANGED
@@ -90,6 +90,7 @@ export const state = {
90
90
  isDocumentModal: true,
91
91
  isLcdModal: true,
92
92
  isDesktopModeModal: true,
93
+ isReceiptGroupingModal: false,
93
94
  container: null,
94
95
  exitButton: false,
95
96
  showCameraPermissionDeniedMessage: true,
@@ -213,6 +214,7 @@ export const state = {
213
214
  lcdModal: null,
214
215
  anydocFlipModal: null,
215
216
  checkFlipModal: null,
217
+ receiptGroupingModal: null,
216
218
  torchButtonRef: null,
217
219
  mirrorButtonRef: null,
218
220
  switchCameraButtonRef: null,
@@ -234,6 +236,9 @@ export const state = {
234
236
  documentModalMessage: MESSAGES.DOCUMENT_NOT_FOUND,
235
237
  blurModalMessage: MESSAGES.BLUR_DETECTED,
236
238
  lcdModalMessage: MESSAGES.LCD_DETECTED,
239
+ receiptGroupingModalMessage: MESSAGES.RECEIPT_GROUPING_PROMPT,
240
+ receiptGroupingOneTransactionText: MESSAGES.RECEIPT_GROUPING_ONE,
241
+ receiptGroupingSeparateText: MESSAGES.RECEIPT_GROUPING_SEPARATE,
237
242
  submitButtonText: MESSAGES.SUBMIT_ANYWAY,
238
243
  retakeButtonText: MESSAGES.RETAKE,
239
244
 
@@ -327,6 +332,7 @@ export const state = {
327
332
  customData: {},
328
333
 
329
334
  packageMode: false,
335
+ sendCroppedImage: false,
330
336
 
331
337
  enforceFingerprint: false,
332
338
  disableFingerprint: false,
@@ -380,6 +386,7 @@ export const resetState = () => {
380
386
  state.lcdModal = null;
381
387
  state.anydocFlipModal = null;
382
388
  state.checkFlipModal = null;
389
+ state.receiptGroupingModal = null;
383
390
  state.frameRef = null;
384
391
  state.cropImgRef = null;
385
392
  state.videoRef = null;
@@ -13,6 +13,7 @@ import { createStitchingButton } from '../components/stitchingButton.js';
13
13
  import { createCreditCardComponent } from '../components/creditCard.js';
14
14
  import { createWrongSideModal } from '../components/wrongSideModal.js';
15
15
  import { createAnydocFlipModal } from '../components/anydocFlipModal.js';
16
+ import { createReceiptGroupingModal } from '../components/receiptGroupingModal.js';
16
17
  import { createWatermark } from '../components/watermark.js';
17
18
  import { createCardDataButton } from '../components/cardDataButton.js';
18
19
  import { createBottleCapOverlay } from '../components/bottleCapOverlay.js';
@@ -325,6 +326,18 @@ export const renderModalsIfNeeded = () => {
325
326
  }
326
327
  );
327
328
  }
329
+
330
+ if (state.isReceiptGroupingModal && state.lensFlavor === 'document') {
331
+ state.receiptGroupingModal = createReceiptGroupingModal(
332
+ state.container,
333
+ state.croppedImage,
334
+ {
335
+ message: state.receiptGroupingModalMessage,
336
+ oneTransactionText: state.receiptGroupingOneTransactionText,
337
+ separateText: state.receiptGroupingSeparateText,
338
+ }
339
+ );
340
+ }
328
341
  };
329
342
 
330
343
  export const renderBottleCapOverlayIfNeeded = () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "veryfi-lens-wasm",
3
- "version": "3.5.8",
3
+ "version": "3.5.9",
4
4
  "description": "Lens For Web SDK with WASM",
5
5
  "main": "veryfi-lens.js",
6
6
  "type": "module",