How to create a subscription model to set the available subscription frequencies and the subscription strategy for a market
Problem
It's time to start providing subscription features in one of your Commerce Layer markets. You want to define the available subscription frequencies for the market in question and set the subscription strategy based on which the necessary order subscriptions will be automatically generated.
Solution
You need to create and configure a subscription model and associate it with your market. To do that:
Send a POST request to the /api/subscription_models endpoint, setting the frequencies and strategy attributes.
Send a PATCH request to the /api/markets/:id endpoint, setting the subscription_model relationship.
Example
1. Create a new subscription model
The following request creates a new subscription model that allows five subscription frequencies:
To create a new subscription model you need an integration token of type admin (or a custom role with the necessary permissions).
On success, the API responds with a 201 Created status code, returning the newly created subscription model object (since the subscription strategy wasn't specified in the request, the default one — by_frequency — is used):
2. Associate the new subscription model with an existing market
The following request associates the subscription model identified by the "AavWRUbbMQ" ID with the market identified by the "bgOEQhznoZ" ID:
To update a market you need an integration token of type admin (or a custom role with the necessary permissions).
On success, the API responds with a 200 OK status code, returning the updated market object:
Additional notes
Updating the subscription model
You may want to add some new frequencies to an existing subscription model or remove some of the allowed ones, change the subscription strategy or the default behavior in terms of automatic subscription activation and cancellation, and more. To do that, you just need to send a PATCH request to the /api/subscription_models/:id endpoint, properly setting the related attributes.
Please note that any changes to an existing subscription model don't affect already generated subscriptions. The changes will apply starting from the next subscription generation only. If you try to update some of the already generated subscriptions in a way that's not consistent with the new changes to the subscription model you will receive a validation error.
Example
The following request updates the subscription model identified by the ID "AavWRUbbMQ", adding an additional allowed frequency and changing both the default subscription strategy and auto-activate / auto-cancel behavior:
To update a subscription model you need an integration token of type admin (or a custom role with the necessary permissions).
On success, the API responds with a 200 OK status code, returning the updated subscription model object: