Required for Apply Discount Offer to work.
To implement the Apply Coupon action, you need to implement an endpoint and define features
Prerequisites
SDK
If you are using the SDK, you can implement the Apply Coupon 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 { Coupons } from '../controllers/Coupons'
export const ApplyCoupon = Integrator.ApplyCoupon.config({
Subscriptions: Subscriptions,
Coupons: Coupons,
features: {
enabled: true
},
async handle(ctx, options) {
const subscription = await this.subscriptions.retrieve({
customerId: options.customerId,
id: options.subscriptionId
})
const coupon = await this.coupons.retrieve({
id: options.coupon
})
await ctx.db.applyCoupon({ subscription, coupon })
}
})
Endpoints
Handle Required
POST /churnkey/actions/subscription/apply-coupon
This endpoint applies coupon to a subscription. You should find the subscription by customerId and subscriptionId and apply coupon to it.
Options for this action, provided in the request body.
Unique identifier of the customer.
Unique identifier of the subscription.
ID of the Coupon to apply
Must return empty response.
See Error Responses.
app.post('/churnkey/actions/subscription/apply-coupon', async (req, res) => {
const subscription = await db.findSubscription(req.body.customerId, req.body.subscriptionId)
const coupon = await db.findCoupon(req.body.id)
await db.applyCoupon({subscription, coupon})
res.send()
})
Features required
Features define which behavior is supported for the Apply Coupon 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.
export interface Features {
enabled: boolean
}
export const features: Features = {
enabled: true,
}