Add Remote Paywalls and a Customer Center
Offerings you publish without an app release, a paywall per placement, self-serve cancel and refund, and anonymous-to-account merge.
Install
npm i @dapplelight/signet-server @dapplelight/signet-db hono drizzle-ormPlans and entitlements come first: see Add plans.
Add the tables
import { monetisationTables } from "@dapplelight/signet-db/columns/sqlite" // or /columns/pg
export const { offerings, placements, customerIdentities, customerEvents, customerRequests } =
monetisationTables()Publish an offering and point a placement at it
import { createOfferings, plansFromProducts } from "@dapplelight/signet-db/offerings"
import { createEntitlements } from "@dapplelight/signet-db/entitlements"
import { actor } from "./actor"
import { db } from "./db"
import { offerings, placements } from "./db/schema"
import { entitlementOverrides, plans, subjectPlans, usageCounters } from "./entitlements"
const products = [
{ id: "pro.monthly", store: "appstore", plan: "pro", interval: "month" },
{ id: "pro_monthly", store: "googleplay", plan: "pro", interval: "month" },
{ id: "price_pro_m", store: "web", plan: "pro", interval: "month", price: { amount: 49900, currency: "INR" } },
] as const
export const off = createOfferings(db, { tables: { offerings, placements }, plans })
export const ent = createEntitlements(db, {
plans,
default: "free",
tables: { subjectPlans, entitlementOverrides, usageCounters },
billing: { plans: plansFromProducts(products) }, // what the paywall sells is what billing grants
})
await off.publish("default", {
products,
paywall: { title: "Go pro", plans: ["pro"], highlight: "pro", badge: "Best value", trial: "7 days free", cta: "Start trial" },
}, { actor })
await off.setPlacement("onboarding", {
offering: "default",
overrides: [{ when: { country: ["IN"], platform: ["web"] }, offering: "india" }],
}, { actor })Each publish is an immutable version. Pin one with version: 1 to roll back.
Record customers and run the Customer Center
import { createCustomers } from "@dapplelight/signet-db/customers"
import { entitlementTables } from "@dapplelight/signet-db/columns/sqlite"
import { createPayments } from "@dapplelight/signet-payments"
import { customerEvents, customerIdentities, customerRequests } from "./db/schema"
import { gateways, store } from "./gateways"
import { applyCoupon, ownerOf } from "./billing"
import { notifier, teamId } from "./notifier"
export const customers = createCustomers(db, {
tables: { ...entitlementTables, customerIdentities, customerEvents, customerRequests },
entitlements: ent,
payments: store,
notify: (n) => notifier.notify(teamId, "customer.request", { kind: n.kind, text: n.text }),
cancel: (s) => payments.gateway(s.gateway).cancelSubscription(s.subscriptionId, { atPeriodEnd: true }),
retention: { offer: { kind: "discount", percent: 50, months: 3 }, apply: ({ source }) => applyCoupon(source) },
})
export const payments = createPayments({
gateways,
db: store,
onEvent: async (e) => {
const subject = await customers.resolve(ownerOf(e))
await ent.applyBillingEvent(e, { subject })
await customers.record(e, { subject })
},
})Merge an anonymous device into an account
import { customers } from "../monetisation"
export async function signIn(orgId: string, userId: string) {
const anon = await customers.anonymousId({ orgId }) // "anon_<uuid>", before sign-in
const r = await customers.merge({ anonymousId: anon, userId, orgId })
// { merged: true, already: false, moved: { subscriptions, overrides, events, requests, payments } }
}Mount the routes
import { monetisationAdminRoutes, monetisationRoutes } from "@dapplelight/signet-server/http/monetisation"
import { Hono } from "hono"
import { customers, off } from "./monetisation"
const app = new Hono()
const me = (c) => c.get("session")?.sub
app.route("/api/billing", monetisationRoutes({
offerings: off,
customers,
subject: (c) => me(c) ?? c.req.header("x-app-user"),
userId: me,
}))
app.route("/api/admin/billing", monetisationAdminRoutes({
offerings: off,
customers,
authorize: (c) => c.get("session")?.role === "admin", // deny by default
actor: (c) => ({ type: "user", id: me(c) }),
}))Put csrfMiddleware in front when the session cookie is SameSite=None.
Render it
import { RemotePaywall } from "@dapplelight/signet-ui/screens/monetisation-screens"