Skip to content
For the complete documentation index, see llms.txt. This page is also available as Markdown.

Orders

The order object and the allowed CRUD operations on the related resource endpoint

Orders get created in draft status and become pending when they have a customer and some line items.

Draft orders act as shopping carts — see our how-to on how to manage shopping carts. Draft orders that aren't associated with a customer are automatically deleted 2 months after their latest update.

Pending orders can be recovered or cancelled when abandoned. The status of an order is closely linked to the related payment (dynamically computed from the associated payment sessions) and fulfillment statuses. Orders are placed synchronously by default, if you need to place them asynchronously and transit through the placing status use the place_async attribute. When an order is placed, it can either get approved or cancelled.

Fulfillment status

An approved order becomes fulfilled when paid and shipped. When an order is cancelled, any associated payment void or payment refund must be created manually against the related payment session.

Action
Order status
Fulfillment status

An empty order is created.

draft

unfulfilled

A customer is associated with the order, and a purchasable item is added.

pending

unfulfilled

The order is placed asynchronously, and errors occur.

placing

unfulfilled

The order is placed.

placed

unfulfilled

The order is being edited.

editing

unfulfilled

The order is cancelled.

cancelled

unfulfilled

The order is approved.

approved

unfulfilled

The payment is captured.

approved

in_progress

The order is fully refunded.

cancelled

unfulfilled if in progress, otherwise fulfilled.

All shipments are shipped.

approved

fulfilled

If the order contains items flagged as do_not_ship only, its fulfillment status is automatically not_required.

Payment status

The payment status of an order is computed dynamically by aggregating the statuses of its associated payment sessions. The remaining amount not yet covered by active sessions is tracked via the session_amount_cents attribute — when it reaches zero, the full order total is covered.

Payment status
Condition

free

The order total is zero.

unpaid

No sessions beyond unpaid or invalidated — an invalidated session never counts, the same as an unpaid one.

authorized

At least one session is authorized and session_amount_cents is zero or negative (e.g. if the order total was reduced after the session was authorized) — the order total is fully covered.

partially_authorized

At least one session is authorized but session_amount_cents is still positive — part of the order total isn't covered by any session yet.

partially_paid

At least one session is paid or partially_paid, while the amount actually captured — across every session that ever captured funds (paid, partially_paid, partially_refunded, or refunded), net of any refunds — doesn't yet cover the order total.

partially_refunded

At least one session is partially_refunded.

paid

At least one session is paid or partially_paid, and the captured amount covers the order total.

refunded

At least one session is refunded.

voided

None of the above match — all remaining sessions are voided.

Since a single payment status can't fully represent the combined state of multiple sessions, the conditions above are evaluated in order, top to bottom, and the first match wins — prioritizing statuses that still need action over settled ones (for example, authorized always takes precedence over voided, and paid always takes precedence over refunded).

The payment status alone doesn't reveal whether an order was ever over-collected — a session whose authorization pushed the order past its total still authorizes and captures normally, counting toward payment_status like any other (more details here).

Payment sessions

The payment flow for an order is driven by payment sessions.

Selecting a payment setting

Before creating a payment session, fetch the list of payment settings available for the order via the available_payment_settings relationship. This list is derived from the payment settings associated with the market, minus those gated by payment setting rules whose conditions the order doesn't meet.

Creating a payment session

Create a payment session by associating it with the order and a chosen payment setting. When the session is created, a gateway interaction may be initiated automatically (e.g. to create a payment intent on Stripe or a session on Adyen). The session starts in unpaid status.

Authorizing

Authorization can be initiated in two ways:

Explicit — Create a payment authorization against the session. The authorization amount always equals the full session amount (partial authorization is not supported). The gateway call is processed asynchronously: the transaction starts in pending and transitions to succeeded, declined, or failed once the gateway responds. For gateways that require additional client-side steps (e.g. 3DS), the transaction may land in requires_action. In this case, the client must then update the authorization and send the _payment_details trigger attribute to resume the flow.

Event-driven — A webhook event from the payment provider signals a successful authorization linked to the session. The transaction is created (or updated) automatically, and no API call is required. This is the natural outcome of a pure client-side flow, where the customer completes payment through the provider's SDK and the gateway notifies Commerce Layer directly.

In both cases, a successful authorization transitions the session to authorized and triggers a recomputation of the order's payment status.

If auto_place is enabled on the payment setting, the order is placed automatically when the session transitions to authorized, without requiring a separate _place call on the order.

Capturing

Capture the authorized funds by creating a payment capture against the session, passing the related payment_authorization. The amount_cents is optional and defaults to the full remaining capture_balance_cents on the authorization.

