Skip to content

Open source CapCut alternative.

TypeScript

feat(site): a Pro period's allowance is multiplied under the allowance promotion

David Le2f3105379e66
diff --git a/site/src/app/cut/_components/landing/cutPricingPlans.ts b/site/src/app/cut/_components/landing/cutPricingPlans.ts
index 1d9b3938..090e720d 100644
--- a/site/src/app/cut/_components/landing/cutPricingPlans.ts
+++ b/site/src/app/cut/_components/landing/cutPricingPlans.ts
@@ -1,10 +1,13 @@
import type { PricingPlan } from "@/app/_components/landing/pricingPlans";
+import { promotionCovers, type AllowancePromotion } from "@/lib/billing/allowance-promotion";
// What Pro costs and what it gets you, in one place: the landing card below
// and the welcome sequence's last slide both read it, so the price can't say
// two different things in two parts of the product.
export const CUT_PRO = {
body: "Generate rich media with AI.",
+ // The plan price in USD a month. The included AI allowance equals it.
+ monthlyDollars: 20,
// What Pro adds. The landing card sits beside the Free card and opens with a
// line pointing at it; nothing else does, so that line isn't in here.
features: [
@@ -15,6 +18,21 @@ export const CUT_PRO = {
price: "$20/month",
};
+// The allowance promotion as the Pro card states it, while a period starting
+// now would get it: the multiplied allowance in USD and the last day. Null
+// when the plain plan applies.
+export function activeProPromotion(
+ promotion: AllowancePromotion,
+ now = new Date(),
+): { allowanceDollars: number; multiplier: number; lastDay: string | null } | null {
+ if (!promotionCovers(promotion, now)) return null;
+ return {
+ allowanceDollars: CUT_PRO.monthlyDollars * promotion.multiplier,
+ multiplier: promotion.multiplier,
+ lastDay: promotion.lastDay,
+ };
+}
+
// Cut's two tiers: the editor itself is free and local; Pro adds monthly AI
// generation credits.
export function cutPricingPlans(): PricingPlan[] {
diff --git a/site/src/app/cut/app/(home)/settings/_components/ProCard.tsx b/site/src/app/cut/app/(home)/settings/_components/ProCard.tsx
index 5e83d676..e637fb71 100644
--- a/site/src/app/cut/app/(home)/settings/_components/ProCard.tsx
+++ b/site/src/app/cut/app/(home)/settings/_components/ProCard.tsx
@@ -16,13 +16,18 @@ import {
CardTitle,
} from "@/components/ui/card";
import { Skeleton } from "@/components/ui/skeleton";
+import { CUT_PRO } from "@/app/cut/_components/landing/cutPricingPlans";
+import { formatDeadline, minutesLeft } from "@/cut/components/SubscribeBonusPill";
import { track } from "@/lib/analytics";
import { formatUsd } from "@/lib/credits/format-usd";
+import { formatCreditExpiry } from "@/lib/credits/top-up";
+import { useSubscribeBonus } from "@/queries/credits";
export function ProCard() {
const pro = useProSubscription();
const checkout = useStartCheckout();
const portal = useOpenBillingPortal();
+ const bonus = useSubscribeBonus();
if (pro.isLoading) {
return (
@@ -39,6 +44,8 @@ export function ProCard() {
const data = pro.data;
const isActive = data?.isActive ?? false;
+ const offer = bonus.data;
+ const offerOpen = offer?.status === "open";
return (
<Card>
@@ -72,8 +79,20 @@ export function ProCard() {
Cancels at the end of the current period.
</div>
) : null}
+ {offer?.status === "claimed" ? (
+ <div>Your {formatUsd(offer.dollars)} subscribe bonus landed in your credits.</div>
+ ) : null}
</div>
</CardContent>
+ ) : offer && offerOpen ? (
+ <CardContent className="text-sm text-foreground">
+ Subscribe for {CUT_PRO.price} by {formatDeadline(offer.closesAt)} and get a one-time{" "}
+ {formatUsd(offer.dollars)} in credits
+ {offer.creditsExpireAt
+ ? `, spendable through ${formatCreditExpiry(new Date(offer.creditsExpireAt))}`
+ : ""}
+ .
+ </CardContent>
) : null}
<CardFooter className="gap-3">
{isActive ? (
@@ -94,6 +113,13 @@ export function ProCard() {
disabled={checkout.isPending}
onClick={() => {
track("pro_checkout_started");
+ if (offer && offerOpen) {
+ track("subscribe_bonus_checkout_started", {
+ dollars: Number(offer.dollars),
+ minutesLeft: minutesLeft(offer.closesAt, Date.now()),
+ source: "settings",
+ });
+ }
checkout.mutate(undefined, {
onSuccess: (result) => window.location.assign(result.url),
});
diff --git a/site/src/lib/billing/allowance-promotion.test.ts b/site/src/lib/billing/allowance-promotion.test.ts
new file mode 100644
index 00000000..200ba60a
--- /dev/null
+++ b/site/src/lib/billing/allowance-promotion.test.ts
@@ -0,0 +1,33 @@
+import { describe, expect, test } from "bun:test";
+
+import { promotedAllowanceMicros, promotionCovers } from "./allowance-promotion";
+
+const base = BigInt(20_000_000);
+const five = BigInt(100_000_000);
+
+describe("promotedAllowanceMicros", () => {
+ test("a multiplier of one is the plain allowance", () => {
+ expect(promotedAllowanceMicros(base, { multiplier: 1, lastDay: null }, new Date())).toBe(base);
+ expect(promotedAllowanceMicros(base, { multiplier: 1, lastDay: "2026-10-29" }, new Date("2026-09-01T00:00:00Z"))).toBe(base);
+ });
+
+ test("a period starting on or before the last day is multiplied", () => {
+ const promo = { multiplier: 5, lastDay: "2026-10-29" };
+ expect(promotedAllowanceMicros(base, promo, new Date("2026-09-10T12:00:00Z"))).toBe(five);
+ expect(promotedAllowanceMicros(base, promo, new Date("2026-10-29T23:59:59Z"))).toBe(five);
+ });
+
+ test("a period starting after the last day reads the plain plan", () => {
+ const promo = { multiplier: 5, lastDay: "2026-10-29" };
+ expect(promotedAllowanceMicros(base, promo, new Date("2026-10-30T00:00:00Z"))).toBe(base);
+ expect(promotedAllowanceMicros(base, promo, new Date("2026-11-10T00:00:00Z"))).toBe(base);
+ });
+
+ test("no last day keeps the multiplier on", () => {
+ expect(promotionCovers({ multiplier: 3, lastDay: null }, new Date("2030-01-01T00:00:00Z"))).toBe(true);
+ });
+
+ test("a malformed day closes the promotion", () => {
+ expect(promotionCovers({ multiplier: 3, lastDay: "not-a-day" }, new Date())).toBe(false);
+ });
+});
diff --git a/site/src/lib/billing/allowance-promotion.ts b/site/src/lib/billing/allowance-promotion.ts
new file mode 100644
index 00000000..61527f1c
--- /dev/null
+++ b/site/src/lib/billing/allowance-promotion.ts
@@ -0,0 +1,29 @@
+// The Pro allowance promotion, as the proAllowancePromotion setting states it:
+// a multiplier on the allowance a billing period starts with, on for every
+// period that starts on or before a last UTC day. Pure, so the grant, the Pro
+// card and the tests read one rule.
+
+export type AllowancePromotion = {
+ multiplier: number;
+ // YYYY-MM-DD, or null for no end.
+ lastDay: string | null;
+};
+
+const DAY_MS = 86_400_000;
+
+/** Whether a period starting at `at` gets the multiplier. */
+export function promotionCovers(promotion: AllowancePromotion, at: Date): boolean {
+ if (promotion.multiplier <= 1) return false;
+ if (promotion.lastDay === null) return true;
+ const closes = Date.parse(`${promotion.lastDay}T00:00:00Z`) + DAY_MS;
+ return Number.isFinite(closes) && at.getTime() < closes;
+}
+
+/** The allowance a period starting at `periodStart` is granted. */
+export function promotedAllowanceMicros(
+ baseMicros: bigint,
+ promotion: AllowancePromotion,
+ periodStart: Date,
+): bigint {
+ return promotionCovers(promotion, periodStart) ? baseMicros * BigInt(promotion.multiplier) : baseMicros;
+}
diff --git a/site/src/lib/billing/pro-subscription.ts b/site/src/lib/billing/pro-subscription.ts
index f8cdd2b4..1ef5cdc7 100644
--- a/site/src/lib/billing/pro-subscription.ts
+++ b/site/src/lib/billing/pro-subscription.ts
@@ -7,6 +7,7 @@ import {
} from "@/lib/billing/stripe";
import { creditMicrosPerCent, zeroCreditMicros } from "@/lib/credits/amounts";
import { grantCredits } from "@/lib/credits/inference";
+import { claimSubscribeBonus, SUBSCRIBE_BONUS_METADATA_KEY } from "@/lib/credits/subscribe-bonus-claim";
import { prisma } from "@/lib/prisma";
// Donkey Pro: the Mac app subscription.
@@ -122,6 +123,13 @@ export async function syncProSubscription(
where: { userId },
});
+ // A subscription whose checkout was taken up from the subscribe bonus
+ // offer names the offer; the claim dedupes, so every later event is a no-op.
+ const offerId = subscription.metadata?.[SUBSCRIBE_BONUS_METADATA_KEY];
+ if (isActiveProStatus(subscription.status) && offerId) {
+ await claimSubscribeBonus(userId, offerId);
+ }
+
// Grant the period's included allowance once it is active and the period is
// known. expiresAt = period end means it is spent before never-expiring
// purchased credits (see compareGrantConsumptionOrder) and does not roll over.
diff --git a/site/src/lib/config/registry.ts b/site/src/lib/config/registry.ts
index d6a3cebb..0c93b9a0 100644
--- a/site/src/lib/config/registry.ts
+++ b/site/src/lib/config/registry.ts
@@ -79,11 +79,32 @@ export const SETTINGS = defineSettings({
title: "Credit expiry notice",
description: "How many days before signup or manually granted credits expire the account is emailed.",
},
+ subscribeBonus: {
+ schema: z
+ .object({
+ // USD granted for subscribing to Pro while the offer is open; 0 makes
+ // no offer.
+ dollars: z.number().int().min(0).max(maxCreditGrantDollars),
+ // Share of the signup grant spent before the offer opens.
+ spentPercent: z.number().int().min(1).max(100),
+ // Hours the offer stays open once the app has shown it.
+ windowHours: z.number().int().min(1).max(24 * 30),
+ // The last UTC day the bonus credit is spendable (YYYY-MM-DD); null
+ // keeps it forever.
+ creditsExpireOn: z.string().regex(/^\d{4}-\d{2}-\d{2}$/, "YYYY-MM-DD").nullable(),
+ })
+ .strict(),
+ default: { dollars: 0, spentPercent: 50, windowHours: 24, creditsExpireOn: null },
+ public: true,
+ title: "Subscribe bonus",
+ description:
+ "Credit offered to an account that has spent this share of its signup grant, for subscribing to Pro within the window. The credit is spendable through the last day.",
+ },
});
// The settings su shows on its Product tab: what an account gets. The
// settings tab under Experiments still lists every key.
-export const PRODUCT_SETTING_KEYS = ["signupCredits", "creditExpiryNotice"] as const satisfies readonly SettingKey[];
+export const PRODUCT_SETTING_KEYS = ["signupCredits", "creditExpiryNotice", "subscribeBonus"] as const satisfies readonly SettingKey[];
export type SettingKey = keyof typeof SETTINGS;
export type Settings = SettingsOf<typeof SETTINGS>;