This guide assumes that you have setup the client as we have in the Setup instructions, and have initialized this to the
client
variable.Grab details on the subscription
You will need to ensure you have details on both:- The user who has subscribed to the plan in question
- The plan which the user is being subscribed to
id
in Kana for both Objects.
If neither the user nor plan exist yet in Kana, then you should create these first and then fetch/store the
id
of both:
Create Users
Create Packages
We will work on the assumption that you have pulled the following user and have all the following details to hand:
Ruby
Fetch the id of the plan
You may have noticed that there’s noid
for the plan
in the details we have provided. We’re therefore going to need to fetch the id
of the plan the user is subscribing to from Kana.
Send a plans query
We’ll need to first fetch all your plans from within Kana via theplans query operation. We’ll define the query string for the request to pull back the name and id of each plan, and then send the request to Kana:Ruby
Filter for the correct plan
We now have a returned list of all the plans within the workspace, which will look a bit like so:plans
hash) so we can filter by this when when iterating over the returned plans.
We also need to ensure that the id
of the plan is captured in an array as an integer. This is what the subscribe mutation requires.
The following should do the trick:
Ruby
What if I want to select more than one plan to subscribe a user to?This is possible - you will need to filter over the plans so that more than one
id
is inserted into the given array.Send the request to subscribe the user
Both theid
of the user and the id
of the plan are now accessible. We’re ready to send the request to subscribe the user to the plan.
We do this through the subscribe mutation. This requires two arguments:
What format should the userId be in?We expect the
userId
argument to be a String - as this matches the id
on the User object. If any other field type is used, an error will occur telling you we can’t accept any field type except String.id
of the plan now in an array as fetched from the previous step.
We’ll define these as variables to send alongside the query:
Ruby
-
The
name
of the plan (which helps in confirming it’s correct) -
If the plan is an add-on through the
isAddon
boolean -
The
features
associated to the plans - with theirname
,type
andlimit
Ruby
Ruby
Congratulations 🎉 You’ve now successfully subscribed a user to a plan.