@relai-fi/x402 0.6.10 → 0.6.11-rc.0

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/README.md CHANGED
@@ -1751,6 +1751,8 @@ import {
1751
1751
  getPayRequest,
1752
1752
  payPayRequest,
1753
1753
  payPayRequestWithCode,
1754
+ payPayRequestWithStoredCode,
1755
+ type PaymentRequestObject,
1754
1756
  } from '@relai-fi/x402';
1755
1757
 
1756
1758
  const config = { facilitatorUrl: 'https://relai.fi/facilitator' };
@@ -1784,6 +1786,16 @@ console.log('Explorer:', result.explorerUrl);
1784
1786
  if (result.changeCode) {
1785
1787
  console.log(`Change $${Number(result.change) / 1e6} USDC → code: ${result.changeCode}`);
1786
1788
  }
1789
+
1790
+ // ── Agent / vault flow: reuse a fetched request object directly ─────────────
1791
+ const fetchedRequest: PaymentRequestObject = await getPayRequest(config, 'MW78SGTW');
1792
+ const storedCodeResult = await payPayRequestWithStoredCode(
1793
+ config,
1794
+ fetchedRequest,
1795
+ 'MYVAULT78',
1796
+ { returnChange: 'code' },
1797
+ );
1798
+ console.log('Stored code payment success:', storedCodeResult.success);
1787
1799
  ```
1788
1800
 
1789
1801
  ### `payPayRequestWithCode` options
@@ -1793,6 +1805,13 @@ if (result.changeCode) {
1793
1805
  | `returnChange` | `'code' \| 'wallet'` | `'code'` | How to return surplus when code value > invoice |
1794
1806
  | `allowOverpayment` | `boolean` | `true` | If `false`, throws when code value ≠ invoice amount |
1795
1807
 
1808
+ `payPayRequestWithStoredCode()` accepts either:
1809
+
1810
+ - a payment request code string, or
1811
+ - a fetched `PaymentRequestObject` from `getPayRequest()`
1812
+
1813
+ Passing the object lets agents skip the extra request lookup when they already have the decoded request in memory. For stored Solana codes, the object must include a valid `code` field because the Solana facilitator endpoint still resolves the merchant request by `requestCode`.
1814
+
1796
1815
  **`returnChange` behaviour:**
1797
1816
 
1798
1817
  | Value | Mechanism | Requires buyer wallet? |
package/dist/index.cjs CHANGED
@@ -5418,6 +5418,9 @@ var EIP3009_TYPES3 = {
5418
5418
  { name: "nonce", type: "bytes32" }
5419
5419
  ]
5420
5420
  };
5421
+ function isPaymentRequestObject(value) {
5422
+ return typeof value !== "string";
5423
+ }
5421
5424
  function isEvmAddress(value) {
5422
5425
  return /^0x[0-9a-fA-F]{40}$/i.test(value);
5423
5426
  }
@@ -5876,12 +5879,16 @@ async function claimPaymentLink(config2, claimUrlOrToken, params) {
5876
5879
  });
5877
5880
  return sanitizeClaimLinkResponse(payload, facilitatorUrl, claimToken);
5878
5881
  }
5879
- async function payPayRequestWithStoredCode(config2, requestCode, paymentCode, options = {}) {
5882
+ async function payPayRequestWithStoredCode(config2, request, paymentCode, options = {}) {
5880
5883
  const facilitatorUrl = config2.facilitatorUrl ?? DEFAULT_FACILITATOR4;
5881
- const normalizedRequestCode = requestCode.trim().toUpperCase();
5882
5884
  const normalizedPaymentCode = paymentCode.trim().toUpperCase();
5885
+ const requestInfo = isPaymentRequestObject(request) ? request : await getPayRequest(config2, request.trim().toUpperCase());
5886
+ const normalizedRequestCode = isPaymentRequestObject(request) ? requestInfo.code.trim().toUpperCase() : request.trim().toUpperCase();
5883
5887
  const codeStatusInfo = await fetchStoredCodeWithFallback(facilitatorUrl, normalizedPaymentCode);
5884
5888
  if (codeStatusInfo.kind === "solana") {
5889
+ if (!normalizedRequestCode) {
5890
+ throw new Error("A payment request object with a valid code is required when paying a request with a stored Solana code");
5891
+ }
5885
5892
  return fetchOrThrow(`${facilitatorUrl}/solana-payment-codes/${normalizedPaymentCode}/pay-request`, {
5886
5893
  method: "POST",
5887
5894
  headers: { "Content-Type": "application/json" },
@@ -5891,7 +5898,6 @@ async function payPayRequestWithStoredCode(config2, requestCode, paymentCode, op
5891
5898
  })
5892
5899
  });
5893
5900
  }
5894
- const requestInfo = await getPayRequest(config2, normalizedRequestCode);
5895
5901
  const codeStatus = codeStatusInfo.payload;
5896
5902
  const allowOverpayment = options.allowOverpayment ?? true;
5897
5903
  const returnChange = options.returnChange ?? "code";