After creating an order with POST /v1/order and directing the user through an external payment flow, use this endpoint to poll for the outcome. The order status will progress from STARTED to one of the terminal states: COMPLETED, FAILED, or EXPIRED.
Order completion is asynchronous. Poll this endpoint after the user completes the external payment step, stopping when status reaches a terminal value (COMPLETED, FAILED, or EXPIRED).
async function waitForOrderCompletion(orderId, clientId, accessToken) { const TERMINAL_STATES = ['COMPLETED', 'FAILED', 'EXPIRED']; const MAX_ATTEMPTS = 40; // ~2 minutes at 3s intervals const POLL_INTERVAL_MS = 3000; for (let i = 0; i < MAX_ATTEMPTS; i++) { const response = await fetch(`https://api.baanx.com/v1/order/${orderId}`, { headers: { 'X-Client-ID': clientId, 'Authorization': `Bearer ${accessToken}` } }); if (!response.ok) throw new Error(`Unexpected error: ${response.status}`); const order = await response.json(); if (TERMINAL_STATES.includes(order.status)) return order; await new Promise(resolve => setTimeout(resolve, POLL_INTERVAL_MS)); } throw new Error('Order polling timed out. Please check back later.');}const order = await waitForOrderCompletion(orderId, clientId, accessToken);if (order.status === 'COMPLETED') { // Unlock product, show success UI} else { // Handle failure or expiry console.warn('Order did not complete:', order.metadata?.note);}