If auto_capture is enabled on the payment setting, a capture is created automatically in succeeded state as soon as the authorization succeeds, without requiring a separate API call.

Voiding

Cancel an authorization before it is captured by creating a payment void against the session, passing the related payment_authorization. The void amount must equal the full authorization amount.

Only one succeeded void per authorization is allowed — a failed attempt can be retried with a new void, but only one can ever succeed (more details here).

Refunding

Refund a captured amount by creating a payment refund against the session, passing the related payment_capture. The amount_cents is optional and defaults to the full remaining refund_balance_cents on the capture.

Wallets

Customers can store vaulted payment instruments for reuse via payment wallets. A wallet can be linked to a payment session at creation time to enable faster checkout. Wallet support depends on the payment setting being configured as vaultable (e.g. Adyen, Braintree, Stripe, and external settings with a token_url configured).

Asynchronous order placement

To place an order asynchronously, you just need to set the place_async flag to true (default is false). In this scenario, only a subset of the standard synchronous validations (needed to check the order integrity) is performed when passing the _place trigger attribute:

  • Customer — the correct association with an existing customer is checked.

  • Billing — the correct association with an existing billing address is checked.

  • Items — the presence of at least one SKU, bundle, positive gift card (i.e. purchasing a gift card), or positive adjustment is checked.

  • Shipping — the correct association with an existing shipping method and shipping address is checked.

If some errors occur at this stage, the order stays pending and cannot be placed. If those validations are successful, the order is moved to an intermediate status (placing) where the remaining validations are added:

  • Coupons — the validity of the associated coupon code (if any) is checked.

  • Stock — the availability of the necessary stock to fulfill the order is checked.

  • Payment rules — any enabled payment rules configured on the market are evaluated. Order rules can block placement if the payment coverage doesn't meet the configured threshold, and with no enabled order rule on the market the order must be fully covered. Payment setting rules verify that the sessions associated with the order use only the available payment settings.

  • External validation — if any (i.e. if an external_order_validation_url is set at the market level).

If those validations are also successful, the order is safely placed and moved through the next steps of its lifecycle. If some of the additional validations throw an error, you need to fix it (e.g. adding the missing stock) and manually re-trigger the _place (which will be again performed asynchronously unless the place_async flag is set back to false).

The orders that are in placing status can be fetched but not edited by a sales channel. To patch them, you need to use integration API credentials. If you need the customer to fix the error that's keeping the order in placing, you can pass the _pending trigger attribute and move it back to the pending status.

Auto-refresh

By default, orders that are still editable (i.e. in draft or pending status) are automatically refreshed each time they get updated and/or one of the related line items gets created, updated, or destroyed. This automatically triggers several actions:

  • If the order is already associated with a shipping address, the related shipments are rebuilt based on the related inventory model's strategy.

  • Any active promotions are applied and the related discounts are recalculated.

  • Any negative adjustment is redistributed on the taxable items.

  • If the market associated with the order has a tax calculator, taxes get updated.

  • All of the internal amounts and counters are refreshed.

Removing both the shipping and billing addresses from an order with an associated tax calculator resets all previously computed tax amounts to zero, since no address means no taxable jurisdiction.

All of the above gets calculated in sequence for each of the order's line items, resulting in a fairly intensive computation, which can considerably slow down in case one or more external resources are involved. This may lead to concurrent request issues and/or timeout errors, making the automatic refresh not ideal, especially when you have to deal with a cart containing a large number of line items (e.g. B2B).

To better handle these specific scenarios, you can leverage the order's autorefresh attribute and set it to false (default is true) to prevent the automatic triggering of the above-mentioned actions. This choice will result in stale order data, but will ensure much faster order editing (the performance gain will build up exponentially with an increasing number of line items — we estimate an average boost of 500%). Once the editing is completed, the attribute can be updated back to true, which automatically triggers a new refresh of the order (in any case, remember that there is always the option to force a refresh manually even when the order auto-refresh is disabled by passing the _refresh trigger attribute).

Order editing

Draft and pending orders are always editable by a sales channel before placement. Once an order is placed but still not approved, it can be moved to the editing status by passing the _start_editing trigger attribute — regardless of its current payment status. As soon as the editing operations are finished, the order must be moved back to the placed status by passing the _stop_editing trigger attribute.

When an order is in editing, you can make almost any change you need (e.g. adding or removing line items, coupons, adjustments, etc.). There is no amount cap enforced at _stop_editing — the order total can be freely increased or decreased. Any payment adjustment required as a result of the edit must be handled manually via the order's payment sessions: a new authorization for an increased total, or an explicit void or refund for a reduced one. In the former case, a payment link can also be created for just the outstanding delta, rather than reattaching the whole order.

