In some cases, you might need to perform a bulk transfer of users from one term to another even though they have different prices and resources. This way you won't be able to take advantage of the Action Manager.
The sample Python code below will allow you to do that on Sandbox. Please be careful when inserting the variables as any changes made after the script's execution cannot be reverted.
import requests
aid = "X"
api_token = "X"
term_name_from = "X" # Name of the term FROM which users are converting
term_id_from = "X" # Id of a term FROM which users will be transferred
term_id_to = "X" # Id of a term TO which users will be transferred
when_to_transfer = "X" # values: 0,1,2,3. Read more about meaning of billing_timing key here: https://docs.piano.io/api/?endpoint=post~2F~2Fpublisher~2Fterm~2Fchange~2Fdo
# All variables have to be strings
app_users_response = requests.post(
"https://sandbox.piano.io/api/v3/publisher/user/search",
data={
"aid": aid,
"converted_terms": term_id_from,
"has_conversion_term": true,
"api_token": api_token
}
)
app_users = app_users_response.json()["users"] # [{first_name, last_name, email, uid}, ...]
# Go over targeted users
for user in app_users:
uid = user["uid"]
# Get sub details about current user
sub_stats_response = requests.post(
"https://sandbox.piano.io/api/v3/publisher/subscription/stats",
data={
"aid": aid,
"uid": uid,
"api_token": api_token
}
)
sub_stats_data = sub_stats_response.json()
sub_stats = sub_stats_data["data"]
# Go over all subs for current user
for sub in sub_stats:
if sub["term_name"] == term_name_from:
current_user_sub_id = sub["subscription_id"]
# Transfer current user from one term to another
term_change_response = requests.post(
"https://sandbox.piano.io/api/v3/publisher/term/change/do",
data={
"aid": aid,
"uid": uid,
"api_token": api_token,
"subscription_from": current_user_sub_id,
"term_to": term_id_to,
"billing_timing": when_to_transfer,
"immediate_access": True
}
)
message = term_change_response.json()["message"]
print(message)
You can fill in all of the variables in lines 1-6 and then grab the code and run it.
If you'd like to utilize the code on your Production environment, please replace the base URL https://sandbox.piano.io for the API calls with one of the values listed here (depending on your application's locale).
This code uses Piano's API /publisher/user/search to find users that converted on the term X. It then grabs the user's UIDs and loops them into the Piano API endpoint /publisher/subscription/stats which searches for subscriptions associated to the current user. With the help of the variable term_name_from, you can select subscriptions to specific terms. Once this is done, the script grabs the relevant subscription ID of the user's UID and runs the Piano API endpoint /publisher/term/change/do to make the final term change.
Please note, that if you upgrade a user to a new term that includes a trial (which is not restricted to new customers only), the upgraded user will be put on this trial as well.