mirror of
https://github.com/yusufipk/OpenFrame.git
synced 2026-09-11 09:36:08 +00:00
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:
@@ -15,8 +15,14 @@ import {
|
||||
createVerificationToken,
|
||||
isEmailVerificationEnabled,
|
||||
sendVerificationEmail,
|
||||
warnIfTrialsSkipVerification,
|
||||
} from '@/lib/email-verification';
|
||||
import { isValidEmailAddress, normalizeEmail } from '@/lib/email-validation';
|
||||
import {
|
||||
isDisposableEmailDomain,
|
||||
isValidEmailAddress,
|
||||
normalizeEmail,
|
||||
} from '@/lib/email-validation';
|
||||
import { startCardlessTrial } from '@/lib/billing';
|
||||
import { recordSignupCompleted } from '@/lib/analytics/signup';
|
||||
import { readRequestVisitor } from '@/lib/analytics/visitor';
|
||||
|
||||
@@ -49,6 +55,12 @@ export async function POST(request: NextRequest) {
|
||||
return apiErrors.validationError('Invalid email format');
|
||||
}
|
||||
|
||||
// Checked before the invitation branch reads its token, but only applied to
|
||||
// people signing themselves up: an invited collaborator was vouched for by a
|
||||
// paying customer, and refusing their address breaks that customer's review
|
||||
// rather than stopping anyone from farming trials.
|
||||
const isDisposableAddress = isDisposableEmailDomain(normalizedEmail);
|
||||
|
||||
// Allow registration via a valid invitation token OR global invite code.
|
||||
let invitationIsValid = false;
|
||||
let validatedInvitationToken: string | null = null;
|
||||
@@ -85,6 +97,12 @@ export async function POST(request: NextRequest) {
|
||||
}
|
||||
}
|
||||
|
||||
if (!invitationIsValid && isDisposableAddress) {
|
||||
return apiErrors.badRequest(
|
||||
'Please sign up with a permanent email address. Disposable mailboxes are not accepted.'
|
||||
);
|
||||
}
|
||||
|
||||
if (!password || typeof password !== 'string' || password.length < 8 || password.length > 128) {
|
||||
return apiErrors.badRequest('Password must be between 8 and 128 characters');
|
||||
}
|
||||
@@ -142,6 +160,15 @@ export async function POST(request: NextRequest) {
|
||||
visitor: await readRequestVisitor(request),
|
||||
});
|
||||
|
||||
// With SMTP configured the trial starts when the address is proven, not here.
|
||||
// Without it there is no verification step to hang the trial on and the
|
||||
// account is already marked verified above, so withholding the trial would
|
||||
// just lock the user out of an instance that has billing switched on.
|
||||
if (!emailVerificationRequired) {
|
||||
warnIfTrialsSkipVerification();
|
||||
await startCardlessTrial(user.id);
|
||||
}
|
||||
|
||||
// Send verification email if SMTP is configured
|
||||
if (emailVerificationRequired) {
|
||||
const verificationToken = await createVerificationToken(normalizedEmail);
|
||||
|
||||
@@ -1,11 +1,7 @@
|
||||
import { NextRequest } from 'next/server';
|
||||
import { auth } from '@/lib/auth';
|
||||
import { apiErrors, successResponse, withCacheControl } from '@/lib/api-response';
|
||||
import {
|
||||
DEFAULT_TRIAL_PERIOD_DAYS,
|
||||
getOrCreateStripeCustomerId,
|
||||
getStripeCheckoutState,
|
||||
} from '@/lib/billing';
|
||||
import { getOrCreateStripeCustomerId, getStripeCheckoutState } from '@/lib/billing';
|
||||
import { rateLimit } from '@/lib/rate-limit';
|
||||
import { isStripeFeatureEnabled } from '@/lib/feature-flags';
|
||||
import { getStripe, getStripePriceId, isStripeConfigured } from '@/lib/stripe';
|
||||
@@ -73,11 +69,13 @@ export async function POST(request: NextRequest) {
|
||||
metadata: {
|
||||
userId: session.user.id,
|
||||
},
|
||||
// No trial here. The free trial is granted in the product when the email
|
||||
// address is verified, so by the time anyone reaches checkout they have
|
||||
// already had it and this subscription bills immediately.
|
||||
subscription_data: {
|
||||
metadata: {
|
||||
userId: session.user.id,
|
||||
},
|
||||
...(checkoutState.isTrialEligible ? { trial_period_days: DEFAULT_TRIAL_PERIOD_DAYS } : {}),
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@ export async function GET() {
|
||||
hasRecoverableSubscription: billing.subscription.hasRecoverableSubscription,
|
||||
hasActiveTrial: billing.subscription.hasActiveTrial,
|
||||
hasBillingAccess: billing.subscription.hasBillingAccess,
|
||||
isTrialEligible: billing.subscription.isTrialEligible,
|
||||
isPaid: billing.subscription.isPaid,
|
||||
priceId: billing.subscription.stripePriceId,
|
||||
currentPeriodEnd: billing.subscription.currentPeriodEnd?.toISOString() ?? null,
|
||||
cancelAtPeriodEnd: billing.subscription.cancelAtPeriodEnd ?? false,
|
||||
|
||||
@@ -3,7 +3,8 @@ import { db } from '@/lib/db';
|
||||
import { auth, checkWorkspaceAccess } from '@/lib/auth';
|
||||
import { ProjectVisibility } from '@prisma/client';
|
||||
import { rateLimit } from '@/lib/rate-limit';
|
||||
import { buildBillingAccessWhereInput } from '@/lib/billing';
|
||||
import { buildBillingAccessWhereInput, isPaidTier } from '@/lib/billing';
|
||||
import { TRIAL_PROJECT_LIMIT } from '@/lib/trial-limits';
|
||||
import { apiErrors, successResponse, withCacheControl } from '@/lib/api-response';
|
||||
import { DEFAULT_COMMENT_TAGS } from '@/lib/comment-tags';
|
||||
import { logError } from '@/lib/logger';
|
||||
@@ -161,6 +162,26 @@ export async function POST(request: NextRequest) {
|
||||
return apiErrors.forbidden('Only workspace owners and admins can create projects');
|
||||
}
|
||||
|
||||
// Counted against the workspace owner rather than the caller, because that is
|
||||
// the account being billed: `ownerId` on the project below is the workspace
|
||||
// owner too. A workspace admin on somebody else's trial hits the same ceiling.
|
||||
const owner = await db.user.findUnique({
|
||||
where: { id: workspace.ownerId },
|
||||
select: { subscriptionStatus: true, stripeCurrentPeriodEnd: true },
|
||||
});
|
||||
|
||||
if (owner && !isPaidTier(owner)) {
|
||||
const ownedProjectCount = await db.project.count({
|
||||
where: { ownerId: workspace.ownerId },
|
||||
});
|
||||
|
||||
if (ownedProjectCount >= TRIAL_PROJECT_LIMIT) {
|
||||
return apiErrors.forbidden(
|
||||
'Your free trial covers one project at a time. Delete the existing project or subscribe to run more in parallel.'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const project = await db.$transaction(async (tx) => {
|
||||
const createdProject = await tx.project.create({
|
||||
data: {
|
||||
|
||||
Reference in New Issue
Block a user