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.
Must return Subscription model. See Subscription model documentation.
See Error Responses.
import { Subscription } from '../models/Subscription'
app.get('/churnkey/customers/:customerId/subscriptions/:id', async (req, res) => {
const subscription = await db.findSubscriptionById(req.params.id)
if (!subscription) {
return res.status(404).send({ code: 404, message: 'Subscription not found' })
}
res.send(new Subscription(subscription))
})
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.
Maximum number of items to return
Cursor for pagination. The actual value is whatever you decided to use as next in the response.
Array of items. The type of item is defined in the endpoint documentation, e.g. Customer for /customers endpoint
Either a next id or an offset for the next page. You decide what to use, we will send next value as a cursor query parameter back to you. If next is empty, there are no more pages
import { Subscription } from '../models/Subscription'
app.get('/churnkey/customers/:customerId/subscriptions', async (req, res) => {
const limit = Number.parseInt(req.query.limit)
const offset = Number.parseInt(req.query.cursor)
const subscriptions = await db.findSubscriptions({
limit, offset,
customerId: req.params.customerId
})
res.send({
data: subscriptions.map(c => new Subscription(c)),
next: subscriptions.length === limit ? offset + limit : undefined
})
})