@12-apps/payments-frontend 3.22.0 → 3.23.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.
Files changed (43) hide show
  1. package/package.json +2 -2
  2. package/src/components/checkout/checkout-actions.ts +55 -9
  3. package/src/components/checkout/checkout-flow.tsx +15 -7
  4. package/src/components/checkout/confirmation-wait.ts +97 -0
  5. package/src/components/checkout/en-US.ts +8 -0
  6. package/src/components/checkout/failure-codes.ts +82 -0
  7. package/src/components/checkout/hosted-store.ts +23 -1
  8. package/src/components/checkout/payment-error-panel.tsx +9 -3
  9. package/src/components/checkout/pix-view.tsx +97 -8
  10. package/src/components/checkout/poll-loop.ts +5 -3
  11. package/src/components/checkout/pt-BR.ts +9 -0
  12. package/src/components/checkout/resolution-actions.ts +43 -0
  13. package/src/components/checkout/types.ts +11 -0
  14. package/src/components/checkout/use-card-checkout.ts +3 -2
  15. package/src/components/checkout/use-checkout-controller.ts +23 -16
  16. package/src/components/checkout/use-payment-polling.ts +58 -7
  17. package/src/components/checkout/use-wallet-charge.ts +24 -1
  18. package/src/components/checkout/view-copy.ts +38 -0
  19. package/src/components/checkout/wallet-pane.tsx +6 -1
  20. package/src/flows/catalog-exit.ts +33 -0
  21. package/src/flows/create-payment-flows.tsx +12 -1
  22. package/src/flows/pipeline/actions.tsx +104 -0
  23. package/src/flows/pipeline/admission.ts +55 -0
  24. package/src/flows/pipeline/context.ts +140 -0
  25. package/src/flows/pipeline/derive-step.ts +234 -0
  26. package/src/flows/pipeline/engine-actions.ts +257 -0
  27. package/src/flows/pipeline/engine-chrome.tsx +123 -0
  28. package/src/flows/pipeline/engine-state.ts +107 -0
  29. package/src/flows/pipeline/engine.tsx +377 -0
  30. package/src/flows/pipeline/methods.ts +71 -0
  31. package/src/flows/pipeline/refusal-routing.ts +106 -0
  32. package/src/flows/pipeline/slices.ts +110 -0
  33. package/src/flows/pipeline/stable-plugins.ts +72 -0
  34. package/src/flows/pipeline/steps/buyer-steps.tsx +297 -0
  35. package/src/flows/pipeline/steps/index.ts +54 -0
  36. package/src/flows/pipeline/steps/pay-steps.tsx +182 -0
  37. package/src/flows/pipeline/steps/status-step.tsx +41 -0
  38. package/src/flows/pipeline/types.ts +232 -0
  39. package/src/flows/public.ts +78 -0
  40. package/src/flows/screens-hosted.tsx +37 -4
  41. package/src/flows/screens-pay.tsx +6 -1
  42. package/src/flows/types.ts +25 -2
  43. package/src/index.ts +7 -19
