z-zero-mcp-server 1.2.3 → 1.3.2
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/LICENSE +21 -0
- package/README.md +76 -39
- package/dist/index.js +203 -134
- package/dist/lib/extract-total-price.d.ts +13 -0
- package/dist/lib/extract-total-price.js +36 -0
- package/dist/lib/url-guard.d.ts +7 -0
- package/dist/lib/url-guard.js +45 -0
- package/dist/lib/web3-detector.js +12 -12
- package/dist/playwright_bridge.js +153 -18
- package/dist/types.d.ts +2 -0
- package/dist/wdk_backend.js +35 -32
- package/package.json +13 -3
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
// Returns null if no price can be confidently found.
|
|
6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
7
|
exports.extractTotalPrice = extractTotalPrice;
|
|
8
|
+
exports.detectCheckoutCurrency = detectCheckoutCurrency;
|
|
8
9
|
/**
|
|
9
10
|
* Multi-strategy total price extractor.
|
|
10
11
|
* Strategy order (most reliable → least reliable):
|
|
@@ -116,6 +117,41 @@ async function extractTotalPrice(page) {
|
|
|
116
117
|
console.error('[PRICE] ❌ Could not detect total price on this page.');
|
|
117
118
|
return null;
|
|
118
119
|
}
|
|
120
|
+
/**
|
|
121
|
+
* Detect whether the checkout charges in USD or a non-USD currency.
|
|
122
|
+
* Purpose: size the FX buffer on the JIT card. A USD checkout total is final —
|
|
123
|
+
* our USD card pays it 1:1, no conversion. A non-USD total gets FX-converted +
|
|
124
|
+
* a conversion fee on our USD card and can drift OVER the authorized limit →
|
|
125
|
+
* issuer decline. So non-USD orders need a slightly larger card limit.
|
|
126
|
+
*
|
|
127
|
+
* Conservative by design: defaults to 'USD' unless a CLEAR non-USD signal is
|
|
128
|
+
* present. Both error directions are non-catastrophic (over-buffer → refunded
|
|
129
|
+
* at settlement; under-buffer → a decline the agent can retry), so we avoid
|
|
130
|
+
* false positives from random uppercase text via a currency-code whitelist.
|
|
131
|
+
*/
|
|
132
|
+
async function detectCheckoutCurrency(page) {
|
|
133
|
+
try {
|
|
134
|
+
return await page.evaluate(() => {
|
|
135
|
+
const body = document.body.innerText || '';
|
|
136
|
+
// 1. Non-USD currency symbols
|
|
137
|
+
if (/[€£¥₹₩₺₽฿₴]/.test(body))
|
|
138
|
+
return 'NON_USD';
|
|
139
|
+
// 2. Ambiguous "$" with a non-US region prefix (CA$, A$, NZ$, HK$, R$, S$)
|
|
140
|
+
if (/\b(?:CA|A|NZ|HK|R|S)\$/.test(body))
|
|
141
|
+
return 'NON_USD';
|
|
142
|
+
// 3. Explicit 3-letter ISO codes — whitelist of common non-USD only
|
|
143
|
+
const NON_USD = ['EUR', 'GBP', 'JPY', 'CNY', 'INR', 'AUD', 'CAD', 'CHF', 'SGD', 'HKD', 'NZD', 'SEK', 'NOK', 'DKK', 'KRW', 'MXN', 'BRL', 'ZAR', 'AED', 'THB', 'MYR', 'PHP', 'VND', 'IDR', 'PLN', 'TRY'];
|
|
144
|
+
for (const code of NON_USD) {
|
|
145
|
+
if (new RegExp(`\\b${code}\\b`).test(body))
|
|
146
|
+
return 'NON_USD';
|
|
147
|
+
}
|
|
148
|
+
return 'USD';
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
catch {
|
|
152
|
+
return 'USD'; // fail safe → smallest buffer, never blocks a payment
|
|
153
|
+
}
|
|
154
|
+
}
|
|
119
155
|
// ─── Utility ─────────────────────────────────────────────────────────────────
|
|
120
156
|
function parseMoneyString(text) {
|
|
121
157
|
// Handles: "$49.99", "USD 49.99", "49,99 $", "49.99 USD"
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Throws if `rawUrl` is not a safe public checkout URL.
|
|
3
|
+
* - must parse as a URL
|
|
4
|
+
* - must be HTTPS (except localhost/127.0.0.1 in non-production, for local testing)
|
|
5
|
+
* - must not resolve to a private / loopback / link-local host (SSRF)
|
|
6
|
+
*/
|
|
7
|
+
export declare function assertSafeCheckoutUrl(rawUrl: string): void;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// url-guard.ts
|
|
3
|
+
// Shared SSRF / scheme guard for any URL we are about to drive Playwright to.
|
|
4
|
+
// Used by BOTH execute_payment and auto_pay_checkout — these tools navigate a
|
|
5
|
+
// headless browser to an agent-supplied URL and (for execute_payment) inject a
|
|
6
|
+
// real PAN/CVV, so the destination must be a public HTTPS host, never an
|
|
7
|
+
// internal/metadata address.
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.assertSafeCheckoutUrl = assertSafeCheckoutUrl;
|
|
10
|
+
const PRIVATE_HOST_PATTERNS = [
|
|
11
|
+
/^localhost$/i,
|
|
12
|
+
/^127\./,
|
|
13
|
+
/^10\./,
|
|
14
|
+
/^172\.(1[6-9]|2\d|3[01])\./,
|
|
15
|
+
/^192\.168\./,
|
|
16
|
+
/^169\.254\./, // link-local incl. cloud metadata 169.254.169.254
|
|
17
|
+
/^\[::1\]$/,
|
|
18
|
+
/^0\.0\.0\.0$/,
|
|
19
|
+
];
|
|
20
|
+
/**
|
|
21
|
+
* Throws if `rawUrl` is not a safe public checkout URL.
|
|
22
|
+
* - must parse as a URL
|
|
23
|
+
* - must be HTTPS (except localhost/127.0.0.1 in non-production, for local testing)
|
|
24
|
+
* - must not resolve to a private / loopback / link-local host (SSRF)
|
|
25
|
+
*/
|
|
26
|
+
function assertSafeCheckoutUrl(rawUrl) {
|
|
27
|
+
let url;
|
|
28
|
+
try {
|
|
29
|
+
url = new URL(rawUrl);
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
throw new Error(`Invalid checkout_url: ${rawUrl}`);
|
|
33
|
+
}
|
|
34
|
+
const hostname = url.hostname;
|
|
35
|
+
const isDev = process.env.NODE_ENV !== "production";
|
|
36
|
+
const isLocalDev = isDev && (hostname === "localhost" || hostname === "127.0.0.1");
|
|
37
|
+
if (isLocalDev)
|
|
38
|
+
return;
|
|
39
|
+
if (url.protocol !== "https:") {
|
|
40
|
+
throw new Error("checkout_url must use HTTPS.");
|
|
41
|
+
}
|
|
42
|
+
if (PRIVATE_HOST_PATTERNS.some((p) => p.test(hostname))) {
|
|
43
|
+
throw new Error(`SSRF blocked host: ${hostname}`);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -19,16 +19,16 @@ async function detectWeb3Payment(page, checkoutUrl) {
|
|
|
19
19
|
const mockEthereum = {
|
|
20
20
|
isMetaMask: true,
|
|
21
21
|
selectedAddress: "0x0000000000000000000000000000000000000001",
|
|
22
|
-
chainId: "
|
|
23
|
-
networkVersion: "
|
|
22
|
+
chainId: "0x2105", // Base mainnet (8453)
|
|
23
|
+
networkVersion: "8453",
|
|
24
24
|
request: async ({ method, params }) => {
|
|
25
25
|
if (method === "eth_requestAccounts" || method === "eth_accounts") {
|
|
26
26
|
return ["0x0000000000000000000000000000000000000001"];
|
|
27
27
|
}
|
|
28
28
|
if (method === "eth_chainId")
|
|
29
|
-
return "
|
|
29
|
+
return "0x2105";
|
|
30
30
|
if (method === "net_version")
|
|
31
|
-
return "
|
|
31
|
+
return "8453";
|
|
32
32
|
if (method === "eth_sendTransaction") {
|
|
33
33
|
const tx = params?.[0];
|
|
34
34
|
window["__wdkCapturedTx"] = tx;
|
|
@@ -43,7 +43,7 @@ async function detectWeb3Payment(page, checkoutUrl) {
|
|
|
43
43
|
},
|
|
44
44
|
on: (event, handler) => {
|
|
45
45
|
if (event === "connect")
|
|
46
|
-
setTimeout(() => handler({ chainId: "
|
|
46
|
+
setTimeout(() => handler({ chainId: "0x2105" }), 100);
|
|
47
47
|
if (event === "accountsChanged")
|
|
48
48
|
setTimeout(() => handler(["0x0000000000000000000000000000000000000001"]), 200);
|
|
49
49
|
},
|
|
@@ -77,14 +77,14 @@ async function detectWeb3Payment(page, checkoutUrl) {
|
|
|
77
77
|
return {
|
|
78
78
|
to: recipientMatch?.[1] ?? contractMatch?.[1] ?? null,
|
|
79
79
|
contract: contractMatch?.[1] ?? null,
|
|
80
|
-
chainId: chainMatch ? parseInt(chainMatch[1]) :
|
|
80
|
+
chainId: chainMatch ? parseInt(chainMatch[1]) : 8453,
|
|
81
81
|
uint256: uint256Match?.[1] ?? null,
|
|
82
82
|
raw: href,
|
|
83
83
|
};
|
|
84
84
|
});
|
|
85
85
|
if (eip681Result?.to) {
|
|
86
|
-
const
|
|
87
|
-
? parseInt(eip681Result.uint256) / 1_000_000
|
|
86
|
+
const usdcAmount = eip681Result.uint256
|
|
87
|
+
? parseInt(eip681Result.uint256) / 1_000_000 // USDC has 6 decimals
|
|
88
88
|
: null;
|
|
89
89
|
return {
|
|
90
90
|
detected: true,
|
|
@@ -92,9 +92,9 @@ async function detectWeb3Payment(page, checkoutUrl) {
|
|
|
92
92
|
params: {
|
|
93
93
|
to: eip681Result.to,
|
|
94
94
|
chainId: eip681Result.chainId,
|
|
95
|
-
eip681_amount:
|
|
95
|
+
eip681_amount: usdcAmount ?? undefined,
|
|
96
96
|
},
|
|
97
|
-
message: `EIP-681 payment link found. Merchant: ${eip681Result.to}, amount: ${
|
|
97
|
+
message: `EIP-681 payment link found. Merchant: ${eip681Result.to}, amount: ${usdcAmount} USDC.`,
|
|
98
98
|
};
|
|
99
99
|
}
|
|
100
100
|
// ─── Strategy B: Wait for eth_sendTransaction after clicking "Pay" ────
|
|
@@ -131,7 +131,7 @@ async function detectWeb3Payment(page, checkoutUrl) {
|
|
|
131
131
|
to: capturedTx["to"],
|
|
132
132
|
value: capturedTx["value"],
|
|
133
133
|
data: capturedTx["data"],
|
|
134
|
-
chainId:
|
|
134
|
+
chainId: 8453,
|
|
135
135
|
},
|
|
136
136
|
message: `Web3 payment detected via eth_sendTransaction. Recipient: ${capturedTx["to"]}`,
|
|
137
137
|
};
|
|
@@ -142,7 +142,7 @@ async function detectWeb3Payment(page, checkoutUrl) {
|
|
|
142
142
|
detected: false,
|
|
143
143
|
method: null,
|
|
144
144
|
params: null,
|
|
145
|
-
message: "No Web3 payment gateway detected. Will proceed with JIT
|
|
145
|
+
message: "No Web3 payment gateway detected. Will proceed with JIT virtual card (Fiat) route.",
|
|
146
146
|
};
|
|
147
147
|
}
|
|
148
148
|
catch (err) {
|
|
@@ -33,11 +33,12 @@ async function fillCheckoutForm(checkoutUrl, cardData, existingPage, hints) {
|
|
|
33
33
|
if (err instanceof with_timeout_js_1.TimeoutError) {
|
|
34
34
|
return {
|
|
35
35
|
success: false,
|
|
36
|
+
status: 'error',
|
|
36
37
|
message: `Checkout timed out after ${CHECKOUT_HARD_TIMEOUT_MS / 1000}s. The merchant page may be too slow or blocking automation.`,
|
|
37
38
|
};
|
|
38
39
|
}
|
|
39
40
|
const errMsg = err instanceof Error ? err.message : String(err);
|
|
40
|
-
return { success: false, message: `Payment failed: ${errMsg}` };
|
|
41
|
+
return { success: false, status: 'error', message: `Payment failed: ${errMsg}` };
|
|
41
42
|
}
|
|
42
43
|
finally {
|
|
43
44
|
if (browser)
|
|
@@ -96,6 +97,101 @@ async function tryFillField(page, selectors, value) {
|
|
|
96
97
|
}
|
|
97
98
|
return false;
|
|
98
99
|
}
|
|
100
|
+
// ============================================================
|
|
101
|
+
// CONFIRMATION DETECTION — the core of "filled ≠ paid"
|
|
102
|
+
// After Pay is clicked we must observe a REAL outcome before
|
|
103
|
+
// telling the caller it succeeded. A blind waitForTimeout()
|
|
104
|
+
// followed by success:true is what made receipts fake.
|
|
105
|
+
// ============================================================
|
|
106
|
+
const CONFIRM_WAIT_MS = 12_000; // total budget to watch for an outcome (well within the 60s hard cap)
|
|
107
|
+
const CONFIRM_POLL_MS = 750;
|
|
108
|
+
// URL of a post-payment page (we additionally require the URL to have moved off the checkout page)
|
|
109
|
+
const SUCCESS_URL_RE = /thank[_-]?you|order[-_]?(?:received|confirmation|confirmed|complete|placed)|\/orders?\/|\/receipts?\/|payment[_-]?success|checkout\/success|confirmation/i;
|
|
110
|
+
// Strong success phrases — deliberately specific so a checkout "Order summary" panel does NOT trigger it
|
|
111
|
+
const SUCCESS_TEXT_RE = /thank you for your (?:order|purchase|payment)|your order (?:has been|is) (?:confirmed|placed|received)|order (?:confirmed|placed successfully)|payment (?:successful|received|approved|complete)|purchase complete|order number[:\s#]/i;
|
|
112
|
+
// Decline / failure signals
|
|
113
|
+
const DECLINE_TEXT_RE = /(?:card|payment|transaction) (?:was )?declined|payment (?:failed|unsuccessful|was not|could not be processed)|(?:invalid|incorrect) (?:card|cvv|cvc|security code|card number|expiry)|insufficient funds|try (?:a |another )?(?:different )?card|card (?:was )?not accepted|we (?:couldn'?t|could not) process/i;
|
|
114
|
+
// Real order/confirmation number to use as receipt_id (only when confirmed)
|
|
115
|
+
const RECEIPT_RE = /(?:order|confirmation|receipt)\s*(?:number|no\.?|#)?[:\s#]*([A-Z0-9][A-Z0-9-]{3,})/i;
|
|
116
|
+
/**
|
|
117
|
+
* Watches the page after Pay is clicked and classifies the real outcome.
|
|
118
|
+
* Polls URL + visible text for a confirmation or decline signal until a
|
|
119
|
+
* signal is seen or CONFIRM_WAIT_MS elapses. Default (no signal) is the
|
|
120
|
+
* SAFE 'unconfirmed' — caller must NOT burn the token on that.
|
|
121
|
+
*/
|
|
122
|
+
async function detectPaymentOutcome(page, checkoutUrl) {
|
|
123
|
+
const checkoutPath = (() => {
|
|
124
|
+
try {
|
|
125
|
+
const u = new URL(checkoutUrl);
|
|
126
|
+
return u.origin + u.pathname;
|
|
127
|
+
}
|
|
128
|
+
catch {
|
|
129
|
+
return checkoutUrl.split('?')[0];
|
|
130
|
+
}
|
|
131
|
+
})();
|
|
132
|
+
const deadline = Date.now() + CONFIRM_WAIT_MS;
|
|
133
|
+
while (Date.now() < deadline) {
|
|
134
|
+
let currentUrl = '';
|
|
135
|
+
let bodyText = '';
|
|
136
|
+
try {
|
|
137
|
+
currentUrl = page.url();
|
|
138
|
+
bodyText = await page.evaluate(() => document.body?.innerText ?? '');
|
|
139
|
+
}
|
|
140
|
+
catch {
|
|
141
|
+
// page is mid-navigation — wait and retry
|
|
142
|
+
await page.waitForTimeout(CONFIRM_POLL_MS);
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
// Decline is a definitive negative — check first.
|
|
146
|
+
if (DECLINE_TEXT_RE.test(bodyText)) {
|
|
147
|
+
return { kind: 'declined' };
|
|
148
|
+
}
|
|
149
|
+
const movedOff = (() => {
|
|
150
|
+
try {
|
|
151
|
+
const u = new URL(currentUrl);
|
|
152
|
+
return (u.origin + u.pathname) !== checkoutPath;
|
|
153
|
+
}
|
|
154
|
+
catch {
|
|
155
|
+
return false;
|
|
156
|
+
}
|
|
157
|
+
})();
|
|
158
|
+
const urlSignal = movedOff && SUCCESS_URL_RE.test(currentUrl);
|
|
159
|
+
const textSignal = SUCCESS_TEXT_RE.test(bodyText);
|
|
160
|
+
if (urlSignal || textSignal) {
|
|
161
|
+
const m = bodyText.match(RECEIPT_RE);
|
|
162
|
+
return { kind: 'confirmed', receipt_id: m?.[1] };
|
|
163
|
+
}
|
|
164
|
+
await page.waitForTimeout(CONFIRM_POLL_MS);
|
|
165
|
+
}
|
|
166
|
+
// No clear signal within the window — safe default, caller will NOT burn.
|
|
167
|
+
return { kind: 'unconfirmed' };
|
|
168
|
+
}
|
|
169
|
+
/** Map a detected outcome to the PaymentResult shape (receipt only ever real). */
|
|
170
|
+
function outcomeToResult(outcome, fieldNote) {
|
|
171
|
+
switch (outcome.kind) {
|
|
172
|
+
case 'confirmed':
|
|
173
|
+
return {
|
|
174
|
+
success: true,
|
|
175
|
+
status: 'confirmed',
|
|
176
|
+
message: `Payment confirmed by the merchant. ${fieldNote}`,
|
|
177
|
+
receipt_id: outcome.receipt_id,
|
|
178
|
+
};
|
|
179
|
+
case 'declined':
|
|
180
|
+
return {
|
|
181
|
+
success: false,
|
|
182
|
+
status: 'declined',
|
|
183
|
+
message: `Payment was declined by the merchant. ${fieldNote}`,
|
|
184
|
+
};
|
|
185
|
+
case 'unconfirmed':
|
|
186
|
+
default:
|
|
187
|
+
return {
|
|
188
|
+
success: false,
|
|
189
|
+
status: 'unconfirmed',
|
|
190
|
+
message: `Card details were submitted but no order confirmation was detected. ${fieldNote} ` +
|
|
191
|
+
`The token was NOT burned — verify whether the order went through before retrying (avoid double-charge).`,
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
}
|
|
99
195
|
/** Internal implementation — called by fillCheckoutForm inside a timeout wrapper */
|
|
100
196
|
async function _fillCheckoutFormInner(page, checkoutUrl, cardData, hints) {
|
|
101
197
|
try {
|
|
@@ -103,9 +199,27 @@ async function _fillCheckoutFormInner(page, checkoutUrl, cardData, hints) {
|
|
|
103
199
|
// Prevents double-navigate which would reload page and lose cart/session state.
|
|
104
200
|
const currentUrl = page.url();
|
|
105
201
|
const baseCheckoutUrl = checkoutUrl.split("?")[0];
|
|
202
|
+
let landedUrl = currentUrl;
|
|
106
203
|
if (!currentUrl || currentUrl === "about:blank" || !currentUrl.startsWith(baseCheckoutUrl)) {
|
|
204
|
+
// ⚠️ COLD BROWSER: execute_payment lands here with a fresh, COOKIELESS context — none of the
|
|
205
|
+
// agent's own browsing session carries over. This only works when checkout_url is RESUMABLE
|
|
206
|
+
// cold: the cart, final total and card fields must be reconstructable server-side from the URL
|
|
207
|
+
// alone (Shopify /checkouts/c/<token>, Etsy token URLs). For cookie/session-bound carts the
|
|
208
|
+
// page loads empty or redirects to /cart|/login and no card fields will be found.
|
|
107
209
|
await page.goto(checkoutUrl, { waitUntil: "domcontentloaded", timeout: 20_000 });
|
|
210
|
+
landedUrl = page.url();
|
|
108
211
|
}
|
|
212
|
+
// Heuristic: did the cold load bounce off the intended checkout (cart/session not carried)?
|
|
213
|
+
const landedElsewhere = (() => {
|
|
214
|
+
try {
|
|
215
|
+
const want = new URL(checkoutUrl);
|
|
216
|
+
const got = new URL(landedUrl);
|
|
217
|
+
return got.host !== want.host || got.pathname.split("/")[1] !== want.pathname.split("/")[1];
|
|
218
|
+
}
|
|
219
|
+
catch {
|
|
220
|
+
return false;
|
|
221
|
+
}
|
|
222
|
+
})();
|
|
109
223
|
// ============================================================
|
|
110
224
|
// STRATEGY 0: Pre-steps — click to open/reveal the payment form
|
|
111
225
|
// Agent provides selectors to click BEFORE filling (e.g. accordion, tab, modal)
|
|
@@ -199,22 +313,31 @@ async function _fillCheckoutFormInner(page, checkoutUrl, cardData, hints) {
|
|
|
199
313
|
const hintSuccesses = hintFillResults.filter(Boolean).length;
|
|
200
314
|
if (hintSuccesses > 0) {
|
|
201
315
|
filledFields += hintSuccesses;
|
|
316
|
+
const modeNote = `${filledFields} fields injected via agent hints${hints.iframe_selector ? ' (iframe mode)' : ''}.`;
|
|
317
|
+
// Must actually submit to have any chance of a real payment.
|
|
318
|
+
let clicked = false;
|
|
202
319
|
if (hints.submit_selector) {
|
|
203
320
|
try {
|
|
204
321
|
const btn = await page.$(hints.submit_selector);
|
|
205
322
|
if (btn && await btn.isVisible()) {
|
|
206
323
|
await btn.click();
|
|
207
|
-
|
|
324
|
+
clicked = true;
|
|
208
325
|
}
|
|
209
326
|
}
|
|
210
327
|
catch { /* non-fatal */ }
|
|
211
328
|
}
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
329
|
+
if (!clicked) {
|
|
330
|
+
// Fields filled but no submit happened → NOT a payment. Do not fabricate success.
|
|
331
|
+
return {
|
|
332
|
+
success: false,
|
|
333
|
+
status: 'not_submitted',
|
|
334
|
+
message: `${modeNote} No submit/pay button was clicked (provide a submit_selector via get_merchant_hints). ` +
|
|
335
|
+
`The order was NOT placed and the token was NOT burned.`,
|
|
336
|
+
};
|
|
337
|
+
}
|
|
338
|
+
// Submitted — observe the real outcome before declaring success.
|
|
339
|
+
const outcome = await detectPaymentOutcome(page, checkoutUrl);
|
|
340
|
+
return outcomeToResult(outcome, modeNote);
|
|
218
341
|
}
|
|
219
342
|
// hints provided but nothing matched → fall through to Strategy 1
|
|
220
343
|
}
|
|
@@ -374,7 +497,14 @@ async function _fillCheckoutFormInner(page, checkoutUrl, cardData, hints) {
|
|
|
374
497
|
if (filledFields === 0) {
|
|
375
498
|
return {
|
|
376
499
|
success: false,
|
|
377
|
-
|
|
500
|
+
status: 'no_fields',
|
|
501
|
+
message: "Could not detect any credit card fields on this page. " +
|
|
502
|
+
(landedElsewhere
|
|
503
|
+
? `The checkout URL redirected to ${landedUrl} — the cart/session likely did not carry over. ` +
|
|
504
|
+
"Z-Zero opens its OWN fresh browser, so checkout_url must be resumable on its own " +
|
|
505
|
+
"(e.g. a Shopify/Etsy checkout-token URL), not a page that needs the agent's logged-in session. "
|
|
506
|
+
: "") +
|
|
507
|
+
"The checkout form may use an unsupported format, or the page requires a logged-in session.",
|
|
378
508
|
};
|
|
379
509
|
}
|
|
380
510
|
// ============================================================
|
|
@@ -397,21 +527,26 @@ async function _fillCheckoutFormInner(page, checkoutUrl, cardData, hints) {
|
|
|
397
527
|
break;
|
|
398
528
|
}
|
|
399
529
|
}
|
|
400
|
-
|
|
401
|
-
if (clicked) {
|
|
402
|
-
|
|
530
|
+
const fieldNote = `${filledFields} fields injected.`;
|
|
531
|
+
if (!clicked) {
|
|
532
|
+
// Form filled but no Pay button found → the order was never submitted.
|
|
533
|
+
return {
|
|
534
|
+
success: false,
|
|
535
|
+
status: 'not_submitted',
|
|
536
|
+
message: `${fieldNote} No Pay/Submit button could be found or clicked, so the order was NOT placed ` +
|
|
537
|
+
`and the token was NOT burned. Use get_merchant_hints to supply a submit_selector.`,
|
|
538
|
+
};
|
|
403
539
|
}
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
receipt_id: receiptId,
|
|
409
|
-
};
|
|
540
|
+
// Submitted — observe the real outcome (confirmed / declined / unconfirmed)
|
|
541
|
+
// instead of blindly assuming success. receipt_id is set ONLY if a real one is scraped.
|
|
542
|
+
const outcome = await detectPaymentOutcome(page, checkoutUrl);
|
|
543
|
+
return outcomeToResult(outcome, fieldNote);
|
|
410
544
|
}
|
|
411
545
|
catch (error) {
|
|
412
546
|
const errMsg = error instanceof Error ? error.message : String(error);
|
|
413
547
|
return {
|
|
414
548
|
success: false,
|
|
549
|
+
status: 'error',
|
|
415
550
|
message: `Payment failed: ${errMsg}`,
|
|
416
551
|
};
|
|
417
552
|
}
|
package/dist/types.d.ts
CHANGED
|
@@ -31,8 +31,10 @@ export interface CardData {
|
|
|
31
31
|
error?: string;
|
|
32
32
|
message?: string;
|
|
33
33
|
}
|
|
34
|
+
export type PaymentOutcome = 'confirmed' | 'declined' | 'unconfirmed' | 'not_submitted' | 'no_fields' | 'error';
|
|
34
35
|
export interface PaymentResult {
|
|
35
36
|
success: boolean;
|
|
37
|
+
status: PaymentOutcome;
|
|
36
38
|
message: string;
|
|
37
39
|
receipt_id?: string;
|
|
38
40
|
amount?: number;
|
package/dist/wdk_backend.js
CHANGED
|
@@ -4,10 +4,10 @@
|
|
|
4
4
|
// Exports IDENTICAL function signatures so index.ts can swap backends without any changes.
|
|
5
5
|
//
|
|
6
6
|
// Key differences from custodial api_backend.ts:
|
|
7
|
-
// - getBalanceRemote() → reads
|
|
8
|
-
// - getDepositAddressesRemote() → returns
|
|
9
|
-
// - issueTokenRemote() → sends
|
|
10
|
-
// - cancelTokenRemote() → triggers on-chain
|
|
7
|
+
// - getBalanceRemote() → reads stablecoin (USDC) balance on-chain on Base (not from Supabase wallets table)
|
|
8
|
+
// - getDepositAddressesRemote() → returns the Base wallet address (not HD custodial address)
|
|
9
|
+
// - issueTokenRemote() → sends USDC on Base first, THEN issues JIT card
|
|
10
|
+
// - cancelTokenRemote() → triggers on-chain USDC refund back to user's Base wallet
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.listCardsRemote = listCardsRemote;
|
|
13
13
|
exports.getBalanceRemote = getBalanceRemote;
|
|
@@ -63,10 +63,14 @@ async function internalApiRequest(endpoint, method, body) {
|
|
|
63
63
|
}
|
|
64
64
|
const url = `${API_BASE_URL.replace(/\/$/, '')}${endpoint}`;
|
|
65
65
|
try {
|
|
66
|
+
// Two independent factors on these PAN/burn endpoints:
|
|
67
|
+
// - x-internal-secret: coarse platform gate (shared deploy secret)
|
|
68
|
+
// - Authorization: Bearer <Passport Key>: per-user identity → server enforces token ownership
|
|
66
69
|
const res = await fetch(url, {
|
|
67
70
|
method,
|
|
68
71
|
headers: {
|
|
69
72
|
"x-internal-secret": INTERNAL_SECRET,
|
|
73
|
+
"Authorization": `Bearer ${(0, key_store_js_1.getPassportKey)()}`,
|
|
70
74
|
"Content-Type": "application/json",
|
|
71
75
|
"X-MCP-Version": version_js_1.CURRENT_MCP_VERSION,
|
|
72
76
|
},
|
|
@@ -92,69 +96,67 @@ async function listCardsRemote() {
|
|
|
92
96
|
return await apiRequest('/api/tokens/cards', 'GET');
|
|
93
97
|
}
|
|
94
98
|
// ──────────────────────────────────────────────────────────────────────────────
|
|
95
|
-
// Balance: On-chain
|
|
99
|
+
// Balance: On-chain USDC (stablecoin) on Base via Dashboard API
|
|
96
100
|
// ──────────────────────────────────────────────────────────────────────────────
|
|
97
101
|
async function getBalanceRemote(cardAlias) {
|
|
98
|
-
// Call Dashboard to get
|
|
99
|
-
// finds connected
|
|
102
|
+
// Call Dashboard to get the agent's Base wallet balance (Dashboard resolves user from
|
|
103
|
+
// passport key, finds the connected Base wallet, queries on-chain USDC balance)
|
|
100
104
|
const data = await apiRequest('/api/wdk/balance', 'GET');
|
|
101
105
|
if (data?.error) {
|
|
102
106
|
return {
|
|
103
107
|
error: true,
|
|
104
|
-
message: '
|
|
108
|
+
message: 'Base wallet not connected. Create one at https://www.clawcard.store/dashboard/agent-wallet'
|
|
105
109
|
};
|
|
106
110
|
}
|
|
107
111
|
return {
|
|
108
|
-
wallet_balance: data.balance_usdt,
|
|
109
|
-
currency: '
|
|
110
|
-
chain: data.chain || '
|
|
112
|
+
wallet_balance: data.base_usdc_balance ?? data.balance_usdt,
|
|
113
|
+
currency: 'USDC',
|
|
114
|
+
chain: data.chain || 'base',
|
|
111
115
|
address: data.address,
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
note: `Non-custodial WDK wallet. On-chain USDT balance. Address: ${data.address}`
|
|
116
|
+
mode: 'base_onchain',
|
|
117
|
+
note: `Base smart-account wallet. On-chain stablecoin (USDC) balance. Address: ${data.address}`
|
|
115
118
|
};
|
|
116
119
|
}
|
|
117
120
|
// ──────────────────────────────────────────────────────────────────────────────
|
|
118
|
-
// Deposit
|
|
121
|
+
// Deposit Address: the agent's Base smart-account address (single chain)
|
|
119
122
|
// ──────────────────────────────────────────────────────────────────────────────
|
|
120
123
|
async function getDepositAddressesRemote() {
|
|
121
124
|
const data = await apiRequest('/api/wdk/balance', 'GET');
|
|
122
125
|
if (data?.error) {
|
|
123
126
|
return {
|
|
124
127
|
error: true,
|
|
125
|
-
message: '
|
|
128
|
+
message: 'Base wallet not connected. Create one at https://www.clawcard.store/dashboard/agent-wallet'
|
|
126
129
|
};
|
|
127
130
|
}
|
|
131
|
+
const balance = data.base_usdc_balance ?? data.balance_usdt;
|
|
128
132
|
return {
|
|
129
|
-
cards: [{ alias: '
|
|
133
|
+
cards: [{ alias: 'base-wallet', balance, currency: 'USDC' }],
|
|
130
134
|
deposit_addresses: {
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
note: 'Send USDT to your WDK wallet. Gasless via ERC-4337 Paymaster (Ethereum) or GasFree (Tron).'
|
|
135
|
+
base: data.address,
|
|
136
|
+
note: 'Send USDC (or any supported stablecoin) on Base to your wallet. Gasless spending via the Coinbase Paymaster.'
|
|
134
137
|
},
|
|
135
138
|
wdk_wallet: {
|
|
136
139
|
address: data.address,
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
balance_usdt: data.balance_usdt
|
|
140
|
+
chain: data.chain || 'base',
|
|
141
|
+
balance_usdt: balance
|
|
140
142
|
}
|
|
141
143
|
};
|
|
142
144
|
}
|
|
143
145
|
// ──────────────────────────────────────────────────────────────────────────────
|
|
144
|
-
// Issue Token: On-chain
|
|
146
|
+
// Issue Token: On-chain USDC (Base) payment → JIT card
|
|
145
147
|
// ──────────────────────────────────────────────────────────────────────────────
|
|
146
148
|
async function issueTokenRemote(cardAlias, amount, merchant) {
|
|
147
|
-
//
|
|
149
|
+
// Base AA Flow (reversed from custodial):
|
|
148
150
|
// 1. Create Partner card first (reservation)
|
|
149
|
-
// 2. Send
|
|
151
|
+
// 2. Send USDC on Base from the agent's wallet → system wallet (gasless via Coinbase Paymaster)
|
|
150
152
|
// 3. Dashboard verifies on-chain tx, activates token
|
|
151
153
|
// This is safe: if on-chain tx fails, Dashboard auto-cancels the card reservation.
|
|
152
154
|
const data = await apiRequest('/api/tokens/issue', 'POST', {
|
|
153
155
|
card_alias: cardAlias,
|
|
154
156
|
amount,
|
|
155
157
|
merchant,
|
|
156
|
-
device_fingerprint: `mcp-
|
|
157
|
-
network_id: process.env.NETWORK_ID || "
|
|
158
|
+
device_fingerprint: `mcp-base-${process.platform}-${process.arch}`,
|
|
159
|
+
network_id: process.env.NETWORK_ID || "base-usdc",
|
|
158
160
|
session_id: `wdk-${Math.random().toString(36).substring(7)}`,
|
|
159
161
|
wallet_mode: 'wdk', // Signals Dashboard to use on-chain payment path
|
|
160
162
|
});
|
|
@@ -208,12 +210,12 @@ async function burnTokenRemote(token, receipt_id) {
|
|
|
208
210
|
return !!data && !data.error;
|
|
209
211
|
}
|
|
210
212
|
// ──────────────────────────────────────────────────────────────────────────────
|
|
211
|
-
// Cancel Token: Refund
|
|
213
|
+
// Cancel Token: Refund USDC back to user's Base wallet on-chain
|
|
212
214
|
// ──────────────────────────────────────────────────────────────────────────────
|
|
213
215
|
async function cancelTokenRemote(token) {
|
|
214
216
|
const data = await apiRequest('/api/tokens/cancel', 'POST', {
|
|
215
217
|
token,
|
|
216
|
-
wallet_mode: 'wdk', // Triggers on-chain
|
|
218
|
+
wallet_mode: 'wdk', // Triggers on-chain USDC refund
|
|
217
219
|
});
|
|
218
220
|
if (data?.error)
|
|
219
221
|
return data;
|
|
@@ -221,12 +223,13 @@ async function cancelTokenRemote(token) {
|
|
|
221
223
|
success: !!data,
|
|
222
224
|
refunded_amount: data?.refunded_amount || 0,
|
|
223
225
|
tx_hash: data?.tx_hash || null, // On-chain refund tx hash
|
|
224
|
-
note: '
|
|
226
|
+
note: 'Stablecoin (USDC) refunded on-chain to your Base wallet.'
|
|
225
227
|
};
|
|
226
228
|
}
|
|
227
229
|
// ──────────────────────────────────────────────────────────────────────────────
|
|
228
230
|
// Refund Underspend: Logged only (full refund logic handled in burnTokenRemote)
|
|
229
231
|
// ──────────────────────────────────────────────────────────────────────────────
|
|
230
232
|
async function refundUnderspendRemote(token, actualSpent) {
|
|
231
|
-
console.
|
|
233
|
+
// console.error (NOT console.log) — stdout is the MCP stdio transport; logging there corrupts the protocol.
|
|
234
|
+
console.error(`[WDK MCP] Token ${token} burned. Actual spent: $${actualSpent}. On-chain refund handled by Dashboard.`);
|
|
232
235
|
}
|
package/package.json
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "z-zero-mcp-server",
|
|
3
|
-
"version": "1.2
|
|
4
|
-
"description": "Z-ZERO MCP Server — Secure JIT Virtual Card Payment tools for AI Agents (Claude, Cursor, AutoGPT) — Real Single-Use
|
|
3
|
+
"version": "1.3.2",
|
|
4
|
+
"description": "Z-ZERO MCP Server — Secure JIT Virtual Card Payment tools for AI Agents (Claude, Cursor, AutoGPT) — Real Single-Use Virtual Cards, settled in USDC on Base",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
|
-
"bin":
|
|
6
|
+
"bin": {
|
|
7
|
+
"z-zero-mcp-server": "dist/index.js"
|
|
8
|
+
},
|
|
7
9
|
"scripts": {
|
|
8
10
|
"build": "tsc",
|
|
9
11
|
"start": "node dist/index.js",
|
|
@@ -24,6 +26,14 @@
|
|
|
24
26
|
],
|
|
25
27
|
"author": "Z-ZERO",
|
|
26
28
|
"license": "MIT",
|
|
29
|
+
"repository": {
|
|
30
|
+
"type": "git",
|
|
31
|
+
"url": "git+https://github.com/Dempty-glitch/Z-Zero-mcp.git"
|
|
32
|
+
},
|
|
33
|
+
"homepage": "https://www.clawcard.store/docs/agent",
|
|
34
|
+
"bugs": {
|
|
35
|
+
"url": "https://github.com/Dempty-glitch/Z-Zero-mcp/issues"
|
|
36
|
+
},
|
|
27
37
|
"files": [
|
|
28
38
|
"dist/",
|
|
29
39
|
"README.md"
|