Required for Change Price Offer to work.
To implement the Change Price action, you need to implement an endpoint and define features
Prerequisites
SDK
If you are using the SDK, you can implement the Change Price action 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 { Subscriptions } from '../controllers/Subscriptions'
import { Prices } from '../controllers/Prices'
export const ChangePrice = Integrator.ChangePrice.config({
Subscriptions: Subscriptions,
Prices: Prices,
features: {
enabled: true,
schedules: {
[Integrator.ChangePrice.Schedule.Immediate]: true,
[Integrator.ChangePrice.Schedule.EndOfPeriod]: true
},
prorate: true
},
async handle(ctx, options) {
const subscription = await this.subscriptions.retrieve({
customerId: options.customerId,
id: options.subscriptionId
})
const price = await this.prices.retrieve({
id: options.price
})
await ctx.db.changePrice({ subscription, price, at: options.scheduledAt, prorate: options.prorate })
}
})
Endpoints
Handle Required
POST /churnkey/actions/subscription/apply-coupon
This endpoint changes a price of subscription. You should find the subscription by customerId and subscriptionId and set a new price to it.
Options for this action provided in the request body.
Unique identifier of the customer.
Unique identifier of the subscription.
ID of the Price to change to.
'immediate' 'end-of-period'
Defines when the price change should occur
Indicates if price change should be prorated.
Must return empty response.
See Error Responses.
app.post('/churnkey/actions/subscription/change-price', async (req, res) => {
const subscription = await db.findSubscription(req.body.customerId, req.body.subscriptionId)
const price = await db.findPrice(req.body.id)
await db.changePrice({subscription, price, at: req.body.scheduledAt, prorate: req.body.prorate})
res.send()
})
Features required
Features define which behavior is supported for the Change Price action. Depending on the features you enabled, requests body will have different options.
If true, the action is enabled and can be used by Churnkey, otherwise it is disabled.
Defines supported duration types. At least one duration type should be enabled.
If your system supports proration, set this to true
export interface Features {
enabled: boolean
schedules: {
immediate: boolean,
'end-of-period': boolean
},
prorate: boolean
}
export const features: Features = {
enabled: true,
schedules: {
immediate: true,
'end-of-period': true
},
prorate: true
}