@asstio-dev/storefront 0.18.0 → 0.20.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 +60 -0
- package/dist/analytics/events.d.ts +20 -0
- package/dist/analytics/events.js +23 -0
- package/dist/checkout/browser.d.ts +13 -1
- package/dist/checkout/browser.js +146 -3
- package/dist/checkout/controllers.d.ts +18 -2
- package/dist/checkout/controllers.js +133 -16
- package/dist/checkout/live-shipping.d.ts +30 -0
- package/dist/checkout/live-shipping.js +193 -0
- package/dist/checkout/server.d.ts +4 -1
- package/dist/checkout/server.js +9 -0
- package/dist/checkout/types.d.ts +12 -0
- package/dist/index.d.ts +3 -2
- package/dist/index.js +2 -1
- package/dist/schema.d.ts +292 -2
- package/package.json +2 -2
- package/src/schema.d.ts +292 -2
package/README.md
CHANGED
|
@@ -243,6 +243,28 @@ config. Holes in the data never throw: a missing image, description or price onl
|
|
|
243
243
|
|
|
244
244
|
hreflang sets are only as reciprocal as the API's `alternateUrls`; the SDK renders what the API gives.
|
|
245
245
|
|
|
246
|
+
### Search events
|
|
247
|
+
|
|
248
|
+
`trackSearchEvents(client, events, { consent })` posts search exposures, product clicks and add-to-carts to
|
|
249
|
+
`POST /v1/events`, for measuring search quality. It sends **nothing** unless `consent === true`: the storefront stores
|
|
250
|
+
no PII and cannot verify consent, so the frontend decides, and passes `sessionId` (opaque, frontend-generated) only
|
|
251
|
+
under the same consent.
|
|
252
|
+
|
|
253
|
+
```ts
|
|
254
|
+
const exposure = { type: "search_exposure", occurredAt: new Date().toISOString(), resultId: listing.search!.resultId, query, page: 1, productIds };
|
|
255
|
+
await trackSearchEvents(client, [exposure], { consent: shopperAcceptedMeasurement });
|
|
256
|
+
await trackSearchEvents(client, [{ type: "product_click", occurredAt: new Date().toISOString(), exposureId: exposure.eventId, productId, position: 3 }], { consent });
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
- An event without `eventId` gets a UUID **written into the object**. Retry the same objects after an uncertain
|
|
260
|
+
answer (a timeout, a dropped connection) and the ids are reused, so the server counts the repeats as `duplicates`.
|
|
261
|
+
A second click from the same exposure is a new event: build a new object.
|
|
262
|
+
- `occurredAt` is UTC ISO-8601 (`new Date().toISOString()`), within 7 days back and 5 minutes ahead.
|
|
263
|
+
- At most 50 events per call (a larger batch throws `RangeError`), 64 KB per body. The answer lists `rejected`
|
|
264
|
+
events with their `index` and `member`; the others are kept. Any non-2xx answer throws: a malformed envelope (a
|
|
265
|
+
`400` problem) and a bare `5xx` alike, so a failed send is never mistaken for "no consent" (`null`).
|
|
266
|
+
- Exposures come only from the client; a catalog GET is never one.
|
|
267
|
+
|
|
246
268
|
### Product facts and GA4
|
|
247
269
|
|
|
248
270
|
Cards and cart lines carry the product facts the product page has: `brand`, `categories`, `shortDescription` and
|
|
@@ -436,6 +458,8 @@ Problem codes you will meet here: `CART_EMPTY`, `CHECKOUT_CART_CHANGED` (with `c
|
|
|
436
458
|
Qliro shows the confirmed total or version.
|
|
437
459
|
- When our validation declines a purchase (the cart moved on, the attempt expired, a line is out of stock), Qliro shows
|
|
438
460
|
its out-of-stock message and fires `q1.onPaymentDeclined`; the SDK then syncs by itself and remounts or resumes.
|
|
461
|
+
- `q1` callbacks replace per event. The SDK owns `onOrderUpdated` and `onPaymentDeclined`, and with `onShipping` also
|
|
462
|
+
`onShippingMethodChanged` and `onShippingPriceChanged`: a listener of your own on these silently stops the SDK's.
|
|
439
463
|
|
|
440
464
|
#### Kustom (`klarna-kco`)
|
|
441
465
|
|
|
@@ -457,6 +481,8 @@ Problem codes you will meet here: `CART_EMPTY`, `CHECKOUT_CART_CHANGED` (with `c
|
|
|
457
481
|
- Never bind `can_not_complete_order` yourself through `window._klarnaCheckout(api => api.on(...))`. Kustom's `api.on`
|
|
458
482
|
replaces the listener per event instead of adding one, so yours and the SDK's would replace each other. Use
|
|
459
483
|
`onProblem`. Listeners for other events are not affected.
|
|
484
|
+
With `onShipping` set (see "Live shipping view" below) the SDK also owns `shipping_option_change`,
|
|
485
|
+
`shipping_address_change` and `order_total_change` (one separate `api.on`); use `onShipping` for those.
|
|
460
486
|
- **CSP hosts** (K-C10, `docs/ops/kustom-playground-checks.md`, 2026-10-09: a card with 3-D Secure and the default
|
|
461
487
|
method, no blocked request). The checkout page itself loads only Kustom's script host. Everything else
|
|
462
488
|
(`kco.assets.kustomcdn.co`, Klarna, Stripe, hCaptcha, Google Pay, Amazon Pay) loads inside Kustom's iframe under
|
|
@@ -505,6 +531,40 @@ if (estimate?.available) {
|
|
|
505
531
|
normally held in Asstio with its reason (rarely the export waits for an operator instead); the page has nothing to
|
|
506
532
|
do for it.
|
|
507
533
|
|
|
534
|
+
#### Live shipping view
|
|
535
|
+
|
|
536
|
+
Optionally show what the shopper has chosen inside the widget, from `GET /v1/checkout/{attemptId}/shipping`. The view is
|
|
537
|
+
not a promise: the order's `lines` and `totals` stay the truth.
|
|
538
|
+
|
|
539
|
+
```ts
|
|
540
|
+
// Server route: forwards the view and Retry-After. The cart token stays on the server.
|
|
541
|
+
const r = await liveShippingFor(client, cartToken, attemptId);
|
|
542
|
+
// r.ok ? r.data : r.problem, with r.retryAfter as the Retry-After header
|
|
543
|
+
|
|
544
|
+
// Page:
|
|
545
|
+
mountCheckout(el, widget, {
|
|
546
|
+
/* … */
|
|
547
|
+
liveShipping: (attemptId) => callMyProxy(attemptId), // returns LiveShippingResult
|
|
548
|
+
onShipping: (view) => {
|
|
549
|
+
if (view === null) return showEstimate(); // invalidated: a change is in flight
|
|
550
|
+
if (view.state === "selected") showLine(view.name, view.shippingInc, view.totalInc);
|
|
551
|
+
},
|
|
552
|
+
});
|
|
553
|
+
```
|
|
554
|
+
|
|
555
|
+
- `liveShipping` and `onShipping` are required together (one without the other throws at mount). Without them nothing
|
|
556
|
+
shipping-related is bound, scheduled or called.
|
|
557
|
+
- `view.state`: `selected`, `unselected`, `unmatched`, `unavailable` (with `reason`: `syncing`, `closed`, `provider`,
|
|
558
|
+
`throttled`, `mismatch`, `erased`, `not_supported`, and `retryAfterSeconds`).
|
|
559
|
+
- Members: `goodsInc`, `shippingInc`, `totalInc`, `otherInc`, `totalSource`, `option`, `name`, `deliveryType`, `point`,
|
|
560
|
+
`readAt`, `cartVersion`. `totalInc` is the provider-reported total, before payment-method charges the provider does
|
|
561
|
+
not report before purchase. `name` can differ from Kustom's display copy.
|
|
562
|
+
- Freshness: a method change on both providers, and an address change on Kustom, typically follows the provider's event
|
|
563
|
+
within a couple of seconds. A Kustom pickup-point change and a Qliro address change fire no client event we know of and
|
|
564
|
+
are seen only by the safety net (every 45 to 60 s, best-effort, up to about 60 s), which runs only while the tab is
|
|
565
|
+
visible.
|
|
566
|
+
- The SDK owns the provider's shipping events while `onShipping` is set; do not bind them yourself.
|
|
567
|
+
|
|
508
568
|
## Headers
|
|
509
569
|
|
|
510
570
|
Every call the client makes carries these; you never set them by hand.
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { Client } from "openapi-fetch";
|
|
2
|
+
import type { components } from "../schema";
|
|
3
|
+
import type { StorefrontPaths } from "../index.js";
|
|
4
|
+
type Schemas = components["schemas"];
|
|
5
|
+
/** One search event. `eventId` may be left out: `trackSearchEvents` writes a UUID into the object. */
|
|
6
|
+
export type SearchEvent = Omit<Schemas["SearchEventDto"], "eventId"> & {
|
|
7
|
+
eventId?: string | null;
|
|
8
|
+
};
|
|
9
|
+
export type SearchEventsResponse = Schemas["SearchEventsResponse"];
|
|
10
|
+
/**
|
|
11
|
+
* `POST /v1/events` (spec 2026-10-13 §9.2). A no-op unless `consent` is exactly `true`: the storefront cannot verify
|
|
12
|
+
* consent, so the caller decides (and sends `sessionId` only under the same consent). An event without `eventId` gets
|
|
13
|
+
* a UUID written into the object, so retrying the same objects after an uncertain response reuses the ids and the
|
|
14
|
+
* server counts them as duplicates. Throws a `RangeError` for more than 50 events (split the batch) and an `Error` on any
|
|
15
|
+
* non-2xx answer (a problem body or none). Build `occurredAt` with `new Date().toISOString()`: the server wants UTC.
|
|
16
|
+
*/
|
|
17
|
+
export declare function trackSearchEvents(client: Client<StorefrontPaths>, events: SearchEvent[], options: {
|
|
18
|
+
consent: boolean;
|
|
19
|
+
}): Promise<SearchEventsResponse | null>;
|
|
20
|
+
export {};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/** The server's per-envelope cap (spec 2026-10-13 §9.2); a larger batch is a `400` that stores nothing. */
|
|
2
|
+
const MAX_EVENTS_PER_REQUEST = 50;
|
|
3
|
+
/**
|
|
4
|
+
* `POST /v1/events` (spec 2026-10-13 §9.2). A no-op unless `consent` is exactly `true`: the storefront cannot verify
|
|
5
|
+
* consent, so the caller decides (and sends `sessionId` only under the same consent). An event without `eventId` gets
|
|
6
|
+
* a UUID written into the object, so retrying the same objects after an uncertain response reuses the ids and the
|
|
7
|
+
* server counts them as duplicates. Throws a `RangeError` for more than 50 events (split the batch) and an `Error` on any
|
|
8
|
+
* non-2xx answer (a problem body or none). Build `occurredAt` with `new Date().toISOString()`: the server wants UTC.
|
|
9
|
+
*/
|
|
10
|
+
export async function trackSearchEvents(client, events, options) {
|
|
11
|
+
if (options?.consent !== true || events.length === 0)
|
|
12
|
+
return null;
|
|
13
|
+
if (events.length > MAX_EVENTS_PER_REQUEST)
|
|
14
|
+
throw new RangeError(`trackSearchEvents takes at most ${MAX_EVENTS_PER_REQUEST} events per call, got ${events.length}`);
|
|
15
|
+
for (const e of events)
|
|
16
|
+
if (!e.eventId)
|
|
17
|
+
e.eventId = crypto.randomUUID();
|
|
18
|
+
const { data, error, response } = await client.POST("/v1/events", { body: { events: events } });
|
|
19
|
+
// `error` is undefined for an empty-body 5xx (openapi-fetch parses nothing), so the status decides: a non-2xx always throws.
|
|
20
|
+
if (!response.ok)
|
|
21
|
+
throw new Error(`trackSearchEvents failed: ${response.status} ${error === undefined ? "" : JSON.stringify(error)}`.trimEnd());
|
|
22
|
+
return data ?? null;
|
|
23
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type ProviderWindow } from "./controllers.js";
|
|
2
|
-
import { type CheckoutDto, type CheckoutOrderRefDto, type CheckoutResult, type StatusResult, type StorefrontProblem } from "./types.js";
|
|
2
|
+
import { type CheckoutDto, type CheckoutOrderRefDto, type CheckoutResult, type CheckoutShippingViewDto, type LiveShippingResult, type StatusResult, type StorefrontProblem } from "./types.js";
|
|
3
3
|
export interface MinimalScript {
|
|
4
4
|
src: string;
|
|
5
5
|
textContent: string | null;
|
|
@@ -62,6 +62,12 @@ export interface MountOptions {
|
|
|
62
62
|
bindRetryMs?: number;
|
|
63
63
|
/** How long before `expiresInSeconds` the SDK syncs by itself (inside the server's five-minute window). */
|
|
64
64
|
preExpiryMs?: number;
|
|
65
|
+
/** A `GET /checkout/{attemptId}/shipping` on the attempt named (server-side, with the cart token, e.g.
|
|
66
|
+
* `liveShippingFor`). Must be provided together with `onShipping`; one without the other throws at mount. */
|
|
67
|
+
liveShipping?: (attemptId: string) => Promise<LiveShippingResult>;
|
|
68
|
+
/** Receives the live shipping view; `null` means invalidated: show the estimate or a placeholder. Must be provided
|
|
69
|
+
* together with `liveShipping`. Without both, nothing shipping-related is bound, scheduled or called. */
|
|
70
|
+
onShipping?: (view: CheckoutShippingViewDto | null) => void;
|
|
65
71
|
}
|
|
66
72
|
export interface CheckoutHandle {
|
|
67
73
|
readonly attemptId: string;
|
|
@@ -73,4 +79,10 @@ export interface CheckoutHandle {
|
|
|
73
79
|
/** The whole rule: suspend → `await update()` once → status first, identity on `open`. Never rejects. */
|
|
74
80
|
sync(update: () => Promise<CheckoutResult>): Promise<void>;
|
|
75
81
|
}
|
|
82
|
+
/** Kustom replays its events once, about 1 s after the first `api.on` registration (K-L15). A shipping event within this
|
|
83
|
+
* window of a mount's first successful shipping subscription, before any later event, is that replay: it carries no
|
|
84
|
+
* change, so it is not an invalidation. It joins the explicit first read if that read has not started, and is dropped
|
|
85
|
+
* if a read already started at or after the subscription (that read already covers it). Applied to `klarna-kco` only:
|
|
86
|
+
* no other provider replays, so their events always invalidate. */
|
|
87
|
+
export declare const REPLAY_WINDOW_MS = 1500;
|
|
76
88
|
export declare function mountCheckout(container: MinimalElement, checkout: CheckoutDto, options: MountOptions): CheckoutHandle;
|
package/dist/checkout/browser.js
CHANGED
|
@@ -1,7 +1,16 @@
|
|
|
1
1
|
import { controllerFor } from "./controllers.js";
|
|
2
|
+
import { createLiveShippingObserver } from "./live-shipping.js";
|
|
2
3
|
import { localProblem } from "./types.js";
|
|
3
4
|
const FINAL_ORDER = new Set(["completed", "pending"]);
|
|
5
|
+
/** Kustom replays its events once, about 1 s after the first `api.on` registration (K-L15). A shipping event within this
|
|
6
|
+
* window of a mount's first successful shipping subscription, before any later event, is that replay: it carries no
|
|
7
|
+
* change, so it is not an invalidation. It joins the explicit first read if that read has not started, and is dropped
|
|
8
|
+
* if a read already started at or after the subscription (that read already covers it). Applied to `klarna-kco` only:
|
|
9
|
+
* no other provider replays, so their events always invalidate. */
|
|
10
|
+
export const REPLAY_WINDOW_MS = 1500;
|
|
4
11
|
export function mountCheckout(container, checkout, options) {
|
|
12
|
+
if ((options.liveShipping === undefined) !== (options.onShipping === undefined))
|
|
13
|
+
throw new Error("liveShipping and onShipping must be provided together");
|
|
5
14
|
if (checkout.status !== "open" || !checkout.widget)
|
|
6
15
|
throw new Error("mountCheckout needs an `open` answer with a widget");
|
|
7
16
|
const doc = options.document ?? globalThis.document;
|
|
@@ -36,6 +45,7 @@ export function mountCheckout(container, checkout, options) {
|
|
|
36
45
|
let queue = Promise.resolve(); // *(plan review 1 #43)*
|
|
37
46
|
let unsubscribeDeclined;
|
|
38
47
|
let unsubscribeCannotComplete;
|
|
48
|
+
let unsubscribeShipping;
|
|
39
49
|
let expiryTimer;
|
|
40
50
|
let firedPreExpiryFor; // *(plan review 1 #42, plan review 2 #12)*
|
|
41
51
|
// Every pending sleep's resolver, so a remount or destroy can settle it instead of clearing its timer and leaving
|
|
@@ -79,6 +89,87 @@ export function mountCheckout(container, checkout, options) {
|
|
|
79
89
|
if (alive(gen))
|
|
80
90
|
options.onProblem(localProblem(code, message(e)), { status: 0 });
|
|
81
91
|
}
|
|
92
|
+
// ---- Live shipping (spec 2026-10-12 §4.6) ---------------------------------------------------------------------
|
|
93
|
+
// A separate observation flow, never driven through `dispatch` or `poll`. One observer per mount: a remount or destroy
|
|
94
|
+
// destroys it, so an answer or a provider callback of an older mount is dropped. Without `onShipping` it never exists.
|
|
95
|
+
let observer;
|
|
96
|
+
let observerSuspended = false; // the observer was told `suspended()` and not yet `resumed()`
|
|
97
|
+
let observerResumeReady = false; // the provider reported it is resumed (or this mount is fresh): resume once drained
|
|
98
|
+
let shippingBoundAt; // this mount's first successful shipping subscription
|
|
99
|
+
let lastReadAt; // when this mount's observer last started a read
|
|
100
|
+
let replayOver = false; // a non-replay shipping event was handled on this mount
|
|
101
|
+
function createObserver(gen) {
|
|
102
|
+
if (!options.onShipping || !options.liveShipping)
|
|
103
|
+
return undefined;
|
|
104
|
+
const onShipping = options.onShipping;
|
|
105
|
+
const liveShipping = options.liveShipping;
|
|
106
|
+
let reported = false; // SDK_LIVE_SHIPPING_FAILED once per mount, for liveShipping and onShipping alike
|
|
107
|
+
const report = (e) => {
|
|
108
|
+
if (reported || !alive(gen))
|
|
109
|
+
return;
|
|
110
|
+
reported = true;
|
|
111
|
+
try {
|
|
112
|
+
reportLocal(gen, "SDK_LIVE_SHIPPING_FAILED", e);
|
|
113
|
+
}
|
|
114
|
+
catch { /* an app callback: never reaches checkout */ }
|
|
115
|
+
};
|
|
116
|
+
shippingBoundAt = undefined;
|
|
117
|
+
lastReadAt = undefined;
|
|
118
|
+
replayOver = false;
|
|
119
|
+
return createLiveShippingObserver({
|
|
120
|
+
// A synchronous throw and a rejection are both reported, then rethrown: the observer still sees a failed read.
|
|
121
|
+
// The attempt is read at call time.
|
|
122
|
+
read: async () => {
|
|
123
|
+
lastReadAt = now();
|
|
124
|
+
try {
|
|
125
|
+
return await liveShipping(attemptId);
|
|
126
|
+
}
|
|
127
|
+
catch (e) {
|
|
128
|
+
report(e);
|
|
129
|
+
throw e;
|
|
130
|
+
}
|
|
131
|
+
},
|
|
132
|
+
deliver: (view) => {
|
|
133
|
+
if (!alive(gen))
|
|
134
|
+
return;
|
|
135
|
+
try {
|
|
136
|
+
onShipping(view);
|
|
137
|
+
}
|
|
138
|
+
catch (e) {
|
|
139
|
+
report(e);
|
|
140
|
+
}
|
|
141
|
+
},
|
|
142
|
+
now,
|
|
143
|
+
// The handle's timers, so a remount or destroy cancels them with every other.
|
|
144
|
+
setTimeout: (fn, ms) => { const h = setT(() => { timers.delete(h); fn(); }, ms); timers.add(h); return h; },
|
|
145
|
+
clearTimeout: (h) => { clearT(h); timers.delete(h); },
|
|
146
|
+
visible: () => doc.visibilityState === "visible",
|
|
147
|
+
random: Math.random,
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
function observerSuspend() {
|
|
151
|
+
if (!observer)
|
|
152
|
+
return;
|
|
153
|
+
observerResumeReady = false;
|
|
154
|
+
if (observerSuspended)
|
|
155
|
+
return;
|
|
156
|
+
observerSuspended = true;
|
|
157
|
+
observer.suspended();
|
|
158
|
+
}
|
|
159
|
+
/** The refresh after a sync waits for both: the provider is actually resumed and the sync queue has drained. */
|
|
160
|
+
function observerTryResume() {
|
|
161
|
+
if (!observer || !observerSuspended || !observerResumeReady || active > 0)
|
|
162
|
+
return;
|
|
163
|
+
observerSuspended = false;
|
|
164
|
+
observerResumeReady = false;
|
|
165
|
+
observer.resumed();
|
|
166
|
+
}
|
|
167
|
+
function observerProviderResumed(target) {
|
|
168
|
+
if (!observer || observer !== target)
|
|
169
|
+
return;
|
|
170
|
+
observerResumeReady = true;
|
|
171
|
+
observerTryResume();
|
|
172
|
+
}
|
|
82
173
|
function render(dto) {
|
|
83
174
|
const w = dto.widget;
|
|
84
175
|
container.innerHTML = "";
|
|
@@ -108,6 +199,7 @@ export function mountCheckout(container, checkout, options) {
|
|
|
108
199
|
}
|
|
109
200
|
function suspend() {
|
|
110
201
|
suspended = true;
|
|
202
|
+
observerSuspend();
|
|
111
203
|
if (suspendedOn === controller)
|
|
112
204
|
return;
|
|
113
205
|
// Recorded before the call, as on main: a controller whose suspend throws (a broken provider `lock()`) stays recorded, so a
|
|
@@ -124,6 +216,13 @@ export function mountCheckout(container, checkout, options) {
|
|
|
124
216
|
suspendedOn = undefined;
|
|
125
217
|
controller.resume(target);
|
|
126
218
|
} }
|
|
219
|
+
/** The app's own `handle.resume()`: no confirmed answer to wait for, so the observer resumes once the queue drains. */
|
|
220
|
+
function appResume() {
|
|
221
|
+
const was = suspended;
|
|
222
|
+
resume();
|
|
223
|
+
if (was)
|
|
224
|
+
observerProviderResumed(observer);
|
|
225
|
+
}
|
|
127
226
|
// ---- The locked burst (spec §10.3) ---------------------------------------------------------------------------
|
|
128
227
|
/** One SDK-owned `POST /checkout`; a transport rejection is reported and swallowed — the caller stops, the widget
|
|
129
228
|
* stays suspended and the queue is released *(plan review 2 #17)*. Posted directly: the rolling-minute ledger, its
|
|
@@ -191,6 +290,8 @@ export function mountCheckout(container, checkout, options) {
|
|
|
191
290
|
unsubscribeDeclined = undefined;
|
|
192
291
|
unsubscribeCannotComplete?.();
|
|
193
292
|
unsubscribeCannotComplete = undefined;
|
|
293
|
+
unsubscribeShipping?.();
|
|
294
|
+
unsubscribeShipping = undefined;
|
|
194
295
|
controller = bound;
|
|
195
296
|
const gen = generation;
|
|
196
297
|
// `render` appends the provider's script; it has not executed when this runs, so the global the controller reads
|
|
@@ -219,6 +320,25 @@ export function mountCheckout(container, checkout, options) {
|
|
|
219
320
|
// Spec F §7.1: Kustom's can_not_complete_order is the app's problem to show, not a validation decline (no sync).
|
|
220
321
|
bind(bound.onCannotComplete, () => { if (alive(gen))
|
|
221
322
|
options.onProblem(localProblem("SDK_PROVIDER_CANNOT_COMPLETE", "can_not_complete_order"), { status: 0 }); }, (off) => { unsubscribeCannotComplete = off; });
|
|
323
|
+
// Live shipping: only with `onShipping`. The provider's shipping signals are a trigger only (spec §4.6).
|
|
324
|
+
const obs = observer;
|
|
325
|
+
// Only Kustom replays its shipping events after registration (K-L15); every other provider's event is real, also
|
|
326
|
+
// inside the window, and always invalidates *(final review B)*.
|
|
327
|
+
const replays = dto.widget.controller === "klarna-kco";
|
|
328
|
+
const onShippingEvent = () => {
|
|
329
|
+
if (!alive(gen))
|
|
330
|
+
return;
|
|
331
|
+
if (replays && !replayOver && shippingBoundAt !== undefined && now() - shippingBoundAt <= REPLAY_WINDOW_MS) {
|
|
332
|
+
// Kustom's replay: no invalidation. Covered by a read started since the subscription, else it joins the first read.
|
|
333
|
+
if (lastReadAt === undefined || lastReadAt < shippingBoundAt)
|
|
334
|
+
obs.trigger("first");
|
|
335
|
+
return;
|
|
336
|
+
}
|
|
337
|
+
replayOver = true;
|
|
338
|
+
obs.trigger("event");
|
|
339
|
+
};
|
|
340
|
+
if (obs)
|
|
341
|
+
bind(bound.onShippingEvent, onShippingEvent, (off) => { unsubscribeShipping = off; shippingBoundAt ??= now(); });
|
|
222
342
|
}
|
|
223
343
|
function remount(dto) {
|
|
224
344
|
if (dto.status !== "open" || !dto.widget)
|
|
@@ -227,11 +347,20 @@ export function mountCheckout(container, checkout, options) {
|
|
|
227
347
|
// dispatch's catch reports it instead of dropping it for a generation that moved on *(final review B3 T2)*.
|
|
228
348
|
const next = controllerFor(dto.widget.controller, win, controllerTimer);
|
|
229
349
|
controller.dispose?.(); // spec F §7.1: the old mount's deferred callbacks and timers do nothing now
|
|
350
|
+
observer?.destroy(); // the old observation's sequence is gone with its mount
|
|
230
351
|
generation++;
|
|
231
352
|
cancelTimers(); // *(plan review 1 #37)*
|
|
232
353
|
expiryTimer = undefined;
|
|
233
354
|
firedPreExpiryFor = undefined;
|
|
234
355
|
attemptId = dto.attemptId;
|
|
356
|
+
// The new mount owns the recovery read: invalidate (`null`) now, read once the sync queue has drained — a remount
|
|
357
|
+
// inside a dispatch runs while `active > 0`, so the refresh is queued, never skipped (spec §4.6 Resume). The new
|
|
358
|
+
// widget needs no provider resume signal (no controller call after a remount); a sync still queued suspends it again.
|
|
359
|
+
observer = createObserver(generation);
|
|
360
|
+
observerSuspended = false;
|
|
361
|
+
observerResumeReady = false;
|
|
362
|
+
observerSuspend();
|
|
363
|
+
observerResumeReady = !!observer;
|
|
235
364
|
// The ruling of round 1 #39: no controller call after a remount (the new widget's script has not loaded), and the
|
|
236
365
|
// flag is cleared. Unless a sync is queued behind the one running: then payment stays suspended, and that sync
|
|
237
366
|
// suspends the new controller when it starts (`suspendedOn` is cleared) *(plan review 2 #11)*.
|
|
@@ -240,6 +369,7 @@ export function mountCheckout(container, checkout, options) {
|
|
|
240
369
|
render(dto);
|
|
241
370
|
bindProvider(dto, next);
|
|
242
371
|
armExpiry(dto);
|
|
372
|
+
observerTryResume();
|
|
243
373
|
}
|
|
244
374
|
const onVisible = () => { if (!destroyed && suspended && doc.visibilityState === "visible")
|
|
245
375
|
autonomousSync("visible", generation); };
|
|
@@ -302,6 +432,7 @@ export function mountCheckout(container, checkout, options) {
|
|
|
302
432
|
finally {
|
|
303
433
|
active--;
|
|
304
434
|
release();
|
|
435
|
+
observerTryResume(); // the refresh after a sync waits for the queue to drain (spec §4.6 Resume)
|
|
305
436
|
}
|
|
306
437
|
})();
|
|
307
438
|
}
|
|
@@ -356,9 +487,13 @@ export function mountCheckout(container, checkout, options) {
|
|
|
356
487
|
// Another update is queued behind this one: its mutation is still to come, so payment stays suspended
|
|
357
488
|
// until its own answer *(plan review 2 #11)*. The provider unlocks once its view shows `dto.cart`; if it
|
|
358
489
|
// never does, the widget is re-rendered from this answer *(plan review 1 #3)*.
|
|
359
|
-
if (active <= 1)
|
|
490
|
+
if (active <= 1) {
|
|
491
|
+
const obs = observer;
|
|
360
492
|
resume({ cart: dto.cart, cartVersion: Number(dto.cartVersion), onUnsynced: () => { if (alive(gen))
|
|
361
|
-
remount(dto); }
|
|
493
|
+
remount(dto); },
|
|
494
|
+
onResumed: () => { if (alive(gen))
|
|
495
|
+
observerProviderResumed(obs); } });
|
|
496
|
+
}
|
|
362
497
|
}
|
|
363
498
|
else {
|
|
364
499
|
remount(dto);
|
|
@@ -428,11 +563,15 @@ export function mountCheckout(container, checkout, options) {
|
|
|
428
563
|
const handle = {
|
|
429
564
|
get attemptId() { return attemptId; },
|
|
430
565
|
get suspended() { return suspended; },
|
|
431
|
-
suspend, resume, remount,
|
|
566
|
+
suspend, resume: appResume, remount,
|
|
432
567
|
destroy() {
|
|
433
568
|
destroyed = true;
|
|
434
569
|
generation++;
|
|
570
|
+
observer?.destroy();
|
|
571
|
+
observer = undefined;
|
|
435
572
|
cancelTimers(); // *(plan review 1 #37)*
|
|
573
|
+
unsubscribeShipping?.();
|
|
574
|
+
unsubscribeShipping = undefined;
|
|
436
575
|
unsubscribeDeclined?.();
|
|
437
576
|
unsubscribeDeclined = undefined;
|
|
438
577
|
unsubscribeCannotComplete?.();
|
|
@@ -456,8 +595,12 @@ export function mountCheckout(container, checkout, options) {
|
|
|
456
595
|
});
|
|
457
596
|
},
|
|
458
597
|
};
|
|
598
|
+
observer = createObserver(generation);
|
|
459
599
|
render(checkout);
|
|
460
600
|
bindProvider(checkout);
|
|
461
601
|
armExpiry(checkout);
|
|
602
|
+
// The explicit first read: Kustom replays its events once after the first registration and Qliro never replays, so the
|
|
603
|
+
// first view never depends on a provider event; a replay inside the debounce coalesces with it (spec §4.6 Binding).
|
|
604
|
+
observer?.trigger("first");
|
|
462
605
|
return handle;
|
|
463
606
|
}
|
|
@@ -20,6 +20,10 @@ export interface ProviderWindow {
|
|
|
20
20
|
/** Fires when our validation callback declined the purchase, with the `DeclineReason` it answered
|
|
21
21
|
* (developers.qliro.com → Listeners). */
|
|
22
22
|
onPaymentDeclined?(cb: (declineReason?: string, declineReasonMessage?: string) => void): void;
|
|
23
|
+
/** Shipping listeners (developers.qliro.com → Listeners). Registration replaces per event and nothing replays; the
|
|
24
|
+
* payload is never read, a call is only a refresh trigger. */
|
|
25
|
+
onShippingMethodChanged?(cb: (payload?: unknown) => void): void;
|
|
26
|
+
onShippingPriceChanged?(cb: (price?: unknown) => void): void;
|
|
23
27
|
};
|
|
24
28
|
_klarnaCheckout?: (cb: (api: KlarnaCheckoutApi) => void) => void;
|
|
25
29
|
walley?: {
|
|
@@ -42,6 +46,10 @@ export interface ResumeTarget {
|
|
|
42
46
|
cartVersion?: number;
|
|
43
47
|
/** The provider never showed `cart` before its deadline: the handle re-renders the widget from the confirmed answer. */
|
|
44
48
|
onUnsynced(): void;
|
|
49
|
+
/** The provider is actually resumed: Qliro on the matching `onOrderUpdated` (with the unlock), Kustom after `api.resume()`
|
|
50
|
+
* ran, Walley after its `resume()`; at once when nothing was held (the provider global is missing, or Kustom's throws).
|
|
51
|
+
* Never called on a Qliro deadline, nor when Qliro's lock was missed (that is `onUnsynced`). */
|
|
52
|
+
onResumed?(): void;
|
|
45
53
|
}
|
|
46
54
|
export interface CheckoutController {
|
|
47
55
|
/** Returns `false` when the suspension could not be handed to the provider yet (its script has not defined its API): the
|
|
@@ -57,6 +65,10 @@ export interface CheckoutController {
|
|
|
57
65
|
/** The provider says no offered payment method can complete the order (Kustom's `can_not_complete_order`). Same binding
|
|
58
66
|
* contract as `onDeclined`: `undefined` until the provider's script exposes it. */
|
|
59
67
|
onCannotComplete?(handler: () => void): (() => void) | undefined;
|
|
68
|
+
/** The provider's shipping-change signals (Kustom `shipping_option_change`, `shipping_address_change`,
|
|
69
|
+
* `order_total_change`; Qliro `onShippingMethodChanged`, `onShippingPriceChanged`). The handler is only a trigger. Same
|
|
70
|
+
* binding contract as `onDeclined`: `undefined` until the provider's script exposes the signals; returns the unsubscribe. */
|
|
71
|
+
onShippingEvent?(handler: () => void): (() => void) | undefined;
|
|
60
72
|
/** This mount is over (remount, destroy): every deferred provider callback and timer of this controller does nothing after it. */
|
|
61
73
|
dispose?(): void;
|
|
62
74
|
}
|
|
@@ -68,9 +80,13 @@ export type ControllerFactory = (win: ProviderWindow, timer: ControllerTimer) =>
|
|
|
68
80
|
/** How long `qliro-q1`'s resume waits for `q1.onOrderUpdated` to show the confirmed cart. Past it the widget is
|
|
69
81
|
* re-rendered, never unlocked blindly *(plan review 1 #3)*. */
|
|
70
82
|
export declare const QLIRO_SYNC_DEADLINE_MS = 10000;
|
|
83
|
+
/** How often `qliro-q1`'s shipping dispatcher checks whether `window.q1` is a new object (a re-render replaces it, and a
|
|
84
|
+
* registration on a stale instance is silent). */
|
|
85
|
+
export declare const QLIRO_REBIND_MS = 500;
|
|
71
86
|
/** Qliro's check before `unlock()` (*Update order*: "Verify that the total amount of the order = total amount in the
|
|
72
|
-
* shopping cart"), in öre, **or** the `MerchantUpdateVersion` our PUT sent (carriers 1b ruling R-S3-K1:
|
|
73
|
-
*
|
|
87
|
+
* shopping cart"), in öre, **or** the `MerchantUpdateVersion` our PUT sent (carriers 1b ruling R-S3-K1: Qliro's client
|
|
88
|
+
* `onOrderUpdated.totalPrice` is goods only while the GET total includes shipping (Q-L2), so on a shipping market the two
|
|
89
|
+
* totals can differ and the total alone is not a reliable match). The echo is a string in Qliro's schema; its
|
|
74
90
|
* name is read case-insensitively (VQ-8 records the real one). */
|
|
75
91
|
export declare function qliroOrderMatches(order: unknown, target: Pick<ResumeTarget, "cart" | "cartVersion">): boolean;
|
|
76
92
|
export declare const controllers: Record<"qliro-q1" | "klarna-kco" | "walley", ControllerFactory>;
|