@final-commerce/command-frame 0.8.0 → 0.8.1-online.2
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/dist/CommonTypes.d.ts +14 -0
- package/dist/actions/add-booking-to-cart/mock.js +12 -8
- package/dist/actions/add-product-to-cart/mock.js +4 -0
- package/dist/actions/attach-checkout-contact/action.d.ts +6 -0
- package/dist/actions/attach-checkout-contact/action.js +8 -0
- package/dist/actions/attach-checkout-contact/mock.d.ts +15 -0
- package/dist/actions/attach-checkout-contact/mock.js +22 -0
- package/dist/actions/attach-checkout-contact/types.d.ts +35 -0
- package/dist/actions/attach-checkout-contact/types.js +16 -0
- package/dist/actions/get-outlets/action.js +2 -2
- package/dist/actions/get-outlets/mock.d.ts +12 -0
- package/dist/actions/get-outlets/mock.js +28 -4
- package/dist/actions/get-outlets/types.d.ts +19 -1
- package/dist/actions/get-products/mock.js +13 -0
- package/dist/actions/get-products/types.d.ts +16 -0
- package/dist/actions/resume-checkout/action.d.ts +6 -0
- package/dist/actions/resume-checkout/action.js +8 -0
- package/dist/actions/resume-checkout/mock.d.ts +16 -0
- package/dist/actions/resume-checkout/mock.js +51 -0
- package/dist/actions/resume-checkout/types.d.ts +59 -0
- package/dist/actions/resume-checkout/types.js +13 -0
- package/dist/actions/resume-parked-order/mock.d.ts +1 -1
- package/dist/actions/resume-parked-order/mock.js +16 -10
- package/dist/actions/set-outlet/action.d.ts +6 -0
- package/dist/actions/set-outlet/action.js +8 -0
- package/dist/actions/set-outlet/mock.d.ts +13 -0
- package/dist/actions/set-outlet/mock.js +24 -0
- package/dist/actions/set-outlet/types.d.ts +31 -0
- package/dist/actions/set-outlet/types.js +21 -0
- package/dist/actions/start-checkout/action.d.ts +6 -0
- package/dist/actions/start-checkout/action.js +8 -0
- package/dist/actions/start-checkout/mock.d.ts +16 -0
- package/dist/actions/start-checkout/mock.js +80 -0
- package/dist/actions/start-checkout/types.d.ts +90 -0
- package/dist/actions/start-checkout/types.js +10 -0
- package/dist/actions/update-cart-item-quantity/mock.js +7 -1
- package/dist/index.d.ts +11 -1
- package/dist/index.js +9 -0
- package/dist/projects/render/mocks.js +10 -0
- package/dist/projects/render/types.d.ts +16 -1
- package/dist/pubsub/topics/checkout/checkout-started/types.d.ts +26 -0
- package/dist/pubsub/topics/checkout/checkout-started/types.js +1 -0
- package/dist/pubsub/topics/checkout/index.d.ts +7 -0
- package/dist/pubsub/topics/checkout/index.js +28 -0
- package/dist/pubsub/topics/checkout/payment-completed/types.d.ts +31 -0
- package/dist/pubsub/topics/checkout/payment-completed/types.js +1 -0
- package/dist/pubsub/topics/checkout/payment-failed/types.d.ts +20 -0
- package/dist/pubsub/topics/checkout/payment-failed/types.js +1 -0
- package/dist/pubsub/topics/checkout/types.d.ts +12 -0
- package/dist/pubsub/topics/checkout/types.js +8 -0
- package/dist/pubsub/topics/index.d.ts +18 -17
- package/dist/pubsub/topics/index.js +18 -17
- package/dist/pubsub/topics/types.d.ts +8 -0
- package/package.json +2 -2
package/dist/CommonTypes.d.ts
CHANGED
|
@@ -181,9 +181,23 @@ export interface CFOutletInfo {
|
|
|
181
181
|
state?: string;
|
|
182
182
|
postCode?: string;
|
|
183
183
|
};
|
|
184
|
+
address2?: string;
|
|
184
185
|
city?: string;
|
|
185
186
|
state?: string;
|
|
186
187
|
country?: string;
|
|
188
|
+
postCode?: string;
|
|
189
|
+
phone?: string;
|
|
190
|
+
/** Short display alias, when the merchant set one (e.g. `DTWN`). */
|
|
191
|
+
alias?: string;
|
|
192
|
+
/**
|
|
193
|
+
* Whether this location can take an ONLINE payment. Storefront only — it is
|
|
194
|
+
* absent on a register, where it has no meaning.
|
|
195
|
+
*
|
|
196
|
+
* `false` means the outlet has no completed payment connection, so starting a
|
|
197
|
+
* checkout against it WILL fail. A pickup picker must not offer it as a
|
|
198
|
+
* selectable location, or must show it as unavailable.
|
|
199
|
+
*/
|
|
200
|
+
connected?: boolean;
|
|
187
201
|
}
|
|
188
202
|
export interface CFContextManage {
|
|
189
203
|
user: unknown;
|
|
@@ -2,14 +2,6 @@ import { MOCK_CART, MOCK_PRODUCTS, mockPublishEvent } from '../../demo/database'
|
|
|
2
2
|
import { mockHoldBooking } from '../hold-booking/mock';
|
|
3
3
|
export const mockAddBookingToCart = async (params) => {
|
|
4
4
|
console.log('[Mock] addBookingToCart called', params);
|
|
5
|
-
// One call, two effects — the window is claimed AND the service is in the cart. The claim goes
|
|
6
|
-
// first and its refusal propagates: a cart line for a window somebody else took is worse than
|
|
7
|
-
// no line at all.
|
|
8
|
-
const held = await mockHoldBooking(params);
|
|
9
|
-
if (!held.success || !held.booking) {
|
|
10
|
-
return { success: false, reason: held.reason, timestamp: new Date().toISOString() };
|
|
11
|
-
}
|
|
12
|
-
const booking = held.booking;
|
|
13
5
|
// An unknown product used to be sold as "Booking" at zero. The host refuses it, and a mock that
|
|
14
6
|
// quietly sells something no catalogue contains teaches a screen a case that cannot happen.
|
|
15
7
|
const product = MOCK_PRODUCTS.find(({ _id }) => _id === params.productId);
|
|
@@ -27,6 +19,18 @@ export const mockAddBookingToCart = async (params) => {
|
|
|
27
19
|
timestamp: new Date().toISOString(),
|
|
28
20
|
};
|
|
29
21
|
}
|
|
22
|
+
// Same refusal and text as the host (kaching add-booking-to-cart).
|
|
23
|
+
if (product.notSoldInPos || variant?.notSoldInPos) {
|
|
24
|
+
return { success: false, reason: `${product.name} is not sold in POS`, timestamp: new Date().toISOString() };
|
|
25
|
+
}
|
|
26
|
+
// One call, two effects — the window is claimed AND the service is in the cart. Like the host, the
|
|
27
|
+
// claim comes after the product checks and before the line; its refusal propagates: a cart line for
|
|
28
|
+
// a window somebody else took is worse than no line at all.
|
|
29
|
+
const held = await mockHoldBooking(params);
|
|
30
|
+
if (!held.success || !held.booking) {
|
|
31
|
+
return { success: false, reason: held.reason, timestamp: new Date().toISOString() };
|
|
32
|
+
}
|
|
33
|
+
const booking = held.booking;
|
|
30
34
|
const price = variant?.price ?? product.minPrice ?? 0;
|
|
31
35
|
const reservation = {
|
|
32
36
|
internalId: `res_${booking.id}`,
|
|
@@ -21,6 +21,10 @@ export const mockAddProductToCart = async (params) => {
|
|
|
21
21
|
}
|
|
22
22
|
}
|
|
23
23
|
}
|
|
24
|
+
// Same refusal and text as the host (kaching add-product-to-cart).
|
|
25
|
+
if (product.notSoldInPos || variant.notSoldInPos) {
|
|
26
|
+
throw new Error(`${product.name} is not sold in POS`);
|
|
27
|
+
}
|
|
24
28
|
// A measured variant carries a resolved unit; the host does the same. What the unit can
|
|
25
29
|
// express is its precision — 1.5 of a kilogram is a sale, 1.5 of a piece is a typo.
|
|
26
30
|
const unit = variant.unitId ? resolveUnit(variant.unitId) : undefined;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Attach Checkout Contact action
|
|
3
|
+
* Calls the attachCheckoutContact action on the parent window
|
|
4
|
+
*/
|
|
5
|
+
import { commandFrameClient } from '../../client';
|
|
6
|
+
export const attachCheckoutContact = async (params) => {
|
|
7
|
+
return await commandFrameClient.call('attachCheckoutContact', params);
|
|
8
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { AttachCheckoutContact } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Standalone mock of attaching contact to an online order.
|
|
4
|
+
*
|
|
5
|
+
* It mirrors the one REFUSAL the real command makes — a missing email — because
|
|
6
|
+
* that is the mistake a builder actually hits, and a mock that accepted anything
|
|
7
|
+
* would let a checkout screen look finished and lose receipts on the published
|
|
8
|
+
* site.
|
|
9
|
+
*
|
|
10
|
+
* It does NOT mirror "no order to attach to". The real command answers
|
|
11
|
+
* `attached: false` there rather than throwing, precisely so a flow can call it
|
|
12
|
+
* optimistically and still let the shopper pay; the mock always has a checkout
|
|
13
|
+
* to attach to, so it reports `true`.
|
|
14
|
+
*/
|
|
15
|
+
export declare const mockAttachCheckoutContact: AttachCheckoutContact;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Standalone mock of attaching contact to an online order.
|
|
3
|
+
*
|
|
4
|
+
* It mirrors the one REFUSAL the real command makes — a missing email — because
|
|
5
|
+
* that is the mistake a builder actually hits, and a mock that accepted anything
|
|
6
|
+
* would let a checkout screen look finished and lose receipts on the published
|
|
7
|
+
* site.
|
|
8
|
+
*
|
|
9
|
+
* It does NOT mirror "no order to attach to". The real command answers
|
|
10
|
+
* `attached: false` there rather than throwing, precisely so a flow can call it
|
|
11
|
+
* optimistically and still let the shopper pay; the mock always has a checkout
|
|
12
|
+
* to attach to, so it reports `true`.
|
|
13
|
+
*/
|
|
14
|
+
export const mockAttachCheckoutContact = async (params) => {
|
|
15
|
+
console.log('[Mock] attachCheckoutContact called', params);
|
|
16
|
+
if (!params)
|
|
17
|
+
throw new Error('Params required');
|
|
18
|
+
if (!params.email || !params.email.trim()) {
|
|
19
|
+
throw new Error('attachCheckoutContact: email is required — it is where the receipt goes');
|
|
20
|
+
}
|
|
21
|
+
return { attached: true, timestamp: new Date().toISOString() };
|
|
22
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Guest contact for the order. There is no shopper account, so this is the only
|
|
3
|
+
* way to reach them — `email` is where the receipt goes and is required.
|
|
4
|
+
*/
|
|
5
|
+
export interface AttachCheckoutContactParams {
|
|
6
|
+
email: string;
|
|
7
|
+
name?: string;
|
|
8
|
+
phone?: string;
|
|
9
|
+
/** Defaults to the outlet the storefront booted against. */
|
|
10
|
+
outletId?: string;
|
|
11
|
+
}
|
|
12
|
+
export interface AttachCheckoutContactResponse {
|
|
13
|
+
/**
|
|
14
|
+
* `false` means there was nothing to attach to — no checkout had been started
|
|
15
|
+
* for this cart. It is NOT a failure to record the contact on an order that
|
|
16
|
+
* exists, and it never means the shopper was charged.
|
|
17
|
+
*/
|
|
18
|
+
attached: boolean;
|
|
19
|
+
timestamp: string;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Put the shopper's contact on the order `startCheckout` already created.
|
|
23
|
+
*
|
|
24
|
+
* Call it BEFORE paying, whenever the email is known — on blur, on submit, or
|
|
25
|
+
* as the shopper leaves the contact step. It reuses the order's idempotency
|
|
26
|
+
* key, so it updates that order rather than creating a second one.
|
|
27
|
+
*
|
|
28
|
+
* SAFE TO CALL TWICE. The server only ever ADDS contact to an order that has
|
|
29
|
+
* none, so a later call cannot redirect a receipt that was already addressed.
|
|
30
|
+
*
|
|
31
|
+
* NEVER THROWS FOR A MISSING ORDER. Failing to record an email must not stop a
|
|
32
|
+
* payment the shopper is trying to make, so the caller gets `attached: false`
|
|
33
|
+
* and can carry on to pay.
|
|
34
|
+
*/
|
|
35
|
+
export type AttachCheckoutContact = (params: AttachCheckoutContactParams) => Promise<AttachCheckoutContactResponse>;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// Attach Checkout Contact Types
|
|
2
|
+
//
|
|
3
|
+
// The second half of the online checkout. `startCheckout` creates the order and
|
|
4
|
+
// mounts the payment fields the moment a shopper ARRIVES; this puts their email
|
|
5
|
+
// on that same order once they have typed it.
|
|
6
|
+
//
|
|
7
|
+
// WHY THESE ARE TWO COMMANDS. The card fields cannot render without a payment
|
|
8
|
+
// session, a session needs a server-priced amount, and an amount needs an order.
|
|
9
|
+
// So the order has to exist before the shopper has typed anything. Demanding an
|
|
10
|
+
// email first is what forced the old "fill the form, then see the card fields"
|
|
11
|
+
// checkout — and it also meant every cart edit minted a NEW order, because the
|
|
12
|
+
// idempotency key moved with the cart.
|
|
13
|
+
//
|
|
14
|
+
// Served ONLY by the storefront runtime (a published website), like
|
|
15
|
+
// `startCheckout` itself. On a register it does not exist.
|
|
16
|
+
export {};
|
|
@@ -1,2 +1,14 @@
|
|
|
1
1
|
import { GetOutlets } from "./types";
|
|
2
|
+
/**
|
|
3
|
+
* A second location that CANNOT take an online payment.
|
|
4
|
+
*
|
|
5
|
+
* The shared demo data has exactly one outlet, so without this a pickup picker
|
|
6
|
+
* built against mocks would never render the case that matters: an outlet a
|
|
7
|
+
* shopper must not be allowed to check out against. `setOutlet` refuses this id,
|
|
8
|
+
* so the two mocks tell the same story.
|
|
9
|
+
*
|
|
10
|
+
* Mock-only, and deliberately NOT added to `MOCK_OUTLETS` — that array is shared
|
|
11
|
+
* with every other outlet mock and `MOCK_OUTLET`.
|
|
12
|
+
*/
|
|
13
|
+
export declare const MOCK_UNCONNECTED_OUTLET_ID = "outlet-mock-unconnected";
|
|
2
14
|
export declare const mockGetOutlets: GetOutlets;
|
|
@@ -1,16 +1,40 @@
|
|
|
1
1
|
import { MOCK_OUTLETS, safeSerialize } from "../../demo/database";
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
/**
|
|
3
|
+
* A second location that CANNOT take an online payment.
|
|
4
|
+
*
|
|
5
|
+
* The shared demo data has exactly one outlet, so without this a pickup picker
|
|
6
|
+
* built against mocks would never render the case that matters: an outlet a
|
|
7
|
+
* shopper must not be allowed to check out against. `setOutlet` refuses this id,
|
|
8
|
+
* so the two mocks tell the same story.
|
|
9
|
+
*
|
|
10
|
+
* Mock-only, and deliberately NOT added to `MOCK_OUTLETS` — that array is shared
|
|
11
|
+
* with every other outlet mock and `MOCK_OUTLET`.
|
|
12
|
+
*/
|
|
13
|
+
export const MOCK_UNCONNECTED_OUTLET_ID = "outlet-mock-unconnected";
|
|
14
|
+
const UNCONNECTED_OUTLET = {
|
|
15
|
+
_id: MOCK_UNCONNECTED_OUTLET_ID,
|
|
16
|
+
name: "Airport (not connected)",
|
|
17
|
+
address: "2 Terminal Road",
|
|
18
|
+
city: "San Francisco",
|
|
19
|
+
state: "CA",
|
|
20
|
+
country: "US",
|
|
21
|
+
postCode: "94128",
|
|
22
|
+
connected: false
|
|
23
|
+
};
|
|
24
|
+
export const mockGetOutlets = async (params) => {
|
|
25
|
+
console.log("[Mock] getOutlets called", params);
|
|
4
26
|
const outlets = safeSerialize(MOCK_OUTLETS).map((o) => ({
|
|
5
27
|
_id: o._id || o.id,
|
|
6
28
|
name: o.name || "",
|
|
7
29
|
address: o.address,
|
|
8
30
|
city: o.city,
|
|
9
31
|
state: o.state,
|
|
10
|
-
country: o.country
|
|
32
|
+
country: o.country,
|
|
33
|
+
connected: true
|
|
11
34
|
}));
|
|
35
|
+
const all = [...outlets, UNCONNECTED_OUTLET];
|
|
12
36
|
return {
|
|
13
|
-
outlets,
|
|
37
|
+
outlets: params?.connectedOnly ? all.filter((o) => o.connected) : all,
|
|
14
38
|
timestamp: new Date().toISOString()
|
|
15
39
|
};
|
|
16
40
|
};
|
|
@@ -1,6 +1,24 @@
|
|
|
1
1
|
import { CFOutletInfo } from "../../CommonTypes";
|
|
2
|
+
export interface GetOutletsParams {
|
|
3
|
+
/**
|
|
4
|
+
* Return only locations that can take an online payment. Storefront only.
|
|
5
|
+
* Default `false`, so a flow sees every outlet and decides how to present
|
|
6
|
+
* the ones it cannot sell from.
|
|
7
|
+
*/
|
|
8
|
+
connectedOnly?: boolean;
|
|
9
|
+
}
|
|
2
10
|
export interface GetOutletsResponse {
|
|
3
11
|
outlets: CFOutletInfo[];
|
|
4
12
|
timestamp: string;
|
|
5
13
|
}
|
|
6
|
-
|
|
14
|
+
/**
|
|
15
|
+
* List the company's outlets.
|
|
16
|
+
*
|
|
17
|
+
* ON A STOREFRONT this is the pickup picker's source. Each outlet carries
|
|
18
|
+
* `connected`, and an outlet with `connected: false` cannot take a payment, so
|
|
19
|
+
* `setOutlet` refuses it — filter those out rather than relying on the refusal.
|
|
20
|
+
*
|
|
21
|
+
* There are no coordinates on an outlet, so "closest branch" is not something
|
|
22
|
+
* this can answer; sort on the address fields or let the shopper choose.
|
|
23
|
+
*/
|
|
24
|
+
export type GetOutlets = (params?: GetOutletsParams) => Promise<GetOutletsResponse>;
|
|
@@ -11,6 +11,19 @@ export const mockGetProducts = async (params) => {
|
|
|
11
11
|
if (!JSON.stringify(query).includes('productType')) {
|
|
12
12
|
products = products.filter((p) => p.productType !== 'booking');
|
|
13
13
|
}
|
|
14
|
+
// Same as the host: `true` = flagged, `false` = not flagged (missing counts).
|
|
15
|
+
for (const flag of ['notSoldInPos', 'notSoldOnline']) {
|
|
16
|
+
if (typeof query[flag] === 'boolean')
|
|
17
|
+
products = products.filter((p) => !!p[flag] === query[flag]);
|
|
18
|
+
}
|
|
19
|
+
if (!params?.includeUnlisted) {
|
|
20
|
+
// Same as the host: flagged variants are stripped, a product left with none is dropped.
|
|
21
|
+
if (query.notSoldInPos === undefined)
|
|
22
|
+
products = products.filter((p) => !p.notSoldInPos);
|
|
23
|
+
products = products
|
|
24
|
+
.filter((p) => !p.variants.length || p.variants.some((v) => !v.notSoldInPos))
|
|
25
|
+
.map((p) => ({ ...p, variants: p.variants.filter((v) => !v.notSoldInPos) }));
|
|
26
|
+
}
|
|
14
27
|
if (query.searchValue) {
|
|
15
28
|
const search = String(query.searchValue).toLowerCase();
|
|
16
29
|
products = products.filter((p) => p.name.toLowerCase().includes(search) || p.sku?.toLowerCase().includes(search));
|
|
@@ -42,8 +42,24 @@ export interface GetProductsParams {
|
|
|
42
42
|
};
|
|
43
43
|
supplier?: string;
|
|
44
44
|
externalId?: string;
|
|
45
|
+
/**
|
|
46
|
+
* Product-level flag filter. `true` = only products flagged `notSoldInPos`; `false` = only
|
|
47
|
+
* products NOT flagged (a missing field counts as not flagged). On a till, naming it switches off
|
|
48
|
+
* the default exclusion, so `true` works without `includeUnlisted`.
|
|
49
|
+
*/
|
|
50
|
+
notSoldInPos?: boolean;
|
|
51
|
+
/** Product-level flag filter: `true` = flagged `notSoldOnline`; `false` = sold online (missing counts). On a storefront, naming it switches off the default exclusion. */
|
|
52
|
+
notSoldOnline?: boolean;
|
|
45
53
|
[key: string]: any;
|
|
46
54
|
};
|
|
55
|
+
/**
|
|
56
|
+
* The sales-channel flag follows the host channel: `notSoldInPos` on a till, `notSoldOnline` on a
|
|
57
|
+
* storefront. Products with that flag are left out unless this is `true` (default `false`) or the
|
|
58
|
+
* query names that flag. They still cannot be sold: `addProductToCart` / `addBookingToCart` refuse a
|
|
59
|
+
* product or a variant with it. Without it, flagged variants are also stripped from `variants[]`
|
|
60
|
+
* and a product whose every variant is flagged is left out — whatever the query says.
|
|
61
|
+
*/
|
|
62
|
+
includeUnlisted?: boolean;
|
|
47
63
|
/** Defaults to 0. */
|
|
48
64
|
offset?: number;
|
|
49
65
|
/** Defaults to 100. */
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resume Checkout action
|
|
3
|
+
* Calls the resumeCheckout action on the parent window
|
|
4
|
+
*/
|
|
5
|
+
import { commandFrameClient } from '../../client';
|
|
6
|
+
export const resumeCheckout = async (params) => {
|
|
7
|
+
return await commandFrameClient.call('resumeCheckout', params ?? {});
|
|
8
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { ResumeCheckout } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Standalone mock of the checkout return leg.
|
|
4
|
+
*
|
|
5
|
+
* It reads the SAME query parameters the real provider sends a shopper back
|
|
6
|
+
* with, so a builder can exercise the redirect path in preview by appending
|
|
7
|
+
* them by hand:
|
|
8
|
+
*
|
|
9
|
+
* ?sessionId=CS_MOCK&redirectResult=MOCK
|
|
10
|
+
*
|
|
11
|
+
* Without them it answers `{ resumed: false }` and does nothing, which is what
|
|
12
|
+
* the real command does on an ordinary page load. That is the branch a
|
|
13
|
+
* checkout screen hits on every normal visit, so it is the one worth getting
|
|
14
|
+
* right in a mock.
|
|
15
|
+
*/
|
|
16
|
+
export declare const mockResumeCheckout: ResumeCheckout;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { mockPublishEvent } from '../../demo/database';
|
|
2
|
+
/**
|
|
3
|
+
* Standalone mock of the checkout return leg.
|
|
4
|
+
*
|
|
5
|
+
* It reads the SAME query parameters the real provider sends a shopper back
|
|
6
|
+
* with, so a builder can exercise the redirect path in preview by appending
|
|
7
|
+
* them by hand:
|
|
8
|
+
*
|
|
9
|
+
* ?sessionId=CS_MOCK&redirectResult=MOCK
|
|
10
|
+
*
|
|
11
|
+
* Without them it answers `{ resumed: false }` and does nothing, which is what
|
|
12
|
+
* the real command does on an ordinary page load. That is the branch a
|
|
13
|
+
* checkout screen hits on every normal visit, so it is the one worth getting
|
|
14
|
+
* right in a mock.
|
|
15
|
+
*/
|
|
16
|
+
export const mockResumeCheckout = async (params) => {
|
|
17
|
+
console.log('[Mock] resumeCheckout called', params);
|
|
18
|
+
const href = params?.url ?? (typeof window === 'undefined' ? '' : window.location.href);
|
|
19
|
+
let redirectResult = null;
|
|
20
|
+
try {
|
|
21
|
+
redirectResult = new URL(href).searchParams.get('redirectResult');
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
redirectResult = null;
|
|
25
|
+
}
|
|
26
|
+
if (!redirectResult) {
|
|
27
|
+
return {
|
|
28
|
+
success: true,
|
|
29
|
+
timestamp: new Date().toISOString(),
|
|
30
|
+
checkout: { resumed: false },
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
const suffix = String(Date.now()).slice(-6);
|
|
34
|
+
const checkout = {
|
|
35
|
+
resumed: true,
|
|
36
|
+
orderId: `mock-order-${suffix}`,
|
|
37
|
+
receiptId: `ON-001-${suffix}`,
|
|
38
|
+
// `Authorised` is what the real provider reports for a completed redirect.
|
|
39
|
+
// It is NOT settlement — see the checkout topic docs.
|
|
40
|
+
resultCode: 'Authorised',
|
|
41
|
+
sessionResult: `mock-session-result-${suffix}`,
|
|
42
|
+
orderPassword: `mock-pw-${suffix}`,
|
|
43
|
+
};
|
|
44
|
+
mockPublishEvent('checkout', 'payment-completed', {
|
|
45
|
+
orderId: checkout.orderId,
|
|
46
|
+
receiptId: checkout.receiptId,
|
|
47
|
+
resultCode: checkout.resultCode,
|
|
48
|
+
sessionResult: checkout.sessionResult,
|
|
49
|
+
});
|
|
50
|
+
return { success: true, timestamp: new Date().toISOString(), checkout };
|
|
51
|
+
};
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
export interface ResumeCheckoutParams {
|
|
2
|
+
/**
|
|
3
|
+
* Where to read the provider's return data from. Defaults to the current
|
|
4
|
+
* page URL, which is where the shopper has just landed.
|
|
5
|
+
*
|
|
6
|
+
* Only override this if you moved the query string somewhere else before
|
|
7
|
+
* calling — for example if your router strips it on mount.
|
|
8
|
+
*/
|
|
9
|
+
url?: string;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* What the return leg produced. Every field past `resumed` is absent when
|
|
13
|
+
* `resumed` is `false`.
|
|
14
|
+
*/
|
|
15
|
+
export interface ResumeCheckoutResult {
|
|
16
|
+
/**
|
|
17
|
+
* `false` means THIS WAS AN ORDINARY PAGE LOAD — no provider return data was
|
|
18
|
+
* in the URL, so there was nothing to finish and nothing happened. It is not
|
|
19
|
+
* an error, and it is the answer on every normal visit to the page. Call this
|
|
20
|
+
* unconditionally when your checkout or confirmation screen mounts and branch
|
|
21
|
+
* on this field.
|
|
22
|
+
*/
|
|
23
|
+
resumed: boolean;
|
|
24
|
+
orderId?: string;
|
|
25
|
+
receiptId?: string;
|
|
26
|
+
/**
|
|
27
|
+
* The provider's result code for the completed attempt, e.g. `Authorised` or
|
|
28
|
+
* `Refused`. As everywhere else in this topic, `Authorised` is an
|
|
29
|
+
* authorisation and NOT a settlement.
|
|
30
|
+
*/
|
|
31
|
+
resultCode?: string;
|
|
32
|
+
/** Opaque proof of the payment — see `PaymentCompletedPayload.sessionResult`. */
|
|
33
|
+
sessionResult?: string;
|
|
34
|
+
/**
|
|
35
|
+
* The one-time order password from the original checkout, recovered from
|
|
36
|
+
* browser storage. The page that held it in memory is gone, so this is the
|
|
37
|
+
* only way back to the order's status after a redirect.
|
|
38
|
+
*/
|
|
39
|
+
orderPassword?: string;
|
|
40
|
+
}
|
|
41
|
+
export interface ResumeCheckoutResponse {
|
|
42
|
+
success: boolean;
|
|
43
|
+
timestamp: string;
|
|
44
|
+
checkout: ResumeCheckoutResult;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Finish a checkout the shopper was redirected away from.
|
|
48
|
+
*
|
|
49
|
+
* SAFE TO CALL ON EVERY PAGE LOAD, and that is how it is meant to be used: on
|
|
50
|
+
* an ordinary visit it finds no return data and answers `{ resumed: false }`
|
|
51
|
+
* without touching the network. Calling it twice for one return is also safe —
|
|
52
|
+
* the return data is consumed the first time.
|
|
53
|
+
*
|
|
54
|
+
* The outcome is ALSO published on the `checkout` topic (`payment-completed` or
|
|
55
|
+
* `payment-failed`), exactly as it would have been had the shopper never left,
|
|
56
|
+
* so a page that already subscribes needs no second code path. Subscribe first,
|
|
57
|
+
* then call this, or the event fires before you are listening.
|
|
58
|
+
*/
|
|
59
|
+
export type ResumeCheckout = (params?: ResumeCheckoutParams) => Promise<ResumeCheckoutResponse>;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// Resume Checkout Types
|
|
2
|
+
//
|
|
3
|
+
// The RETURN LEG of an online checkout. Some payment methods — and 3-D Secure
|
|
4
|
+
// on a card — take the shopper away to their bank or wallet and send them back
|
|
5
|
+
// to `returnUrl` on a FRESH PAGE LOAD. The payment is not finished at that
|
|
6
|
+
// point: the provider hands the browser a one-time result in the URL, and it
|
|
7
|
+
// has to be handed back to the provider to complete the charge.
|
|
8
|
+
//
|
|
9
|
+
// `startCheckout` cannot do this. It ran in the page that is now gone.
|
|
10
|
+
//
|
|
11
|
+
// Served ONLY by the storefront runtime (a published website), like
|
|
12
|
+
// `startCheckout`. On a register it does not exist.
|
|
13
|
+
export {};
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { ResumeParkedOrder } from
|
|
1
|
+
import { ResumeParkedOrder } from './types';
|
|
2
2
|
export declare const mockResumeParkedOrder: ResumeParkedOrder;
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { MOCK_PARKED_ORDERS, MOCK_CART, resetMockCart, mockPublishEvent } from
|
|
1
|
+
import { MOCK_PARKED_ORDERS, MOCK_CART, MOCK_PRODUCTS, resetMockCart, mockPublishEvent } from '../../demo/database';
|
|
2
2
|
export const mockResumeParkedOrder = async (params) => {
|
|
3
|
-
console.log(
|
|
3
|
+
console.log('[Mock] resumeParkedOrder called', params);
|
|
4
4
|
const orderId = params?.orderId;
|
|
5
5
|
let orderToResume = null;
|
|
6
6
|
let index = -1;
|
|
7
7
|
if (orderId) {
|
|
8
|
-
index = MOCK_PARKED_ORDERS.findIndex(o => o._id === orderId);
|
|
8
|
+
index = MOCK_PARKED_ORDERS.findIndex((o) => o._id === orderId);
|
|
9
9
|
if (index !== -1)
|
|
10
10
|
orderToResume = MOCK_PARKED_ORDERS[index];
|
|
11
11
|
}
|
|
@@ -17,8 +17,14 @@ export const mockResumeParkedOrder = async (params) => {
|
|
|
17
17
|
}
|
|
18
18
|
}
|
|
19
19
|
if (!orderToResume) {
|
|
20
|
-
throw new Error(
|
|
20
|
+
throw new Error('Parked order not found');
|
|
21
21
|
}
|
|
22
|
+
// Same refusal and text as the host, before anything is restored: the parked order stays.
|
|
23
|
+
const notSoldNames = orderToResume.lineItems
|
|
24
|
+
.filter((li) => MOCK_PRODUCTS.some((p) => p.variants.some((v) => v._id === li.variantId && (p.notSoldInPos || v.notSoldInPos))))
|
|
25
|
+
.map((li) => li.name);
|
|
26
|
+
if (notSoldNames.length)
|
|
27
|
+
throw new Error(`${notSoldNames.join(', ')} is not sold in POS`);
|
|
22
28
|
// Restore to MOCK_CART
|
|
23
29
|
// Logic to convert Order back to Cart (simplified)
|
|
24
30
|
resetMockCart();
|
|
@@ -26,7 +32,7 @@ export const mockResumeParkedOrder = async (params) => {
|
|
|
26
32
|
MOCK_CART.customer = orderToResume.customer; // Cast if needed
|
|
27
33
|
// MOCK_CART.products = orderToResume.lineItems... (Mapping needed)
|
|
28
34
|
// For Mock demo, we'll do a best effort mapping
|
|
29
|
-
MOCK_CART.products = orderToResume.lineItems.map(li => ({
|
|
35
|
+
MOCK_CART.products = orderToResume.lineItems.map((li) => ({
|
|
30
36
|
id: li.productId,
|
|
31
37
|
name: li.name,
|
|
32
38
|
quantity: li.quantity,
|
|
@@ -36,8 +42,8 @@ export const mockResumeParkedOrder = async (params) => {
|
|
|
36
42
|
sku: li.sku,
|
|
37
43
|
images: [li.image],
|
|
38
44
|
stock: 100,
|
|
39
|
-
taxTableId:
|
|
40
|
-
attributes: li.attributes
|
|
45
|
+
taxTableId: '',
|
|
46
|
+
attributes: li.attributes,
|
|
41
47
|
}));
|
|
42
48
|
MOCK_CART.subtotal = orderToResume.summary.subtotalAfterFees;
|
|
43
49
|
MOCK_CART.total = orderToResume.summary.total;
|
|
@@ -46,11 +52,11 @@ export const mockResumeParkedOrder = async (params) => {
|
|
|
46
52
|
// Remove from parked
|
|
47
53
|
MOCK_PARKED_ORDERS.splice(index, 1);
|
|
48
54
|
// Refresh the restored cart and the order lists.
|
|
49
|
-
mockPublishEvent(
|
|
50
|
-
mockPublishEvent(
|
|
55
|
+
mockPublishEvent('cart', 'parked-order-resumed', { orderId });
|
|
56
|
+
mockPublishEvent('orders', 'parked-order-resumed', { orderId });
|
|
51
57
|
return {
|
|
52
58
|
success: true,
|
|
53
59
|
order: orderToResume,
|
|
54
|
-
timestamp: new Date().toISOString()
|
|
60
|
+
timestamp: new Date().toISOString(),
|
|
55
61
|
};
|
|
56
62
|
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { SetOutlet } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Standalone mock of switching location.
|
|
4
|
+
*
|
|
5
|
+
* It mirrors the two REFUSALS the real command makes — a missing id, and an
|
|
6
|
+
* outlet that cannot take a payment — because those are the mistakes a builder
|
|
7
|
+
* actually hits. A mock that accepted anything would let a location picker look
|
|
8
|
+
* finished and then strand a shopper at a dead payment step on the real site.
|
|
9
|
+
*
|
|
10
|
+
* The unconnected id comes from the `getOutlets` mock rather than being repeated
|
|
11
|
+
* here, so the two cannot drift into telling different stories.
|
|
12
|
+
*/
|
|
13
|
+
export declare const mockSetOutlet: SetOutlet;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { MOCK_UNCONNECTED_OUTLET_ID } from '../get-outlets/mock';
|
|
2
|
+
/**
|
|
3
|
+
* Standalone mock of switching location.
|
|
4
|
+
*
|
|
5
|
+
* It mirrors the two REFUSALS the real command makes — a missing id, and an
|
|
6
|
+
* outlet that cannot take a payment — because those are the mistakes a builder
|
|
7
|
+
* actually hits. A mock that accepted anything would let a location picker look
|
|
8
|
+
* finished and then strand a shopper at a dead payment step on the real site.
|
|
9
|
+
*
|
|
10
|
+
* The unconnected id comes from the `getOutlets` mock rather than being repeated
|
|
11
|
+
* here, so the two cannot drift into telling different stories.
|
|
12
|
+
*/
|
|
13
|
+
export const mockSetOutlet = async (params) => {
|
|
14
|
+
console.log('[Mock] setOutlet called', params);
|
|
15
|
+
if (!params)
|
|
16
|
+
throw new Error('Params required');
|
|
17
|
+
if (!params.outletId || !params.outletId.trim()) {
|
|
18
|
+
throw new Error('setOutlet: outletId is required');
|
|
19
|
+
}
|
|
20
|
+
if (params.outletId === MOCK_UNCONNECTED_OUTLET_ID) {
|
|
21
|
+
throw new Error(`setOutlet: outlet ${params.outletId} cannot take an online payment — offer only outlets with connected: true`);
|
|
22
|
+
}
|
|
23
|
+
return { outletId: params.outletId, rehydrated: true, timestamp: new Date().toISOString() };
|
|
24
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export interface SetOutletParams {
|
|
2
|
+
/** An `id` from `getOutlets`. */
|
|
3
|
+
outletId: string;
|
|
4
|
+
}
|
|
5
|
+
export interface SetOutletResponse {
|
|
6
|
+
/** The location now in effect. */
|
|
7
|
+
outletId: string;
|
|
8
|
+
/**
|
|
9
|
+
* `true` when the catalog and tax tables were re-pulled for this location.
|
|
10
|
+
*
|
|
11
|
+
* `false` means the switch took effect but the refresh could not reach the
|
|
12
|
+
* server — the shop is running on what it already had cached. Totals may be
|
|
13
|
+
* the previous location's until the next refresh pass lands, so a flow can
|
|
14
|
+
* tell the shopper to try again rather than quietly showing stale tax.
|
|
15
|
+
*/
|
|
16
|
+
rehydrated: boolean;
|
|
17
|
+
timestamp: string;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Switch the shop to another of the company's locations.
|
|
21
|
+
*
|
|
22
|
+
* REFUSES AN OUTLET THAT CANNOT TAKE A PAYMENT, and one that does not belong to
|
|
23
|
+
* this company, rather than letting either fail later at the payment step. Check
|
|
24
|
+
* `connected` from `getOutlets` and do not offer those, so this refusal is a
|
|
25
|
+
* backstop rather than something a shopper ever sees.
|
|
26
|
+
*
|
|
27
|
+
* The cart survives the switch. Its lines are kept and re-priced; a line that is
|
|
28
|
+
* not purchasable at the new location is not silently dropped here, it is
|
|
29
|
+
* reported when the checkout is next priced.
|
|
30
|
+
*/
|
|
31
|
+
export type SetOutlet = (params: SetOutletParams) => Promise<SetOutletResponse>;
|