@visa/cli 4.1.0-rc.15 → 4.1.0-rc.151
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +188 -232
- package/dist/checkout-engine/adapters/generic.d.ts +4 -0
- package/dist/checkout-engine/adapters/generic.js +28 -13
- package/dist/checkout-engine/adapters/index.d.ts +4 -1
- package/dist/checkout-engine/adapters/index.js +10 -3
- package/dist/checkout-engine/adapters/shopify.d.ts +31 -0
- package/dist/checkout-engine/adapters/shopify.js +423 -0
- package/dist/checkout-engine/amount.d.ts +15 -0
- package/dist/checkout-engine/amount.js +72 -0
- package/dist/checkout-engine/cli-engine.d.ts +216 -2
- package/dist/checkout-engine/cli-engine.js +682 -27
- package/dist/checkout-engine/detect.d.ts +1 -1
- package/dist/checkout-engine/detect.js +26 -0
- package/dist/checkout-engine/evidence.d.ts +4 -1
- package/dist/checkout-engine/evidence.js +51 -6
- package/dist/checkout-engine/executor.d.ts +34 -4
- package/dist/checkout-engine/executor.js +266 -115
- package/dist/checkout-engine/hosted-approval.d.ts +134 -8
- package/dist/checkout-engine/hosted-approval.js +386 -49
- package/dist/checkout-engine/index.d.ts +4 -1
- package/dist/checkout-engine/index.js +3 -0
- package/dist/checkout-engine/instrument.d.ts +7 -0
- package/dist/checkout-engine/instrument.js +4 -0
- package/dist/checkout-engine/live-fill-approval.d.ts +0 -20
- package/dist/checkout-engine/live-fill-approval.js +15 -51
- package/dist/checkout-engine/mandate/card-mandate.d.ts +121 -0
- package/dist/checkout-engine/mandate/card-mandate.js +227 -0
- package/dist/checkout-engine/mandate/mandate-ledger.d.ts +165 -0
- package/dist/checkout-engine/mandate/mandate-ledger.js +373 -0
- package/dist/checkout-engine/outcome.d.ts +2 -2
- package/dist/checkout-engine/outcome.js +36 -1
- package/dist/checkout-engine/owner-only-file.d.ts +9 -0
- package/dist/checkout-engine/owner-only-file.js +20 -1
- package/dist/checkout-engine/trace-handles.d.ts +8 -0
- package/dist/checkout-engine/trace-handles.js +12 -0
- package/dist/checkout-engine/types.d.ts +20 -2
- package/dist/checkout-engine/vgs-gateway/server-mint-client.d.ts +101 -0
- package/dist/checkout-engine/vgs-gateway/server-mint-client.js +218 -0
- package/dist/checkout-engine/vgs-live-instrument.d.ts +38 -35
- package/dist/checkout-engine/vgs-live-instrument.js +51 -74
- package/dist/checkout-engine/vic-confirmation.js +2 -2
- package/dist/cli.js +658 -492
- package/dist/mcp-apps/ucp-checkout.html +280 -0
- package/dist/mcp-server/index.js +512 -174
- package/dist/skills/pair-visa-agent/RUNTIMES.md +92 -0
- package/dist/skills/pair-visa-agent/SKILL.md +467 -0
- package/dist/skills/pair-visa-agent/scripts/setup.mjs +48 -0
- package/dist/subway-direct.mjs +1 -0
- package/install.ps1 +3 -41
- package/install.sh +4 -36
- package/native/bin/win32-x64/visa-keychain-win.exe +0 -0
- package/package.json +17 -12
- package/server.json +3 -3
- package/dist/checkout-engine/inline-target.d.ts +0 -13
- package/dist/checkout-engine/inline-target.js +0 -37
- package/dist/checkout-engine/pay-args.d.ts +0 -14
- package/dist/checkout-engine/pay-args.js +0 -44
- package/dist/checkout-engine/pay.d.ts +0 -1
- package/dist/checkout-engine/pay.js +0 -13
- package/dist/checkout-engine/repo-env.d.ts +0 -11
- package/dist/checkout-engine/repo-env.js +0 -23
- package/dist/checkout-engine/run-live-fill.d.ts +0 -1
- package/dist/checkout-engine/run-live-fill.js +0 -443
- package/dist/checkout-engine/vgs-gateway/fetch-credential.d.mts +0 -74
- package/dist/checkout-engine/vgs-gateway/fetch-credential.mjs +0 -240
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
// Server-side mint client (Phase 1) — the checkout runner mints the payment
|
|
2
|
+
// credential by calling the verify-web deployment's gateway routes with a
|
|
3
|
+
// short-lived, purpose-scoped MINT TOKEN, instead of holding the shared VGS
|
|
4
|
+
// service-account secret (the gateway client id/secret) on the machine.
|
|
5
|
+
//
|
|
6
|
+
// The mint token is issued at approval time (agent-approval/complete, which is
|
|
7
|
+
// session + internal-tester + token-ownership gated) and released to this
|
|
8
|
+
// runner by the claim leg. It is bound to the exact approved purchase, so these
|
|
9
|
+
// calls can only mint the credential the human approved.
|
|
10
|
+
//
|
|
11
|
+
// Request derivation here mirrors ../fetch-credential.mjs EXACTLY (mandate cap,
|
|
12
|
+
// consumer prompt, PENDING retry cadence) — the only change is the transport:
|
|
13
|
+
// verify-web routes + Bearer mint token, never the VGS gateway + VGS secret.
|
|
14
|
+
import { traceHandleFields } from '../trace-handles.js';
|
|
15
|
+
const defaultSleep = (ms) => new Promise((r) => {
|
|
16
|
+
const t = setTimeout(r, ms);
|
|
17
|
+
t.unref?.();
|
|
18
|
+
});
|
|
19
|
+
function stripTrailingSlashes(value) {
|
|
20
|
+
let out = value;
|
|
21
|
+
while (out.endsWith('/'))
|
|
22
|
+
out = out.slice(0, -1);
|
|
23
|
+
return out;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* The merchant ORIGIN (scheme + host) — not the full checkout URL. A merchant's
|
|
27
|
+
* identity is its origin; a real checkout URL can carry hundreds of chars of
|
|
28
|
+
* campaign/tracking query params. Confirmed live: an oversized `merchantUrl`
|
|
29
|
+
* (a ~280-char Wikimedia donation URL) makes the cryptogram mint fail downstream
|
|
30
|
+
* with a vague `502 "card network could not complete"`, while the same merchant
|
|
31
|
+
* with a trimmed URL mints fine. Sending the origin is both correct (that IS the
|
|
32
|
+
* merchant) and safely bounded. Falls back to the raw value if it does not parse
|
|
33
|
+
* — the mint must never throw here.
|
|
34
|
+
*/
|
|
35
|
+
export function merchantOrigin(url) {
|
|
36
|
+
try {
|
|
37
|
+
return new URL(url).origin;
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
return url;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* A definitive refusal from the cryptogram mint route — the request will not
|
|
45
|
+
* succeed if retried unchanged.
|
|
46
|
+
*
|
|
47
|
+
* `terminal` is the load-bearing field. Draw terminality used to be decided by
|
|
48
|
+
* regex over the error MESSAGE, which made it hostage to text nobody controls:
|
|
49
|
+
* this client now interpolates the provider's own `upstream_codes` into the
|
|
50
|
+
* message for diagnosability, and a provider identifier such as
|
|
51
|
+
* `request_timeout` would match the transient matcher's unanchored
|
|
52
|
+
* `tim(e|ed)-out` alternative — flipping a permanent 422 back to "transient",
|
|
53
|
+
* skipping markUnhonored, and stranding the mandate in the exact retry-forever
|
|
54
|
+
* loop this whole change exists to kill. Terminality is therefore carried
|
|
55
|
+
* structurally, and consumers must consult it BEFORE any message matching.
|
|
56
|
+
*/
|
|
57
|
+
export class ServerCryptogramRefusedError extends Error {
|
|
58
|
+
status;
|
|
59
|
+
terminal = true;
|
|
60
|
+
constructor(status, detail) {
|
|
61
|
+
super(`server cryptogram refused (${status}): ${detail}`);
|
|
62
|
+
this.name = 'ServerCryptogramRefusedError';
|
|
63
|
+
this.status = status;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
/** Read a stable, non-secret error message from a route's JSON body. */
|
|
67
|
+
async function routeError(res) {
|
|
68
|
+
const doc = (await res.json().catch(() => null));
|
|
69
|
+
const base = doc?.error || doc?.error_code || `HTTP ${res.status}`;
|
|
70
|
+
// Carry the provider's own status and rejected-attribute identifiers into the
|
|
71
|
+
// message when the route reflected them. Without this the operator sees only
|
|
72
|
+
// our status and has to go log-diving to learn WHICH field the gateway
|
|
73
|
+
// objected to — the gap that left a live create_intent 422 undiagnosable.
|
|
74
|
+
const parts = [];
|
|
75
|
+
if (typeof doc?.upstream_status === 'number')
|
|
76
|
+
parts.push(`upstream_status=${doc.upstream_status}`);
|
|
77
|
+
if (Array.isArray(doc?.upstream_codes) && doc.upstream_codes.length > 0) {
|
|
78
|
+
parts.push(`upstream_codes=${doc.upstream_codes.join(',')}`);
|
|
79
|
+
}
|
|
80
|
+
return parts.length > 0 ? `${base} [${parts.join(' ')}]` : base;
|
|
81
|
+
}
|
|
82
|
+
function bearer(mintToken) {
|
|
83
|
+
return { 'content-type': 'application/json', authorization: `Bearer ${mintToken}` };
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Create a fresh intent via POST {base}/api/vgs/intent. The server holds the VGS
|
|
87
|
+
* credential and calls the gateway; we send the same mandate shape the local
|
|
88
|
+
* path built (cap = ceil(amount)+10, min 25; merchant category Retail/5999) —
|
|
89
|
+
* unless `input.mandate` overrides the threshold/quantity/window for the
|
|
90
|
+
* card-mandate (budget) layer.
|
|
91
|
+
*/
|
|
92
|
+
export async function serverCreateIntent(base, mintToken, input, deps = {}) {
|
|
93
|
+
const fetchImpl = deps.fetchImpl ?? fetch;
|
|
94
|
+
const { tokenId, assuranceData, transaction: t } = input;
|
|
95
|
+
const override = input.mandate ?? {};
|
|
96
|
+
const defaultCap = Math.max(Math.ceil(Number(t.transactionAmount) || 0) + 10, 25);
|
|
97
|
+
const declineThresholdAmount = override.declineThresholdAmount ?? String(defaultCap);
|
|
98
|
+
const quantity = override.quantity ?? 1;
|
|
99
|
+
const effectiveUntil = override.effectiveUntil ?? new Date(Date.now() + 30 * 24 * 60 * 60 * 1000).toISOString();
|
|
100
|
+
const consumerPrompt = override.consumerPrompt ??
|
|
101
|
+
`Buy an item from ${t.merchantName} for ${t.transactionCurrencyCode.toUpperCase()} ${t.transactionAmount}`;
|
|
102
|
+
const res = await fetchImpl(`${stripTrailingSlashes(base)}/api/vgs/intent`, {
|
|
103
|
+
method: 'POST',
|
|
104
|
+
headers: bearer(mintToken),
|
|
105
|
+
body: JSON.stringify({
|
|
106
|
+
tokenId,
|
|
107
|
+
consumerPrompt,
|
|
108
|
+
assuranceData,
|
|
109
|
+
mandates: [
|
|
110
|
+
{
|
|
111
|
+
description: `Purchase at ${t.merchantName}`,
|
|
112
|
+
declineThresholdAmount,
|
|
113
|
+
declineThresholdCurrencyCode: t.transactionCurrencyCode.toUpperCase(),
|
|
114
|
+
effectiveUntil,
|
|
115
|
+
merchantCategory: 'Retail',
|
|
116
|
+
merchantCategoryCode: '5999',
|
|
117
|
+
preferredMerchantName: t.merchantName,
|
|
118
|
+
quantity,
|
|
119
|
+
},
|
|
120
|
+
],
|
|
121
|
+
}),
|
|
122
|
+
});
|
|
123
|
+
if (!res.ok)
|
|
124
|
+
throw new Error(`server intent failed (${res.status}): ${await routeError(res)}`);
|
|
125
|
+
const doc = (await res.json().catch(() => null));
|
|
126
|
+
if (!doc?.intentId)
|
|
127
|
+
throw new Error('server intent response missing intentId');
|
|
128
|
+
return { intentId: doc.intentId, status: typeof doc.status === 'string' ? doc.status : null };
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Mint the FULL payment credential via POST {base}/api/vgs/payment-cryptogram.
|
|
132
|
+
* The server's route does a SINGLE gateway call and 502s on a not-COMPLETED
|
|
133
|
+
* (e.g. PENDING) cryptogram, but live intent approval is asynchronous and the
|
|
134
|
+
* first cryptogram answer can be PENDING (#5709). So we retry the route on any
|
|
135
|
+
* non-2xx up to PENDING_ATTEMPTS, matching fetch-credential.mjs's cadence
|
|
136
|
+
* exactly — a genuinely hard failure just surfaces after the same bounded wait.
|
|
137
|
+
* Re-POSTing for the same intentId is idempotent (mirrors the old client loop).
|
|
138
|
+
*/
|
|
139
|
+
export async function serverFetchCryptogram(base, mintToken, input, deps = {}) {
|
|
140
|
+
const fetchImpl = deps.fetchImpl ?? fetch;
|
|
141
|
+
const sleep = deps.sleep ?? defaultSleep;
|
|
142
|
+
const env = deps.env ?? process.env;
|
|
143
|
+
const attempts = Math.max(1, Number(env.VGS_CRYPTOGRAM_PENDING_ATTEMPTS) || 4);
|
|
144
|
+
const waitMs = Number.isFinite(Number(env.VGS_CRYPTOGRAM_PENDING_WAIT_MS))
|
|
145
|
+
? Number(env.VGS_CRYPTOGRAM_PENDING_WAIT_MS)
|
|
146
|
+
: 5000;
|
|
147
|
+
const { tokenId, intentId, transaction: t } = input;
|
|
148
|
+
const body = JSON.stringify({
|
|
149
|
+
tokenId,
|
|
150
|
+
intentId,
|
|
151
|
+
transaction: {
|
|
152
|
+
merchantName: t.merchantName,
|
|
153
|
+
// Send the merchant ORIGIN, not the full (possibly huge) checkout URL — an
|
|
154
|
+
// oversized merchantUrl makes the downstream cryptogram mint 502 (proven live).
|
|
155
|
+
merchantUrl: merchantOrigin(t.merchantUrl),
|
|
156
|
+
merchantCountryCode: t.merchantCountryCode,
|
|
157
|
+
transactionAmount: t.transactionAmount,
|
|
158
|
+
transactionCurrencyCode: t.transactionCurrencyCode.toUpperCase(),
|
|
159
|
+
},
|
|
160
|
+
});
|
|
161
|
+
let lastError = 'unknown';
|
|
162
|
+
for (let attempt = 1; attempt <= attempts; attempt++) {
|
|
163
|
+
const res = await fetchImpl(`${stripTrailingSlashes(base)}/api/vgs/payment-cryptogram`, {
|
|
164
|
+
method: 'POST',
|
|
165
|
+
headers: bearer(mintToken),
|
|
166
|
+
body,
|
|
167
|
+
});
|
|
168
|
+
if (res.ok) {
|
|
169
|
+
const c = (await res.json().catch(() => null));
|
|
170
|
+
if (!c || typeof c.networkToken !== 'string' || typeof c.cryptogramValue !== 'string') {
|
|
171
|
+
throw new Error('server cryptogram response missing credential fields');
|
|
172
|
+
}
|
|
173
|
+
return {
|
|
174
|
+
networkToken: c.networkToken,
|
|
175
|
+
expMonth: Number(c.expMonth) || 0,
|
|
176
|
+
expYear: Number(c.expYear) || 0,
|
|
177
|
+
cryptogramType: c.cryptogramType ?? '',
|
|
178
|
+
cryptogramValue: c.cryptogramValue,
|
|
179
|
+
...(typeof c.cryptogramExpiresAt === 'string'
|
|
180
|
+
? { cryptogramExpiresAt: c.cryptogramExpiresAt }
|
|
181
|
+
: {}),
|
|
182
|
+
...traceHandleFields(c),
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
// A 4xx (bad request / binding refusal / auth) is terminal — never retry it.
|
|
186
|
+
// 429 is the sole exception: it is an explicitly retryable 4xx, and treating
|
|
187
|
+
// it as terminal would let a transient rate limit permanently disable a
|
|
188
|
+
// healthy mandate. The route maps upstream rate limits to 503 today, so this
|
|
189
|
+
// is a guard against a future emitter, not a live path.
|
|
190
|
+
if (res.status >= 400 && res.status < 500 && res.status !== 429) {
|
|
191
|
+
throw new ServerCryptogramRefusedError(res.status, await routeError(res));
|
|
192
|
+
}
|
|
193
|
+
lastError = `${res.status}: ${await routeError(res)}`;
|
|
194
|
+
if (attempt < attempts)
|
|
195
|
+
await sleep(waitMs);
|
|
196
|
+
}
|
|
197
|
+
throw new Error(`server cryptogram not completed after ${attempts} attempts (last: ${lastError})`);
|
|
198
|
+
}
|
|
199
|
+
/** Report the observed merchant outcome via POST {base}/api/vgs/confirmation. */
|
|
200
|
+
export async function serverPostConfirmation(base, mintToken, input, deps = {}) {
|
|
201
|
+
const fetchImpl = deps.fetchImpl ?? fetch;
|
|
202
|
+
const res = await fetchImpl(`${stripTrailingSlashes(base)}/api/vgs/confirmation`, {
|
|
203
|
+
method: 'POST',
|
|
204
|
+
headers: bearer(mintToken),
|
|
205
|
+
body: JSON.stringify({
|
|
206
|
+
tokenId: input.tokenId,
|
|
207
|
+
intentId: input.intentId,
|
|
208
|
+
transactionStatus: input.transactionStatus,
|
|
209
|
+
transactionType: input.transactionType,
|
|
210
|
+
transactionTimestamp: input.transactionTimestamp,
|
|
211
|
+
transactionAmount: input.transaction.transactionAmount,
|
|
212
|
+
transactionCurrencyCode: input.transaction.transactionCurrencyCode.toUpperCase(),
|
|
213
|
+
}),
|
|
214
|
+
});
|
|
215
|
+
if (!res.ok)
|
|
216
|
+
throw new Error(`server confirmation failed (${res.status}): ${await routeError(res)}`);
|
|
217
|
+
return { ok: true };
|
|
218
|
+
}
|
|
@@ -42,6 +42,15 @@ export type PurchaseAssurance = {
|
|
|
42
42
|
transactionAmount: string;
|
|
43
43
|
/** ISO 4217 currency the ceremony was scoped to. */
|
|
44
44
|
transactionCurrencyCode: string;
|
|
45
|
+
/**
|
|
46
|
+
* True iff the approval server marked this token passkey-exempt ('otp'
|
|
47
|
+
* cardholder ID&V / 'none' — verified against the owner's account card
|
|
48
|
+
* record at complete-time): no ceremony ran, `assuranceData` is null by
|
|
49
|
+
* design, and the intent is minted without it (the signed mint token
|
|
50
|
+
* carries the same exemption for the server's own check). Absent/false →
|
|
51
|
+
* assuranceData is REQUIRED, exactly the historical contract.
|
|
52
|
+
*/
|
|
53
|
+
assuranceExempt?: boolean;
|
|
45
54
|
};
|
|
46
55
|
/**
|
|
47
56
|
* Our fail-closed freshness bound, matching the runner's 15-minute checkout
|
|
@@ -57,6 +66,8 @@ export type VgsPaymentCredential = {
|
|
|
57
66
|
cryptogramType: string;
|
|
58
67
|
cryptogramValue: string;
|
|
59
68
|
cryptogramExpiresAt?: string;
|
|
69
|
+
vgsTraceId?: string;
|
|
70
|
+
networkCorrelationId?: string;
|
|
60
71
|
};
|
|
61
72
|
export type FetchVgsPaymentCredential = (input: {
|
|
62
73
|
tokenId: string;
|
|
@@ -69,20 +80,39 @@ export type FetchVgsPaymentCredential = (input: {
|
|
|
69
80
|
transactionCurrencyCode: string;
|
|
70
81
|
};
|
|
71
82
|
}) => Promise<VgsPaymentCredential>;
|
|
72
|
-
export type MintFreshVgsPaymentCredential = (input: {
|
|
73
|
-
tokenId: string;
|
|
74
|
-
assuranceData: unknown;
|
|
75
|
-
transaction: VgsCheckoutTarget;
|
|
76
|
-
}) => Promise<{
|
|
77
|
-
payment: VgsPaymentCredential;
|
|
78
|
-
intentId: string;
|
|
79
|
-
}>;
|
|
80
83
|
/** The (token, intent) pair a VIC confirmation is posted against. */
|
|
81
84
|
export type VicConfirmationTarget = {
|
|
82
85
|
tokenId: string;
|
|
83
86
|
intentId: string;
|
|
84
87
|
};
|
|
85
88
|
export declare function decimalToMinor(value: unknown): number | null;
|
|
89
|
+
/**
|
|
90
|
+
* Currencies whose minor unit is 1/100 of the major unit (2 decimal places).
|
|
91
|
+
* minorToDecimal's /100 + pad-to-2 rendering is ONLY correct for these. The
|
|
92
|
+
* card mandate path accepts any 3-letter ISO code, so a 0- or 3-decimal
|
|
93
|
+
* currency (JPY, BHD, …) would otherwise be emitted with a wrong wire amount.
|
|
94
|
+
* We only support USD/USDC today; extend deliberately (and fix the exponent)
|
|
95
|
+
* before adding any non-2-decimal currency.
|
|
96
|
+
*
|
|
97
|
+
* NOTE (scanner FP pre-empt): this is a CLIENT-SIDE currency-exponent lookup for
|
|
98
|
+
* rendering a wire amount in @visa/checkout-engine — it is NOT the cross-store
|
|
99
|
+
* `PAYMENT_CURRENCIES` enum governed by apps/auth/src/shared-validators.ts, and
|
|
100
|
+
* this package cannot import from apps/auth (no dependency edge). It intentionally
|
|
101
|
+
* does not mirror that enum's membership; do not "reconcile" the two.
|
|
102
|
+
*/
|
|
103
|
+
export declare const TWO_DECIMAL_CURRENCIES: Set<string>;
|
|
104
|
+
/**
|
|
105
|
+
* Inverse of decimalToMinor: render integer minor units as a major-unit decimal
|
|
106
|
+
* string ("D.DD") for the VGS wire (e.g. a ceiling of 50000 minor → "500.00").
|
|
107
|
+
* Integer-only; never floating-point money. Throws on a non-integer/negative so
|
|
108
|
+
* a bad accounting value cannot silently reach the network.
|
|
109
|
+
*
|
|
110
|
+
* The /100 + pad-to-2 rendering assumes a 2-decimal (exponent-2) currency. Pass
|
|
111
|
+
* `currencyCode` on any path that accepts arbitrary currencies (the mandate
|
|
112
|
+
* path) and it REFUSES a non-2-decimal currency rather than emit a wrong wire
|
|
113
|
+
* amount. Omit it only where the currency is already known to be USD/USDC.
|
|
114
|
+
*/
|
|
115
|
+
export declare function minorToDecimal(minor: number, currencyCode?: string): string;
|
|
86
116
|
/**
|
|
87
117
|
* Refuse to mint an intent unless the assurance is fresh and its declared
|
|
88
118
|
* scope matches the checkout target exactly. `now` is injectable so the
|
|
@@ -112,30 +142,3 @@ export declare class VgsLiveInstrument implements Instrument {
|
|
|
112
142
|
confirmationTarget(): VicConfirmationTarget | null;
|
|
113
143
|
getCredential(ctx: InstrumentContext): Promise<CardCredential>;
|
|
114
144
|
}
|
|
115
|
-
/**
|
|
116
|
-
* Consumes #5614's claimed CLI enrollment artifact (for the tokenId) plus a
|
|
117
|
-
* FRESH purchase-scoped assurance, and creates a fresh, transaction-scoped VIC
|
|
118
|
-
* intent + credential after checkout approval. This is the production-shaped
|
|
119
|
-
* bridge; unlike VgsLiveInstrument it never needs a pre-created intent ID.
|
|
120
|
-
*
|
|
121
|
-
* The enrollment artifact's stored assuranceData is deliberately never sent:
|
|
122
|
-
* replaying it makes intent creation succeed (HTTP 201) while the cryptogram
|
|
123
|
-
* deterministically never completes — the #5709 dead end. Purchase
|
|
124
|
-
* authorization is the fresh, merchant+amount+currency-scoped assurance,
|
|
125
|
-
* validated against the checkout target BEFORE any intent is minted so a
|
|
126
|
-
* doomed intent is never created.
|
|
127
|
-
*/
|
|
128
|
-
export declare class VgsAssuranceInstrument implements Instrument {
|
|
129
|
-
private readonly enrollment;
|
|
130
|
-
private readonly purchase;
|
|
131
|
-
private readonly target;
|
|
132
|
-
private readonly cardholderName;
|
|
133
|
-
private readonly mintCredential;
|
|
134
|
-
readonly kind: "agentic-token";
|
|
135
|
-
private used;
|
|
136
|
-
private minted;
|
|
137
|
-
constructor(enrollment: CliAgentCredential, purchase: PurchaseAssurance, target: VgsCheckoutTarget, cardholderName: string, mintCredential: MintFreshVgsPaymentCredential);
|
|
138
|
-
/** See VgsLiveInstrument.confirmationTarget — same contract. */
|
|
139
|
-
confirmationTarget(): VicConfirmationTarget | null;
|
|
140
|
-
getCredential(ctx: InstrumentContext): Promise<CardCredential>;
|
|
141
|
-
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { traceHandleFields } from './trace-handles.js';
|
|
1
2
|
/**
|
|
2
3
|
* Our fail-closed freshness bound, matching the runner's 15-minute checkout
|
|
3
4
|
* review window — NOT a claim about VGS's actual assurance TTL (unpublished).
|
|
@@ -19,6 +20,43 @@ export function decimalToMinor(value) {
|
|
|
19
20
|
const minor = Number(whole) * 100 + Number(fraction.padEnd(2, '0'));
|
|
20
21
|
return Number.isSafeInteger(minor) ? minor : null;
|
|
21
22
|
}
|
|
23
|
+
/**
|
|
24
|
+
* Currencies whose minor unit is 1/100 of the major unit (2 decimal places).
|
|
25
|
+
* minorToDecimal's /100 + pad-to-2 rendering is ONLY correct for these. The
|
|
26
|
+
* card mandate path accepts any 3-letter ISO code, so a 0- or 3-decimal
|
|
27
|
+
* currency (JPY, BHD, …) would otherwise be emitted with a wrong wire amount.
|
|
28
|
+
* We only support USD/USDC today; extend deliberately (and fix the exponent)
|
|
29
|
+
* before adding any non-2-decimal currency.
|
|
30
|
+
*
|
|
31
|
+
* NOTE (scanner FP pre-empt): this is a CLIENT-SIDE currency-exponent lookup for
|
|
32
|
+
* rendering a wire amount in @visa/checkout-engine — it is NOT the cross-store
|
|
33
|
+
* `PAYMENT_CURRENCIES` enum governed by apps/auth/src/shared-validators.ts, and
|
|
34
|
+
* this package cannot import from apps/auth (no dependency edge). It intentionally
|
|
35
|
+
* does not mirror that enum's membership; do not "reconcile" the two.
|
|
36
|
+
*/
|
|
37
|
+
export const TWO_DECIMAL_CURRENCIES = new Set(['USD', 'USDC']);
|
|
38
|
+
/**
|
|
39
|
+
* Inverse of decimalToMinor: render integer minor units as a major-unit decimal
|
|
40
|
+
* string ("D.DD") for the VGS wire (e.g. a ceiling of 50000 minor → "500.00").
|
|
41
|
+
* Integer-only; never floating-point money. Throws on a non-integer/negative so
|
|
42
|
+
* a bad accounting value cannot silently reach the network.
|
|
43
|
+
*
|
|
44
|
+
* The /100 + pad-to-2 rendering assumes a 2-decimal (exponent-2) currency. Pass
|
|
45
|
+
* `currencyCode` on any path that accepts arbitrary currencies (the mandate
|
|
46
|
+
* path) and it REFUSES a non-2-decimal currency rather than emit a wrong wire
|
|
47
|
+
* amount. Omit it only where the currency is already known to be USD/USDC.
|
|
48
|
+
*/
|
|
49
|
+
export function minorToDecimal(minor, currencyCode) {
|
|
50
|
+
if (!Number.isSafeInteger(minor) || minor < 0) {
|
|
51
|
+
throw new Error('minor units must be a non-negative safe integer');
|
|
52
|
+
}
|
|
53
|
+
if (currencyCode !== undefined && !TWO_DECIMAL_CURRENCIES.has(currencyCode.toUpperCase())) {
|
|
54
|
+
throw new Error(`minorToDecimal only supports 2-decimal currencies (${[...TWO_DECIMAL_CURRENCIES].join('/')}); refusing to emit a wire amount for ${currencyCode}`);
|
|
55
|
+
}
|
|
56
|
+
const whole = Math.floor(minor / 100);
|
|
57
|
+
const fraction = minor % 100;
|
|
58
|
+
return `${whole}.${String(fraction).padStart(2, '0')}`;
|
|
59
|
+
}
|
|
22
60
|
function validateTarget(reference, ctx) {
|
|
23
61
|
if (typeof reference.merchantName !== 'string' ||
|
|
24
62
|
typeof reference.merchantCountryCode !== 'string' ||
|
|
@@ -49,13 +87,6 @@ function validateTarget(reference, ctx) {
|
|
|
49
87
|
throw new Error(`VGS credential currency mismatch: ${currency} vs ${ctx.currency}`);
|
|
50
88
|
}
|
|
51
89
|
}
|
|
52
|
-
function validateCliCredential(value) {
|
|
53
|
-
if (typeof value.tokenId !== 'string' || !value.tokenId.trim()) {
|
|
54
|
-
throw new Error('CLI agent credential requires tokenId');
|
|
55
|
-
}
|
|
56
|
-
// Deliberately no assuranceData requirement: the artifact's enrollment-time
|
|
57
|
-
// assurance is identity/enrollment material and is never read here (#5709).
|
|
58
|
-
}
|
|
59
90
|
/**
|
|
60
91
|
* Refuse to mint an intent unless the assurance is fresh and its declared
|
|
61
92
|
* scope matches the checkout target exactly. `now` is injectable so the
|
|
@@ -68,7 +99,10 @@ export function validatePurchaseAssurance(value, target, now = new Date()) {
|
|
|
68
99
|
if (value == null || typeof value !== 'object') {
|
|
69
100
|
throw new Error(`purchase assurance is missing — ${remedy}`);
|
|
70
101
|
}
|
|
71
|
-
|
|
102
|
+
// A passkey-exempt approval (marked by the approval SERVER, never assumed
|
|
103
|
+
// locally) carries no assurance by design — every scope/freshness check
|
|
104
|
+
// below still applies to it unchanged.
|
|
105
|
+
if (value.assuranceData == null && value.assuranceExempt !== true) {
|
|
72
106
|
throw new Error(`purchase assurance requires assuranceData — ${remedy}`);
|
|
73
107
|
}
|
|
74
108
|
const mintedAtMs = typeof value.mintedAt === 'string' ? Date.parse(value.mintedAt) : NaN;
|
|
@@ -125,13 +159,14 @@ export function validateCredential(value, now = new Date()) {
|
|
|
125
159
|
(value.expYear === now.getFullYear() && value.expMonth < now.getMonth() + 1)) {
|
|
126
160
|
throw new Error('VGS credential is expired');
|
|
127
161
|
}
|
|
128
|
-
if (value.cryptogramExpiresAt
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
162
|
+
if (value.cryptogramExpiresAt === undefined) {
|
|
163
|
+
throw new Error('VGS credential expiry is missing');
|
|
164
|
+
}
|
|
165
|
+
const expiresAtMs = Date.parse(value.cryptogramExpiresAt);
|
|
166
|
+
if (!Number.isFinite(expiresAtMs))
|
|
167
|
+
throw new Error('VGS credential expiry is invalid');
|
|
168
|
+
if (expiresAtMs - now.getTime() < 60_000) {
|
|
169
|
+
throw new Error('VGS credential has less than 60 seconds of validity remaining');
|
|
135
170
|
}
|
|
136
171
|
}
|
|
137
172
|
/**
|
|
@@ -188,65 +223,7 @@ export class VgsLiveInstrument {
|
|
|
188
223
|
cvc: value.cryptogramValue,
|
|
189
224
|
cardholderName: this.cardholderName.trim(),
|
|
190
225
|
...(value.cryptogramExpiresAt ? { credentialExpiresAt: value.cryptogramExpiresAt } : {}),
|
|
191
|
-
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
/**
|
|
195
|
-
* Consumes #5614's claimed CLI enrollment artifact (for the tokenId) plus a
|
|
196
|
-
* FRESH purchase-scoped assurance, and creates a fresh, transaction-scoped VIC
|
|
197
|
-
* intent + credential after checkout approval. This is the production-shaped
|
|
198
|
-
* bridge; unlike VgsLiveInstrument it never needs a pre-created intent ID.
|
|
199
|
-
*
|
|
200
|
-
* The enrollment artifact's stored assuranceData is deliberately never sent:
|
|
201
|
-
* replaying it makes intent creation succeed (HTTP 201) while the cryptogram
|
|
202
|
-
* deterministically never completes — the #5709 dead end. Purchase
|
|
203
|
-
* authorization is the fresh, merchant+amount+currency-scoped assurance,
|
|
204
|
-
* validated against the checkout target BEFORE any intent is minted so a
|
|
205
|
-
* doomed intent is never created.
|
|
206
|
-
*/
|
|
207
|
-
export class VgsAssuranceInstrument {
|
|
208
|
-
enrollment;
|
|
209
|
-
purchase;
|
|
210
|
-
target;
|
|
211
|
-
cardholderName;
|
|
212
|
-
mintCredential;
|
|
213
|
-
kind = 'agentic-token';
|
|
214
|
-
used = false;
|
|
215
|
-
minted = null;
|
|
216
|
-
constructor(enrollment, purchase, target, cardholderName, mintCredential) {
|
|
217
|
-
this.enrollment = enrollment;
|
|
218
|
-
this.purchase = purchase;
|
|
219
|
-
this.target = target;
|
|
220
|
-
this.cardholderName = cardholderName;
|
|
221
|
-
this.mintCredential = mintCredential;
|
|
222
|
-
}
|
|
223
|
-
/** See VgsLiveInstrument.confirmationTarget — same contract. */
|
|
224
|
-
confirmationTarget() {
|
|
225
|
-
return this.minted;
|
|
226
|
-
}
|
|
227
|
-
async getCredential(ctx) {
|
|
228
|
-
if (this.used)
|
|
229
|
-
throw new Error('VGS assurance instrument is single-use');
|
|
230
|
-
this.used = true;
|
|
231
|
-
validateCliCredential(this.enrollment);
|
|
232
|
-
validateTarget(this.target, ctx);
|
|
233
|
-
validatePurchaseAssurance(this.purchase, this.target);
|
|
234
|
-
if (!this.cardholderName.trim())
|
|
235
|
-
throw new Error('cardholder name is required');
|
|
236
|
-
const { payment: value, intentId } = await this.mintCredential({
|
|
237
|
-
tokenId: this.enrollment.tokenId,
|
|
238
|
-
assuranceData: this.purchase.assuranceData,
|
|
239
|
-
transaction: this.target,
|
|
240
|
-
});
|
|
241
|
-
this.minted = { tokenId: this.enrollment.tokenId, intentId };
|
|
242
|
-
validateCredential(value);
|
|
243
|
-
return {
|
|
244
|
-
pan: value.networkToken,
|
|
245
|
-
expMonth: value.expMonth,
|
|
246
|
-
expYear: value.expYear,
|
|
247
|
-
cvc: value.cryptogramValue,
|
|
248
|
-
cardholderName: this.cardholderName.trim(),
|
|
249
|
-
...(value.cryptogramExpiresAt ? { credentialExpiresAt: value.cryptogramExpiresAt } : {}),
|
|
226
|
+
...traceHandleFields(value),
|
|
250
227
|
};
|
|
251
228
|
}
|
|
252
229
|
}
|
|
@@ -32,8 +32,8 @@ export async function reportVicOutcome(input) {
|
|
|
32
32
|
catch (err) {
|
|
33
33
|
return {
|
|
34
34
|
posted: false,
|
|
35
|
-
reason: `confirmation POST failed: ${err.message} — the merchant outcome
|
|
36
|
-
'retry the confirmation for this intent out-of-band',
|
|
35
|
+
reason: `confirmation POST failed: ${err.message} — the merchant-reported outcome ` +
|
|
36
|
+
'remains unverified; retry the confirmation for this intent out-of-band',
|
|
37
37
|
};
|
|
38
38
|
}
|
|
39
39
|
}
|