@@ -0,0 +1,43 @@
1
+ import { useCallback, type Dispatch, type SetStateAction } from "react";
2
+
3
+ import type { Step } from "./checkout-actions";
4
+ import type { CheckoutDecline } from "./decline";
5
+ import type { BuyerInfo, OrderStatus, PaymentMethod } from "./types";
6
+
7
+ /**
8
+ * The two actions the payment step raises on its own: paying once an e-mail
9
+ * has been typed, and reporting the result the provider's screen reached.
10
+ *
11
+ * Extracted from `useCheckoutController` for the same reason `useRetryAction`
12
+ * and `useGoToPayment` were: the controller is the one function that has to
13
+ * hold every piece of checkout state at once, and each action it also spells
14
+ * out inline is a line it cannot spend on the state. FUT-1170 and FUT-1240
15
+ * together pushed it past the size gate; these two callbacks are the part that
16
+ * reads the same wherever it lives.
17
+ */
18
+ export function useResolutionActions(input: {
19
+ buyer: BuyerInfo;
20
+ method: PaymentMethod | null;
21
+ startPayment: (method: PaymentMethod, buyer: BuyerInfo) => Promise<void>;
22
+ setBuyerState: Dispatch<SetStateAction<BuyerInfo>>;
23
+ setDecline: Dispatch<SetStateAction<CheckoutDecline | null>>;
24
+ setFinalStatus: Dispatch<SetStateAction<OrderStatus | null>>;
25
+ setStep: Dispatch<SetStateAction<Step>>;
26
+ }): {
27
+ payWithEmail: (email: string) => void;
28
+ handleResolved: (status: OrderStatus, refusal?: CheckoutDecline | null) => void;
29
+ } {
30
+ const { buyer, method, startPayment, setBuyerState, setDecline, setFinalStatus, setStep } = input;
31
+ const payWithEmail = useCallback((email: string) => {
32
+ if (!method) return;
33
+ const next = { ...buyer, email };
34
+ setBuyerState(next);
35
+ void startPayment(method, next);
36
+ }, [buyer, method, setBuyerState, startPayment]);
37
+ const handleResolved = useCallback((status: OrderStatus, refusal?: CheckoutDecline | null) => {
38
+ setDecline(refusal ?? null);
39
+ setFinalStatus(status);
40
+ setStep("status");
41
+ }, [setDecline, setFinalStatus, setStep]);
42
+ return { payWithEmail, handleResolved };
43
+ }
@@ -185,6 +185,17 @@ export interface CreateOrderRequest {
185
185
  buyer: BuyerInfo;
186
186
  /** Opt-in: save buyer name/phone (not CPF — never persisted here) for next-checkout pre-fill. */
187
187
  saveProfile: boolean;
188
+ /**
189
+ * WHICH settlement the buyer chose, when it is not one of the two above
190
+ * (FUT-1240).
191
+ *
192
+ * `method` is what the CHAIN can be asked to charge, so a pipeline host's own
193
+ * registered settlement — "pay the courier", "pay the waiter" — cannot be
194
+ * said there, and without this every one of them arrives as the same request.
195
+ * ABSENT for `PIX` and `CARD`, which is what keeps it additive; otherwise the
196
+ * registered `SettlementMethodDescriptor.id` verbatim (`flows/pipeline`).
197
+ */
198
+ settlementMethod?: string;
188
199
  }
189
200
 
190
201
  /** What the flow hands the host's `saveBuyerContact` port on "Continuar". */
@@ -169,8 +169,9 @@ type CardSubmit = Pick<
169
169
  * It was 36 healthy polls, which is the same 90 s at the default 2500 ms
170
170
  * interval and an unbounded wait at any other — including the one that
171
171
  * mattered, where every poll is FAILING and the healthy count never moves.
172
- * PIX passes no bound at all and keeps today's behavior: its charge expires
173
- * server-side and comes back as a terminal EXPIRED.
172
+ * PIX is bounded too since FUT-1170, but by the CHARGE's own expiry rather
173
+ * than by a constant: a code the buyer can still scan is still worth watching,
174
+ * and one that has died is not.
174
175
  */
175
176
  const CARD_AWAITING_WAIT_MS = 90_000;
176
177
 
@@ -12,6 +12,7 @@ import {
12
12
  useStartPayment,
13
13
  type Step,
14
14
  } from "./checkout-actions";
15
+ import { useConfirmationWait } from "./confirmation-wait";
15
16
  import { useCheckoutCopy } from "./copy-context";
16
17
 
17
18
  import { forgetHostedOrder } from "./hosted-return";
@@ -27,6 +28,7 @@ import type {
27
28
  OrderStatus,
28
29
  PaymentMethod,
29
30
  } from "./types";
31
+ import { useResolutionActions } from "./resolution-actions";
30
32
  import { useHostedResume } from "./use-hosted-resume";
31
33
 
32
34
  const STEP_ORDER: Step[] = ["dados", "payment", "status"];
