Generating the subscriptions

How to trigger the subscription generation based on the source order and the subscription model configuration

Problem

You have associated a subscription model with your market, setting the allowed subscription frequencies and choosing the subscription strategy. You also have selected a source order containing some line items with frequency from which to start generating the subscriptions and placed it. You now want to trigger the subscription generation.

Solution

To start generating the subscription from a placed source order, send a PATCH request to the /api/orders/:id endpoint setting the _create_subscriptions attribute to true.

Make sure to start the subscription generation upon/after the order placement, otherwise no order subscription will be generated even if the source order contains some line items with frequency.

Example

The following request generates the subscriptions from the source order identified by the "NgojhelVGm" ID:

curl -g -X PATCH \
  'https://yourdomain.commercelayer.io/api/orders/NgojhelVGm' \
  -H 'Authorization: Bearer your-access-token' \
  -H 'Accept: application/vnd.api+json' \
  -H 'Content-Type: application/vnd.api+json' \
  -d '{
    "data": {
      "type": "orders",
      "id": "NgojhelVGm",
      "attributes": {
        "_create_subscriptions": true
      }
    }
  }'

Additional notes

How automatic subscription generation works

The automatic order subscription generation process is triggered manually by using a specific attribute. The necessary order subscriptions are automatically generated based on the source order’s line item for which you’ve specified a frequency, accordingly to the strategy set at the subscription model level.

  • The source order’s customer email is stored, that order is considered the subscription's first run, and the generated order subscriptions are activated (unless differently specified at the subscription model level by setting the auto_activate attribute to true).

  • For each automatically generated order subscription, some source order information is copied (e.g. addresses, customer payment source, etc.), the related order subscription items are created and associated with the order subscriptions, and the target order is placed at the scheduled time using the saved customer payment source (if still valid).

During an automatic order subscription generation, the source order is never reused to generate any next runs, which are built on top of the associated order subscription and related order subscription items only.

Which and how many order subscriptions and order subscription items are generated depends on the subscription strategy and source order's line items.

Order subscriptions

  • by_frequency — as many order subscriptions as the different frequencies specified for the source order’s line items are generated, each with the related order subscription items (default strategy).

  • by_line_items — an order subscription is generated for any source order’s line item that has a frequency, each with the related order subscription item.

Order subscription items

  • by_frequency — for each automatically generated order subscription as many order subscription items as the source order’s line items that have that specific frequency are generated.

  • by_line_items — for each automatically generated order subscription a single order subscription item is generated.

Example

Let's look at the source order we used throughout this guide:

The following request fetches the source order, requesting some basic fields and including the line items to check their frequency:

curl -g -X GET \
  'http://yourdomain.commercelayer.io/api/orders/NgojhelVGm?include=line_items&fields[orders]=number,customer_email,formatted_total_amount_with_taxes,skus_count&fields[line_items]=name,sku_code,quantity,formatted_unit_amount,formatted_total_amount,frequency' \
  -H 'Authorization: Bearer your-access-token' \
  -H 'Accept: application/vnd.api+json'

As you can see inspecting the above response it contains 4 line items of type skus for a total of 5 SKUs:

  • 2 SKUs with monthly frequency (both with quantity 1 — SKU codes LENSPACKL125 and LENSPACKR075)

  • 1 SKU with weekly frequency (with quantity 2 and SKU code RAZRFILLPACK4)

  • 1 SKU with no specified frequency (with quantity 1 and SKU code MUGXXXAUFFFFFF00000011OZ)

Based on the subscription strategy these are the subscriptions and subscription items that will be generated (remember that line items without frequency don't generate subscriptions):

By frequency

2 order subscriptions and 3 order subscription items:

  • 1 for the monthly frequency, associated with 2 order subscription items (SKU codes LENSPACKL125 and LENSPACKR075, both with quantity 1)

  • 1 for the weekly frequency, associated with 1 order subscription item (SKU code RAZRFILLPACK4 with quantity 2)

In this case, in terms of generated recurring target orders:

  • every month, 1 order with 2 line items of type skus will be placed (codes LENSPACKL125 and LENSPACKR075, both with quantity 1)

  • every week, 1 order with 1 line item of type skus will be placed (code RAZRFILLPACK4 with quantity 2)

By line items

3 order subscriptions and 3 order subscription items

  • 1 for the LENSPACKL125 line item, associated with 1 order subscription item (with quantity 1 and frequency monthly)

  • 1 for the LENSPACKR075 line item, associated with 1 order subscription item (with quantity 1 and frequency monthly)

  • 1 for the RAZRFILLPACK4 line item, associated with 1 order subscription item (with quantity 2 and frequency weekly)

In this case, in terms of generated recurring target orders:

  • every month 2 orders with 1 line item of type skus will be placed (one for the product with code LENSPACKL125, the other for the product with code LENSPACKR075, both with quantity 1)

  • every week 1 order with 1 line item of type skus will be placed (code RAZRFILLPACK4 with quantity 2)

The generated recurring target orders cannot be used as source orders from which to trigger a new subscription generation.

You can check the generated order subscriptions and related order subscription items in the Subscriptions section of our admin dashboard, or via API by sending a GET request to the /api/order_subscriptions endpoint, filtered by the source order ID with order subscription items included (the following request is related to the by_frequency example above):

The following request fetches all the order subscriptions associated with the customer identified by the "nDNahABVZR" ID and generated by the source order identified by the "NgojhelVGm" ID, including the related order subscription items:

curl -g -X GET \
  'http://yourdomain.commercelayer.io/api/customers/nDNahABVZR/order_subscriptions?filter[q][source_order_id_eq]=NgojhelVGm&include=order_subscription_items' \
  -H 'Authorization: Bearer your-access-token' \
  -H 'Accept: application/vnd.api+json'

Managing failed subscriptions

You can track subscriptions' renewal failures by activating a webhook listening for the order_subscriptions.last_run_failed event and acting accordingly (e.g. setting up an automatic notification system for the customer).

The order_subscriptions.last_run_succeeded event is available as well.

Notifying upcoming subscriptions

You can notify customers about upcoming recurring orders associated with subscriptions by properly setting the renewal_alert_period attribute at the order subscription level and leveraging the order_subscriptions.renewal webhook event that will be fired accordingly.

The renewal alert period must be expressed in hours (e.g. if you want to send the notification 3 days before, set it to 72). The minimum value is 1.

More to read

See our API reference if you need more information on how to retrieve and update an order, filter data, include associations, fetch relationships, or about how order subscriptions and order subscription items work.

Last updated