feat(billing): let people try the product before handing over a card

The trial now starts inside the product, at email verification, and Stripe
grants none at all: checkout creates a subscription that bills immediately.
Verifying an address is what buys the seven days, which is also the cheapest
abuse control there is.

An unexpired trial is treated as an entitlement the account already holds, so a
Stripe sync can add access but never retracts a trial that has not run out. That
matters most for the abandoned checkout: the resulting incomplete subscription
carries no trial_end, and writing it through would have erased the days the
account still had and locked it out.

Unpaid accounts are bounded by what they can cost us rather than by what they
can do: one workspace, one project, 3 GiB of direct uploads. YouTube imports,
share links, guests, comments and approvals stay unlimited, because those are
the parts worth trying and they cost nothing. isPaidTier() is the new seam;
hasBillingAccess() answers a different question now that access no longer
implies a card.

Signup CTAs, the pricing card, the comparison pages, the terms and the refund
policy all said the trial converts to a paid plan by itself. It no longer does,
so they say what happens instead. Settings and a banner name both dates that
matter: when the trial ends, and the fifteen days after that during which
nothing is deleted.

/admin/growth compares the two funnels on signup to paid within a fixed 30 day
window, not trial to paid. Dropping the card requirement multiplies trials, so
the old ratio can fall while more people actually pay, and reading it that way
would retire the change for the wrong reason.
This commit is contained in:
yusufipk
2026-08-05 19:40:36 +03:00
parent b8d68a9196
commit 39e81042bb
34 changed files with 1541 additions and 134 deletions
+36 -8
View File
@@ -3,10 +3,29 @@ import { db } from '@/lib/db';
import { apiErrors } from '@/lib/api-response';
import { isStripeFeatureEnabled } from '@/lib/feature-flags';
import { getCachedUserBunnyStorage } from '@/lib/admin-stats';
import { isPaidTier } from '@/lib/billing';
import { getStorageLimitBytes } from '@/lib/trial-limits';
// 200 GB expressed in bytes
export const PLAN_STORAGE_LIMIT_BYTES = BigInt(200) * BigInt(1024) * BigInt(1024) * BigInt(1024);
/**
* The ceiling this particular account is held to.
*
* A cardless trial gets a much smaller one: it is the only thing standing between
* a throwaway signup and 200 GB of our storage. Reads the two billing columns
* directly rather than taking a flag from the caller, so no upload route can
* forget to pass it.
*/
async function getStorageLimitForUser(userId: string): Promise<bigint> {
const user = await db.user.findUnique({
where: { id: userId },
select: { subscriptionStatus: true, stripeCurrentPeriodEnd: true },
});
return getStorageLimitBytes(user ? isPaidTier(user) : false, PLAN_STORAGE_LIMIT_BYTES);
}
// TTL for upload reservations: 30 minutes is enough for R2 image/audio uploads
const RESERVATION_TTL_MS = 30 * 60 * 1000;
@@ -61,8 +80,10 @@ export async function getUserStorageInfo(userId: string): Promise<{
limitBytes: bigint;
percentage: number;
}> {
const usedBytes = await getUserTotalStorageBytes(userId);
const limitBytes = PLAN_STORAGE_LIMIT_BYTES;
const [usedBytes, limitBytes] = await Promise.all([
getUserTotalStorageBytes(userId),
getStorageLimitForUser(userId),
]);
const percentage =
limitBytes > BigInt(0)
? Math.min(100, Number((usedBytes * BigInt(10000)) / limitBytes) / 100)
@@ -88,9 +109,12 @@ export async function enforceStorageQuota(
return null;
}
const usedBytes = await getUserTotalStorageBytes(userId);
const [usedBytes, limitBytes] = await Promise.all([
getUserTotalStorageBytes(userId),
getStorageLimitForUser(userId),
]);
if (usedBytes + incomingSizeBytes >= PLAN_STORAGE_LIMIT_BYTES) {
if (usedBytes + incomingSizeBytes >= limitBytes) {
return apiErrors.storageExceeded() as NextResponse;
}
@@ -121,9 +145,13 @@ export async function reserveStorageQuota(
const expiresAt = new Date(Date.now() + reservationTtlMs);
// Fetch Bunny storage BEFORE entering the transaction to avoid holding the
// advisory lock during a potentially slow/failing HTTP call on cache miss.
const bunnyData = await getCachedUserBunnyStorage();
// Fetch Bunny storage and the account's ceiling BEFORE entering the transaction,
// to avoid holding the advisory lock during a potentially slow/failing HTTP call
// on cache miss or an extra round trip to Postgres.
const [bunnyData, limitBytes] = await Promise.all([
getCachedUserBunnyStorage(),
getStorageLimitForUser(userId),
]);
const bunnyBytes = BigInt(bunnyData[userId] ?? 0);
try {
@@ -166,7 +194,7 @@ export async function reserveStorageQuota(
const reservedBytes = resRow?.total ?? BigInt(0);
const totalUsed = r2Bytes + reservedBytes + bunnyBytes;
if (totalUsed + incomingSizeBytes >= PLAN_STORAGE_LIMIT_BYTES) {
if (totalUsed + incomingSizeBytes >= limitBytes) {
throw new QuotaExceededError();
}