Server 在收到带有X-PAYMENT头信息的请求之后,会去校验这个 Payload 是否是有效的(5),校验的角色就是 Faciltator。Faciltator 可以自己写,也可以用已有的服务。Faciltator 校验之后返回给 Server 校验结果(6)。
如果校验失败,那么 Server 就会再次返回 402 状态码,让 Client 重新组装 Payload。如果校验成功,则继续(7)。
之后 Server 将结算请求发送给 Faciltator(8),由 Faciltator 进行结算并发送到链上(9)。Faciltator 在交易确认之后(10),将结算的结果返回给 Server(11)。
Server 将请求的资源结果放在正文中返回给用户,并且在X-PAYMENT-RESPONSE头里面带上 Base64 编码后的结算结果。
付款要求结构约定
在第(2)步的时候,Server 会返回一个付款要求的结构,现在规定这个付款要求需要包含以下内容:
1 2 3 4 5 6 7 8 9 10
{ // Version of the x402 payment protocol x402Version: int,
// List of payment requirements that the resource server accepts. A resource server may accept on multiple chains, or in multiple currencies. accepts: [paymentRequirements]
// Message from the resource server to the client to communicate errors in processing payment error: string }
{ // Scheme of the payment protocol to use scheme: string;
// Network of the blockchain to send payment on network: string;
// Maximum amount required to pay for the resource in atomic units of the asset maxAmountRequired: uint256 asstring;
// URL of resource to pay for resource: string;
// Description of the resource description: string;
// MIME type of the resource response mimeType: string;
// Output schema of the resource response outputSchema?: object | null;
// Address to pay value to payTo: string;
// Maximum time in seconds for the resource server to respond maxTimeoutSeconds: number;
// Address of the EIP-3009 compliant ERC20 contract asset: string;
// Extra information about the payment details specific to the scheme // For `exact` scheme on a EVM network, expects extra to contain the records `name` and `version` pertaining to asset extra: object | null; }
这里主要解释一下 scheme。scheme 主要是为了约定支付的形式,比如upto或者exact,前者规定最多可以转多少代币,后者规定这次精准转移多少代币,当然,这些都可以根据 Server 和 Faciltator 自行来新增,只需要在验证的时候可以满足规则就可以。