@visa/cli 4.1.0-rc.6 → 4.1.0-rc.61

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 (70) hide show
  1. package/README.md +168 -235
  2. package/dist/checkout-engine/adapters/generic.d.ts +23 -0
  3. package/dist/checkout-engine/adapters/generic.js +216 -0
  4. package/dist/checkout-engine/adapters/index.d.ts +8 -0
  5. package/dist/checkout-engine/adapters/index.js +21 -0
  6. package/dist/checkout-engine/adapters/shopify.d.ts +31 -0
  7. package/dist/checkout-engine/adapters/shopify.js +423 -0
  8. package/dist/checkout-engine/adapters/stripe-like.d.ts +10 -0
  9. package/dist/checkout-engine/adapters/stripe-like.js +21 -0
  10. package/dist/checkout-engine/amount.d.ts +15 -0
  11. package/dist/checkout-engine/amount.js +72 -0
  12. package/dist/checkout-engine/browser-launch.d.ts +46 -0
  13. package/dist/checkout-engine/browser-launch.js +81 -0
  14. package/dist/checkout-engine/ceremony.d.ts +64 -0
  15. package/dist/checkout-engine/ceremony.js +261 -0
  16. package/dist/checkout-engine/cli-engine.d.ts +200 -0
  17. package/dist/checkout-engine/cli-engine.js +659 -0
  18. package/dist/checkout-engine/detect.d.ts +61 -0
  19. package/dist/checkout-engine/detect.js +398 -0
  20. package/dist/checkout-engine/evidence.d.ts +25 -0
  21. package/dist/checkout-engine/evidence.js +104 -0
  22. package/dist/checkout-engine/executor.d.ts +176 -0
  23. package/dist/checkout-engine/executor.js +1322 -0
  24. package/dist/checkout-engine/hosted-approval.d.ts +142 -0
  25. package/dist/checkout-engine/hosted-approval.js +339 -0
  26. package/dist/checkout-engine/index.d.ts +6 -0
  27. package/dist/checkout-engine/index.js +8 -0
  28. package/dist/checkout-engine/inline-target.d.ts +13 -0
  29. package/dist/checkout-engine/inline-target.js +37 -0
  30. package/dist/checkout-engine/instrument.d.ts +61 -0
  31. package/dist/checkout-engine/instrument.js +87 -0
  32. package/dist/checkout-engine/live-fill-approval.d.ts +43 -0
  33. package/dist/checkout-engine/live-fill-approval.js +90 -0
  34. package/dist/checkout-engine/mandate/card-mandate.d.ts +121 -0
  35. package/dist/checkout-engine/mandate/card-mandate.js +227 -0
  36. package/dist/checkout-engine/mandate/mandate-ledger.d.ts +142 -0
  37. package/dist/checkout-engine/mandate/mandate-ledger.js +335 -0
  38. package/dist/checkout-engine/mandate.d.ts +25 -0
  39. package/dist/checkout-engine/mandate.js +100 -0
  40. package/dist/checkout-engine/outcome.d.ts +30 -0
  41. package/dist/checkout-engine/outcome.js +225 -0
  42. package/dist/checkout-engine/owner-only-file.d.ts +19 -0
  43. package/dist/checkout-engine/owner-only-file.js +41 -0
  44. package/dist/checkout-engine/package.json +3 -0
  45. package/dist/checkout-engine/receipt.d.ts +81 -0
  46. package/dist/checkout-engine/receipt.js +109 -0
  47. package/dist/checkout-engine/repo-env.d.ts +11 -0
  48. package/dist/checkout-engine/repo-env.js +23 -0
  49. package/dist/checkout-engine/trace-handles.d.ts +8 -0
  50. package/dist/checkout-engine/trace-handles.js +12 -0
  51. package/dist/checkout-engine/types.d.ts +44 -0
  52. package/dist/checkout-engine/types.js +2 -0
  53. package/dist/checkout-engine/vgs-gateway/fetch-credential.d.mts +74 -0
  54. package/dist/checkout-engine/vgs-gateway/fetch-credential.mjs +248 -0
  55. package/dist/checkout-engine/vgs-gateway/server-mint-client.d.ts +82 -0
  56. package/dist/checkout-engine/vgs-gateway/server-mint-client.js +180 -0
  57. package/dist/checkout-engine/vgs-live-instrument.d.ts +170 -0
  58. package/dist/checkout-engine/vgs-live-instrument.js +293 -0
  59. package/dist/checkout-engine/vic-confirmation.d.ts +34 -0
  60. package/dist/checkout-engine/vic-confirmation.js +39 -0
  61. package/dist/cli.js +391 -427
  62. package/dist/mcp-server/index.js +312 -163
  63. package/dist/skills/pair-visa-agent/RUNTIMES.md +92 -0
  64. package/dist/skills/pair-visa-agent/SKILL.md +315 -0
  65. package/dist/skills/pair-visa-agent/scripts/setup.mjs +48 -0
  66. package/install.ps1 +3 -41
  67. package/install.sh +3 -35
  68. package/native/bin/win32-x64/visa-keychain-win.exe +0 -0
  69. package/package.json +11 -7
  70. package/server.json +3 -3
