@tokenoftrust/storefront-runner 7.8.7 → 7.8.8

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.
@@ -29,10 +29,12 @@ import {
29
29
  carryCheckoutVerification,
30
30
  checkoutLineFromForm,
31
31
  clearStoredRef,
32
+ isGuestShopper,
32
33
  isStoreNotPublicRefusal,
33
34
  parsePreflightResponse,
34
35
  pollVerification,
35
36
  readStoredRef,
37
+ requestFastTrackAccountSave,
36
38
  verificationPhase,
37
39
  verificationRefKey,
38
40
  verifiedRefKey,
@@ -63,6 +65,7 @@ const STORE_NOT_PUBLIC_MESSAGE =
63
65
 
64
66
  export default function CheckoutComplianceGate({ preflightUrl, minAge }: Props) {
65
67
  const [pending, setPending] = useState<PendingLine | null>(null);
68
+ const activeDialogLine = useRef<PendingLine | null>(null);
66
69
  const [region, setRegion] = useState("");
67
70
  const [affirmed, setAffirmed] = useState(false);
68
71
  const [loading, setLoading] = useState(false);
@@ -75,6 +78,9 @@ export default function CheckoutComplianceGate({ preflightUrl, minAge }: Props)
75
78
  const [email, setEmail] = useState("");
76
79
  const [givenName, setGivenName] = useState("");
77
80
  const [familyName, setFamilyName] = useState("");
81
+ const [guestShopper, setGuestShopper] = useState(false);
82
+ const [saveForFastTrack, setSaveForFastTrack] = useState(true);
83
+ const fastTrackSentRef = useRef(false);
78
84
  const [verificationRef, setVerificationRef] = useState<string | null>(null);
79
85
  const [continuationUrl, setContinuationUrl] = useState<string | null>(null);
80
86
  const [popupBlocked, setPopupBlocked] = useState(false);
@@ -129,12 +135,18 @@ export default function CheckoutComplianceGate({ preflightUrl, minAge }: Props)
129
135
  }
130
136
  }
131
137
  function open(line: PendingLine) {
138
+ stopPolling();
139
+ activeDialogLine.current = line;
132
140
  setPending(line);
133
141
  setDecision(null);
134
142
  setError(null);
143
+ setLoading(false);
135
144
  setPhase("none");
136
145
  setContinuationUrl(null);
137
146
  setPopupBlocked(false);
147
+ setGuestShopper(false);
148
+ setSaveForFastTrack(true);
149
+ fastTrackSentRef.current = false;
138
150
  // A reload mid-trip must not lose the correlation: replay the stored ref so
139
151
  // the server re-reads THIS attempt's verification instead of starting over.
140
152
  setVerificationRef(line.shopperCart ? null :
@@ -150,10 +162,21 @@ export default function CheckoutComplianceGate({ preflightUrl, minAge }: Props)
150
162
  }
151
163
  document.addEventListener("submit", onSubmit, true);
152
164
  return () => document.removeEventListener("submit", onSubmit, true);
153
- }, [preflightUrl]);
165
+ }, [preflightUrl, stopPolling]);
166
+
167
+ useEffect(() => {
168
+ if (phase !== "collect" || !pending || pending.shopperCart) return;
169
+ // An open store uses the managed cart for signed-in shoppers too.
170
+ let cancelled = false;
171
+ void isGuestShopper(preflightUrl).then((guest) => {
172
+ if (!cancelled && activeDialogLine.current === pending) setGuestShopper(guest);
173
+ });
174
+ return () => { cancelled = true; };
175
+ }, [pending, phase, preflightUrl]);
154
176
 
155
177
  const close = useCallback(() => {
156
178
  stopPolling();
179
+ activeDialogLine.current = null;
157
180
  setPending(null);
158
181
  setDecision(null);
159
182
  setError(null);
@@ -183,6 +206,7 @@ export default function CheckoutComplianceGate({ preflightUrl, minAge }: Props)
183
206
  line.form.dataset.complianceCleared = "1";
184
207
  line.form.dataset.paymentAuthority = "none";
185
208
  document.documentElement.style.overflow = "";
209
+ activeDialogLine.current = null;
186
210
  setPending(null);
187
211
  if (typeof line.form.requestSubmit === "function") line.form.requestSubmit();
188
212
  else line.form.submit();
@@ -353,14 +377,22 @@ export default function CheckoutComplianceGate({ preflightUrl, minAge }: Props)
353
377
  ref: verificationRef,
354
378
  includeShopper: phase === "collect" || phase === "stalled",
355
379
  });
380
+ if (activeDialogLine.current !== pending) return;
356
381
  setLoading(false);