@@ -124,10 +126,14 @@ export function useCheckoutController(
124
126
  const { back, editBuyer } = useCheckoutNav(taxIdOnFile, onExitToMenu, setStep);
125
127
  const setMethod = useCallback((next: PaymentMethod) => {
126
128
  setMethodState((prev) => {
127
- if (prev !== next) { setOrder(null); forgetHostedOrder(); clearError(); }
129
+ // SCOPED (FUT-1240): the order dropped here is THIS store's, and so is
130
+ // the parked entry that goes with it. Unscoped, changing method at store
131
+ // B threw away store A's parked hand-off — the same cross-store slot the
132
+ // slug closed on the read side, still open on the write side.
133
+ if (prev !== next) { setOrder(null); forgetHostedOrder(tenantSlug); clearError(); }
128
134
  return next;
129
135
  });
130
- }, [clearError]);
136
+ }, [clearError, tenantSlug]);
131
137
  const goToPayment = useGoToPayment({
132
138
  buyer, buyerFields, taxIdOnFile, saveProfile, saveBuyerContact, validation, failure, setStep,
133
139
  });
@@ -135,28 +141,29 @@ export function useCheckoutController(
135
141
  buyer, saveProfile, createOrder, navigate, tenantSlug, basket, failure,
136
142
  setCreating, setDecline, setOrder, setFinalStatus,
137
143
  });
138
- const payWithEmail = useCallback((email: string) => {
139
- if (!method) return;
140
- const next = { ...buyer, email };
141
- setBuyerState(next);
142
- void startPayment(method, next);
143
- }, [buyer, method, startPayment]);
144
- const handleResolved = useCallback((s: OrderStatus, refusal?: CheckoutDecline | null) => {
145
- setDecline(refusal ?? null);
146
- setFinalStatus(s);
147
- setStep("status");
148
- }, []);
144
+ const { payWithEmail, handleResolved } = useResolutionActions({
145
+ buyer, method, startPayment, setBuyerState, setDecline, setFinalStatus, setStep,
146
+ });
149
147
  const retry = useRetryAction({
150
148
  decline, order, clearError, setOrder, setDecline, setFinalStatus, setStep, setFreshInstrument,
151
149
  });
152
150
  const completed = useMemo(() => new Set(STEP_ORDER.slice(0, STEP_ORDER.indexOf(step))), [step]);
153
- useSettledPort(finalStatus ?? resume.status, onPaid);
151
+ const settled = finalStatus ?? resume.status;
152
+ useSettledPort(settled, onPaid);
153
+ // The confirmation screen's own wait (FUT-1170). Live only where nothing else
154
+ // is polling and nothing has answered: a resumed leg brings its own wait, and
155
+ // every method's screen polls for itself on the payment step.
156
+ const confirming = useConfirmationWait({
157
+ orderId: order?.orderId ?? null,
158
+ active: step === "status" && settled === null && resume.order === null,
159
+ onSettled: setFinalStatus,
160
+ });
154
161
 
