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

Order subscriptions

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

Order subscriptions allow repeating a given source order according to a specified frequency: smaller is hourly, larger is yearly. If you need a custom frequency that is not in the list of allowed values, you can use a crontab expression.

Order subscriptions can be generated automatically from the line items that have a frequency using a trigger attribute at the order level, in which case recurring order copies are used as the order factory that generates the target orders.

To suspend or cancel an order subscription, you can use the _deactivate and _cancel trigger attributes.

Currently, order subscriptions have no retry policy: in case, for any reason, the order copy fails, the errors_count counter is incremented, but the subscription is kept active (unless expired, deactivated, or cancelled). As soon as the order copy process starts, the order subscription status is moved to running and moved back to active once the process is successfully completed.

It is possible to check the succeeded_on_last_run attribute to inspect the subscription's last run state: if false, you can inspect the last associated recurring_order_copy to fix any missing/bad data.

You can attach webhooks on order subscription and recurring order copy events, to act promptly in case something unexpected happens. You can also set the renewal_alert_period attribute (expressed in hours — the minimum value is 1, the maximum value is 720 e.g. 30 days) so that the related webhook event is fired accordingly and you can send customers a notification about upcoming recurring orders associated with subscriptions.

Automatic order subscription generation

To automatically generate order subscriptions based on a source order's line items with frequency, you need to associate a subscription model with your market, then send the _create_subscriptions trigger attribute on the order. The source order is considered the subscription's first run. Some of its information is copied (e.g. addresses, payment sessions, etc.), the involved order subscription items are created and associated with the generated order subscriptions, and — upon successful recurring order copy — the target order is placed.

The _create_subscriptions trigger is only effective once the order is placed, so it can be sent together with the _place trigger or on a subsequent update.

In between each run, you can edit the subscription (e.g. changing its frequency, expiration date, status, etc.) to dynamically manage the next occurrences.

By default, automatically generated order subscriptions are directly activated, unless differently specified at the subscription model level.

Payment sessions

The subscription stores the payment instrument needed to fund each recurring run. At creation time, it is automatically derived by checking the source order's payment sessions:

  • If any of them is linked to a payment wallet, that wallet is stored on the subscription and reused on every run.

  • Otherwise, if any of them uses a non-vaultable payment setting, that payment setting is stored directly on the subscription instead.

  • Failing that, if the source order's only payment is a single gift card session, its payment setting and its code are both stored on the subscription, the latter in the gift_card_code attribute.

A wallet always wins over a gift card on the same order: a gift card used once alongside another payment method is a one-off, not a recurring instrument. To renew on a gift card, set the gift card payment setting and code on the subscription explicitly.

The subscription can only auto-activate if its payment instrument is ready: a payment wallet or a payment setting must be present, and — for a gift card — the card must still resolve against the order and hold a balance that covers it. When it isn't, the subscription moves to pending status, records the reason among its errors, and must be activated again once the instrument is usable.

On each automatic run, a recurring order copy creates a single new payment session on the target order from the subscription's stored payment wallet, payment setting, or gift card code — it is not a copy of the source order's sessions. The new session is authorized automatically as part of the run — no manual payment authorization is required. Free target orders are skipped entirely and no session is created.

A subscription model is required to generate order subscriptions for orders using payment sessions, so runs always go through recurring order copies.

Gift card coverage

Each run opens its own gift card session on the target order and checks that the card covers the renewal in full before committing to it. A card that can no longer be resolved (e.g. deleted or expired), or whose balance falls short of the target order total, fails the run: the target order is left unplaced, the gift card balance is left untouched, errors_count is incremented, and the subscription drops to pending.

Top the card up — or update the subscription with a different gift card code — then send the _activate trigger attribute to resume it. Coverage is re-checked at that point, so a subscription only goes back to active if the card can actually pay.

Unattended renewals

A renewal is a merchant-initiated payment: nobody is at a browser when it runs. Commerce Layer tells the gateway so, charging the stored instrument off session — no 3DS challenge and no CVC re-entry are attempted, and no client-side checkout step is involved.

The instrument that makes this possible is either an existing payment wallet used to pay the source order, or one stored during that order's own checkout: when the payment session is created with vaulting set to true — after your storefront has collected the shopper's consent — the gateway returns a reusable token, which Commerce Layer turns into the payment wallet the subscription then renews on. Vaulting is never enabled automatically.

Instrument-pending subscriptions

A subscription generated from a checkout whose payment session asked for vaulting is born without a payment instrument: the instrument is vaulted moments later, and for a vaultable gateway a linked payment wallet is the only thing that can pay a run. The same holds when the session didn't ask for vaulting at all, until a wallet is created for the customer. Such a subscription starts in pending, and — since runs only walk active subscriptions — nothing would look at it again.

When a payment wallet reaches succeeded status, it offers itself to that customer's pending subscriptions and activates the ones it can pay for. Candidates are matched through the source order they were created from, which must carry a payment session on the same payment setting as the wallet. This applies to every wallet, however it was created, and runs in the background shortly after it succeeds. An expired wallet is never offered.

Only subscriptions created within the last 30 days, and which resolved neither a payment wallet nor a payment setting of their own, are considered — so saving a new card doesn't revive a subscription abandoned months ago. Anything outside that window is still recoverable manually, by associating a payment wallet and sending the _activate trigger attribute.

How-to

Check the related guide to learn more about how to generate automatic subscriptions from a source order.

Last updated