Skip to Content

Add Web-to-App Funnels With Hosted Checkout

A hosted quiz and checkout page with Apple Pay, Google Pay and URL discounts, then a one-time link that moves the purchase into the app.

Install

npm i @dapplelight/signet-server @dapplelight/signet-payments @dapplelight/signet-db hono drizzle-orm

Define the funnel

src/server/funnels.ts
import { createFunnels, funnelTables } from "@dapplelight/signet-db/funnels"
import { redemptionLink } from "@dapplelight/signet-server/http/funnels"
import { sink } from "./analytics"
import { db } from "./db"
import { customerEvents } from "./db/schema"
import { entitlements } from "./entitlements"
import { env } from "./env"
import { sendMail } from "./mail"
import { customers, off as offerings } from "./monetisation"
import { payments } from "./payments"

const opts = { secret: env.FUNNEL_SECRET, redeemUrl: "https://app.example.com/redeem" }

export const funnels = createFunnels(db, {
  tables: { ...funnelTables(), customerEvents },
  funnels: [{
    key: "spring", placement: "web", title: "Sleep better",
    questions: [{ id: "goal", title: "Your goal?", options: [{ value: "sleep", label: "Sleep" }] }],
  }],
  discounts: [{ code: "SPRING25", percentOff: 25, expiresAt: "2027-04-01", maxRedemptions: 500, plans: ["pro"] }],
  offerings, customers, entitlements,
  checkout: (input) => payments.checkout(input),
  track: (e) => sink.write([e]),
  onPaid: async (s) => s.email && sendMail(s.email, (await redemptionLink(opts, s.id)).link),
})

Apply gateway events

src/server/payments.ts
import { createPayments } from "@dapplelight/signet-payments"
import { entitlements } from "./entitlements"
import { gateways } from "./gateways"
import { funnels } from "./funnels"
import { customers } from "./monetisation"

export const payments = createPayments({
  ...gateways,
  onEvent: async (e) => {
    await entitlements.applyBillingEvent(e)
    await customers.record(e)
    await funnels.applyEvent(e)
  },
})

Nothing is granted until the gateway webhook signature verifies.

Mount the routes

src/app.ts
import { Hono } from "hono"
import { funnelRoutes } from "@dapplelight/signet-server/http/funnels"
import { rateLimit } from "@dapplelight/signet-server/middleware/rate-limit"
import { env } from "./server/env"
import { funnels } from "./server/funnels"
import { payments } from "./server/payments"

const app = new Hono()

app.use("/f/:key/checkout", rateLimit({ scope: "funnel-checkout", max: 10, trust: "cloudflare" }))
app.use("/f/redeem", rateLimit({ scope: "funnel-redeem", max: 10, trust: "cloudflare" }))
app.route("/f", funnelRoutes({
  funnels,
  secret: env.FUNNEL_SECRET,
  redeemUrl: "https://app.example.com/redeem",
  userId: (c) => c.get("session")?.sub,
  page: { stripePublishableKey: env.STRIPE_PUBLISHABLE_KEY, razorpayKeyId: env.RAZORPAY_KEY_ID },
}))
app.post("/webhooks/:gateway", (c) => payments.handleWebhook(c.req.param("gateway"), c.req.raw))

FUNNEL_SECRET must be 32 or more characters.

Redeem in the app

src/web/redeem.ts
import { api } from "./api"

export async function redeem(token: string) {
  await api.post("/f/redeem", { token })
}

This moves the anonymous buyer's plan, payments and history to the signed-in account. A token works once.

Read drop-off

src/server/reports.ts
import { funnelSteps, type FunnelDefinition } from "@dapplelight/signet-db/funnels"
import { store } from "./analytics"

export async function readDropOff(def: FunnelDefinition) {
  return store.funnel({ steps: funnelSteps(def) })
}

Visit /f/spring?code=SPRING25 to price the discount on the server.

Next