|
| 1 | +--- |
| 2 | +title: "forwardToArbiter()" |
| 3 | +description: "Forward settlement data to an arbiter service for quality evaluation" |
| 4 | +icon: "arrow-right" |
| 5 | +--- |
| 6 | + |
| 7 | +The `forwardToArbiter()` function creates an `onAfterSettle` hook that forwards the response body and payment payload to an arbiter service. It runs fire-and-forget so it never blocks the response to the client. |
| 8 | + |
| 9 | +- Only fires for successful **commerce** scheme settlements |
| 10 | +- POSTs `{ responseBody, transaction, paymentPayload }` to `{arbiterUrl}/verify` |
| 11 | +- Errors are silently caught so an unavailable arbiter cannot break the payment flow |
| 12 | + |
| 13 | +## Usage |
| 14 | + |
| 15 | +```typescript |
| 16 | +import { forwardToArbiter } from '@x402r/helpers'; |
| 17 | + |
| 18 | +const resourceServer = new x402ResourceServer(facilitatorClient) |
| 19 | + .register(networkId, new EscrowServerScheme()) |
| 20 | + .onAfterSettle( |
| 21 | + forwardToArbiter('http://arbiter:3001') |
| 22 | + ); |
| 23 | +``` |
| 24 | + |
| 25 | +## Function signature |
| 26 | + |
| 27 | +```typescript |
| 28 | +function forwardToArbiter( |
| 29 | + arbiterUrl: string, |
| 30 | + options?: ForwardToArbiterOptions |
| 31 | +): (context: SettleResultContext) => Promise<void> |
| 32 | +``` |
| 33 | + |
| 34 | +### Parameters |
| 35 | + |
| 36 | +| Parameter | Type | Description | |
| 37 | +|-----------|------|-------------| |
| 38 | +| `arbiterUrl` | `string` | Base URL of your arbiter service (e.g. `http://arbiter:3001`) | |
| 39 | +| `options` | `ForwardToArbiterOptions` | Optional configuration (see below) | |
| 40 | + |
| 41 | +### Options |
| 42 | + |
| 43 | +```typescript |
| 44 | +interface ForwardToArbiterOptions { |
| 45 | + /** Custom error handler. Defaults to `console.warn`. */ |
| 46 | + onError?: (error: unknown) => void; |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +## Payload shape |
| 51 | + |
| 52 | +When a commerce settlement succeeds, the hook POSTs the following JSON to `{arbiterUrl}/verify`: |
| 53 | + |
| 54 | +```typescript |
| 55 | +{ |
| 56 | + responseBody: string; // UTF-8 encoded response body |
| 57 | + transaction: string; // Settlement transaction hash |
| 58 | + paymentPayload: { |
| 59 | + x402Version: number; |
| 60 | + accepted: { |
| 61 | + scheme: string; // e.g. "commerce" |
| 62 | + network: string; // e.g. "eip155:84532" |
| 63 | + // ...other accepted fields |
| 64 | + }; |
| 65 | + payload: { |
| 66 | + paymentInfo: { |
| 67 | + operator: string; |
| 68 | + payer: string; |
| 69 | + receiver: string; |
| 70 | + // ...full PaymentInfo |
| 71 | + }; |
| 72 | + }; |
| 73 | + }; |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +<Tip> |
| 78 | +Arbiters that need `paymentInfo` for `release()` can read it directly from `paymentPayload.payload.paymentInfo` — no extra RPC call needed. |
| 79 | +</Tip> |
| 80 | + |
| 81 | +## Error handling |
| 82 | + |
| 83 | +By default, fetch errors are logged with `console.warn`. You can override this with a custom handler: |
| 84 | + |
| 85 | +```typescript |
| 86 | +import { forwardToArbiter } from '@x402r/helpers'; |
| 87 | +
|
| 88 | +const resourceServer = new x402ResourceServer(facilitatorClient) |
| 89 | + .register(networkId, new EscrowServerScheme()) |
| 90 | + .onAfterSettle( |
| 91 | + forwardToArbiter('http://arbiter:3001', { |
| 92 | + onError: (err) => sentry.captureException(err), |
| 93 | + }) |
| 94 | + ); |
| 95 | +``` |
| 96 | + |
| 97 | +Errors are wrapped in an `X402rError` with the arbiter URL and request details for easier debugging. |
| 98 | + |
| 99 | +## Skipped scenarios |
| 100 | + |
| 101 | +The hook silently returns without making a request when: |
| 102 | + |
| 103 | +- The settlement was not successful (`context.result.success === false`) |
| 104 | +- The scheme is not `commerce` (e.g. direct or other custom schemes) |
| 105 | +- No response body is available in the transport context |
| 106 | + |
| 107 | +## Address re-exports |
| 108 | + |
| 109 | +The `@x402r/helpers` package re-exports chain-invariant address constants from `@x402r/core` for convenience: |
| 110 | + |
| 111 | +```typescript |
| 112 | +import { |
| 113 | + authCaptureEscrow, |
| 114 | + tokenCollector, |
| 115 | + protocolFeeConfig, |
| 116 | + arbiterRegistry, |
| 117 | + receiverRefundCollector, |
| 118 | + usdcTvlLimit, |
| 119 | + factories, |
| 120 | + conditions, |
| 121 | + getChainConfig, |
| 122 | + supportedChainIds, |
| 123 | +} from '@x402r/helpers'; |
| 124 | +``` |
| 125 | + |
| 126 | +These are the same CREATE3 addresses available from `@x402r/core`. You can import from either package depending on which you already have installed. |
| 127 | + |
| 128 | +## Next steps |
| 129 | + |
| 130 | +<CardGroup cols={2}> |
| 131 | + <Card title="refundable()" icon="wrench" href="/sdk/helpers/refundable"> |
| 132 | + Configure escrow options and fee bounds. |
| 133 | + </Card> |
| 134 | + <Card title="Arbiter SDK" icon="gavel" href="/sdk/arbiter/quickstart"> |
| 135 | + Build an arbiter that processes forwarded settlements. |
| 136 | + </Card> |
| 137 | + <Card title="Examples" icon="code" href="/sdk/examples"> |
| 138 | + See working merchant server examples. |
| 139 | + </Card> |
| 140 | +</CardGroup> |
0 commit comments