跳到正文

付款 SDK

解析 PaymentRef、验证 signed offer、请求 EIP-3009 签名并提交 x402 支付。

x402-paysdk/pay 负责付款方流程,包括读取支付要求、提交支付、验证收据、恢复结算和查询交易状态。

钱包适配器

围绕钱包库实现精简的 WalletSigner 接口:

TypeScript
import type { WalletSigner } from 'x402-paysdk/pay';const wallet: WalletSigner = { address: account.address, signTypedData: (typedData) => walletClient.signTypedData(typedData)};

不要向网关发送私钥、助记词或 signer 实例。

支付固定金额引用

TypeScript
import { getPaymentRequired, pay } from 'x402-paysdk/pay';const required = await getPaymentRequired(refUrl);console.log(required.accepts[0].amount);const settlement = await pay({ refUrl, paymentId: `checkout_${checkoutId}`, wallet});

开放金额引用需要传入 token 最小单位表示的 amountpaymentId 应当随机生成,或唯一绑定到一次业务支付尝试。

验证终局性

TypeScript
import { getTxStatus, verifyReceipt } from 'x402-paysdk/pay';const status = await getTxStatus(gateway, settlement.txHash);const verification = await verifyReceipt({ gateway, signedReceipt: settlement.signedPaymentReceipt!, receipt: settlement.receipt!, expectedPayTo, expectedAmount, minConfirmations: 3});

Signed receipt 用于验证网关输出,链上验证用于确认结算终局性。