mirror of
https://github.com/yusufipk/OpenFrame.git
synced 2026-09-11 17:46:06 +00:00
feat(billing): defer the cardless trial for invited collaborators
An account that signs up through an invitation works on the inviter's billing, so handing it a trial at signup spent its only trial before it owned anything. The trial is now held back for collaborators and claimed only explicitly: a Start Free Trial button on the new-workspace and billing screens calls the new POST /api/billing/trial endpoint, which grants the once-per-account trial atomically. Nothing starts the clock as a side effect, and pure collaborators no longer see a trial-ending banner about work that is not theirs.
This commit is contained in:
@@ -22,7 +22,7 @@ import {
|
||||
isValidEmailAddress,
|
||||
normalizeEmail,
|
||||
} from '@/lib/email-validation';
|
||||
import { startCardlessTrial } from '@/lib/billing';
|
||||
import { startCardlessTrialOnSignup } from '@/lib/billing';
|
||||
import { recordSignupCompleted } from '@/lib/analytics/signup';
|
||||
import { readRequestVisitor } from '@/lib/analytics/visitor';
|
||||
|
||||
@@ -166,7 +166,7 @@ export async function POST(request: NextRequest) {
|
||||
// just lock the user out of an instance that has billing switched on.
|
||||
if (!emailVerificationRequired) {
|
||||
warnIfTrialsSkipVerification();
|
||||
await startCardlessTrial(user.id);
|
||||
await startCardlessTrialOnSignup(user.id);
|
||||
}
|
||||
|
||||
// Send verification email if SMTP is configured
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
import { NextRequest } from 'next/server';
|
||||
import { auth } from '@/lib/auth';
|
||||
import { apiErrors, successResponse } from '@/lib/api-response';
|
||||
import { startCardlessTrial } from '@/lib/billing';
|
||||
import { rateLimit } from '@/lib/rate-limit';
|
||||
import { isStripeFeatureEnabled } from '@/lib/feature-flags';
|
||||
import { isTrustedSameOriginRequest } from '@/lib/request-origin';
|
||||
import { logError } from '@/lib/logger';
|
||||
import { db } from '@/lib/db';
|
||||
|
||||
/**
|
||||
* The explicit claim of a deferred cardless trial.
|
||||
*
|
||||
* An invited collaborator has their trial held back at signup; nothing else in
|
||||
* the product is allowed to start it as a side effect, because the clock spends
|
||||
* the account's only trial. This endpoint is the one place the user says "start
|
||||
* it now", from the workspace-creation and billing screens.
|
||||
*/
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const limited = await rateLimit(request, 'mutate');
|
||||
if (limited) return limited;
|
||||
|
||||
if (!isTrustedSameOriginRequest(request)) {
|
||||
return apiErrors.forbidden('Invalid request origin');
|
||||
}
|
||||
|
||||
const session = await auth();
|
||||
if (!session?.user?.id) {
|
||||
return apiErrors.unauthorized();
|
||||
}
|
||||
|
||||
if (!isStripeFeatureEnabled()) {
|
||||
return apiErrors.badRequest('Stripe billing is disabled by this host');
|
||||
}
|
||||
|
||||
const started = await startCardlessTrial(session.user.id);
|
||||
if (!started) {
|
||||
return apiErrors.conflict('Your free trial has already been used');
|
||||
}
|
||||
|
||||
const user = await db.user.findUnique({
|
||||
where: { id: session.user.id },
|
||||
select: { trialEndsAt: true },
|
||||
});
|
||||
|
||||
return successResponse({ trialEndsAt: user?.trialEndsAt ?? null });
|
||||
} catch (error) {
|
||||
logError('billing.trial.start', error);
|
||||
return apiErrors.internalError();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user