Submit a Visa TAP payment
This example checks the merchant?s locked cart against the buyer-approved order before signing a Visa TAP payment. Place invoice-hash.js and visa-tap-sign.js in an examples directory beside your agent. For protocol requirements, see the Agent Developer Guide.
const { invoiceHash } = require("./examples/invoice-hash.js");
const { signVisaTap } = require("./examples/visa-tap-sign.js");
// approvedCart is the immutable order the buyer approved, loaded from your own order store.
async function buy({ storeOrigin, wanted, approvedCart, policy, signer }) {
// 1. Discover and sanity-check the merchant
if (!storeOrigin.startsWith("https://")) throw new Error("merchant must be HTTPS");
const manifest = await (await fetch(`${storeOrigin}/.well-known/ai-payments.json`)).json();
if (!manifest.supportedProtocols.includes("VISA_TAP")) throw new Error("no common protocol");
// 2. Ask the merchant to lock the cart (merchant prices it)
const lock = await (
await fetch(`${storeOrigin}/checkout/lock`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ items: wanted }),
})
).json();
const cart = lock.cart; // the exact cart the merchant locked (the merchant's /checkout/lock returns it, see the Integration Guide)
// Verify the merchant lock against the buyer-approved merchant, currency, total and line items.
const approvedHash = invoiceHash(approvedCart);
if (invoiceHash(cart) !== approvedHash || lock.invoiceHash !== approvedHash) {
throw new Error("The locked cart differs from the approved order");
}
// 4. Check the user's policy locally before spending
if (cart.amount > policy.maxAmountCents) return { needsHuman: true, reason: "over cap" };
// 5. Sign and submit (Visa TAP shown)
// paymentContainer.invoiceHash binds the payment signature to the approved cart
const body = {
consumerRecognition: { tavv: signer.tavv },
paymentContainer: { ...signer.container, invoiceHash: lock.invoiceHash },
};
// Obtain IXOPAY_URL and its key from trusted server configuration during onboarding.
const transactUrl = new URL("/v1/transact", process.env.IXOPAY_URL);
if (transactUrl.protocol !== "https:" || !process.env.IXOPAY_GATEWAY_KEY || !process.env.TOKENEX_ID)
throw new Error("Configure the HTTPS gateway and its key");
const headers = signVisaTap(body, signer.privateKey, signer.keyId, { authority: transactUrl.host });
const r = await fetch(transactUrl, {
redirect: "error",
method: "POST",
headers: {
"Content-Type": "application/json",
"X-API-Key": process.env.IXOPAY_GATEWAY_KEY,
"tx-tokenex-id": process.env.TOKENEX_ID,
...headers,
},
body: JSON.stringify({ sessionId: lock.sessionId, protocol: "VISA_TAP", cartParams: cart, body }),
signal: AbortSignal.timeout(5000),
});
const out = await r.json();
// 6. Handle the result; never blind-retry a 401
if (r.ok && out.status === "SETTLEMENT_HANDOVER")
return { status: "AWAITING_PROCESSOR", transactionId: out.transactionId };
if (out.error === "BUDGET_CAP_EXCEEDED") return { needsHuman: true, stepUp: out.stepUpChallenge };
throw Object.assign(new Error(out.error || "payment failed"), { out });
}
What this teaches
- Step 3 binds the signed hash to the buyer’s independently approved order.
- The agent never edits the cart; only the merchant's locked cart is signed, and its hash travels inside the signed body (
paymentContainer.invoiceHash). BUDGET_CAP_EXCEEDEDbecomes a human decision, not a retry.