Price Model
Type
Depending on your app's architecture, prices can belong to a Product or be standalone entities. In both cases, you can start by implementing a standalone Price model, and later refactor it to add products support.
If you implement a product-based price, you will need to create a Products controller and set the productId property on the price.
Properties
The unique identifier of the price
'standalone' 'product'
The type of the price. All prices should have the same type.
Describes the billing cycle of the price
Describes the price amount. There are two types of amounts: tiered and fixed. Some apps support both types, but most common pricing model is fixed. All numeric values are in cents (or equivalent minor currency unit), e.g. $1.10 is represented as 110.
ID of the product this price belongs to. type should be set to 'product'
Customer-facing name of the price. If not provided, a name will be generated based on the amount and duration
Internal description of the price, hidden from the customers
Code Example
import { Integrator } from '@churnkey/sdk'
// export class Price extends Integrator.Price.Product {
export class Price extends Integrator.Price.Standalone {
constructor(price: YourPrice) {
super(
{
id: price.id,
... // map other properties
}
)
}
}
interface Price {
id: string
type: Type
duration: Duration
amount: Amount
productId?: string
name?: string
description?: string
}
export function Price(price: YourPrice): Price {
return {
id: price.id,
... // map other properties
}
}
enum Type {
Standalone = "standalone",
Product = "product"
}
interface Duration {
amount: number
unit: Unit
}
enum Unit {
Month = "month",
Year = "year"
}
type Amount = FixedAmount | TieredAmount
interface FixedAmount {
model: Model.Fixed
currency: string
unit?: number
flat?: number
}
interface TieredAmount {
model: Model.Tiered
currency: string
mode: Mode
tiers: Tier[]
}
enum Model {
Fixed = "fixed",
Tiered = "tiered"
}
enum Mode {
Graduated = "graduated",
Total = "total"
}
interface Tier {
unit?: number
flat?: number
upTo?: number
}
package models
type Price struct {
ID string `json:"id"`
Type Type `json:"type"`
Duration Duration `json:"duration"`
Amount Amount `json:"amount"`
ProductID *string `json:"productId"`
Name *string `json:"name"`
Description *string `json:"description"`
}
type Type string
const (
Standalone Type = "standalone"
Product Type = "product"
)
type Duration struct {
Amount int `json:"amount"`
Unit Unit `json:"unit"`
}
type Unit string
const (
Month Unit = "month"
Year Unit = "year"
)
type Amount interface {
PricingModel() Model
}
type FixedAmount struct {
Model Model `json:"model"`
Currency string `json:"currency"`
Unit *int `json:"unit"`
Flat *int `json:"flat"`
}
func (FixedAmount) PricingModel() Model {
return Fixed
}
type TieredAmount struct {
Model Model `json:"model"`
Currency string `json:"currency"`
Mode Mode `json:"mode"`
Tiers []Tier `json:"tiers"`
}
func (TieredAmount) PricingModel() Model {
return Tiered
}
type Model string
const (
Fixed Model = "fixed"
Tiered Model = "tiered"
)
type Mode string
const (
Graduated Mode = "graduated"
Total Mode = "total"
)
type Tier struct {
Unit *int `json:"unit"`
Flat *int `json:"flat"`
UpTo *int `json:"upTo"`
}
func Price(price YourPrice) Price {
return Price{
ID: price.ID,
... // map other properties
}
}
Customer Model
A customer is a user who is being billed for a subscription. This model includes information about the customer, such as their name, email, address, etc.
Subscription Model
A subscription is a recurring payment for a product or service. This model includes information about the subscription, including its status, billing cycle, items, and discounts.