@@ -0,0 +1,293 @@
1
+ import { traceHandleFields } from './trace-handles.js';
2
+ /**
3
+ * Our fail-closed freshness bound, matching the runner's 15-minute checkout
4
+ * review window — NOT a claim about VGS's actual assurance TTL (unpublished).
5
+ * The only proven-working configuration mints the intent right after the
6
+ * ceremony; anything older is refused before an intent is created.
7
+ */
8
+ export const PURCHASE_ASSURANCE_MAX_AGE_MS = 15 * 60 * 1000;
9
+ /** Tolerated forward clock skew on `mintedAt` before it is treated as invalid. */
10
+ const PURCHASE_ASSURANCE_MAX_SKEW_MS = 2 * 60 * 1000;
11
+ export function decimalToMinor(value) {
12
+ // JSON files routinely carry a number where the contract says decimal string
13
+ // ("transactionAmount": 5): refuse non-strings HERE so every caller surfaces
14
+ // its scoped invalid/mismatch remedy instead of a raw TypeError crash.
15
+ if (typeof value !== 'string')
16
+ return null;
17
+ if (!/^\d+(\.\d{1,2})?$/.test(value))
18
+ return null;
19
+ const [whole, fraction = ''] = value.split('.');
20
+ const minor = Number(whole) * 100 + Number(fraction.padEnd(2, '0'));
21
+ return Number.isSafeInteger(minor) ? minor : null;
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
+ }
60
+ function validateTarget(reference, ctx) {
61
+ if (typeof reference.merchantName !== 'string' ||
62
+ typeof reference.merchantCountryCode !== 'string' ||
63
+ !reference.merchantName.trim() ||
64
+ !/^[A-Z]{2}$/.test(reference.merchantCountryCode)) {
65
+ throw new Error('VGS checkout reference merchant name/country is invalid');
66
+ }
67
+ let referenceHost;
68
+ try {
69
+ referenceHost = new URL(reference.merchantUrl).hostname;
70
+ }
71
+ catch {
72
+ throw new Error('VGS checkout reference merchant URL is invalid');
73
+ }
74
+ if (referenceHost !== ctx.merchantHost) {
75
+ throw new Error(`VGS credential merchant mismatch: ${referenceHost} vs ${ctx.merchantHost}`);
76
+ }
77
+ const amountMinor = decimalToMinor(reference.transactionAmount);
78
+ if (amountMinor === null)
79
+ throw new Error('VGS checkout reference amount is invalid');
80
+ if (amountMinor !== ctx.amountMinor) {
81
+ throw new Error(`VGS credential amount mismatch: ${amountMinor} vs ${ctx.amountMinor} minor units`);
82
+ }
83
+ // String() so a JSON-number currency (840) mismatches with a readable
84
+ // message instead of crashing on .toUpperCase().
85
+ const currency = String(reference.transactionCurrencyCode).toUpperCase();
86
+ if (currency !== ctx.currency) {
87
+ throw new Error(`VGS credential currency mismatch: ${currency} vs ${ctx.currency}`);
88
+ }
89
+ }
90
+ function validateCliCredential(value) {
91
+ if (typeof value.tokenId !== 'string' || !value.tokenId.trim()) {
92
+ throw new Error('CLI agent credential requires tokenId');
93
+ }
94
+ // Deliberately no assuranceData requirement: the artifact's enrollment-time
95
+ // assurance is identity/enrollment material and is never read here (#5709).
96
+ }
97
+ /**
98
+ * Refuse to mint an intent unless the assurance is fresh and its declared
99
+ * scope matches the checkout target exactly. `now` is injectable so the
100
+ * freshness rules are testable with a pinned clock (same pattern as
101
+ * validateCredential).
102
+ */
103
+ export function validatePurchaseAssurance(value, target, now = new Date()) {
104
+ const remedy = 'run a fresh device-binding ceremony scoped to this exact merchant, amount, and ' +
105
+ 'currency, and pass its output via --purchase-assurance-file (#5709)';
106
+ if (value == null || typeof value !== 'object') {
107
+ throw new Error(`purchase assurance is missing — ${remedy}`);
108
+ }
109
+ if (value.assuranceData == null) {
110
+ throw new Error(`purchase assurance requires assuranceData — ${remedy}`);
111
+ }
112
+ const mintedAtMs = typeof value.mintedAt === 'string' ? Date.parse(value.mintedAt) : NaN;
113
+ if (!Number.isFinite(mintedAtMs)) {
114
+ throw new Error(`purchase assurance requires an ISO 8601 mintedAt — ${remedy}`);
115
+ }
116
+ const age = now.getTime() - mintedAtMs;
117
+ if (age < -PURCHASE_ASSURANCE_MAX_SKEW_MS) {
118
+ throw new Error(`purchase assurance mintedAt is in the future — ${remedy}`);
119
+ }
120
+ if (age > PURCHASE_ASSURANCE_MAX_AGE_MS) {
121
+ throw new Error(`purchase assurance is stale (minted ${value.mintedAt}, limit ` +
122
+ `${PURCHASE_ASSURANCE_MAX_AGE_MS / 60000} minutes) — ${remedy}`);
123
+ }
124
+ let targetHost;
125
+ try {
126
+ targetHost = new URL(target.merchantUrl).hostname;
127
+ }
128
+ catch {
129
+ throw new Error('VGS checkout reference merchant URL is invalid');
130
+ }
131
+ if (value.merchantHost !== targetHost) {
132
+ throw new Error(`purchase assurance merchant mismatch: ${value.merchantHost} vs ${targetHost} — ${remedy}`);
133
+ }
134
+ const assuranceMinor = decimalToMinor(value.transactionAmount);
135
+ const targetMinor = decimalToMinor(target.transactionAmount);
136
+ if (assuranceMinor === null || assuranceMinor !== targetMinor) {
137
+ throw new Error(`purchase assurance amount mismatch: ${value.transactionAmount} vs ` +
138
+ `${target.transactionAmount} — ${remedy}`);
139
+ }
140
+ if (typeof value.transactionCurrencyCode !== 'string' ||
141
+ value.transactionCurrencyCode.toUpperCase() !==
142
+ String(target.transactionCurrencyCode).toUpperCase()) {
143
+ throw new Error(`purchase assurance currency mismatch: ${value.transactionCurrencyCode} vs ` +
144
+ `${target.transactionCurrencyCode} — ${remedy}`);
145
+ }
146
+ }
147
+ // `now` is injectable so expiry rules are testable with a pinned clock.
148
+ export function validateCredential(value, now = new Date()) {
149
+ if (!/^\d{12,19}$/.test(value.networkToken)) {
150
+ throw new Error('VGS credential network token must contain 12-19 digits');
151
+ }
152
+ if (!/^\d{3,4}$/.test(value.cryptogramValue)) {
153
+ throw new Error('VGS credential short DAVV must contain 3-4 digits');
154
+ }
155
+ if (typeof value.cryptogramType !== 'string' || value.cryptogramType.toUpperCase() !== 'DAVV') {
156
+ throw new Error(`VGS credential type ${value.cryptogramType || '<missing>'} is not DAVV`);
157
+ }
158
+ if (!Number.isInteger(value.expMonth) || value.expMonth < 1 || value.expMonth > 12) {
159
+ throw new Error('VGS credential expiration month is invalid');
160
+ }
161
+ if (!Number.isInteger(value.expYear) ||
162
+ value.expYear < now.getFullYear() ||
163
+ (value.expYear === now.getFullYear() && value.expMonth < now.getMonth() + 1)) {
164
+ throw new Error('VGS credential is expired');
165
+ }
166
+ if (value.cryptogramExpiresAt === undefined) {
167
+ throw new Error('VGS credential expiry is missing');
168
+ }
169
+ const expiresAtMs = Date.parse(value.cryptogramExpiresAt);
170
+ if (!Number.isFinite(expiresAtMs))
171
+ throw new Error('VGS credential expiry is invalid');
172
+ if (expiresAtMs - now.getTime() < 60_000) {
173
+ throw new Error('VGS credential has less than 60 seconds of validity remaining');
174
+ }
175
+ }
176
+ /**
177
+ * Mints the full DPAN + short DAVV only after the executor revalidates the
178
+ * reviewed merchant, amount, and currency. The payment credential remains in
179
+ * memory, is never returned to the caller, and can be requested only once.
180
+ */
181
+ export class VgsLiveInstrument {
182
+ reference;
183
+ cardholderName;
184
+ fetchCredential;
185
+ kind = 'agentic-token';
186
+ used = false;
187
+ minted = null;
188
+ constructor(reference, cardholderName, fetchCredential) {
189
+ this.reference = reference;
190
+ this.cardholderName = cardholderName;
191
+ this.fetchCredential = fetchCredential;
192
+ }
193
+ /**
194
+ * Non-null once VGS has actually issued a credential (the intent is consumed
195
+ * from that moment, even if local validation later rejects the credential) —
196
+ * exactly the population a VIC confirmation can be owed for.
197
+ */
198
+ confirmationTarget() {
199
+ return this.minted;
200
+ }
201
+ async getCredential(ctx) {
202
+ if (this.used)
203
+ throw new Error('VGS live credential instrument is single-use');
204
+ this.used = true;
205
+ validateTarget(this.reference, ctx);
206
+ if (!this.reference.tokenId || !this.reference.intentId) {
207
+ throw new Error('VGS checkout reference requires tokenId and intentId');
208
+ }
209
+ if (!this.cardholderName.trim())
210
+ throw new Error('cardholder name is required');
211
+ const { tokenId, intentId, ...transaction } = this.reference;
212
+ const value = await this.fetchCredential({ tokenId, intentId, transaction });
213
+ this.minted = { tokenId, intentId };
214
+ try {
215
+ validateCredential(value);
216
+ }
217
+ catch (err) {
218
+ // VGS already issued a cryptogram for this intent even though we refuse
219
+ // to fill it; "correct inputs and retry" would silently reuse a consumed
220
+ // intent, so steer the operator to a fresh one.
221
+ throw new Error(`${err.message} — a credential was issued by VGS but rejected locally; obtain a fresh intent before retrying`);
222
+ }
223
+ return {
224
+ pan: value.networkToken,
225
+ expMonth: value.expMonth,
226
+ expYear: value.expYear,
227
+ cvc: value.cryptogramValue,
228
+ cardholderName: this.cardholderName.trim(),
229
+ ...(value.cryptogramExpiresAt ? { credentialExpiresAt: value.cryptogramExpiresAt } : {}),
230
+ ...traceHandleFields(value),
231
+ };
232
+ }
233
+ }
234
+ /**
235
+ * Consumes #5614's claimed CLI enrollment artifact (for the tokenId) plus a
236
+ * FRESH purchase-scoped assurance, and creates a fresh, transaction-scoped VIC
237
+ * intent + credential after checkout approval. This is the production-shaped
238
+ * bridge; unlike VgsLiveInstrument it never needs a pre-created intent ID.
239
+ *
240
+ * The enrollment artifact's stored assuranceData is deliberately never sent:
241
+ * replaying it makes intent creation succeed (HTTP 201) while the cryptogram
242
+ * deterministically never completes — the #5709 dead end. Purchase
243
+ * authorization is the fresh, merchant+amount+currency-scoped assurance,
244
+ * validated against the checkout target BEFORE any intent is minted so a
245
+ * doomed intent is never created.
246
+ */
247
+ export class VgsAssuranceInstrument {
248
+ enrollment;
249
+ purchase;
250
+ target;
251
+ cardholderName;
252
+ mintCredential;
253
+ kind = 'agentic-token';
254
+ used = false;
255
+ minted = null;
256
+ constructor(enrollment, purchase, target, cardholderName, mintCredential) {
257
+ this.enrollment = enrollment;
258
+ this.purchase = purchase;
259
+ this.target = target;
260
+ this.cardholderName = cardholderName;
261
+ this.mintCredential = mintCredential;
262
+ }
263
+ /** See VgsLiveInstrument.confirmationTarget — same contract. */
264
+ confirmationTarget() {
265
+ return this.minted;
266
+ }
267
+ async getCredential(ctx) {
268
+ if (this.used)
269
+ throw new Error('VGS assurance instrument is single-use');
270
+ this.used = true;
271
+ validateCliCredential(this.enrollment);
272
+ validateTarget(this.target, ctx);
273
+ validatePurchaseAssurance(this.purchase, this.target);
274
+ if (!this.cardholderName.trim())
275
+ throw new Error('cardholder name is required');
276
+ const { payment: value, intentId } = await this.mintCredential({
277
+ tokenId: this.enrollment.tokenId,
278
+ assuranceData: this.purchase.assuranceData,
279
+ transaction: this.target,
280
+ });
281
+ this.minted = { tokenId: this.enrollment.tokenId, intentId };
282
+ validateCredential(value);
283
+ return {
284
+ pan: value.networkToken,
285
+ expMonth: value.expMonth,
286
+ expYear: value.expYear,
287
+ cvc: value.cryptogramValue,
288
+ cardholderName: this.cardholderName.trim(),
289
+ ...(value.cryptogramExpiresAt ? { credentialExpiresAt: value.cryptogramExpiresAt } : {}),
290
+ ...traceHandleFields(value),
291
+ };
292
+ }
293
+ }
@@ -0,0 +1,34 @@
1
+ import type { CheckoutOutcome } from './executor.js';
2
+ import type { VicConfirmationTarget } from './vgs-live-instrument.js';
3
+ export type VicTransactionStatus = 'APPROVED' | 'DECLINED';
4
+ export type PostVicConfirmation = (input: {
5
+ tokenId: string;
6
+ intentId: string;
7
+ transactionStatus: VicTransactionStatus;
8
+ transactionType: 'PURCHASE';
9
+ transactionTimestamp: string;
10
+ transaction: {
11
+ transactionAmount: string;
12
+ transactionCurrencyCode: string;
13
+ };
14
+ }) => Promise<unknown>;
15
+ export type VicConfirmationReport = {
16
+ posted: true;
17
+ transactionStatus: VicTransactionStatus;
18
+ } | {
19
+ posted: false;
20
+ reason: string;
21
+ };
22
+ /** Definitive merchant answers map to a status; everything else maps to none. */
23
+ export declare function outcomeToTransactionStatus(outcome: CheckoutOutcome): VicTransactionStatus | null;
24
+ export declare function reportVicOutcome(input: {
25
+ target: VicConfirmationTarget | null;
26
+ outcome: CheckoutOutcome;
27
+ transaction: {
28
+ transactionAmount: string;
29
+ transactionCurrencyCode: string;
30
+ };
31
+ post: PostVicConfirmation;
32
+ /** Injectable for tests; defaults to now. */
33
+ timestamp?: Date;
34
+ }): Promise<VicConfirmationReport>;
@@ -0,0 +1,39 @@
1
+ /** Definitive merchant answers map to a status; everything else maps to none. */
2
+ export function outcomeToTransactionStatus(outcome) {
3
+ if (outcome === 'confirmed')
4
+ return 'APPROVED';
5
+ if (outcome === 'declined')
6
+ return 'DECLINED';
7
+ return null;
8
+ }
9
+ export async function reportVicOutcome(input) {
10
+ const transactionStatus = outcomeToTransactionStatus(input.outcome);
11
+ if (!transactionStatus) {
12
+ return {
13
+ posted: false,
14
+ reason: `outcome '${input.outcome}' is not a definitive merchant answer — ` +
15
+ 'resolve it with the merchant, then post the confirmation manually',
16
+ };
17
+ }
18
+ if (!input.target) {
19
+ return { posted: false, reason: 'no VIC credential was minted in this run' };
20
+ }
21
+ try {
22
+ await input.post({
23
+ tokenId: input.target.tokenId,
24
+ intentId: input.target.intentId,
25
+ transactionStatus,
26
+ transactionType: 'PURCHASE',
27
+ transactionTimestamp: (input.timestamp ?? new Date()).toISOString(),
28
+ transaction: input.transaction,
29
+ });
30
+ return { posted: true, transactionStatus };
31
+ }
32
+ catch (err) {
33
+ return {
34
+ posted: false,
35
+ reason: `confirmation POST failed: ${err.message} — the merchant-reported outcome ` +
36
+ 'remains unverified; retry the confirmation for this intent out-of-band',
37
+ };
38
+ }
39
+ }