Promotions and coupons

The discounts due to the active promotions applied at placement time are refreshed according to the changes applied to the order, even if the promotions should have expired in the meantime. New promotions (if any) triggered by the changes due to the order editing are not applied. Coupons applied at placement time are still valid (even single-use ones). New coupons can be applied, as long as one wasn't already present at the time of placement.

Gift cards are managed as payment instruments via payment setting gift cards and payment sessions. A gift card code set on the order follows the legacy workflow instead — see payment sessions.

Shipments and shipping methods

Shipments are rebuilt so, after any editing operation and before exiting the editing status, you need to check the available shipping methods again (which may be different) and choose a new one to be re-associated with the edited order, otherwise the API will return an error on the _stop_editing.

Orders can be edited multiple times before approval. Please note that once an editing operation is started, it can't be reverted. It can always be aborted by cancelling the order.

Non-editable attributes

Integrations can edit almost every order attribute or relationship even if the order is in a non-editable status (i.e. a different one from draft, pending, or editing — more information here), with a few exceptions, listed below. If the order is no longer editable, the following attributes and relationships cannot be edited using any API credentials:

  • market

  • customer

  • shipping_address

  • coupon_code

You can still edit the attributes above if the order is in the placed status by entering order editing, otherwise they are considered frozen and not editable anymore.

Changing the order number

The default order numeric identifier can be changed according to your needs as long as the passed value is unique within the specific organization environment.

This feature is available for enterprise plans only and can be activated by environment (meaning you can request to enable it in test mode, in live mode, or both — inspect the related organization's flags order_number_editable_test and order_number_editable_live to check your configuration).

Stock reservation

When an order is placed, the stock reservations needed to block the whole order's stock are automatically created and the associated stock is reserved without decrementing the stock item quantities. Once the order is approved, the stock item quantities are decremented. If the order is cancelled, the reserved stock is released, becoming available again. You can temporarily reserve the stock associated with a line item before the order placement by sending a specific trigger attribute when adding the line item to the order.

Automatic subscriptions generation

If a subscription model is associated with the same market as an order, subscriptions can be automatically generated for all the line items that have a frequency, based on the strategy set at the subscription model level. To trigger the automatic order subscription generation, you just need to update the source order after placement and pass the _create_subscriptions trigger attribute.

Order validation

Automatic order validation is performed at the time of the order placement. Sometimes, you may need to implement custom validation (e.g. you want to support more complex validation rules specific to your business logic) on some orders, as an additional step before the order placement. To do that, you can leverage Commerce Layer's external order validation feature — just make sure to correctly set up the URL of your external service (that will be in charge of computing the validation logic) at the market level and update the order(s) in question by setting the _validate trigger attribute to true.

How-to

Check the related guide to learn how to validate orders via external services.

Timed placement

You can add an expiration date for the placement of an order by setting the order's expires_at attribute (you need to use integration API credentials), after which the order placement attempt will fail. If the expires_at timestamp is set, you can also fill the expiration_info JSON object with any key/value pair you need to add useful information (e.g. messages to be shown on the frontend, a return URL for orders not placed due to expired time, etc.).

This option can come in handy when dealing with quite common use cases such as ticketing processes, time-limited inventory, flash sales, and more.

Order errors

The latest 10 errors occurring during the attempt to place an order (both synchronously and asynchronously), including the ones coming from external validations or subscription processes, are stored in the related resource errors array until order approval. To inspect them, you just need to fetch the error including the resource errors association. You can also leverage the errors_count attribute (which is reset to 0 once the order is approved) at the order level.

Taxable items

The taxable items of an order are used for tax and discount distributions. Line items of type skus and bundles are taxable by default. As for the other item types, you need to set this property by specifying the related attribute at the order level:

  • Set freight_taxable to true if you want to add the line items of type shipments (i.e. shipping costs) to the taxable items list.

  • Set adjustment_taxable to true if you want to add the line items of type adjustments (i.e. positive adjustments) to the taxable items list.

  • Set gift_card_taxable to true if you want to add the line items of type gift_cards (i.e. purchased gift cards) to the taxable items list.

Idempotency

Order status changes are idempotent. Triggering _place, _cancel, or other status transitions multiple times is safe — no duplicate side effects (stock reservations, webhook events, fulfillment actions, etc.) will be produced.

Since the payment status is computed dynamically from the statuses of the associated payment sessions, it is always kept consistent automatically. If it appears out of sync — for example due to a gateway timeout during event processing — re-triggering the order status transition will force a recomputation without risk of creating duplicate transactions.

Webhooks events and stock item updates are likewise guaranteed to execute only once.

Last updated