@zappie/foundations 0.11.1 → 0.12.1

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/CHANGELOG.md CHANGED
@@ -1,5 +1,28 @@
1
1
  # @zappie/foundations
2
2
 
3
+ ## 0.12.1
4
+
5
+ ### Patch Changes
6
+
7
+ - 8a46a9e: `useCheckoutFlow` closes the gaps its first template found.
8
+
9
+ - `setDelivery({ address, regionId, note })` changes the address, area and rider note in one step. `setAddress`, `setRegion` and `setNote` each build on the change before it, so an address and an area set in the same event both stick. A new address's own `instructions` become the rider note instead of being replaced by the old one.
10
+ - `initialMode` picks the mode a cart that hasn't chosen one starts on. It never overrides the order type a cart already holds.
11
+ - `offers` leaves out a mode the branch's menus don't serve. `checkoutOffers(branch, menu)` lists every mode the branch and its menus take, in venue (`dine_in`) included. The flow holds an in-venue order on the cart and prices it. Opening payment for one fails with `open_failed`, because checkout sessions don't take in-venue orders yet, so it is offered only when you list it in `offers`.
12
+ - `account` gives the signed-in customer's first name and masked phone.
13
+ - The `issue` and `contact` steps carry `target`, the selector of the element you marked with `issueMarker(issue)` or `fieldMarker(key)`.
14
+
15
+ ## 0.12.0
16
+
17
+ ### Minor Changes
18
+
19
+ - 919ca1c: `@zappie/foundations/checkout` holds checkout behaviour with no markup: `useCheckoutFlow` for a bag or a cart page. It keeps how the order arrives and where, the contact details, the time, issues and their fixes and held totals, and one Pay button that guides the shopper to what is missing before it opens payment. A guest pays with a phone number, and sign-in is asked only when the business requires a customer. Payment comes back to `/checkout/return`, and a cancel to `/checkout`. Your template draws every surface. The subpath loads only where you import it, never with the initial entry.
20
+
21
+ ### Patch Changes
22
+
23
+ - Updated dependencies [208a60c]
24
+ - @zappie/sdk@0.10.6
25
+
3
26
  ## 0.11.1
4
27
 
5
28
  ### Patch Changes
package/README.md CHANGED
@@ -36,6 +36,7 @@ Want ready-made, styled components instead? Copy them into your project from the
36
36
  | `@zappie/foundations` | Every part |
37
37
  | `@zappie/foundations/next` | Keeping overlays in step with Back and Forward in a Next.js app |
38
38
  | `@zappie/foundations/branch-switch` | Choosing a branch in a React Router site without waiting for the server |
39
+ | `@zappie/foundations/checkout` | Checkout behaviour for a bag or a cart page, with no markup |
39
40
  | `@zappie/foundations/rich-text` | Sanitising and parsing merchant rich text before you render it |
40
41
 
41
42
  ## Docs
@@ -153,6 +154,38 @@ return isOpen ? (
153
154
  ) : null;
