Subscriptions - Controller
To implement the Subscriptions
controller, you need to implement 2 API endpoints, we will use these endpoints to fetch subscriptions from your system.
Prerequisites
SDK
If you are using the SDK, you can implement the Subscriptions
controller by following the code example below. You don't need to get into the details of the API endpoints, the SDK will take care of that for you.
import { Integrator } from '@churnkey/sdk'
import { Context } from '../Context'
import { Subscription } from '../models/Subscription'
import { Prices } from './Prices'
export const Subscriptions = Integrator.Subscriptions.config({
ctx: Context,
Prices: Prices,
async retrieve(ctx, options) {
const yourSubscription = await ctx.db.findSubscription(options.customerId, options.id)
return new Subscription(yourSubscription)
},
async list(ctx, options) {
const yourSubscriptions = await ctx.db.listSubscriptions({
customerId: options.customerId,
limit: options.limit,
offset: options.cursor // the value you passed as `next` below
})
return {
data: yourSubscriptions.map(subscription => new Subscription(subscription)),
// pass the next cursor if there are more items
next: yourSubscriptions.length === options.limit ? offset + limit : undefined
}
}
})
Endpoints
Retrieve Required
GET /churnkey/customers/:customerId/subscriptions/:id
This endpoint fetches Subscription
by its id
. Usually, implementation will include finding a subscription in your database and mapping it to the Subscription model.
List Optional
GET /churnkey/customers/:customerId/subscriptions
This endpoint fetches a list of subscriptions from your database. You should find subscriptions in your database (with pagination), map them to the Subscription
model and return a paginated list.