[错误]: 当付费请求返回 HTTP 400/500 时, Axios 会跳过 onPaymentResponse 钩子
import assert from 'node:assert/strict'; import { once } from 'node:events'; import { createServer } from 'node:http'; import axios from 'axios'; import { x402Client } from '@x402/core/client'; import { wrapAxiosWithPayment } from '@x402/axios'; import { wrapFetchWithPayment } from '@x402/fetch';
const encode = value => Buffer.from(JSON.stringify(value)).toString('base64');
const requirements = { scheme: 'exact', network: 'eip155:84532', amount: '1', asset: '0x0000000000000000000000000000000000000001', payTo: '0x0000000000000000000000000000000000000002', maxTimeoutSeconds: 60, extra: {} };
const server = createServer((req, res) => {
req.resume();
const paidStatus = Number(req.url.slice(1));
if (!req.headers['payment-signature']) {
const declaration = { x402Version: 2, resource: { url: http://${req.headers.host}${req.url}, description: 'local test', mimeType: 'application/json' }, accepts: [requirements] };
res.writeHead(402, { 'PAYMENT-REQUIRED': encode(declaration) });
} else {
res.writeHead(paidStatus, { 'PAYMENT-RESPONSE': encode({ success: false, errorReason: 'local_test_failure', network: requirements.network, transaction: '' }) });
}
res.end('{}');
});
server.listen(0, '127.0.0.1');
await once(server,
…
内容来源: x402-foundation/x402