154
155
  ```
155
156
 
157
+ ## Checkout flow
158
+
159
+ `useCheckoutFlow` from `@zappie/foundations/checkout` holds a checkout's behaviour and renders nothing. Your template draws the bag or cart page, each field and every word. Call it inside a `ZappieProvider`, and pass the clients once, not on every render.
160
+
161
+ ```tsx
162
+ import { useCheckoutFlow } from "@zappie/foundations/checkout";
163
+
164
+ const flow = useCheckoutFlow({ branch, menu, contactFields });
165
+
166
+ <button
167
+ type="button"
168
+ data-zappie-checkout=""
169
+ data-state={flow.pay.status}
170
+ disabled={!flow.pay.enabled}
171
+ aria-busy={flow.pay.status === "busy" || undefined}
172
+ onClick={async () => show(await flow.pay.activate())}
173
+ >
174
+ {labels[flow.pay.status]}
175
+ </button>;
176
+ ```
177
+
178
+ - `pay.status` is one of, in this order: `busy`, `empty`, `checking`, `closed`, `address`, `area`, `time`, `fix_issues`, `sign_in`, `phone`, `contact`, `quoting`, `retry` and `pay`. The first that applies wins.
179
+ - `pay.activate()` opens payment for `pay`, signs in for `sign_in` and asks for the total again for `retry`. For the other enabled statuses it returns the step to show: `{ step: "address" }`, `"area"`, `"time"`, `{ step: "issue", issue, target }` or `{ step: "contact", field, target }`.
180
+ - `target` is the selector of the element you marked for that issue or field. Spread `issueMarker(issue)` on the element that shows each issue, and `fieldMarker(key)` on each contact control, with `phone` for the phone. They set `data-zappie-issue`, `data-zappie-issue-line` for a line's issue, and `data-zappie-field`, so `document.querySelector(intent.target)` finds the element to scroll to and focus. A field taken in a sheet is marked inside the sheet: open it, then find the field.
181
+ - `offers` lists delivery and pickup where the branch takes them and its menus serve them. `initialMode` picks the mode a cart that hasn't chosen one starts on. Pickup prices the total straight away. `checkoutOffers(branch, menu)` lists every mode the branch and its menus take, in venue (`dine_in`) included. Name it with `inVenueLabel(branch)` from `@zappie/sdk/display`. In venue is offered only when you list it in `offers`, because checkout sessions don't take in-venue orders yet.
182
+ - `setDelivery({ address, regionId, note })` changes the address, area and rider note in one step. `setAddress`, `setRegion` and `setNote` change one each, and each builds on the change before it, even in the same event. A new address's own `instructions` become the rider note. Without them, the note already typed stays.
183
+ - `account` is the signed-in customer's first name and masked phone, for "Signed in as Ada". It is undefined for a guest.
184
+ - A guest pays with a phone number. Sign-in is asked only when the quote, or opening payment, says the business requires a customer.
185
+ - Payment comes back to `/checkout/return`, and a cancel to `reviewPath`, `/checkout` by default. A sign-in comes back there too, and the details the shopper typed are kept in session storage.
186
+ - Pressing Pay counts the visit's `checkout` event.
187
+ - `error` is `quote_failed`, `open_failed` or `sign_in_failed`, for your own message.
188
+
156
189
  ## Branch switch
157
190
 
158
191
  Choosing a branch shows that branch's menu without waiting for the server. A
@@ -0,0 +1,48 @@
1
+ // src/checkout/attempt.ts
2
+ import { checkoutFulfilment } from "@zappie/sdk/checkout";
3
+ function fulfilment(payload) {
4
+ const value = checkoutFulfilment(payload.fulfilment);
5
+ if (value.type === "dine_in") {
6
+ throw new Error("Checkout sessions don't take in-venue orders yet");
7
+ }
8
+ const scheduled = value.scheduled_for !== void 0 ? { scheduled_for: value.scheduled_for } : {};
9
+ if (value.type === "pickup") {
10
+ return { type: "pickup", ...scheduled };
11
+ }
12
+ return {
13
+ type: "delivery",
14
+ ...value.address !== void 0 ? { address: value.address } : {},
15
+ ...value.delivery_region_id !== void 0 ? { delivery_region_id: value.delivery_region_id } : {},
16
+ ...value.instructions !== void 0 ? { instructions: value.instructions } : {},
17
+ ...scheduled
18
+ };
19
+ }
20
+ function createCheckoutAttempt() {
21
+ let current;
22
+ return (input) => {
23
+ const { payload, attribution } = input;
24
+ const phone = input.contactPhone.trim().replace(/\s+/g, "");
25
+ const digest = JSON.stringify({ payload, phone });
26
+ if (current?.digest === digest) {
27
+ return current.attempt;
28
+ }
29
+ const request = {
30
+ branch_id: input.branchId,
31
+ menu_version: payload.menu_version,
32
+ lines: payload.lines,
33
+ fulfilment: fulfilment(payload),
34
+ ...phone ? { contact_phone: phone } : {},
35
+ ...payload.promo_code ? { promo_code: payload.promo_code } : {},
36
+ ...attribution.source ? { source: attribution.source } : {},
37
+ ...attribution.campaign ? { campaign: attribution.campaign } : {},
38
+ return_url: input.returnUrl,
39
+ cancel_url: input.cancelUrl
40
+ };
41
+ current = { digest, attempt: { key: crypto.randomUUID(), request } };
42
+ return current.attempt;
43
+ };
44
+ }
45
+ export {
46
+ createCheckoutAttempt
47
+ };
48
+ //# sourceMappingURL=attempt.js.map
@@ -0,0 +1,6 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/checkout/attempt.ts"],
4
+ "mappings": ";AACA,SAAS,0BAAgD;AAOzD,SAAS,WAAW,SAAiD;AACnE,QAAM,QAAQ,mBAAmB,QAAQ,UAAU;AAEnD,MAAI,MAAM,SAAS,WAAW;AAC5B,UAAM,IAAI,MAAM,kDAAkD;AAAA,EACpE;AACA,QAAM,YACJ,MAAM,kBAAkB,SACpB,EAAE,eAAe,MAAM,cAAc,IACrC,CAAC;AACP,MAAI,MAAM,SAAS,UAAU;AAC3B,WAAO,EAAE,MAAM,UAAU,GAAG,UAAU;AAAA,EACxC;AACA,SAAO;AAAA,IACL,MAAM;AAAA,IACN,GAAI,MAAM,YAAY,SAAY,EAAE,SAAS,MAAM,QAAQ,IAAI,CAAC;AAAA,IAChE,GAAI,MAAM,uBAAuB,SAC7B,EAAE,oBAAoB,MAAM,mBAAmB,IAC/C,CAAC;AAAA,IACL,GAAI,MAAM,iBAAiB,SACvB,EAAE,cAAc,MAAM,aAAa,IACnC,CAAC;AAAA,IACL,GAAG;AAAA,EACL;AACF;AAQO,SAAS,wBAAwB;AACtC,MAAI;AACJ,SAAO,CAAC,UAOe;AACrB,UAAM,EAAE,SAAS,YAAY,IAAI;AACjC,UAAM,QAAQ,MAAM,aAAa,KAAK,EAAE,QAAQ,QAAQ,EAAE;AAC1D,UAAM,SAAS,KAAK,UAAU,EAAE,SAAS,MAAM,CAAC;AAChD,QAAI,SAAS,WAAW,QAAQ;AAC9B,aAAO,QAAQ;AAAA,IACjB;AACA,UAAM,UAA2B;AAAA,MAC/B,WAAW,MAAM;AAAA,MACjB,cAAc,QAAQ;AAAA,MACtB,OAAO,QAAQ;AAAA,MACf,YAAY,WAAW,OAAO;AAAA,MAC9B,GAAI,QAAQ,EAAE,eAAe,MAAM,IAAI,CAAC;AAAA,MACxC,GAAI,QAAQ,aAAa,EAAE,YAAY,QAAQ,WAAW,IAAI,CAAC;AAAA,MAC/D,GAAI,YAAY,SAAS,EAAE,QAAQ,YAAY,OAAO,IAAI,CAAC;AAAA,MAC3D,GAAI,YAAY,WAAW,EAAE,UAAU,YAAY,SAAS,IAAI,CAAC;AAAA,MACjE,YAAY,MAAM;AAAA,MAClB,YAAY,MAAM;AAAA,IACpB;AACA,cAAU,EAAE,QAAQ,SAAS,EAAE,KAAK,OAAO,WAAW,GAAG,QAAQ,EAAE;AACnE,WAAO,QAAQ;AAAA,EACjB;AACF;",
5
+ "names": []
6
+ }
@@ -0,0 +1,36 @@
1
+ // src/checkout/contact.ts
2
+ import { isContactPhoneValid } from "@zappie/sdk/checkout";
3
+ function fullPhone(typed, dialCode = "+234") {
4
+ const value = typed.trim().replace(/\s+/g, "");
5
+ if (!value) {
6
+ return "";
7
+ }
8
+ return value.startsWith("+") ? value : `${dialCode}${value.replace(/^0/, "")}`;
9
+ }
10
+ var emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
11
+ function contactProblems(fields, values, phone, accountPhone, dialCode = "+234") {
12
+ const problems = {};
13
+ if (!phone.trim()) {
14
+ if (!accountPhone) {
15
+ problems.phone = "missing";
16
+ }
17
+ } else if (!isContactPhoneValid(fullPhone(phone, dialCode))) {
18
+ problems.phone = "invalid";
19
+ }
20
+ for (const field of fields) {
21
+ const value = values[field.key]?.trim() ?? "";
22
+ if (!value) {
23
+ if (field.required) {
24
+ problems[field.key] = "missing";
25
+ }
26
+ } else if (field.type === "email" && !emailPattern.test(value) || field.type === "tel" && !isContactPhoneValid(value)) {
27
+ problems[field.key] = "invalid";
28
+ }
29
+ }
30
+ return problems;
31
+ }
32
+ export {
33
+ contactProblems,
34
+ fullPhone
35
+ };
36
+ //# sourceMappingURL=contact.js.map
@@ -0,0 +1,6 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/checkout/contact.ts"],
4
+ "mappings": ";AAAA,SAAS,2BAA2B;AA0B7B,SAAS,UAAU,OAAe,WAAW,QAAgB;AAClE,QAAM,QAAQ,MAAM,KAAK,EAAE,QAAQ,QAAQ,EAAE;AAC7C,MAAI,CAAC,OAAO;AACV,WAAO;AAAA,EACT;AACA,SAAO,MAAM,WAAW,GAAG,IACvB,QACA,GAAG,QAAQ,GAAG,MAAM,QAAQ,MAAM,EAAE,CAAC;AAC3C;AAEA,IAAM,eAAe;AAad,SAAS,gBACd,QACA,QACA,OACA,cACA,WAAW,QACqB;AAChC,QAAM,WAA2C,CAAC;AAClD,MAAI,CAAC,MAAM,KAAK,GAAG;AACjB,QAAI,CAAC,cAAc;AACjB,eAAS,QAAQ;AAAA,IACnB;AAAA,EACF,WAAW,CAAC,oBAAoB,UAAU,OAAO,QAAQ,CAAC,GAAG;AAC3D,aAAS,QAAQ;AAAA,EACnB;AACA,aAAW,SAAS,QAAQ;AAC1B,UAAM,QAAQ,OAAO,MAAM,GAAG,GAAG,KAAK,KAAK;AAC3C,QAAI,CAAC,OAAO;AACV,UAAI,MAAM,UAAU;AAClB,iBAAS,MAAM,GAAG,IAAI;AAAA,MACxB;AAAA,IACF,WACG,MAAM,SAAS,WAAW,CAAC,aAAa,KAAK,KAAK,KAClD,MAAM,SAAS,SAAS,CAAC,oBAAoB,KAAK,GACnD;AACA,eAAS,MAAM,GAAG,IAAI;AAAA,IACxB;AAAA,EACF;AACA,SAAO;AACT;",
5
+ "names": []
6
+ }
@@ -0,0 +1,414 @@
1
+ import { CartModule, CartIssue, Branch, Menu, CartState, CartLine, CartFulfilment, DeliveryAddress, Quote, CartFix } from '@zappie/sdk';
2
+ import { CheckoutRequest, CheckoutClient } from '@zappie/sdk/checkout';
3
+ import { CustomerClient } from '@zappie/sdk/customer';
4
+
5
+ type Payload = ReturnType<CartModule["toCheckoutPayload"]>;
6
+ /** One checkout request and the idempotency key it keeps while its payload stays the same. */
7
+ type CheckoutAttempt = {
8
+ key: string;
9
+ request: CheckoutRequest;
10
+ };
11
+ /**
12
+ * Builds checkout requests that reuse one idempotency key for retries of the same payload and
13
+ * phone, and take a new key when either changes.
14
+ *
15
+ * @returns A function from the payload and its addresses to the attempt to open.
16
+ */
17
+ declare function createCheckoutAttempt(): (input: {
18
+ payload: Payload;
19
+ branchId: string;
20
+ contactPhone: string;
21
+ returnUrl: string;
22
+ cancelUrl: string;
23
+ attribution: {
24
+ source: string;
25
+ campaign: string;
26
+ };
27
+ }) => CheckoutAttempt;
28
+
29
+ /**
30
+ * A contact detail the business asks for beside the phone number, such as a name or an email,
31
+ * required or optional.
32
+ */
33
+ type ContactField = {
34
+ key: string;
35
+ label: string;
36
+ type: "text" | "email" | "tel";
37
+ required: boolean;
38
+ };
39
+ /** What the shopper typed in each contact field, by its key. */
40
+ type ContactValues = Readonly<Record<string, string>>;
41
+ /** Why a contact detail stops Pay: it is empty and required, or it doesn't read as valid. */
42
+ type ContactProblem = "missing" | "invalid";
43
+ /**
44
+ * The number to send: the shopper's own international number, or a local number typed without
45
+ * its dial code, with any leading 0 dropped.
46
+ *
47
+ * @param typed - The number as typed.
48
+ * @param dialCode - The dial code a local number takes, such as `+234`.
49
+ */
50
+ declare function fullPhone(typed: string, dialCode?: string): string;
51
+ /**
52
+ * What is wrong with each contact detail, by key, with `phone` for the phone. Empty when the
53
+ * shopper can pay. A guest always gives a phone. A signed-in shopper who types none pays with
54
+ * their account's.
55
+ *
56
+ * @param fields - The business's contact fields beside the phone.
57
+ * @param values - What the shopper typed in them.
58
+ * @param phone - The phone as typed.
59
+ * @param accountPhone - The signed-in customer's masked phone, when they have one.
60
+ * @param dialCode - The dial code a local number takes.
61
+ */
62
+ declare function contactProblems(fields: readonly ContactField[], values: ContactValues, phone: string, accountPhone?: string, dialCode?: string): Record<string, ContactProblem>;
63
+
64
+ /** The attributes that mark an issue's element, so a `fix_issues` step can find it. */
65
+ type IssueMarker = {
66
+ "data-zappie-issue": string;
67
+ "data-zappie-issue-line"?: string;
68
+ };
69
+ /** The attribute that marks a contact field's control, so a `contact` step can find it. */
70
+ type FieldMarker = {
71
+ "data-zappie-field": string;
72
+ };
73
+ /**
74
+ * The attributes to put on the element that shows an issue: its code, and its line when it
75
+ * belongs to one. Spread them on the element: `<div {...issueMarker(issue)}>`.
76
+ *
77
+ * @param issue - The issue the element shows.
78
+ */
79
+ declare function issueMarker(issue: Pick<CartIssue, "code" | "lineId">): IssueMarker;
80
+ /**
81
+ * The selector that finds the element `issueMarker` marked for this issue.
82
+ *
83
+ * @param issue - The issue to find.
84
+ */
85
+ declare function issueSelector(issue: Pick<CartIssue, "code" | "lineId">): string;
86
+ /**
87
+ * The attribute to put on a contact field's control: `phone` for the phone, else the business
88
+ * field's key. Spread it on the input: `<input {...fieldMarker("phone")} />`.
89
+ *
90
+ * @param key - The field's key.
91
+ */
92
+ declare function fieldMarker(key: string): FieldMarker;
93
+ /**
94
+ * The selector that finds the control `fieldMarker` marked for this key.
95
+ *
96
+ * @param key - The field's key.
97
+ */
98
+ declare function fieldSelector(key: string): string;
99
+
100
+ /**
101
+ * How the order reaches the shopper: delivered, picked up, or placed in the venue (`dine_in`).
102
+ * Name the in-venue mode with `inVenueLabel(branch)` from `@zappie/sdk/display`: "Walk-in" at a
103
+ * retail branch, "Dine-in" otherwise.
104
+ */
105
+ type CheckoutMode = "delivery" | "pickup" | "dine_in";
106
+ /**
107
+ * The modes the branch takes and its menus serve, in the order delivery, pickup, in venue. A
108
+ * menu that lists no modes serves every mode the branch takes.
109
+ *
110
+ * @param branch - The cart's branch.
111
+ * @param menu - The branch's menu, whose source menus can each list the modes they serve.
112
+ */
113
+ declare function checkoutOffers(branch: Branch, menu?: Menu | undefined): CheckoutMode[];
114
+
115
+ /**
116
+ * Sends the shopper to sign in and come back to `path`, unless a sign-in for `path` just came
117
+ * back to this page without a session: then it stays and returns `false`, once.
118
+ */
119
+ declare function signInOnce(path: string): boolean;
120
+
121
+ /**
122
+ * What the one Pay button does now, in precedence order: the first that applies wins.
123
+ *
124
+ * - `busy`: payment or sign-in is opening. Disabled until the page unloads.
125
+ * - `empty`: the cart has no lines. Disabled.
126
+ * - `checking`: the cart hasn't hydrated or the session isn't known yet. Disabled.
127
+ * - `closed`: the branch takes no order now or ahead. Disabled.
128
+ * - `address`, `area`, `time`: enabled. A press asks for that step.
129
+ * - `fix_issues`: enabled. A press points at the first blocking issue.
130
+ * - `sign_in`: enabled, only when the business requires a customer for this order.
131
+ * - `phone`, `contact`: enabled. A press points at the first invalid field.
132
+ * - `quoting`: disabled while the total updates.
133
+ * - `retry`: enabled. A press asks for the total again.
134
+ * - `pay`: enabled. A press opens payment.
135
+ */
136
+ type CheckoutStatus = "busy" | "empty" | "checking" | "closed" | "address" | "area" | "time" | "fix_issues" | "sign_in" | "phone" | "contact" | "quoting" | "retry" | "pay";
137
+ /** Whether the shopper's session has been checked yet, and its answer. */
138
+ type CheckoutAuth = "unknown" | "authenticated" | "anonymous";
139
+ /** What decides the Pay button's status. */
140
+ type CheckoutStatusInput = {
141
+ busy: boolean;
142
+ hydrated: boolean;
143
+ lines: number;
144
+ auth: CheckoutAuth;
145
+ closed: boolean;
146
+ /** Delivery is chosen and the cart has no address for it. */
147
+ needsAddress: boolean;
148
+ needsTime: boolean;
149
+ state: CartState;
150
+ /** The last quote request for this cart failed. */
151
+ quoteFailed: boolean;
152
+ /** Opening payment answered that the business needs a signed-in customer. */
153
+ signInRequired: boolean;
154
+ /** The first contact field that stops Pay, with `phone` for the phone. */
155
+ contactField?: string | undefined;
156
+ };
157
+ /** Whether the Pay button takes a press in this status. */
158
+ declare function payEnabled(status: CheckoutStatus): boolean;
159
+ /** The issue `fix_issues` points at: the first blocking one, else the first the cart raised. */
160
+ declare function firstIssue(state: CartState): CartIssue | undefined;
161
+ /**
162
+ * The Pay button's status, in the precedence `CheckoutStatus` lists. Sign-in is asked only when
163
+ * the quote, or opening payment, says the business requires a customer. Otherwise a guest pays
164
+ * with a phone number.
165
+ *
166
+ * @param input - The cart, session, timing and contact state.
167
+ */
168
+ declare function checkoutStatus(input: CheckoutStatusInput): CheckoutStatus;
169
+
170
+ /** How the bag can be timed, as checkout decides it. */
171
+ type CheckoutTiming = {
172
+ /** Whether the branch takes orders for a later slot. */
173
+ ahead: boolean;
174
+ /** Whether a line sits only on pre-order menus that ask for notice, so it must be scheduled. */
175
+ notice: boolean;
176
+ /** Whether the order can be placed for now: the branch is open and nothing needs notice. */
177
+ canNow: boolean;
178
+ /** Whether the branch takes no order at all: closed, and not taking orders ahead either. */
179
+ closed: boolean;
180
+ /** Whether Pay waits for a time: the order can't be placed for now and no time is chosen. */
181
+ needsTime: boolean;
182
+ /** When a closed branch opens next, as an ISO instant, for a label such as "Opens at 7:00". */
183
+ opensAt?: string;
184
+ };
185
+ /**
186
+ * Whether the bag must be scheduled: a line sits only on pre-order menus that ask for notice. A
187
+ * line that names its menu is judged by it, otherwise by every menu that lists the item, as the
188
+ * API reads it.
189
+ *
190
+ * @param menu - The branch's menu, with its source menus and their schedules.
191
+ * @param lines - The cart's lines.
192
+ */
193
+ declare function mustSchedule(menu: Menu | undefined, lines: readonly Pick<CartLine, "itemId" | "menuId">[]): boolean;
194
+ /**
195
+ * How the bag can be timed. A branch that takes orders ahead offers now and later, now only while
196
+ * it is open and nothing in the bag needs notice. A branch that doesn't offers now while open and
197
+ * nothing while closed.
198
+ *
199
+ * @param branch - The cart's branch.
200
+ * @param menu - The branch's menu, for pre-order notice.
201
+ * @param lines - The cart's lines.
202
+ * @param scheduledFor - The chosen time, if any.
203
+ */
204
+ declare function checkoutTiming(branch: Branch, menu: Menu | undefined, lines: readonly Pick<CartLine, "itemId" | "menuId">[], scheduledFor?: string): CheckoutTiming;
205
+
206
+ /** The cart's delivery fulfilment. */
207
+ type CheckoutDelivery = Extract<CartFulfilment, {
208
+ type: "delivery";
209
+ }>;
210
+ /**
211
+ * The step the template shows next: the address, the delivery area, the time, an issue, a contact
212
+ * field (`phone` or the business field's key), or the item a line's option fix opens.
213
+ *
214
+ * An issue and a contact field come with `target`, the selector of the element the template
215
+ * marked for them with `issueMarker(issue)` or `fieldMarker(key)`, so it can scroll to it and
216
+ * focus it.
217
+ */
218
+ type CheckoutIntent = {
219
+ step: "address";
220
+ } | {
221
+ step: "area";
222
+ } | {
223
+ step: "time";
224
+ } | {
225
+ step: "issue";
226
+ issue: CartIssue;
227
+ target: string;
228
+ } | {
229
+ step: "contact";
230
+ field: string;
231
+ target: string;
232
+ } | {
233
+ step: "item";
234
+ itemId: string;
235
+ lineId: string;
236
+ };
237
+ /** A change to the delivery: the address, the area and the rider note, applied together. */
238
+ type CheckoutDeliveryChange = {
239
+ /**
240
+ * The address. Its own `instructions` become the rider note, unless `note` is given. Without
241
+ * them the rider note already typed is kept.
242
+ */
243
+ address?: DeliveryAddress;
244
+ /** The delivery area's id, from the branch's `deliveryRegions`. */
245
+ regionId?: string;
246
+ /** The rider note. An empty note removes it. */
247
+ note?: string;
248
+ };
249
+ /** The signed-in customer, as the session gives them. */
250
+ type CheckoutAccount = {
251
+ /** Their first name, when the account has one. */
252
+ name?: string | undefined;
253
+ /** Their masked phone, such as `+234•••••4567`. */
254
+ phone?: string | undefined;
255
+ };
256
+ /**
257
+ * A failure the template words: the total couldn't be priced, payment couldn't open, or a
258
+ * sign-in came back without a session the site can use.
259
+ */
260
+ type CheckoutFlowError = "quote_failed" | "open_failed" | "sign_in_failed";
261
+ /** The customer client methods the flow reads. */
262
+ type CheckoutCustomer = Pick<CustomerClient, "state" | "customer" | "onChange" | "addresses" | "session">;
263
+ /** Where the checkout flow reads its branch and how it leaves the page. */
264
+ type CheckoutFlowOptions = {
265
+ /** The cart's branch. */
266
+ branch: Branch;
267
+ /** The branch's menu, for pre-order notice on its source menus. */
268
+ menu?: Menu | undefined;
269
+ /** The contact fields the business asks for beside the phone. */
270
+ contactFields?: readonly ContactField[];
271
+ /**
272
+ * How the order can reach the shopper, in the order to show them. Defaults to delivery and
273
+ * pickup, where the branch takes them and its menus serve them. A mode the branch doesn't take,
274
+ * or its menus don't serve, is left out (`checkoutOffers`). In venue is offered only when you
275
+ * list it here: checkout sessions don't take in-venue orders yet, so opening payment for one
276
+ * fails with `open_failed`.
277
+ */
278
+ offers?: readonly CheckoutMode[];
279
+ /**
280
+ * The mode a cart that hasn't chosen one starts on, when it is offered. Defaults to the first
281
+ * offer. Pickup prices the total straight away, while delivery waits for an address.
282
+ */
283
+ initialMode?: CheckoutMode;
284
+ /**
285
+ * The page payment's cancel and sign-in come back to. Defaults to `/checkout`, which reopens
286
+ * the bag on a food site and shows the cart page on a retail site.
287
+ */
288
+ reviewPath?: string;
289
+ /** The dial code a local phone number takes. Defaults to `+234`. */
290
+ dialCode?: string;
291
+ /** The session storage key prefix that keeps typed details across a sign-in. */
292
+ storageKey?: string;
293
+ /** The checkout client. Defaults to `createCheckout()`. */
294
+ checkout?: CheckoutClient;
295
+ /** The customer client. Defaults to `createCustomer()`, disposed on unmount. */
296
+ customer?: CheckoutCustomer;
297
+ /** Leaves for the payment page. Defaults to `redirectToCheckout` on the top window. */
298
+ redirect?: (url: string) => void;
299
+ /** Leaves to sign in and come back to `path`. Returns `false` when the shopper stayed. */
300
+ signIn?: (path: string) => boolean;
301
+ /** The visit's source and campaign. Defaults to `currentSource`. */
302
+ attribution?: () => {
303
+ source: string;
304
+ campaign: string;
305
+ };
306
+ };
307
+ /** The shopper's contact details and what stops Pay among them. */
308
+ type CheckoutContact = {
309
+ /** The phone as typed. */
310
+ phone: string;
311
+ /** The phone payment receives, with its dial code, or empty to use the account's. */
312
+ contactPhone: string;
313
+ setPhone(phone: string): void;
314
+ /** The signed-in customer's masked phone, used when the shopper types none. */
315
+ accountPhone?: string | undefined;
316
+ fields: readonly ContactField[];
317
+ values: ContactValues;
318
+ setField(key: string, value: string): void;
319
+ /** What stops Pay, by key, with `phone` for the phone. */
320
+ problems: Readonly<Record<string, ContactProblem>>;
321
+ /** The keys whose problem the template shows: blurred fields, and all of them after Pay. */
322
+ shown: ReadonlySet<string>;
323
+ show(key: string): void;
324
+ };
325
+ /** The one Pay button: its status, whether it takes a press, and what a press does. */
326
+ type CheckoutPay = {
327
+ status: CheckoutStatus;
328
+ enabled: boolean;
329
+ /** Where a `busy` button is going. */
330
+ leavingFor?: "payment" | "sign_in" | undefined;
331
+ /** The issue `fix_issues` points at. */
332
+ issue?: CartIssue | undefined;
333
+ /** The contact field `phone` or `contact` points at. */
334
+ field?: string | undefined;
335
+ /**
336
+ * Acts on the status: opens payment, signs in, asks for the total again, or returns the step
337
+ * the template shows next. Counts the visit's `checkout` event when it opens payment or sign-in.
338
+ */
339
+ activate(): Promise<CheckoutIntent | undefined>;
340
+ };
341
+ /** Checkout state and actions, with no markup. */
342
+ type CheckoutFlow = {
343
+ /**
344
+ * How the order reaches the shopper. Name `dine_in` with `inVenueLabel(branch)` from
345
+ * `@zappie/sdk/display`.
346
+ */
347
+ mode: CheckoutMode;
348
+ offers: readonly CheckoutMode[];
349
+ /** Chooses how the order arrives. Returns the address step when delivery has no known address. */
350
+ choose(mode: CheckoutMode): CheckoutIntent | undefined;
351
+ delivery?: CheckoutDelivery | undefined;
352
+ /**
353
+ * Changes the address, area and rider note in one step, and chooses delivery. A new address is
354
+ * remembered on this device unless `remember` is false. Returns the address step when there is
355
+ * no address yet.
356
+ */
357
+ setDelivery(change: CheckoutDeliveryChange, options?: {
358
+ remember?: boolean;
359
+ }): CheckoutIntent | undefined;
360
+ /**
361
+ * Sets the delivery address and remembers it on this device. The address's own `instructions`
362
+ * become the rider note. Without them the rider note already typed is kept.
363
+ */
364
+ setAddress(address: DeliveryAddress, options?: {
365
+ remember?: boolean;
366
+ }): void;
367
+ /** Sets the delivery area. Returns the address step when there is no address yet. */
368
+ setRegion(regionId: string): CheckoutIntent | undefined;
369
+ /** Sets the rider note. Returns the address step when there is no address yet. */
370
+ setNote(note: string): CheckoutIntent | undefined;
371
+ scheduledFor?: string | undefined;
372
+ /** Chooses a time, or now with `null`. */
373
+ schedule(at: string | null): void;
374
+ timing: CheckoutTiming;
375
+ contact: CheckoutContact;
376
+ auth: CheckoutAuth;
377
+ /** The signed-in customer, such as for "Signed in as Ada". Undefined for a guest. */
378
+ account?: CheckoutAccount | undefined;
379
+ quote?: Quote | undefined;
380
+ /** Whether the total is being updated. */
381
+ pricing: boolean;
382
+ /**
383
+ * Whether the quote holds lines until a time is chosen (`time`) or the branch opens
384
+ * (`opening`), and so prices none of them: show the cart's subtotal and a note, never zero.
385
+ */
386
+ held?: "time" | "opening" | undefined;
387
+ /** The cart's issues, without Choose a time fixes where the branch takes no orders ahead. */
388
+ issues: readonly CartIssue[];
389
+ /** Applies an issue's fix, or returns the step the template shows for it. */
390
+ fix(issue: CartIssue, action: CartFix): CheckoutIntent | undefined;
391
+ error?: CheckoutFlowError | undefined;
392
+ clearError(): void;
393
+ pay: CheckoutPay;
394
+ };
395
+ /**
396
+ * The whole checkout's behaviour, headless: how the order arrives and where, contact details, the
397
+ * time, issues and their fixes, totals while held, and one Pay button that guides the shopper to
398
+ * what is missing before it opens payment. It renders nothing. The template draws every surface.
399
+ *
400
+ * A guest pays with a phone number. Sign-in is asked only when the quote, or opening payment,
401
+ * says the business requires a customer. Payment returns to `/checkout/return` and a cancel comes
402
+ * back to `reviewPath`. Typed details are kept in session storage, so a sign-in loses nothing.
403
+ * Call it inside a `ZappieProvider`.
404
+ *
405
+ * Mark the element that shows each issue with `issueMarker(issue)` and each contact control with
406
+ * `fieldMarker(key)`. The `issue` and `contact` steps carry the selector that finds them.
407
+ *
408
+ * @param options - The branch, menu and contact fields, and the clients to use.
409
+ * @returns State and actions for the bag or cart page.
410
+ */
411
+ declare function useCheckoutFlow(options: CheckoutFlowOptions): CheckoutFlow;
412
+
413
+ export { checkoutOffers, checkoutStatus, checkoutTiming, contactProblems, createCheckoutAttempt, fieldMarker, fieldSelector, firstIssue, fullPhone, issueMarker, issueSelector, mustSchedule, payEnabled, signInOnce, useCheckoutFlow };
414
+ export type { CheckoutAccount, CheckoutAttempt, CheckoutAuth, CheckoutContact, CheckoutCustomer, CheckoutDelivery, CheckoutDeliveryChange, CheckoutFlow, CheckoutFlowError, CheckoutFlowOptions, CheckoutIntent, CheckoutMode, CheckoutPay, CheckoutStatus, CheckoutStatusInput, CheckoutTiming, ContactField, ContactProblem, ContactValues, FieldMarker, IssueMarker };
@@ -0,0 +1,34 @@
1
+ "use client";
2
+
3
+ // src/checkout/index.ts
4
+ import { createCheckoutAttempt } from "./attempt.js";
5
+ import { contactProblems, fullPhone } from "./contact.js";
6
+ import {
7
+ fieldMarker,
8
+ fieldSelector,
9
+ issueMarker,
10
+ issueSelector
11
+ } from "./markers.js";
12
+ import { checkoutOffers } from "./offers.js";
13
+ import { signInOnce } from "./sign-in.js";
14
+ import { checkoutStatus, firstIssue, payEnabled } from "./status.js";
15
+ import { checkoutTiming, mustSchedule } from "./timing.js";
16
+ import { useCheckoutFlow } from "./use-checkout-flow.js";
17
+ export {
18
+ checkoutOffers,
19
+ checkoutStatus,
20
+ checkoutTiming,
21
+ contactProblems,
22
+ createCheckoutAttempt,
23
+ fieldMarker,
24
+ fieldSelector,
25
+ firstIssue,
26
+ fullPhone,
27
+ issueMarker,
28
+ issueSelector,
29
+ mustSchedule,
30
+ payEnabled,
31
+ signInOnce,
32
+ useCheckoutFlow
33
+ };
34
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,6 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/checkout/index.ts"],
4
+ "mappings": ";;;AAGA,SAAS,6BAA6B;AAEtC,SAAS,iBAAiB,iBAAiB;AAE3C;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAEP,SAAS,sBAAsB;AAC/B,SAAS,kBAAkB;AAC3B,SAAS,gBAAgB,YAAY,kBAAkB;AAMvD,SAAS,gBAAgB,oBAAoB;AAE7C,SAAS,uBAAuB;",
5
+ "names": []
6
+ }
@@ -0,0 +1,26 @@
1
+ // src/checkout/markers.ts
2
+ var quoted = (value) => `"${value.replace(/\\/g, "\\\\").replace(/"/g, '\\"')}"`;
3
+ function issueMarker(issue) {
4
+ const marker = { "data-zappie-issue": issue.code };
5
+ if (issue.lineId) {
6
+ marker["data-zappie-issue-line"] = issue.lineId;
7
+ }
8
+ return marker;
9
+ }
10
+ function issueSelector(issue) {
11
+ const code = `[data-zappie-issue=${quoted(issue.code)}]`;
12
+ return issue.lineId ? `${code}[data-zappie-issue-line=${quoted(issue.lineId)}]` : code;
13
+ }
14
+ function fieldMarker(key) {
15
+ return { "data-zappie-field": key };
16
+ }
17
+ function fieldSelector(key) {
18
+ return `[data-zappie-field=${quoted(key)}]`;
19
+ }
20
+ export {
21
+ fieldMarker,
22
+ fieldSelector,
23
+ issueMarker,
24
+ issueSelector
25
+ };
26
+ //# sourceMappingURL=markers.js.map
@@ -0,0 +1,6 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/checkout/markers.ts"],
4
+ "mappings": ";AAYA,IAAM,SAAS,CAAC,UACd,IAAI,MAAM,QAAQ,OAAO,MAAM,EAAE,QAAQ,MAAM,KAAK,CAAC;AAQhD,SAAS,YAAY,OAA2C;AACrE,QAAM,SAAsB,EAAE,qBAAqB,MAAM,KAAK;AAC9D,MAAI,MAAM,QAAQ;AAChB,WAAO,wBAAwB,IAAI,MAAM;AAAA,EAC3C;AACA,SAAO;AACT;AAOO,SAAS,cAAc,OAA2C;AACvE,QAAM,OAAO,sBAAsB,OAAO,MAAM,IAAI,CAAC;AACrD,SAAO,MAAM,SACT,GAAG,IAAI,2BAA2B,OAAO,MAAM,MAAM,CAAC,MACtD;AACN;AAQO,SAAS,YAAY,KAA0B;AACpD,SAAO,EAAE,qBAAqB,IAAI;AACpC;AAOO,SAAS,cAAc,KAAa;AACzC,SAAO,sBAAsB,OAAO,GAAG,CAAC;AAC1C;",
5
+ "names": []
6
+ }