@visa/cli 4.1.0-rc.166 → 4.1.0-rc.167

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.
@@ -1251,6 +1251,12 @@ export async function submitApprovedCheckout(reviewId, opts, store = defaultPrep
1251
1251
  credentialExpiresAt,
1252
1252
  });
1253
1253
  }
1254
+ // ORDER IS LOAD-BEARING: record the click BEFORE performing it. The catch
1255
+ // block classifies a throw by whether this step exists — recorded means
1256
+ // "we may have charged" (`unverified`), absent means "retry is safe"
1257
+ // (`failed`). Recording after `submit.click()` would let a throw raised by
1258
+ // the click itself look retry-safe, which is the double-charge direction.
1259
+ // Pinned by "a throw AFTER the pay control was clicked reports unverified".
1254
1260
  evidence.step('submit', { clicked: true, target: submit.desc });
1255
1261
  await submit.click();
1256
1262
  await settle(page);
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Outcomes whose receipt cannot rule out a charge.
3
+ *
4
+ * Both of these record a run where the pay control was clicked and no
5
+ * definitive answer came back:
6
+ * - `unverified` — no confirmation or decline observed before the deadline
7
+ * - `action-required` — an issuer challenge appeared, which happens AFTER the
8
+ * click, so the authorization may already exist
9
+ *
10
+ * Everything else is either definitive (`confirmed`, `declined`) or provably
11
+ * pre-click (`reviewed-dry-run`, `partial-fill`, `failed`, ...). Widening this
12
+ * set blocks legitimate retries; narrowing it lets a double charge through, so
13
+ * an outcome earns a place here only by being genuinely post-click.
14
+ */
15
+ export declare const MAY_HAVE_CHARGED_OUTCOMES: ReadonlySet<string>;
16
+ /** One local attempt that may have taken money and was never resolved. */
17
+ export type UnresolvedCharge = {
18
+ recordedAt: string;
19
+ host: string;
20
+ amount: string;
21
+ amountMinor: number;
22
+ currency: string;
23
+ outcome: string;
24
+ /** Receipt file basename, so a caller can name the evidence a human must check. */
25
+ receiptFile: string;
26
+ };
27
+ /**
28
+ * Local attempts that may have charged, newest first.
29
+ *
30
+ * Never throws: a missing directory (nothing has ever been checked out here)
31
+ * and an unreadable one both read as "no unresolved charges". The caller
32
+ * decides what an empty answer means — see the fail-open note at its call site.
33
+ */
34
+ export declare function readUnresolvedCharges(receiptDir?: string): Promise<UnresolvedCharge[]>;
@@ -0,0 +1,125 @@
1
+ // "Did this device already submit this exact purchase?", read back from the
2
+ // checkout receipts on disk.
3
+ //
4
+ // The engine now reports a submitted-but-unobserved checkout as `unverified`
5
+ // instead of `failed`, and pay_merchant tells the caller not to retry it. This
6
+ // module is the belt to that suspenders: even a caller that ignores the words
7
+ // can be stopped, because the previous attempt left a receipt.
8
+ //
9
+ // It answers one question — which local receipts record an attempt that MAY
10
+ // HAVE CHARGED and was never resolved — and deliberately answers nothing else.
11
+ // The matching (same merchant, same amount, recent enough) lives in the caller,
12
+ // which owns the policy; this side owns only the read.
13
+ //
14
+ // Sibling of confirmed-merchants.ts and loaded through the same seam: it
15
+ // imports node built-ins and types only, so reading receipts never pulls
16
+ // playwright into the CLI process.
17
+ //
18
+ // Reading is best-effort by construction. The receipts directory is an operator
19
+ // artifact that anything on the box can touch, so an unreadable or malformed
20
+ // file is skipped rather than failing the whole read.
21
+ import { readdir, readFile } from 'node:fs/promises';
22
+ import { join } from 'node:path';
23
+ import { RECEIPT_DIR } from './receipt-dir.js';
24
+ /**
25
+ * Outcomes whose receipt cannot rule out a charge.
26
+ *
27
+ * Both of these record a run where the pay control was clicked and no
28
+ * definitive answer came back:
29
+ * - `unverified` — no confirmation or decline observed before the deadline
30
+ * - `action-required` — an issuer challenge appeared, which happens AFTER the
31
+ * click, so the authorization may already exist
32
+ *
33
+ * Everything else is either definitive (`confirmed`, `declined`) or provably
34
+ * pre-click (`reviewed-dry-run`, `partial-fill`, `failed`, ...). Widening this
35
+ * set blocks legitimate retries; narrowing it lets a double charge through, so
36
+ * an outcome earns a place here only by being genuinely post-click.
37
+ */
38
+ export const MAY_HAVE_CHARGED_OUTCOMES = new Set([
39
+ 'unverified',
40
+ 'action-required',
41
+ ]);
42
+ /**
43
+ * Every field this module reads, validated. Deliberately permissive about
44
+ * everything else: older and newer engines only have to carry these.
45
+ *
46
+ * Both receipt schemas expose all of them at the same paths, so unlike
47
+ * confirmed-merchants.ts there is nothing here that needs a per-schema branch.
48
+ */
49
+ function parseUnresolved(json, receiptFile) {
50
+ let parsed;
51
+ try {
52
+ parsed = JSON.parse(json);
53
+ }
54
+ catch {
55
+ return null;
56
+ }
57
+ if (typeof parsed !== 'object' || parsed === null)
58
+ return null;
59
+ const receipt = parsed;
60
+ if (receipt.schema !== 'checkout-agent-receipt/v1' &&
61
+ receipt.schema !== 'checkout-agent-receipt/v2') {
62
+ return null;
63
+ }
64
+ if (typeof receipt.outcome !== 'string' || !MAY_HAVE_CHARGED_OUTCOMES.has(receipt.outcome)) {
65
+ return null;
66
+ }
67
+ if (typeof receipt.recordedAt !== 'string' || Number.isNaN(Date.parse(receipt.recordedAt))) {
68
+ return null;
69
+ }
70
+ const host = receipt.merchant?.host;
71
+ if (typeof host !== 'string' || host.length === 0)
72
+ return null;
73
+ const transaction = receipt.transaction;
74
+ if (typeof transaction?.amount !== 'string')
75
+ return null;
76
+ if (typeof transaction.currency !== 'string')
77
+ return null;
78
+ // A receipt whose amount cannot be compared cannot gate a retry on amount, and
79
+ // a guard that silently matched every amount would be worse than none.
80
+ if (typeof transaction.amountMinor !== 'number' || !Number.isFinite(transaction.amountMinor)) {
81
+ return null;
82
+ }
83
+ return {
84
+ recordedAt: receipt.recordedAt,
85
+ host,
86
+ amount: transaction.amount,
87
+ amountMinor: transaction.amountMinor,
88
+ currency: transaction.currency,
89
+ outcome: receipt.outcome,
90
+ receiptFile,
91
+ };
92
+ }
93
+ /**
94
+ * Local attempts that may have charged, newest first.
95
+ *
96
+ * Never throws: a missing directory (nothing has ever been checked out here)
97
+ * and an unreadable one both read as "no unresolved charges". The caller
98
+ * decides what an empty answer means — see the fail-open note at its call site.
99
+ */
100
+ export async function readUnresolvedCharges(receiptDir = RECEIPT_DIR) {
101
+ let names;
102
+ try {
103
+ names = await readdir(receiptDir);
104
+ }
105
+ catch {
106
+ return [];
107
+ }
108
+ const charges = [];
109
+ for (const name of names) {
110
+ if (!name.endsWith('.json'))
111
+ continue;
112
+ let raw;
113
+ try {
114
+ raw = await readFile(join(receiptDir, name), 'utf8');
115
+ }
116
+ catch {
117
+ continue;
118
+ }
119
+ const charge = parseUnresolved(raw, name);
120
+ if (charge)
121
+ charges.push(charge);
122
+ }
123
+ charges.sort((left, right) => (left.recordedAt < right.recordedAt ? 1 : -1));
124
+ return charges;
125
+ }