155
162
  return {
156
163
  step, setStep, method, setMethod, buyer, setBuyer, saveProfile, setSaveProfile,
157
- order, finalStatus: finalStatus ?? resume.status, creating,
164
+ order, finalStatus: settled, creating,
158
165
  decline, freshInstrument,
159
- ...resumeSurface(resume),
166
+ ...resumeSurface(resume, confirming),
160
167
  createError: failure.message, errorField: failure.field, errorCode: failure.code,
161
168
  goToMenu: onExitToMenu, back, editBuyer,
162
169
  goToPayment, startPayment, payWithEmail, handleResolved, retry, completed,
@@ -1,8 +1,8 @@
1
- import { useCallback, useEffect, useRef, useState } from "react";
1
+ import { useCallback, useEffect, useRef, useState, type Dispatch, type SetStateAction } from "react";
2
2
 
3
3
  import { useCheckoutClientApi } from "./client-context";
4
4
  import { useCheckoutCopy } from "./copy-context";
5
- import { createPollLoop, type PollLoop, type PollingOptions } from "./poll-loop";
5
+ import { createPollLoop, type PollLoop, type PollingOptions, type PollSink } from "./poll-loop";
6
6
  import type { OrderStatus } from "./types";
7
7
 
8
8
  /** What a consumer reads off the wait, and the one action it can take. */
@@ -24,6 +24,50 @@ interface PaymentPollingState {
24
24
  checkAgain: () => void;
25
25
  }
26
26
 
27
+ /**
28
+ * What one wait has learned, and WHICH order it learned it about (FUT-1170).
29
+ *
30
+ * The three facts used to be three `useState`s with no order id attached, so a
31
+ * consumer re-pointed at a new charge read the PREVIOUS charge's answer until
32
+ * the new loop's first poll returned. That is not a cosmetic flash: a terminal
33
+ * status is acted on, and `PixView` acts on it by handing the flow to the
34
+ * confirmation screen — so a fresh, unpaid PIX code was reported settled by the
35
+ * code it had just replaced. Regenerating an expired charge is the path that
36
+ * does exactly this, and it is the one FUT-1170 was reported from.
37
+ *
38
+ * Stamping the answer and comparing at READ time (rather than clearing the
39
+ * state in an effect) is what makes the guard hold in the render that changes
40
+ * the id, with no intermediate frame in which the stale answer is still live.
41
+ */
42
+ interface PollAnswer {
43
+ orderId: string | null;
44
+ status: OrderStatus | null;
45
+ error: string | null;
46
+ timedOut: boolean;
47
+ }
48
+
49
+ const NO_ANSWER: PollAnswer = { orderId: null, status: null, error: null, timedOut: false };
50
+
51
+ /**
52
+ * Where a wait for ONE order writes what it learns.
53
+ *
54
+ * Every write is stamped with the order the loop was started for, and a write
55
+ * arriving against a DIFFERENT stamp starts from `NO_ANSWER` rather than
56
+ * merging into the previous order's facts.
57
+ */
58
+ function sinkFor(
59
+ orderId: string,
60
+ setAnswer: Dispatch<SetStateAction<PollAnswer>>,
61
+ ): PollSink {
62
+ const write = (patch: Partial<PollAnswer>): void =>
63
+ setAnswer((prev) => ({ ...(prev.orderId === orderId ? prev : NO_ANSWER), ...patch, orderId }));
64
+ return {
65
+ setStatus: (status) => write({ status }),
66
+ setError: (error) => write({ error }),
67
+ setTimedOut: (timedOut) => write({ timedOut }),
68
+ };
69
+ }
70
+
27
71
 
28
72
  /**
29
73
  * Ask again the moment the shopper could plausibly have an answer for us.
@@ -77,9 +121,7 @@ export function usePaymentPolling(
77
121
  askTimeoutMs,
78
122
  }: PollingOptions = {},
79
123
  ): PaymentPollingState {
80
- const [status, setStatus] = useState<OrderStatus | null>(null);
81
- const [error, setError] = useState<string | null>(null);
82
- const [timedOut, setTimedOut] = useState(false);
124
+ const [answer, setAnswer] = useState<PollAnswer>(NO_ANSWER);
83
125
  // Whichever mount this tree is bound to (FUT-741). Stable per provider, so it
84
126
  // belongs in the deps below rather than being read out of a ref: a checkout
85
127
  // re-pointed at another mount must re-poll against THAT one.
@@ -106,7 +148,7 @@ export function usePaymentPolling(
106
148
  askTimeoutMs,
107
149
  askTimeoutError: transportCopy.offline,
108
150
  },
109
- { setStatus, setError, setTimedOut },
151
+ sinkFor(orderId, setAnswer),
110
152
  );
111
153
  loop.current = running;
112
154
  running.restart();
@@ -133,6 +175,15 @@ export function usePaymentPolling(
133
175
  loop.current?.restart();
134
176
  }, []);
135
177
 
136
- return { status, error, timedOut, checkAgain };
178
+ // The answer is this order's, or there is no answer yet. Read here rather
179
+ // than reset in an effect: an effect runs after paint, so the render that
180
+ // re-points the hook would still hand a consumer the old charge's status.
181
+ const current = answer.orderId === orderId ? answer : NO_ANSWER;
182
+ return {
183
+ status: current.status,
184
+ error: current.error,
185
+ timedOut: current.timedOut,
186
+ checkAgain,
187
+ };
137
188
  }
138
189
 
@@ -1,5 +1,7 @@
1
1
  import { useEffect, useState } from "react";
2
2
 
3
+ import { parkedBasket } from "./basket";
4
+ import type { ChallengeScope } from "./card-outcome";
3
5
  import { useCheckoutClientApi } from "./client-context";
4
6
  import { UNRESOLVED_CODE } from "./failure-codes";
5
7
  import { rememberHostedOrder } from "./hosted-return";
@@ -62,12 +64,33 @@ export interface WalletCharge {
62
64
  */
63
65
  const WALLET_AWAITING_WAIT_MS = 90_000;
64
66
 
67
+ /**
68
+ * Park the order before the tab leaves, WITH the store and the basket
69
+ * (FUT-1240).
70
+ *
71
+ * Both absences are read as "no opinion" by the resume — `belongsHere` passes
72
+ * a slug-less entry at any store and the basket rule passes a basket-less one
73
+ * against any basket — so a wallet's 3-D Secure hand-off that named neither
74
+ * resumed over whatever checkout mounted next. The card path has carried these
75
+ * since FUT-1213; this call site is the one that did not.
76
+ */
77
+ function parkForHandover(order: CheckoutOrder, scope: ChallengeScope): void {
78
+ const basket = parkedBasket(scope.basket);
79
+ rememberHostedOrder(order, {
80
+ ...(scope.tenantSlug === undefined ? {} : { tenantSlug: scope.tenantSlug }),
81
+ ...(basket === undefined ? {} : { basket }),
82
+ handoff: true,
83
+ });
84
+ }
85
+
65
86
  /** The wallet charge state machine. See the module comment. */
66
87
  export function useWalletCharge(
67
88
  order: CheckoutOrder,
68
89
  buyer: BuyerInfo,
69
90
  onResolved: (status: OrderStatus) => void,
70
91
  pollIntervalMs = 2500,
92
+ /** WHOSE store and WHICH basket — see {@link parkForHandover} (FUT-1240). */
93
+ scope: ChallengeScope = {},
71
94
  ): WalletCharge {
72
95
  const [phase, setPhase] = useState<WalletPhase>("idle");
73
96
  const [error, setError] = useState<string | null>(null);
@@ -116,7 +139,7 @@ export function useWalletCharge(
116
139
  // FUT-698): park the order and hand the buyer over, exactly as the card
117
140
  // path does. `phase` stays as-is — the tab is navigating away.
118
141
  if (charged.data.hostedCheckoutUrl) {
119
- rememberHostedOrder(order);
142
+ parkForHandover(order, scope);
120
143
  navigate(charged.data.hostedCheckoutUrl);
121
144
  return true;
122
145
  }
@@ -130,12 +130,50 @@ export interface PaymentStatusCopy {
130
130
  receiptEmailLabel: string;
131
131
  }
132
132
 
133
+ /**
134
+ * The pipeline engine's own two sentences (FUT-1240).
135
+ *
136
+ * REQUIRED, both of them, and stated here rather than defaulted for the reason
137
+ * every other string in this file is: a default in the origin host's language
138
+ * reads as finished right up until a shopper sees it. These two are the whole
139
+ * of what the ENGINE renders on its own account — everything else on screen
140
+ * belongs to a step, a gate or a settlement method.
141
+ */
142
+ export interface CheckoutPipelineCopy {
143
+ /**
144
+ * What is on screen while the checkout has nothing to show yet: a gate still
145
+ * deciding, the store's protocol still in flight, or no step applying.
146
+ *
147
+ * It replaces a blank frame. A shopper who taps "pagar" and gets an empty
148
+ * page taps again.
149
+ */
150
+ loading: string;
151
+ /**
152
+ * PER SETTLEMENT METHOD, what the shopper reads between choosing it and the
153
+ * surface that takes the money arriving — the hand-off interstitial's own
154
+ * line. Keyed by `SettlementMethodDescriptor.id`.
155
+ *
156
+ * Per method because the sentences are not interchangeable: a Pix hand-off
157
+ * and a card challenge send the shopper to different places for different
158
+ * reasons, and one shared "aguarde" describes neither. A method with no
159
+ * entry falls back to {@link CheckoutPipelineCopy.loading}, which is what a
160
+ * host registering a new charged method gets until it writes the sentence.
161
+ */
162
+ awaitingHandover: Readonly<Record<string, string>>;
163
+ }
164
+
133
165
  /** What the legacy `CheckoutFlow` itself renders and must be handed. */
134
166
  export interface CheckoutViewCopy {
135
167
  steps: CheckoutStepperCopy;
136
168
  dados: DadosStepCopy;
137
169
  emptyCart: EmptyCartCopy;
138
170
  status: PaymentStatusCopy;
171
+ /**
172
+ * The engine's own two sentences (FUT-1240). Carried here, beside the
173
+ * stepper labels, so a host still answers copy exactly once — the pipeline
174
+ * is another way of rendering this same checkout, not a second surface.
175
+ */
176
+ pipeline: CheckoutPipelineCopy;
139
177
  /**
140
178
  * The words the screens BELOW these read — the card fields, the wallet
141
179
  * panes, the buyer-details inputs (FUT-760).
@@ -150,7 +150,12 @@ export function WalletCardPane(props: WalletPaneProps): JSX.Element {
150
150
  const copy = useCheckoutCopy().screens.settling;
151
151
  const { order, buyer, config, tenantSlug, onResolved, pollIntervalMs } = props;
152
152
  const { freshInstrument, basket } = props;
153
- const wallet = useWalletCharge(order, buyer, onResolved, pollIntervalMs);
153
+ // The same store and basket the card path already parks with (FUT-1213,
154
+ // FUT-1240): a wallet's 3-D Secure hand-off is a hand-off like any other.
155
+ const wallet = useWalletCharge(order, buyer, onResolved, pollIntervalMs, {
156
+ ...(tenantSlug === undefined ? {} : { tenantSlug }),
157
+ ...(basket === undefined ? {} : { basket }),
158
+ });
154
159
  // A sheet failure the wallet reported before any charge existed (pay.js
155
160
  // refused, merchant validation unavailable, the sheet errored) — shown
156
161
  // beside the form, which stays usable.
@@ -0,0 +1,33 @@
1
+ /**
2
+ * ONE WAY OUT OF A CHECKOUT (FUT-1240).
3
+ *
4
+ * A checkout has two controls that leave it — the chrome's back link on the
5
+ * first step, and "voltar ao cardápio" on the confirmation — and until this
6
+ * existed they answered differently: back honoured a registered
7
+ * {@link CheckoutExit} while the confirmation went straight to
8
+ * `ports.exitToCatalog`. So a host that registered its router's own catalog
9
+ * route got it from one control and a full page navigation from the other,
10
+ * with the difference visible only to the shopper who pressed the second one.
11
+ *
12
+ * `exit` is a FACTORY-scope constant, so calling its `useCatalog()` here is a
13
+ * hook call whose presence never changes between renders.
14
+ */
15
+ import { useCallback } from "react";
16
+
17
+ import type { FlowsRuntime } from "./runtime";
18
+
19
+ /**
20
+ * Leave for the host's catalog, through whichever door it registered.
21
+ *
22
+ * `exit` wins when there is one — it is the host's own router, and a router
23
+ * navigation keeps the SPA alive. `ports.exitToCatalog` is the fallback every
24
+ * host already wires, and stays the answer for a host that registered nothing.
25
+ */
26
+ export function useCatalogExit(runtime: FlowsRuntime): () => void {
27
+ const exit = runtime.config.exit;
28
+ const catalog = exit?.useCatalog();
29
+ return useCallback(() => {
30
+ if (exit && catalog) exit.navigate(catalog.to);
31
+ else runtime.config.ports.exitToCatalog();
32
+ }, [exit, catalog, runtime]);
33
+ }
@@ -22,6 +22,8 @@ import { createCheckoutClient } from "../components/checkout/transport";
22
22
  import type { CheckoutProviderConfig, SettlementCheckout } from "../components/checkout/types";
23
23
  import { useCheckoutController } from "../components/checkout/use-checkout-controller";
24
24
 
25
+ import { buildPipeline } from "./pipeline/engine";
26
+ import { pipelineRequested } from "./pipeline/types";
25
27
  import { FlowsProvider, useResolvedConfig, type FlowsRuntime } from "./runtime";
26
28
  import { buyerScreens } from "./screens-buyer";
27
29
  import { hostedScreens } from "./screens-hosted";
@@ -203,12 +205,21 @@ export function createPaymentFlows(config: PaymentFlowsConfig): PaymentFlows {
203
205
  );
204
206
  }
205
207
 
208
+ // THE ADDITIVE SWITCH (FUT-1240). A host that registered a step, a gate, a
209
+ // settlement method, an intent, an open payable, an exit or a settle
210
+ // callback gets the pipeline; a host that registered none gets the flat
211
+ // three-step flow, unchanged, down to its test ids. `useAdmission` is built
212
+ // either way — with no gates registered it passes, which is the honest
213
+ // answer for a host that has declared no admission rules.
214
+ const pipeline = buildPipeline(runtime, screens);
215
+
206
216
  return {
207
- Checkout: buildCheckout(runtime, screens),
217
+ Checkout: pipelineRequested(config) ? pipeline.Checkout : buildCheckout(runtime, screens),
208
218
  Provider,
209
219
  screens,
210
220
  useCheckout: buildUseCheckout(runtime),
211
221
  useCheckoutConfig: () => useResolvedConfig(runtime),
222
+ useAdmission: pipeline.useAdmission,
212
223
  client: runtime.client,
213
224
  };
214
225
  }
@@ -0,0 +1,104 @@
1
+ /**
2
+ * WHAT A STEP CAN DO, as a context (FUT-1240).
3
+ *
4
+ * A step's `render` is handed only what §4.2 declares — its context, its
5
+ * facts, its slice, and the two navigations. Everything a step needs to CHANGE
6
+ * (choose a method, place the order, report a terminal status, edit the buyer)
7
+ * belongs to the engine, and the engine is the only writer of each.
8
+ *
9
+ * It arrives as a React context rather than as a closure, for one mechanical
10
+ * reason: the registered arrays must be identity-stable across renders (see
11
+ * `stable-plugins.ts`), so a step object cannot be rebuilt each render to
12
+ * capture this render's callbacks. A context is read where it is used, in a
13
+ * component, and leaves the step objects frozen.
14
+ */
15
+ import { createContext, useContext, type JSX, type ReactNode } from "react";
16
+
17
+ import type { CheckoutDecline } from "../../components/checkout/decline";
18
+ import type {
19
+ BuyerInfo,
20
+ CheckoutError,
21
+ CheckoutOrder,
22
+ OrderStatus,
23
+ } from "../../components/checkout/types";
24
+ import type { CheckoutViewCopy } from "../../components/checkout/view-copy";
25
+ import type { CheckoutScreens } from "../types";
26
+
27
+ import type { AnySettlementMethod, CheckoutContext } from "./types";
28
+
29
+ /** The engine's writers, plus the two tables a step renders from. */
30
+ export interface PipelineActions {
31
+ /** Every screen the factory built, already bound to transport and slots. */
32
+ screens: CheckoutScreens;
33
+ /** The words. The engine's own two live under `copy.pipeline`. */
34
+ copy: CheckoutViewCopy;
35
+ /** Every registered settlement method, in picker order. */
36
+ methods: readonly AnySettlementMethod[];
37
+ /** The subset this shopper is actually offered, in the same order. */
38
+ offered: readonly AnySettlementMethod[];
39
+ /** The shopper picked a way to settle. Raises the payable unless a Review owns that. */
40
+ choose(methodId: string): void;
41
+ /** Raise the payable for the chosen method — a `Review`'s own action. */
42
+ place(): void;
43
+ /** A payable is being raised right now. */
44
+ placing: boolean;
45
+ /** "Continuar" on the buyer-details step: gate, persist, advance. */
46
+ continueFromDados(): void;
47
+ setBuyer(buyer: BuyerInfo): void;
48
+ saveProfile: boolean;
49
+ setSaveProfile(save: boolean): void;
50
+ /** A terminal status, carrying the refusal when the charge produced one. */
51
+ resolve(status: OrderStatus, decline?: CheckoutDecline | null): void;
52
+ /**
53
+ * Take up an order this visit did not raise — a resumed on-page charge.
54
+ * Separate from {@link PipelineActions.place} because nothing is being
55
+ * created: the charge exists, and what changes is only which order the walk
56
+ * is about.
57
+ */
58
+ adoptOrder(order: CheckoutOrder): void;
59
+ /** Leave for the host's catalog. */
60
+ exitToCatalog(): void;
61
+ /** The refusal the engine currently holds, whoever claimed it. */
62
+ error: CheckoutError | null;
63
+ /** Reopen the buyer-details step — the payer block's "alterar". */
64
+ editBuyer: (() => void) | undefined;
65
+ }
66
+
67
+ const PipelineActionsContext = createContext<PipelineActions | null>(null);
68
+
69
+ /** The engine's actions. Throws outside the engine, on purpose. */
70
+ export function usePipelineActions(): PipelineActions {
71
+ const actions = useContext(PipelineActionsContext);
72
+ if (!actions) {
73
+ throw new Error(
74
+ "usePipelineActions() was called outside the checkout pipeline. A step's " +
75
+ "render only runs inside <Checkout />; mounting one on its own is what " +
76
+ "`flows.screens.*` is for.",
77
+ );
78
+ }
79
+ return actions;
80
+ }
81
+
82
+ /** Supplied once by the engine, above every step. */
83
+ export function PipelineActionsProvider({
84
+ actions,
85
+ children,
86
+ }: {
87
+ actions: PipelineActions;
88
+ children: ReactNode;
89
+ }): JSX.Element {
90
+ return (
91
+ <PipelineActionsContext.Provider value={actions}>
92
+ {children}
93
+ </PipelineActionsContext.Provider>
94
+ );
95
+ }
96
+
97
+ /** The descriptor for a chosen method, or `undefined` when nobody registered it. */
98
+ export function descriptorFor(
99
+ methods: readonly AnySettlementMethod[],
100
+ ctx: CheckoutContext,
101
+ ): AnySettlementMethod | undefined {
102
+ if (ctx.method === null) return undefined;
103
+ return methods.find((entry) => entry.id === ctx.method);
104
+ }
@@ -0,0 +1,55 @@
1
+ /**
2
+ * MAY THIS SHOPPER CHECK OUT — one answer, offered headless (FUT-1240).
3
+ *
4
+ * The gates run in ARRAY ORDER and the first non-`pass` verdict wins. Order
5
+ * matters and is the host's to choose: a gate that would curtain the screen
6
+ * must not speak before the gate that is still waiting for the cart, or a
7
+ * shopper meets "loja fechada" because a fact had not arrived yet.
8
+ *
9
+ * Exported as `flows.useAdmission()` so the cart drawer's CTA and a
10
+ * buy-now button consume the SAME list the checkout does. Two surfaces
11
+ * deciding this separately is how a storefront ends up with a drawer that
12
+ * offers a checkout the checkout itself refuses.
13
+ */
14
+ import { hostedCheckoutReturnPending } from "../../components/checkout/hosted-return";
15
+
16
+ import type { AnyCheckoutGate, CheckoutContext, GateVerdict } from "./types";
17
+
18
+ /** Nothing said otherwise. */
19
+ const PASS: GateVerdict = { kind: "pass" };
20
+
21
+ /**
22
+ * The gates' verdict, given the facts each of them returned.
23
+ *
24
+ * Pure: the hooks run in the caller's body, in array order, and their answers
25
+ * arrive here as a list. That is what makes this testable without a renderer
26
+ * and what keeps hook order a property of the ARRAY rather than of the
27
+ * verdicts.
28
+ */
29
+ export function decideAdmission(input: {
30
+ gates: readonly AnyCheckoutGate[];
31
+ facts: readonly unknown[];
32
+ ctx: CheckoutContext;
33
+ /** A hand-off from this tab is still waiting — see `standsAsideForResume`. */
34
+ resuming: boolean;
35
+ }): GateVerdict {
36
+ for (const [at, gate] of input.gates.entries()) {
37
+ // A gate that stands aside for a resume is standing aside from the one
38
+ // route where money gets confirmed. Skipped for that visit only.
39
+ if (input.resuming && gate.standsAsideForResume) continue;
40
+ const verdict = gate.decide(input.ctx, input.facts[at]);
41
+ if (verdict.kind !== "pass") return verdict;
42
+ }
43
+ return PASS;
44
+ }
45
+
46
+ /**
47
+ * Whether a hand-off from this tab is still waiting to be resolved.
48
+ *
49
+ * Asked with the SAME slug and basket the resume asks with, so a gate and the
50
+ * flow behind it cannot disagree about whose return this is — the property
51
+ * `hostedCheckoutReturnPending`'s own doc argues for at length.
52
+ */
53
+ export function resumePending(ctx: CheckoutContext): boolean {
54
+ return hostedCheckoutReturnPending(ctx.tenantSlug, ctx.cart.identity);
55
+ }