@relyon/kit 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +75 -0
- package/CHANGELOG.md +1053 -0
- package/LICENSE +21 -0
- package/QUICKSTART.md +239 -0
- package/README.md +117 -2
- package/dist/adapters/express.d.ts +39 -0
- package/dist/adapters/express.js +106 -0
- package/dist/adapters/hono.d.ts +16 -0
- package/dist/adapters/hono.js +15 -0
- package/dist/adapters/next.d.ts +29 -0
- package/dist/adapters/next.js +38 -0
- package/dist/agent-card.d.ts +14 -0
- package/dist/agent-card.js +58 -0
- package/dist/agent-docs.d.ts +36 -0
- package/dist/agent-docs.js +78 -0
- package/dist/agent-hooks.d.ts +66 -0
- package/dist/agent-hooks.js +280 -0
- package/dist/agents.d.ts +24 -0
- package/dist/agents.js +120 -0
- package/dist/api-key.d.ts +6 -0
- package/dist/api-key.js +33 -0
- package/dist/cache.d.ts +46 -0
- package/dist/cache.js +86 -0
- package/dist/cart-tokens.d.ts +31 -0
- package/dist/cart-tokens.js +58 -0
- package/dist/cart-transfer.d.ts +95 -0
- package/dist/cart-transfer.js +387 -0
- package/dist/catalog-enrichment.d.ts +6 -0
- package/dist/catalog-enrichment.js +71 -0
- package/dist/catalog-reads.d.ts +126 -0
- package/dist/catalog-reads.js +104 -0
- package/dist/catalog.d.ts +78 -0
- package/dist/catalog.js +288 -0
- package/dist/charge-claims.d.ts +11 -0
- package/dist/charge-claims.js +47 -0
- package/dist/check.d.ts +44 -0
- package/dist/check.js +152 -0
- package/dist/checklist-manifest.d.ts +72 -0
- package/dist/checklist-manifest.js +341 -0
- package/dist/checkout.d.ts +222 -0
- package/dist/checkout.js +353 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +727 -0
- package/dist/confirmation.d.ts +78 -0
- package/dist/confirmation.js +124 -0
- package/dist/context.d.ts +81 -0
- package/dist/context.js +10 -0
- package/dist/discovery.d.ts +151 -0
- package/dist/discovery.js +346 -0
- package/dist/doctor-cart-transfer.d.ts +33 -0
- package/dist/doctor-cart-transfer.js +186 -0
- package/dist/doctor-catalog.d.ts +8 -0
- package/dist/doctor-catalog.js +47 -0
- package/dist/doctor-evidence.d.ts +22 -0
- package/dist/doctor-evidence.js +57 -0
- package/dist/doctor-merchant.d.ts +48 -0
- package/dist/doctor-merchant.js +74 -0
- package/dist/doctor-source.d.ts +5 -0
- package/dist/doctor-source.js +142 -0
- package/dist/doctor-variants.d.ts +70 -0
- package/dist/doctor-variants.js +102 -0
- package/dist/doctor.d.ts +215 -0
- package/dist/doctor.js +1689 -0
- package/dist/events.d.ts +95 -0
- package/dist/events.js +200 -0
- package/dist/feed-mapping.d.ts +114 -0
- package/dist/feed-mapping.js +152 -0
- package/dist/feed-values.d.ts +13 -0
- package/dist/feed-values.js +40 -0
- package/dist/feeds.d.ts +104 -0
- package/dist/feeds.js +366 -0
- package/dist/identify.d.ts +102 -0
- package/dist/identify.js +124 -0
- package/dist/identity.d.ts +37 -0
- package/dist/identity.js +54 -0
- package/dist/index.d.ts +42 -0
- package/dist/index.js +41 -0
- package/dist/init.d.ts +157 -0
- package/dist/init.js +912 -0
- package/dist/kit-config.d.ts +19 -0
- package/dist/kit-config.js +43 -0
- package/dist/kit-environment.d.ts +20 -0
- package/dist/kit-environment.js +32 -0
- package/dist/kit-updates.d.ts +47 -0
- package/dist/kit-updates.js +77 -0
- package/dist/kit-version-project.d.ts +18 -0
- package/dist/kit-version-project.js +214 -0
- package/dist/kit-version.d.ts +37 -0
- package/dist/kit-version.js +162 -0
- package/dist/kit.d.ts +516 -0
- package/dist/kit.js +1309 -0
- package/dist/layers/pages.d.ts +3 -0
- package/dist/layers/pages.js +5 -0
- package/dist/layers/payments.d.ts +7 -0
- package/dist/layers/payments.js +9 -0
- package/dist/layers/products.d.ts +10 -0
- package/dist/layers/products.js +13 -0
- package/dist/live.d.ts +13 -0
- package/dist/live.js +52 -0
- package/dist/mcp.d.ts +270 -0
- package/dist/mcp.js +312 -0
- package/dist/next-steps.d.ts +101 -0
- package/dist/next-steps.js +257 -0
- package/dist/order-pending.d.ts +57 -0
- package/dist/order-pending.js +127 -0
- package/dist/orders.d.ts +12 -0
- package/dist/orders.js +21 -0
- package/dist/payments-init.d.ts +19 -0
- package/dist/payments-init.js +70 -0
- package/dist/payments-routes.d.ts +54 -0
- package/dist/payments-routes.js +123 -0
- package/dist/payments.d.ts +165 -0
- package/dist/payments.js +192 -0
- package/dist/pnpm-workspace.d.ts +58 -0
- package/dist/pnpm-workspace.js +250 -0
- package/dist/policies.d.ts +111 -0
- package/dist/policies.js +317 -0
- package/dist/preview-hosts.d.ts +64 -0
- package/dist/preview-hosts.js +175 -0
- package/dist/private-config.d.ts +169 -0
- package/dist/private-config.js +820 -0
- package/dist/progress.d.ts +54 -0
- package/dist/progress.js +153 -0
- package/dist/protocol-inputs.d.ts +22 -0
- package/dist/protocol-inputs.js +151 -0
- package/dist/public-origin.d.ts +50 -0
- package/dist/public-origin.js +180 -0
- package/dist/refusals.d.ts +86 -0
- package/dist/refusals.js +157 -0
- package/dist/relay.d.ts +107 -0
- package/dist/relay.js +655 -0
- package/dist/release-notes.d.ts +2 -0
- package/dist/release-notes.js +290 -0
- package/dist/relyon-cart-tokens.d.ts +101 -0
- package/dist/relyon-cart-tokens.js +521 -0
- package/dist/relyon.d.ts +141 -0
- package/dist/relyon.js +238 -0
- package/dist/robots.d.ts +27 -0
- package/dist/robots.js +127 -0
- package/dist/rules.d.ts +144 -0
- package/dist/rules.js +347 -0
- package/dist/sdk/generated/public-api.d.ts +4816 -0
- package/dist/sdk/generated/public-api.js +1 -0
- package/dist/sdk/index.d.ts +24 -0
- package/dist/sdk/index.js +34 -0
- package/dist/sdk/signatures.d.ts +17 -0
- package/dist/sdk/signatures.js +33 -0
- package/dist/setup.d.ts +37 -0
- package/dist/setup.js +94 -0
- package/dist/signatures.d.ts +112 -0
- package/dist/signatures.js +443 -0
- package/dist/state.d.ts +185 -0
- package/dist/state.js +96 -0
- package/dist/store-payments.d.ts +48 -0
- package/dist/store-payments.js +143 -0
- package/dist/structured-fields.d.ts +26 -0
- package/dist/structured-fields.js +200 -0
- package/dist/templates.d.ts +19 -0
- package/dist/templates.js +193 -0
- package/dist/test-checkout-command.d.ts +32 -0
- package/dist/test-checkout-command.js +161 -0
- package/dist/test-checkout.d.ts +88 -0
- package/dist/test-checkout.js +292 -0
- package/dist/types.d.ts +103 -0
- package/dist/types.js +18 -0
- package/dist/ucp-versions.d.ts +57 -0
- package/dist/ucp-versions.js +129 -0
- package/dist/util.d.ts +23 -0
- package/dist/util.js +68 -0
- package/dist/variants.d.ts +67 -0
- package/dist/variants.js +146 -0
- package/dist/version.d.ts +9 -0
- package/dist/version.js +10 -0
- package/dist/waiting.d.ts +43 -0
- package/dist/waiting.js +57 -0
- package/dist/webhook-command.d.ts +48 -0
- package/dist/webhook-command.js +367 -0
- package/dist/webhook-registration.d.ts +47 -0
- package/dist/webhook-registration.js +69 -0
- package/dist/webmcp-schema.d.ts +14 -0
- package/dist/webmcp-schema.js +140 -0
- package/dist/webmcp-script.d.ts +3 -0
- package/dist/webmcp-script.js +176 -0
- package/dist/webmcp-store.d.ts +102 -0
- package/dist/webmcp-store.js +116 -0
- package/dist/webmcp.d.ts +242 -0
- package/dist/webmcp.js +866 -0
- package/dist/wire-names.d.ts +85 -0
- package/dist/wire-names.js +109 -0
- package/package.json +118 -4
- package/release-notes.json +290 -0
- package/scripts/install-notice.cjs +7 -0
- package/skills/relyon-agents/SKILL.md +327 -0
- package/skills/relyon-catalog/SKILL.md +499 -0
- package/skills/relyon-catalog/references/example-store.md +218 -0
- package/skills/relyon-checkout/SKILL.md +603 -0
- package/skills/relyon-checkout/references/checkout-flow.md +257 -0
- package/skills/relyon-checkout/references/checkout-page.md +315 -0
- package/skills/relyon-go-live/SKILL.md +344 -0
- package/skills/relyon-orders/SKILL.md +191 -0
- package/skills/relyon-orders/references/webhook-receiver.md +113 -0
- package/skills/relyon-payments/SKILL.md +117 -0
- package/skills/relyon-setup/SKILL.md +1061 -0
- package/skills/relyon-setup/references/QUICKSTART.md +239 -0
- package/skills/relyon-setup/references/durable-state.md +209 -0
- package/skills/relyon-setup/references/express-server.md +388 -0
- package/skills/relyon-setup/references/next-app-router.md +251 -0
- package/templates/common/STORE-README.md +67 -0
- package/templates/common/gitignore +8 -0
- package/templates/common/scripts/check-pricing.ts +22 -0
- package/templates/common/scripts/serve.mjs +13 -0
- package/templates/common/server/catalog.ts +79 -0
- package/templates/common/server/checkout-flow.d.ts +34 -0
- package/templates/common/server/pages.ts +184 -0
- package/templates/common/server/storage.ts +114 -0
- package/templates/common/server/store.ts +67 -0
- package/templates/express/server/express.ts +20 -0
- package/templates/express/tsconfig.build.json +6 -0
- package/templates/express/tsconfig.json +9 -0
- package/templates/next/app/[[...path]]/route.ts +24 -0
- package/templates/next/next.config.mjs +19 -0
- package/templates/next/tsconfig.json +12 -0
- package/templates/payments/express.ts +39 -0
- package/templates/payments/next.ts +38 -0
- package/templates/payments/node.ts +39 -0
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
import { type AgentIdentity, type RelyonRuleRef } from "./identify.js";
|
|
2
|
+
import type { CartTokenResolver } from "./cart-tokens.js";
|
|
3
|
+
import type { KitContext, OrderOnce } from "./context.js";
|
|
4
|
+
import { RelyonProblemError, RelyonUnavailableError } from "./relyon.js";
|
|
5
|
+
import type { AgentProtocol, Cart, CartInputs, PaymentChannel, PaymentLike, PaymentSession, RequestInfo } from "./types.js";
|
|
6
|
+
import { StoreRefusal } from "./types.js";
|
|
7
|
+
export declare const CHANNEL_FOR: Record<AgentProtocol, PaymentChannel>;
|
|
8
|
+
/**
|
|
9
|
+
* The channel the kit claims on a payment for a hand-off of this protocol.
|
|
10
|
+
* Relyon records the channel of every agent checkout and its record wins,
|
|
11
|
+
* but it refuses a claim that differs from it. A UCP checkout is "google"
|
|
12
|
+
* only when Relyon verified Google's own profile, which the store can't see,
|
|
13
|
+
* so for UCP the kit claims nothing ("web") and Relyon applies its record.
|
|
14
|
+
*/
|
|
15
|
+
export declare function paymentChannelFor(protocol: AgentProtocol): PaymentChannel;
|
|
16
|
+
/** Card-field session creation was unavailable; no charge was attempted. */
|
|
17
|
+
export declare class PaymentUnavailableError extends Error {
|
|
18
|
+
constructor(message?: string);
|
|
19
|
+
}
|
|
20
|
+
/** A charge is in flight; the server may not yet have assigned a payment ID. */
|
|
21
|
+
export interface PendingPayment extends Record<string, unknown> {
|
|
22
|
+
object: "payment";
|
|
23
|
+
status: "pending";
|
|
24
|
+
id?: string;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* A payment may have charged. Reconcile/retry the original request, never a
|
|
28
|
+
* replacement order. The original card-entry request and credentials are
|
|
29
|
+
* never retained on this error.
|
|
30
|
+
*/
|
|
31
|
+
export declare class PaymentOutcomeUnknownError extends Error {
|
|
32
|
+
readonly orderReference: string;
|
|
33
|
+
readonly idempotencyKey: string;
|
|
34
|
+
readonly reason: "pending" | RelyonUnavailableError["kind"];
|
|
35
|
+
readonly payment?: PendingPayment | undefined;
|
|
36
|
+
constructor(orderReference: string, idempotencyKey: string, reason: "pending" | RelyonUnavailableError["kind"], payment?: PendingPayment | undefined);
|
|
37
|
+
}
|
|
38
|
+
/** A Relyon agent rule refuses this payment at the store's edge. `rule` names it. */
|
|
39
|
+
export declare class AgentRefusedError extends Error {
|
|
40
|
+
readonly code: "agent_refused" | "channel_paused";
|
|
41
|
+
readonly rule?: RelyonRuleRef | undefined;
|
|
42
|
+
constructor(code: "agent_refused" | "channel_paused", message: string, rule?: RelyonRuleRef | undefined);
|
|
43
|
+
}
|
|
44
|
+
/** The note on a `payAndConfirm` rule refusal for a payment that may already have been charged. */
|
|
45
|
+
export declare const PAY_CHARGE_NEUTRAL_NOTE: string;
|
|
46
|
+
/**
|
|
47
|
+
* A kit agent-rules refusal's message when the order reference may already
|
|
48
|
+
* have been charged (a retry): the rule (or outage), never "nothing was charged".
|
|
49
|
+
*/
|
|
50
|
+
export declare function chargeNeutralGateMessage(error: AgentRefusedError | PaymentUnavailableError): string;
|
|
51
|
+
/**
|
|
52
|
+
* Relyon refused the payment under its agent rules or limits (`agent_refused`
|
|
53
|
+
* on `createPayment`). Still a `RelyonProblemError`; `rule` names the Relyon
|
|
54
|
+
* rule from what the Problem says.
|
|
55
|
+
*/
|
|
56
|
+
export declare class RelyonRuleRefusalError extends RelyonProblemError {
|
|
57
|
+
readonly rule: RelyonRuleRef;
|
|
58
|
+
constructor(error: RelyonProblemError);
|
|
59
|
+
}
|
|
60
|
+
export interface CardFieldsInput {
|
|
61
|
+
/** The amount the shopper will pay (the cart's `totals.total`), in minor units. */
|
|
62
|
+
amount: number;
|
|
63
|
+
currency: string;
|
|
64
|
+
/** The store's order reference; the same one goes to `createPayment`. */
|
|
65
|
+
orderReference: string;
|
|
66
|
+
metadata?: Record<string, string>;
|
|
67
|
+
/** Passed to the element's `field-styles` attribute. */
|
|
68
|
+
fieldStyles?: string;
|
|
69
|
+
/** The element's id attribute. Default `relyon-card-fields`. */
|
|
70
|
+
elementId?: string;
|
|
71
|
+
}
|
|
72
|
+
export interface CardFields {
|
|
73
|
+
session: PaymentSession;
|
|
74
|
+
/** The `<script>` tag loading the card fields: the only Relyon URL a shopper loads. */
|
|
75
|
+
scriptTag: string;
|
|
76
|
+
/** The `<relyon-card-fields>` element for this session. */
|
|
77
|
+
element: string;
|
|
78
|
+
/** Both, ready to place in the checkout page. */
|
|
79
|
+
html: string;
|
|
80
|
+
}
|
|
81
|
+
export declare function isPaymentSession(data: unknown): data is PaymentSession;
|
|
82
|
+
/** Creates a payment session and renders the card fields for the store's checkout page. */
|
|
83
|
+
export declare function createCardFields(ctx: KitContext, input: CardFieldsInput, scriptUrl?: string): Promise<CardFields>;
|
|
84
|
+
export interface CreatePaymentInput {
|
|
85
|
+
/** The cart the shopper saw and agreed to; `totals.total` is charged. */
|
|
86
|
+
cart: Cart;
|
|
87
|
+
/** The object `<relyon-card-fields>`'s `tokenize()` resolved to, passed through unchanged. */
|
|
88
|
+
paymentMethod: Record<string, unknown>;
|
|
89
|
+
/** The store's own order reference. The Idempotency-Key is derived from it, so a retry never charges twice. */
|
|
90
|
+
orderReference: string;
|
|
91
|
+
/**
|
|
92
|
+
* The payment attempt for this order reference. Default 0. Raise it only
|
|
93
|
+
* after a confirmed decline (a final `failed` payment), so the next card
|
|
94
|
+
* gets its own Idempotency-Key. Keep it unchanged after
|
|
95
|
+
* `PaymentOutcomeUnknownError`, so the retry reconciles the same request.
|
|
96
|
+
*/
|
|
97
|
+
attempt?: number;
|
|
98
|
+
/** Default: the handed-off agent's channel when `agentCheckoutId` is set, else `web`. */
|
|
99
|
+
channel?: PaymentChannel;
|
|
100
|
+
/** The agent checkout a handed-off shopper came from (from the landing handler). */
|
|
101
|
+
agentCheckoutId?: string | null;
|
|
102
|
+
metadata?: Record<string, string>;
|
|
103
|
+
/** The shopper's request. When it claims to be an agent, the store's agent rules are applied. */
|
|
104
|
+
request?: Request;
|
|
105
|
+
info?: RequestInfo;
|
|
106
|
+
}
|
|
107
|
+
export interface CreatePaymentResult {
|
|
108
|
+
payment: PaymentLike;
|
|
109
|
+
/** True when this call created the store's order (exactly once per succeeded payment). */
|
|
110
|
+
orderCreated: boolean;
|
|
111
|
+
/**
|
|
112
|
+
* The store's order: what `createOrder` returned (`created`), or, on a
|
|
113
|
+
* replay (`already_created`), what the optional `findOrderByPaymentId` hook
|
|
114
|
+
* found for this payment ID.
|
|
115
|
+
*/
|
|
116
|
+
order?: unknown;
|
|
117
|
+
/** Set when the payment succeeded but `createOrder` threw. */
|
|
118
|
+
orderError?: unknown;
|
|
119
|
+
/**
|
|
120
|
+
* For a succeeded payment, what happened to the store's order (see
|
|
121
|
+
* `OrderOnce`): `created` (by this call), `already_created` (by an earlier
|
|
122
|
+
* call), `in_progress` (another request holds the order claim, or the
|
|
123
|
+
* state store can't say), or `failed` (`createOrder` threw, or the state
|
|
124
|
+
* store failed around the claim). Absent when no order was attempted
|
|
125
|
+
* (a failed payment).
|
|
126
|
+
*/
|
|
127
|
+
orderStatus?: OrderOnce["status"];
|
|
128
|
+
/** Who paid, as the store kit identified the request (edge view; Relyon's naming is on the payment). */
|
|
129
|
+
agent?: AgentIdentity;
|
|
130
|
+
}
|
|
131
|
+
/** Today's server cart for POST /v1/payments (see `paymentCartFormat`). */
|
|
132
|
+
export declare function legacyPaymentCart(cart: Cart): Record<string, unknown>;
|
|
133
|
+
/**
|
|
134
|
+
* Charges the cart with `createPayment`: the final amount is the cart's own
|
|
135
|
+
* total, the channel and agent checkout attribute the payment, and the
|
|
136
|
+
* Idempotency-Key comes from the order reference. On success the store's
|
|
137
|
+
* order is created once. An agent refused, or an identified agent whose
|
|
138
|
+
* rules can't be read, throws before any charge is attempted
|
|
139
|
+
* (`AgentRefusedError` / `PaymentUnavailableError`). Pending answers,
|
|
140
|
+
* network failures and unusable replies from the charge itself are UNKNOWN
|
|
141
|
+
* outcomes, not evidence of no charge: `PaymentOutcomeUnknownError` keeps
|
|
142
|
+
* the original reconciliation keys.
|
|
143
|
+
*/
|
|
144
|
+
/** The Idempotency-Key for an order reference and attempt (attempt 0 keeps the original key). */
|
|
145
|
+
export declare function paymentIdempotencyKey(orderReference: string, attempt?: number): string;
|
|
146
|
+
/** Internal options for kit-owned flows (WebMCP). */
|
|
147
|
+
export interface CreatePaymentOptions {
|
|
148
|
+
/**
|
|
149
|
+
* The identity the caller already identified and decided on, just before
|
|
150
|
+
* claiming the payment (the agent-rules decision is not repeated after a
|
|
151
|
+
* claim, where a refusal could no longer leave the approval unused).
|
|
152
|
+
*/
|
|
153
|
+
decidedIdentity?: AgentIdentity;
|
|
154
|
+
/** false: don't create the store's order here; the caller does, once it has recorded the outcome. Default true. */
|
|
155
|
+
createOrder?: boolean;
|
|
156
|
+
/** Runs after the agent-rules gate allowed the payment, right before it is sent to Relyon. Must not throw. */
|
|
157
|
+
beforeCharge?: () => Promise<void>;
|
|
158
|
+
/** Runs after a succeeded payment, before the store's order is created. Must not throw. */
|
|
159
|
+
beforeOrder?: (payment: PaymentLike) => Promise<void>;
|
|
160
|
+
}
|
|
161
|
+
export declare function createPayment(ctx: KitContext, input: CreatePaymentInput, options?: CreatePaymentOptions): Promise<CreatePaymentResult>;
|
|
162
|
+
/**
|
|
163
|
+
* What `kit.landing()` reads: the checkout page's Request, its URL (or a
|
|
164
|
+
* path with the query), or its search params (a Next.js server component's
|
|
165
|
+
* awaited `searchParams`, or a URLSearchParams).
|
|
166
|
+
*/
|
|
167
|
+
export type LandingInput = Request | URL | string | URLSearchParams | Readonly<Record<string, string | string[] | undefined>>;
|
|
168
|
+
/**
|
|
169
|
+
* The checkout page marker doctor looks for: put this attribute, set to the
|
|
170
|
+
* number of items in the cart (0 for an empty cart), on an element of the
|
|
171
|
+
* store's checkout page, for example
|
|
172
|
+
* `<main data-relyon-cart-items="2">`. The kit's `<relyon-card-fields>`
|
|
173
|
+
* mount also counts as a checkout page.
|
|
174
|
+
*/
|
|
175
|
+
export declare const CHECKOUT_CART_ITEMS_ATTRIBUTE = "data-relyon-cart-items";
|
|
176
|
+
/** The number of items in a landed (or any) cart's inputs: the sum of the line quantities. */
|
|
177
|
+
export declare function cartItemCount(inputs: {
|
|
178
|
+
lines: ReadonlyArray<{
|
|
179
|
+
quantity: number;
|
|
180
|
+
}>;
|
|
181
|
+
} | null | undefined): number;
|
|
182
|
+
export interface Landing {
|
|
183
|
+
/** True when the shopper arrived with a valid `relyon_cart` token. */
|
|
184
|
+
attributed: boolean;
|
|
185
|
+
agentCheckoutId: string | null;
|
|
186
|
+
protocol: AgentProtocol | null;
|
|
187
|
+
/**
|
|
188
|
+
* The channel to pass to `createPayment`: "chatgpt" for ACP, "mcp" for
|
|
189
|
+
* MCP, and "web" for UCP, where Relyon applies the channel it recorded
|
|
190
|
+
* ("google" only for Google's own verified profile).
|
|
191
|
+
*/
|
|
192
|
+
channel: PaymentChannel;
|
|
193
|
+
/** The cart re-priced by the store now; null when unattributed or the store refused to price it. */
|
|
194
|
+
cart: Cart | null;
|
|
195
|
+
inputs: CartInputs | null;
|
|
196
|
+
/**
|
|
197
|
+
* Set when `priceCart` refused the landed cart. On a cart already paid
|
|
198
|
+
* (`alreadyPaid`) its message never says nothing was charged.
|
|
199
|
+
*/
|
|
200
|
+
refusal?: StoreRefusal;
|
|
201
|
+
/**
|
|
202
|
+
* Set when this hand-off cart's agent checkout was already paid, or may have
|
|
203
|
+
* been, on the store's own checkout page (the kit remembers a `payAndConfirm`
|
|
204
|
+
* payment for it): show the order status, never a fresh payment form.
|
|
205
|
+
* `orderPending` when that payment succeeded and the order isn't created yet.
|
|
206
|
+
*/
|
|
207
|
+
alreadyPaid?: {
|
|
208
|
+
orderPending: boolean;
|
|
209
|
+
paymentId: string | null;
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
/** Appended to a landing refusal's message when the cart was already paid, in place of any "nothing was charged". */
|
|
213
|
+
export declare const LANDING_PAID_NOTE = "This order may already have been paid; check the order status before paying again.";
|
|
214
|
+
/**
|
|
215
|
+
* Handles a shopper landing on the store's checkout from an agent hand-off.
|
|
216
|
+
* A valid token (a Relyon-issued link the kit saw relayed for that checkout,
|
|
217
|
+
* landed for the first time, or one the store's resolver vouches for)
|
|
218
|
+
* re-prices the cart through `priceCart` and attributes the checkout;
|
|
219
|
+
* anything else (tampered, expired, unknown, replayed) is a normal checkout.
|
|
220
|
+
* Never throws because of the token.
|
|
221
|
+
*/
|
|
222
|
+
export declare function landing(ctx: KitContext, tokens: CartTokenResolver, request: Request, info?: RequestInfo): Promise<Landing>;
|
package/dist/checkout.js
ADDED
|
@@ -0,0 +1,353 @@
|
|
|
1
|
+
import { PAYMENTS_PATH, PAYMENT_OBJECT, PAYMENT_SESSIONS_PATH, PAYMENT_SESSION_OBJECT } from "./wire-names.js";
|
|
2
|
+
import { decide, forwardedShopperHeaders } from "./identify.js";
|
|
3
|
+
import { describeError, resolveRelyonCartToken } from "./relyon-cart-tokens.js";
|
|
4
|
+
import { requestFacts } from "./events.js";
|
|
5
|
+
import { RelyonProblemError, RelyonUnavailableError, cardFieldsScriptUrl } from "./relyon.js";
|
|
6
|
+
import { StoreRefusal } from "./types.js";
|
|
7
|
+
import { escapeHtml, isRecord, randomId, sha256, upperCurrency, withUpperCurrency } from "./util.js";
|
|
8
|
+
import { claimsNothingCharged, withoutNothingChargedClaims } from "./charge-claims.js";
|
|
9
|
+
import { orderPendingFor } from "./relay.js";
|
|
10
|
+
import { handoffPaymentKey, storePaymentRecord, storePaymentStanding } from "./store-payments.js";
|
|
11
|
+
export const CHANNEL_FOR = { ucp: "google", acp: "chatgpt", mcp: "mcp" };
|
|
12
|
+
/**
|
|
13
|
+
* The channel the kit claims on a payment for a hand-off of this protocol.
|
|
14
|
+
* Relyon records the channel of every agent checkout and its record wins,
|
|
15
|
+
* but it refuses a claim that differs from it. A UCP checkout is "google"
|
|
16
|
+
* only when Relyon verified Google's own profile, which the store can't see,
|
|
17
|
+
* so for UCP the kit claims nothing ("web") and Relyon applies its record.
|
|
18
|
+
*/
|
|
19
|
+
export function paymentChannelFor(protocol) {
|
|
20
|
+
return protocol === "ucp" ? "web" : CHANNEL_FOR[protocol];
|
|
21
|
+
}
|
|
22
|
+
/** Card-field session creation was unavailable; no charge was attempted. */
|
|
23
|
+
export class PaymentUnavailableError extends Error {
|
|
24
|
+
constructor(message = "Payments are temporarily unavailable. Nothing was charged; try again shortly.") {
|
|
25
|
+
super(message);
|
|
26
|
+
this.name = "PaymentUnavailableError";
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* A payment may have charged. Reconcile/retry the original request, never a
|
|
31
|
+
* replacement order. The original card-entry request and credentials are
|
|
32
|
+
* never retained on this error.
|
|
33
|
+
*/
|
|
34
|
+
export class PaymentOutcomeUnknownError extends Error {
|
|
35
|
+
orderReference;
|
|
36
|
+
idempotencyKey;
|
|
37
|
+
reason;
|
|
38
|
+
payment;
|
|
39
|
+
constructor(orderReference, idempotencyKey, reason, payment) {
|
|
40
|
+
super("The payment outcome is not yet known; a charge may have succeeded. Reconcile or retry with the same order reference and idempotency key. Do not start a new payment.");
|
|
41
|
+
this.orderReference = orderReference;
|
|
42
|
+
this.idempotencyKey = idempotencyKey;
|
|
43
|
+
this.reason = reason;
|
|
44
|
+
this.payment = payment;
|
|
45
|
+
this.name = "PaymentOutcomeUnknownError";
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/** A Relyon agent rule refuses this payment at the store's edge. `rule` names it. */
|
|
49
|
+
export class AgentRefusedError extends Error {
|
|
50
|
+
code;
|
|
51
|
+
rule;
|
|
52
|
+
constructor(code, message, rule) {
|
|
53
|
+
super(message);
|
|
54
|
+
this.code = code;
|
|
55
|
+
this.rule = rule;
|
|
56
|
+
this.name = "AgentRefusedError";
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/** The note on a `payAndConfirm` rule refusal for a payment that may already have been charged. */
|
|
60
|
+
export const PAY_CHARGE_NEUTRAL_NOTE = "Check the order status before paying again; a retry must resend the original payment request"
|
|
61
|
+
+ " (same order reference and card payload), never a new card entry.";
|
|
62
|
+
/**
|
|
63
|
+
* A kit agent-rules refusal's message when the order reference may already
|
|
64
|
+
* have been charged (a retry): the rule (or outage), never "nothing was charged".
|
|
65
|
+
*/
|
|
66
|
+
export function chargeNeutralGateMessage(error) {
|
|
67
|
+
const base = error instanceof PaymentUnavailableError
|
|
68
|
+
? "Payments are unavailable: the store can't read its Relyon agent rules right now."
|
|
69
|
+
: error.rule ? `Refused by Relyon agent rule: ${error.rule.label}.` : "Refused by the store's Relyon agent rules.";
|
|
70
|
+
return `${base} ${PAY_CHARGE_NEUTRAL_NOTE}`;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Relyon refused the payment under its agent rules or limits (`agent_refused`
|
|
74
|
+
* on `createPayment`). Still a `RelyonProblemError`; `rule` names the Relyon
|
|
75
|
+
* rule from what the Problem says.
|
|
76
|
+
*/
|
|
77
|
+
export class RelyonRuleRefusalError extends RelyonProblemError {
|
|
78
|
+
rule;
|
|
79
|
+
constructor(error) {
|
|
80
|
+
super(error.status, error.code, error.message, error.param, error.problem);
|
|
81
|
+
this.name = "RelyonRuleRefusalError";
|
|
82
|
+
const extra = error.problem ?? {};
|
|
83
|
+
const declared = extra.rule_source === "platform" || extra.rule_source === "merchant" ? extra.rule_source : null;
|
|
84
|
+
const source = declared ?? (/^this store/i.test(error.message) ? "merchant" : /^relyon/i.test(error.message) ? "platform" : "relyon");
|
|
85
|
+
const kind = /paused/i.test(error.message) ? (/channel/i.test(error.message) ? "paused_channels" : "paused") : "blocked_agents";
|
|
86
|
+
const reason = typeof extra.reason === "string" ? ` ${extra.reason}` : "";
|
|
87
|
+
this.rule = { source, kind, rulesVersion: "server", label: `${source} ${kind}${reason} (applied by Relyon on payment): ${error.message}` };
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
export function isPaymentSession(data) {
|
|
91
|
+
return isRecord(data) && data.object === PAYMENT_SESSION_OBJECT && typeof data.id === "string"
|
|
92
|
+
&& typeof data.client_token === "string" && data.client_token.length > 0 && data.status === "open";
|
|
93
|
+
}
|
|
94
|
+
/** Creates a payment session and renders the card fields for the store's checkout page. */
|
|
95
|
+
export async function createCardFields(ctx, input, scriptUrl) {
|
|
96
|
+
if (ctx.gateway.waitingForSandboxKey)
|
|
97
|
+
throw new StoreRefusal("sandbox_key_pending", "Checkout is waiting for sandbox key.", 503);
|
|
98
|
+
let session;
|
|
99
|
+
try {
|
|
100
|
+
({ data: session } = await ctx.gateway.call((client) => client.POST(PAYMENT_SESSIONS_PATH, {
|
|
101
|
+
// A fresh session per page render: sessions are single use.
|
|
102
|
+
params: { header: { "Idempotency-Key": randomId("sk-ps-") } },
|
|
103
|
+
body: {
|
|
104
|
+
amount: input.amount, currency: upperCurrency(input.currency), order_reference: input.orderReference,
|
|
105
|
+
...(input.metadata ? { metadata: input.metadata } : {}),
|
|
106
|
+
},
|
|
107
|
+
}), isPaymentSession));
|
|
108
|
+
}
|
|
109
|
+
catch (error) {
|
|
110
|
+
if (error instanceof RelyonUnavailableError)
|
|
111
|
+
throw new PaymentUnavailableError();
|
|
112
|
+
throw error;
|
|
113
|
+
}
|
|
114
|
+
const src = scriptUrl ?? cardFieldsScriptUrl(ctx.gateway.baseUrl);
|
|
115
|
+
const scriptTag = `<script src="${escapeHtml(src)}" async></script>`;
|
|
116
|
+
const attributes = [
|
|
117
|
+
`id="${escapeHtml(input.elementId ?? "relyon-card-fields")}"`,
|
|
118
|
+
`client-token="${escapeHtml(session.client_token)}"`,
|
|
119
|
+
...(input.fieldStyles ? [`field-styles="${escapeHtml(input.fieldStyles)}"`] : []),
|
|
120
|
+
];
|
|
121
|
+
const element = `<relyon-card-fields ${attributes.join(" ")}></relyon-card-fields>`;
|
|
122
|
+
return { session, scriptTag, element, html: `${scriptTag}\n${element}` };
|
|
123
|
+
}
|
|
124
|
+
/** Today's server cart for POST /v1/payments (see `paymentCartFormat`). */
|
|
125
|
+
export function legacyPaymentCart(cart) {
|
|
126
|
+
return {
|
|
127
|
+
lines: cart.lines.map((line) => ({
|
|
128
|
+
product_id: line.merchant_product_id, title: line.title, quantity: line.quantity,
|
|
129
|
+
...(line.unit_amount !== undefined ? { unit_amount: line.unit_amount } : {}),
|
|
130
|
+
...(line.discount !== undefined ? { discount: line.discount } : {}),
|
|
131
|
+
amount: line.amount,
|
|
132
|
+
...(line.options !== undefined ? { options: line.options } : {}),
|
|
133
|
+
})),
|
|
134
|
+
discounts: cart.discounts ?? [],
|
|
135
|
+
totals: cart.totals,
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
function isPayment(data) {
|
|
139
|
+
return isRecord(data) && typeof data.id === "string" && (data.status === "succeeded" || data.status === "failed");
|
|
140
|
+
}
|
|
141
|
+
function isPaymentAnswer(data) {
|
|
142
|
+
return isPayment(data) || (isRecord(data) && data.object === PAYMENT_OBJECT && data.status === "pending"
|
|
143
|
+
&& (data.id === undefined || typeof data.id === "string"));
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Charges the cart with `createPayment`: the final amount is the cart's own
|
|
147
|
+
* total, the channel and agent checkout attribute the payment, and the
|
|
148
|
+
* Idempotency-Key comes from the order reference. On success the store's
|
|
149
|
+
* order is created once. An agent refused, or an identified agent whose
|
|
150
|
+
* rules can't be read, throws before any charge is attempted
|
|
151
|
+
* (`AgentRefusedError` / `PaymentUnavailableError`). Pending answers,
|
|
152
|
+
* network failures and unusable replies from the charge itself are UNKNOWN
|
|
153
|
+
* outcomes, not evidence of no charge: `PaymentOutcomeUnknownError` keeps
|
|
154
|
+
* the original reconciliation keys.
|
|
155
|
+
*/
|
|
156
|
+
/** The Idempotency-Key for an order reference and attempt (attempt 0 keeps the original key). */
|
|
157
|
+
export function paymentIdempotencyKey(orderReference, attempt = 0) {
|
|
158
|
+
if (!Number.isSafeInteger(attempt) || attempt < 0)
|
|
159
|
+
throw new TypeError("createPayment's attempt must be a non-negative integer.");
|
|
160
|
+
return `sk-pay-${sha256(attempt === 0 ? orderReference : `${orderReference}#attempt-${attempt}`)}`;
|
|
161
|
+
}
|
|
162
|
+
export async function createPayment(ctx, input, options = {}) {
|
|
163
|
+
if (ctx.gateway.waitingForSandboxKey)
|
|
164
|
+
throw new StoreRefusal("sandbox_key_pending", "Checkout is waiting for sandbox key.", 503);
|
|
165
|
+
if (!input.orderReference)
|
|
166
|
+
throw new TypeError("createPayment needs the store's orderReference.");
|
|
167
|
+
const orderReference = input.orderReference;
|
|
168
|
+
const idempotencyKey = paymentIdempotencyKey(orderReference, input.attempt ?? 0);
|
|
169
|
+
const record = input.agentCheckoutId ? await ctx.state.byAgentCheckoutId(input.agentCheckoutId) : null;
|
|
170
|
+
const channel = input.channel ?? (input.agentCheckoutId ? (record ? paymentChannelFor(record.protocol) : "agent") : "web");
|
|
171
|
+
let identity = options.decidedIdentity ?? null;
|
|
172
|
+
if (input.request && !options.decidedIdentity) {
|
|
173
|
+
identity = await ctx.identify(input.request);
|
|
174
|
+
// A paused channel is checked by the hand-off's protocol (UCP pauses with
|
|
175
|
+
// "google"), even though a UCP payment claims "web".
|
|
176
|
+
const pauses = !input.agentCheckoutId ? null : record ? CHANNEL_FOR[record.protocol] : channel !== "web" ? channel : null;
|
|
177
|
+
const rules = identity.identified || pauses ? await ctx.rules.get() : null;
|
|
178
|
+
const decision = decide(identity, rules, { channel: pauses, failClosed: ctx.failClosed });
|
|
179
|
+
if (decision.outcome !== "allow") {
|
|
180
|
+
const facts = requestFacts(input.request, ctx.publicUrl(input.request), input.info);
|
|
181
|
+
if (facts)
|
|
182
|
+
ctx.events.record({ type: "crawler_visit", request: facts, status: decision.status });
|
|
183
|
+
if (decision.outcome === "unavailable")
|
|
184
|
+
throw new PaymentUnavailableError(decision.message);
|
|
185
|
+
throw new AgentRefusedError(decision.code, decision.message, decision.rule);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
if (options.beforeCharge)
|
|
189
|
+
await options.beforeCharge();
|
|
190
|
+
let payment;
|
|
191
|
+
try {
|
|
192
|
+
({ data: payment } = await ctx.gateway.call((client) => client.POST(PAYMENTS_PATH, {
|
|
193
|
+
params: { header: { "Idempotency-Key": idempotencyKey } },
|
|
194
|
+
// The shopper's identifying headers, so Relyon names a browser agent
|
|
195
|
+
//. Never cookies or authorization.
|
|
196
|
+
...(input.request ? { headers: forwardedShopperHeaders(input.request.headers) } : {}),
|
|
197
|
+
body: {
|
|
198
|
+
amount: input.cart.totals.total,
|
|
199
|
+
currency: upperCurrency(input.cart.currency ?? input.cart.totals.currency),
|
|
200
|
+
cart: withUpperCurrency(ctx.paymentCartFormat === "server-legacy" ? legacyPaymentCart(input.cart) : input.cart),
|
|
201
|
+
// Passed through as the card fields produced it.
|
|
202
|
+
payment_method: input.paymentMethod,
|
|
203
|
+
channel,
|
|
204
|
+
...(input.agentCheckoutId ? { agent_checkout_id: input.agentCheckoutId } : {}),
|
|
205
|
+
order_reference: orderReference,
|
|
206
|
+
...(input.metadata ? { metadata: input.metadata } : {}),
|
|
207
|
+
},
|
|
208
|
+
}), isPaymentAnswer));
|
|
209
|
+
}
|
|
210
|
+
catch (error) {
|
|
211
|
+
if (error instanceof RelyonUnavailableError) {
|
|
212
|
+
throw new PaymentOutcomeUnknownError(orderReference, idempotencyKey, error.kind);
|
|
213
|
+
}
|
|
214
|
+
if (error instanceof RelyonProblemError && error.code === "agent_refused")
|
|
215
|
+
throw new RelyonRuleRefusalError(error);
|
|
216
|
+
throw error;
|
|
217
|
+
}
|
|
218
|
+
if (!isPayment(payment)) {
|
|
219
|
+
throw new PaymentOutcomeUnknownError(orderReference, idempotencyKey, "pending", payment);
|
|
220
|
+
}
|
|
221
|
+
const agent = identity ? { agent: identity } : {};
|
|
222
|
+
if (payment.status !== "succeeded" || options.createOrder === false)
|
|
223
|
+
return { payment, orderCreated: false, ...agent };
|
|
224
|
+
if (options.beforeOrder)
|
|
225
|
+
await options.beforeOrder(payment);
|
|
226
|
+
let outcome;
|
|
227
|
+
try {
|
|
228
|
+
outcome = await ctx.createOrderOnce(input.cart, payment);
|
|
229
|
+
}
|
|
230
|
+
catch (error) {
|
|
231
|
+
// The checkout state store failed around the claim, after the charge:
|
|
232
|
+
// the order isn't known to exist, and the payment must not read as failed.
|
|
233
|
+
ctx.logger.error("creating the store's order failed after a succeeded payment", { payment: payment.id, error: describeError(error, true) });
|
|
234
|
+
outcome = { created: false, status: "failed", error };
|
|
235
|
+
}
|
|
236
|
+
if (outcome.status === "already_created" && outcome.order === undefined && ctx.hooks.findOrderByPaymentId) {
|
|
237
|
+
// A replay of a payment whose order exists: hand back that order, read-only.
|
|
238
|
+
try {
|
|
239
|
+
const existing = await ctx.hooks.findOrderByPaymentId(payment.id);
|
|
240
|
+
if (existing !== undefined && existing !== null)
|
|
241
|
+
outcome = { ...outcome, order: existing };
|
|
242
|
+
}
|
|
243
|
+
catch (error) {
|
|
244
|
+
ctx.logger.error("findOrderByPaymentId failed for an already created order", { payment: payment.id, error: describeError(error, true) });
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
return {
|
|
248
|
+
payment, orderCreated: outcome.created, orderStatus: outcome.status, ...agent,
|
|
249
|
+
...(outcome.order !== undefined ? { order: outcome.order } : {}), ...(outcome.status === "failed" ? { orderError: outcome.error } : {}),
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* The checkout page marker doctor looks for: put this attribute, set to the
|
|
254
|
+
* number of items in the cart (0 for an empty cart), on an element of the
|
|
255
|
+
* store's checkout page, for example
|
|
256
|
+
* `<main data-relyon-cart-items="2">`. The kit's `<relyon-card-fields>`
|
|
257
|
+
* mount also counts as a checkout page.
|
|
258
|
+
*/
|
|
259
|
+
export const CHECKOUT_CART_ITEMS_ATTRIBUTE = "data-relyon-cart-items";
|
|
260
|
+
/** The number of items in a landed (or any) cart's inputs: the sum of the line quantities. */
|
|
261
|
+
export function cartItemCount(inputs) {
|
|
262
|
+
return (inputs?.lines ?? []).reduce((sum, line) => sum + (Number.isSafeInteger(line.quantity) && line.quantity > 0 ? line.quantity : 0), 0);
|
|
263
|
+
}
|
|
264
|
+
/** Appended to a landing refusal's message when the cart was already paid, in place of any "nothing was charged". */
|
|
265
|
+
export const LANDING_PAID_NOTE = "This order may already have been paid; check the order status before paying again.";
|
|
266
|
+
const UNATTRIBUTED = { attributed: false, agentCheckoutId: null, protocol: null, channel: "web", cart: null, inputs: null };
|
|
267
|
+
/**
|
|
268
|
+
* Handles a shopper landing on the store's checkout from an agent hand-off.
|
|
269
|
+
* A valid token (a Relyon-issued link the kit saw relayed for that checkout,
|
|
270
|
+
* landed for the first time, or one the store's resolver vouches for)
|
|
271
|
+
* re-prices the cart through `priceCart` and attributes the checkout;
|
|
272
|
+
* anything else (tampered, expired, unknown, replayed) is a normal checkout.
|
|
273
|
+
* Never throws because of the token.
|
|
274
|
+
*/
|
|
275
|
+
export async function landing(ctx, tokens, request, info = {}) {
|
|
276
|
+
const token = new URL(request.url).searchParams.get("relyon_cart");
|
|
277
|
+
if (!token)
|
|
278
|
+
return UNATTRIBUTED;
|
|
279
|
+
let resolved = null;
|
|
280
|
+
try {
|
|
281
|
+
// A Relyon-issued hand-off link first: accepted only when it is exactly
|
|
282
|
+
// the token the kit saw Relyon issue for that checkout, once. One the kit
|
|
283
|
+
// refuses (used, wrong host, expired) is final; anything else that isn't
|
|
284
|
+
// Relyon's falls through to the store's resolver.
|
|
285
|
+
const relyon = await resolveRelyonCartToken(ctx, token, request);
|
|
286
|
+
if (relyon.kind === "refused")
|
|
287
|
+
return UNATTRIBUTED;
|
|
288
|
+
resolved = relyon.kind === "accepted" ? relyon.cart : await tokens.resolve(token);
|
|
289
|
+
}
|
|
290
|
+
catch (error) {
|
|
291
|
+
ctx.logger.warn("a cart token could not be resolved", { error: describeError(error) });
|
|
292
|
+
return UNATTRIBUTED;
|
|
293
|
+
}
|
|
294
|
+
if (!resolved)
|
|
295
|
+
return UNATTRIBUTED;
|
|
296
|
+
// The event names the agent checkout; the token itself never leaves the
|
|
297
|
+
// store (requestFacts drops it from the URL too).
|
|
298
|
+
const facts = requestFacts(request, ctx.publicUrl(request), info);
|
|
299
|
+
if (facts) {
|
|
300
|
+
ctx.events.record({
|
|
301
|
+
type: "handoff_landing", request: facts, status: 200,
|
|
302
|
+
...(/^ac_/.test(resolved.agentCheckoutId) ? { agent_checkout_id: resolved.agentCheckoutId } : {}),
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
const paid = await handoffPaid(ctx, resolved.agentCheckoutId);
|
|
306
|
+
const base = {
|
|
307
|
+
attributed: true, agentCheckoutId: resolved.agentCheckoutId, protocol: resolved.protocol,
|
|
308
|
+
channel: paymentChannelFor(resolved.protocol), cart: null, inputs: resolved.inputs, ...(paid ? { alreadyPaid: paid } : {}),
|
|
309
|
+
};
|
|
310
|
+
try {
|
|
311
|
+
const { lines, address, coupons, shipping_option_id } = resolved.inputs;
|
|
312
|
+
const cart = await ctx.hooks.priceCart(lines, address, coupons, {
|
|
313
|
+
protocol: "web", operation: "landing", shippingOptionId: shipping_option_id, agentCheckoutId: resolved.agentCheckoutId,
|
|
314
|
+
});
|
|
315
|
+
return { ...base, cart };
|
|
316
|
+
}
|
|
317
|
+
catch (error) {
|
|
318
|
+
if (error instanceof StoreRefusal) {
|
|
319
|
+
if (!paid)
|
|
320
|
+
return { ...base, refusal: error };
|
|
321
|
+
// Already paid: the store's message never says nothing was charged.
|
|
322
|
+
const kept = claimsNothingCharged(error.message) ? withoutNothingChargedClaims(error.message) : error.message;
|
|
323
|
+
const message = kept ? `${kept} ${LANDING_PAID_NOTE}` : LANDING_PAID_NOTE;
|
|
324
|
+
return { ...base, refusal: new StoreRefusal(error.code, message, error.status) };
|
|
325
|
+
}
|
|
326
|
+
ctx.logger.error("priceCart failed for a landed cart", { error: describeError(error, true) });
|
|
327
|
+
return base;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* Whether a store-page payment paid this hand-off cart's agent checkout, or
|
|
332
|
+
* may have: its order-pending marker, or the store-payment memory for it.
|
|
333
|
+
* Null when neither says so (or the state store can't be read). Never throws.
|
|
334
|
+
*/
|
|
335
|
+
async function handoffPaid(ctx, agentCheckoutId) {
|
|
336
|
+
let marker = null;
|
|
337
|
+
try {
|
|
338
|
+
const record = await ctx.state.byAgentCheckoutId(agentCheckoutId);
|
|
339
|
+
marker = record ? await orderPendingFor(ctx, record.protocol, record.sessionId) : null;
|
|
340
|
+
}
|
|
341
|
+
catch (error) {
|
|
342
|
+
ctx.logger.error("The checkout state store failed to load a landed hand-off checkout", { error: describeError(error) });
|
|
343
|
+
}
|
|
344
|
+
if (marker)
|
|
345
|
+
return { orderPending: true, paymentId: marker.paymentId };
|
|
346
|
+
const standing = await storePaymentStanding(ctx, handoffPaymentKey(agentCheckoutId));
|
|
347
|
+
if (standing.kind === "order_pending")
|
|
348
|
+
return { orderPending: true, paymentId: standing.paymentId };
|
|
349
|
+
const record = await storePaymentRecord(ctx, handoffPaymentKey(agentCheckoutId));
|
|
350
|
+
if (!record || record.state === "declined")
|
|
351
|
+
return null;
|
|
352
|
+
return { orderPending: false, paymentId: record.paymentId };
|
|
353
|
+
}
|
package/dist/cli.d.ts
ADDED