357
382
  if (!res) {
358
383
  // Fail closed: a regulated tenant never proceeds on an unreadable result.
359
384
  setError(gateRefused.current ? STORE_NOT_PUBLIC_MESSAGE : FAIL_CLOSED_MESSAGE);
360
385
  return;
361
386
  }
387
+ if (phase === "collect" && guestShopper && !fastTrackSentRef.current && saveForFastTrack && email.trim()) {
388
+ const next = verificationPhase(res);
389
+ if (next === "continue" || next === "waiting" || next === "cleared") {
390
+ fastTrackSentRef.current = true;
391
+ void requestFastTrackAccountSave({ preflightUrl, email, saveForFastTrack, shopperCart: pending.shopperCart });
392
+ }
393
+ }
362
394
  apply(pending, res);
363
- }, [pending, send, apply, verificationRef, phase, stopPolling]);
395
+ }, [pending, send, apply, verificationRef, phase, stopPolling, guestShopper, email, saveForFastTrack, preflightUrl]);
364
396
 
365
397
  /** Abandon a denied attempt and start a brand-new verification (new ref). */
366
398
  const restartVerification = useCallback(() => {
@@ -501,6 +533,19 @@ export default function CheckoutComplianceGate({ preflightUrl, minAge }: Props)
501
533
  />
502
534
  </label>
503
535
  </div>
536
+ {guestShopper && (
537
+ <label class="flex items-start gap-2.5 text-sm text-[var(--color-text)]">
538
+ <input
539
+ type="checkbox"
540
+ name="tot-save-fast-track"
541
+ class="mt-0.5 h-4 w-4"
542
+ checked={saveForFastTrack}
543
+ disabled={loading}
544
+ onChange={(e) => setSaveForFastTrack((e.target as HTMLInputElement).checked)}
545
+ />
546
+ <span>Save my verified info for 1-click fast-track checkout &amp; order history.</span>
547
+ </label>
548
+ )}
504
549
  </div>
505
550
  )}
506
551
  </div>
@@ -134,6 +134,55 @@ export function bypassesComplianceGate(res: PreflightResponse | null): boolean {
134
134
  return res !== null && res.gate === "not-required" && res.decision.status === "clear";
135
135
  }
136
136
 
137
+ function shopperAccountUrl(preflightUrl: string, endpoint: string): string {
138
+ return preflightUrl.replace(/\/api\/compliance\/preflight$/, `/api/account/${endpoint}`);
139
+ }
140
+
141
+ export function shopperAuthSendUrl(preflightUrl: string): string {
142
+ return shopperAccountUrl(preflightUrl, "auth/send");
143
+ }
144
+
145
+ export async function isGuestShopper(preflightUrl: string): Promise<boolean> {
146
+ const url = shopperAccountUrl(preflightUrl, "me");
147
+ if (url === preflightUrl) return false;
148
+
149
+ try {
150
+ const response = await fetch(url, { credentials: "same-origin" });
151
+ return response.status === 401 && (await response.json())?.signedIn === false;
152
+ } catch {
153
+ return false;
154
+ }
155
+ }
156
+
157
+ export async function requestFastTrackAccountSave(opts: {
158
+ preflightUrl: string;
159
+ email: string;
160
+ saveForFastTrack: boolean;
161
+ shopperCart: boolean;
162
+ fetchImpl?: typeof fetch;
163
+ }): Promise<boolean> {
164
+ const identifier = opts.email.trim().toLowerCase();
165
+ if (!opts.saveForFastTrack || opts.shopperCart || !identifier) return false;
166
+
167
+ const url = shopperAuthSendUrl(opts.preflightUrl);
168
+ if (url === opts.preflightUrl) return false;
169
+
170
+ try {
171
+ const response = await (opts.fetchImpl ?? fetch)(url, {
172
+ method: "POST",
173
+ headers: { "content-type": "application/json" },
174
+ credentials: "same-origin",
175
+ keepalive: true,
176
+ body: JSON.stringify({ identifier }),
177
+ });
178
+ if (!response.ok) return false;
179
+ await response.json();
180
+ return true;
181
+ } catch {
182
+ return false;
183
+ }
184
+ }
185
+
137
186
  export interface PendingLine {
138
187
  form: HTMLFormElement;
139
188
  shopperCart: boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tokenoftrust/storefront-runner",
3
- "version": "7.8.7",
3
+ "version": "7.8.8",
4
4
  "license": "SEE LICENSE IN LICENSE",
5
5
  "description": "World-shareable storefront runner: multi-tenant renderer on Astro/Cloudflare. No control plane.",
6
6
  "packageManager": "pnpm@11.9.0",