Switch to dark mode

Orders

An order is the purchase of a booking. It holds the booking's tickets and passes. A new order is PENDING, with its tickets pre-booked. Finalizing it completes the purchase and starts ticket issuance.

Order lifecycle

An order goes through two stages:

  1. Created: The tickets are pre-booked with the train operators. They are reserved, but not issued yet.

  2. Finalized: The purchase is complete, and the tickets are issued automatically.

You can create an order and finalize it later, but no tickets are issued until it is finalized.

An order keeps each segment's distance and co2 as they were when the journey was priced. On orders placed before these figures were introduced, they are null. See Distance and CO2e.

Creating orders

Every payment method starts the same way: create the order with createOrder. This pre-books the tickets with the train operators. It can take up to 30 seconds, depending on the operator, so send it over a WebSocket to avoid timeouts.

Example: Create an order

graphql
mutation CreateOrder {
  createOrder(booking: "booking_q3r442033740w4v650h457w552") {
    id
    status
    reference
  }
}

createOrder also takes metadata: a JSON object of your own, such as your reference for the purchase. It is stored with the order and comes back on the order's metadata field.

graphql
mutation CreateOrder {
  createOrder(
    booking: "booking_q3r442033740w4v650h457w552"
    metadata: { yourReference: "INV-1042" }
  ) {
    id
    metadata
  }
}

Finalizing orders

Finalizing an order completes the purchase, and the tickets are then issued. How you finalize depends on the payment method.

Wallet credits

Call finalizeOrder. The order total is deducted from your wallet balance. If the balance is too low, finalization fails.

You can finalize right after creating the order, or store the order id and finalize later, before the booking expires.

Invoice billing

Call finalizeOrder in the same way. The order is added to your monthly invoice instead of being paid right away.

Example: Finalize an order paid by wallet or invoice

graphql
mutation FinalizeOrder {
  finalizeOrder(order: "order_v4s2f6j2y1x3x3f5n7h0j0r5a6") {
    id
    status
    reference
  }
}

Payment gateway

Don't call finalizeOrder yourself. Pass the order's id to createPayment as orderId, and the order is finalized automatically when the customer has paid. See Payment Methods for the payment gateway flow.

Order status

An order's status field is one of:

  • PENDING: Order created but not yet finalized or paid
  • CONFIRMED: Purchase complete. Tickets are fetched from the operator afterward, so they may not be available yet
  • FAILED: Order creation or processing failed
  • REFUNDED: All refundable items have been refunded
  • PARTIALLY_REFUNDED: Some refundable items have been refunded
  • REFUND_FAILED: The refund attempt failed
  • VOIDED: The order has been voided
  • UNKNOWN: Order status could not be determined

CONFIRMED means the purchase is complete, not that the tickets are ready. To know when they are, poll the order with node until each item has its resources, or for rail passes its code. Most tickets are ready within 15 minutes. See Retrieving Tickets and Retrieving Pass Codes.

When finalizing fails

finalizeOrder can fail with NOT_ENOUGH_FUNDS, PAYMENT_METHOD_NOT_ALLOWED, WALLET_NOT_AVAILABLE, RATE_LIMITED, FINALIZE_FAILED or one of the finalization errors from operators. If the booking has expired in the meantime, you need to create a new one.

With these two errors nothing is booked or charged, and you can finalize the same order again:

  • NOT_ENOUGH_FUNDS: The wallet balance is too low. Top up the wallet in the Dashboard first. The API can't top up the wallet or show its balance.

  • RATE_LIMITED: finalizeOrder is rate limited per sales agent, to 20 orders per minute by default. Queue your finalizations and retry later.

Finding orders

To read one order you have the id of, use node. To look orders up, use orders, which lists the orders of your sales agent, newest first. filter.query finds orders by their reference, an operator's booking reference, or a passenger's name or email. Listing orders needs the orders:read scope.

The list comes in pages of 10 unless you ask for more with first, up to 100. Pass a page's endCursor as after to read the next one.

Example: Find orders by email

graphql
query FindOrders {
  orders(filter: { query: "anna@example.com" }, first: 20) {
    totalCount
    pageInfo {
      hasNextPage
      endCursor
    }
    edges {
      node {
        id
        reference
        status
      }
    }
  }
}

The full flow

For where orders fit in the whole flow from search to tickets, for every payment method, see Booking Flows.

Next steps

Retrieving tickets
Tickets are issued once an order is finalized. Fetch them from the order.