GPT function calling + idempotent checkout
Wire OpenAI's function calling to OrderCore so a GPT agent can create a checkout and complete a real order — with retries that never double-charge. Define two tools, run the loop, done.
The two tools
Map GPT functions onto OrderCore endpoints. Two are enough for a working checkout:
const tools = [ { type: "function", function: { name: "create_checkout_session", description: "Open a checkout for catalog line items.", parameters: { type: "object", properties: { sku_id: { type: "string" }, quantity: { type: "integer" } }, required: ["sku_id", "quantity"] } } }, { type: "function", function: { name: "complete_checkout_session", description: "Complete a checkout into an order. Idempotent per session; safe to retry.", parameters: { type: "object", properties: { session_id: { type: "string" } }, required: ["session_id"] } } } ];
Handle the tool calls
When GPT emits a tool call, run the matching OrderCore request and feed the result back into the conversation:
async function runTool(name, args, ctx) { const BASE = 'https://api.ordercore.ai'; const H = { 'Content-Type': 'application/json', 'X-Checkout-Token': ctx.token }; if (name === 'create_checkout_session') { return (await fetch(BASE + '/ucp/public/checkout/sessions', { method: 'POST', headers: H, body: JSON.stringify({ line_items: [{ item: { id: args.sku_id }, quantity: args.quantity }], currency: 'USD' }) })).json(); } if (name === 'complete_checkout_session') { return (await fetch(BASE + `/ucp/public/checkout/sessions/${args.session_id}/complete`, { method: 'POST', headers: H, body: JSON.stringify({ session_id: args.session_id, payment_data: { handler_id: 'card', payment_intent_id: 'pi_...' } }) })).json(); } }
GPT (and retry middleware) can re-emit
complete_checkout_session for the same
session_id. OrderCore returns the same order each time — no duplicate, no double charge.Skip the schema-writing
Machine-readable tool schemas for OpenAI (and Claude, Gemini, DeepSeek, Grok) are published at /direct-ai/tooling — pull them instead of hand-writing. Using Claude or an MCP client? See the MCP commerce server guide.
Try the flow (no signup)
The demo runs create → complete → retry offline in mock mode and asserts the order matches: