feat(billing): payment infrastructure pre-prep

db: add plan, planExpiresAt, planProvider to tenant_settings (Appwrite MCP)

- schema.ts: TenantPlan type, TenantSettings plan fields
- subscription-types.ts: Emlak plan catalog (Free / Pro 499₺/ay)
- plan-limits.ts: resource limits (properties/customers/members/presentations)
  + getPlanUsage, requirePlanCapacity, PlanLimitError helpers
- subscription-actions.ts: startCheckoutAction (Polar→Shopier→mock fallback),
  activatePlanInDb / deactivatePlanInDb for webhook handlers,
  downgradeToFreeAction, getCurrentPlanAction
- /api/payments/polar/callback: verify webhook → activatePlanInDb on order/subscription events
- /api/payments/shopier/callback: verify HMAC → activate on fulfilled+paid (tenant
  email-matching TODO pending Shopier metadata support)
- /settings/billing: CurrentPlanCard with usage progress bars + UpgradeSection
- sidebar: Plan & Faturalama nav item
- PlanLimitDialog: Emlak-specific feature list
This commit is contained in:
egecankomur
2026-05-08 15:26:18 +03:00
parent 95a7cbaf0d
commit 3cce632eb3
11 changed files with 633 additions and 4 deletions
+107
View File
@@ -0,0 +1,107 @@
import "server-only";
import { Query } from "node-appwrite";
import { createAdminClient } from "./server";
import { DATABASE_ID, TABLES, type TenantPlan } from "./schema";
import type { TenantContext } from "./tenant-guard";
export type PlanResource = "properties" | "customers" | "members" | "presentations";
export const PLAN_LIMIT_EXCEEDED = "PLAN_LIMIT_EXCEEDED";
const INF = Number.POSITIVE_INFINITY;
export const PLAN_LIMITS: Record<TenantPlan, Record<PlanResource, number>> = {
free: {
properties: 5,
customers: 10,
members: 2,
presentations: 3,
},
pro: {
properties: INF,
customers: INF,
members: INF,
presentations: INF,
},
};
export const RESOURCE_LABELS: Record<PlanResource, string> = {
properties: "ilan",
customers: "müşteri",
members: "ekip üyesi",
presentations: "sunum",
};
export function getEffectivePlan(ctx: TenantContext): TenantPlan {
const plan = (ctx.settings?.plan as TenantPlan | undefined) ?? "free";
if (plan === "pro") {
const expires = ctx.settings?.planExpiresAt;
if (expires && new Date(expires).getTime() < Date.now()) return "free";
}
return plan;
}
async function countResource(tenantId: string, resource: PlanResource): Promise<number> {
const { tablesDB, teams } = createAdminClient();
if (resource === "members") {
const result = await teams.listMemberships(tenantId);
return result.total;
}
const tableMap: Record<Exclude<PlanResource, "members">, string> = {
properties: TABLES.properties,
customers: TABLES.customers,
presentations: TABLES.presentations,
};
const result = await tablesDB.listRows({
databaseId: DATABASE_ID,
tableId: tableMap[resource],
queries: [Query.equal("tenantId", tenantId), Query.limit(1)],
});
return result.total;
}
export type PlanUsage = {
plan: TenantPlan;
usage: Record<PlanResource, { used: number; limit: number; reached: boolean }>;
};
export async function getPlanUsage(ctx: TenantContext): Promise<PlanUsage> {
const plan = getEffectivePlan(ctx);
const limits = PLAN_LIMITS[plan];
const resources: PlanResource[] = ["properties", "customers", "members", "presentations"];
const counts = await Promise.all(resources.map((r) => countResource(ctx.tenantId, r)));
const usage = {} as PlanUsage["usage"];
resources.forEach((r, i) => {
usage[r] = { used: counts[i], limit: limits[r], reached: counts[i] >= limits[r] };
});
return { plan, usage };
}
export class PlanLimitError extends Error {
code = PLAN_LIMIT_EXCEEDED;
constructor(public resource: PlanResource, public limit: number) {
super(`Plan limit reached for ${resource} (${limit})`);
}
}
export async function requirePlanCapacity(ctx: TenantContext, resource: PlanResource): Promise<void> {
const plan = getEffectivePlan(ctx);
const limit = PLAN_LIMITS[plan][resource];
if (limit === INF) return;
const used = await countResource(ctx.tenantId, resource);
if (used >= limit) throw new PlanLimitError(resource, limit);
}
export function isPlanLimitError(e: unknown): e is PlanLimitError {
return e instanceof PlanLimitError;
}
export function planLimitMessage(resource: PlanResource, limit: number): string {
return `Ücretsiz planda en fazla ${limit} ${RESOURCE_LABELS[resource]} ekleyebilirsiniz. Pro'ya geçerek sınırı kaldırın.`;
}
+4
View File
@@ -31,6 +31,7 @@ export type SystemRow = {
type Row = SystemRow;
export type TenantPlan = "free" | "pro";
export type TenantRole = "owner" | "admin" | "member";
export type InviteRole = "admin" | "member";
@@ -169,6 +170,9 @@ export interface TenantSettings extends Row {
email?: string;
address?: string;
createdBy: string;
plan?: TenantPlan;
planExpiresAt?: string;
planProvider?: string;
}
export type PropertyFeature =
+152
View File
@@ -0,0 +1,152 @@
"use server";
import { redirect } from "next/navigation";
import { Query } from "node-appwrite";
import { DATABASE_ID, TABLES, type TenantPlan } from "./schema";
import { createAdminClient } from "./server";
import { requireRole, requireTenant } from "./tenant-guard";
import { getEffectivePlan } from "./plan-limits";
import { PLAN_CATALOG } from "./subscription-types";
import { getShopierPlanUrl, isShopierEnabled } from "../payments/shopier";
import { createPolarCheckout, isPolarEnabled } from "../payments/polar";
const PRO_VALIDITY_DAYS = 30;
function generateOrderId(): string {
const t = Date.now().toString(36);
const r = Math.random().toString(36).slice(2, 10);
return `ord_${t}_${r}`;
}
// Webhook handler'larından da çağrılabilir — provider "polar" | "shopier" | "mock"
export async function activatePlanInDb(
tenantId: string,
plan: TenantPlan,
provider: string,
): Promise<void> {
const { tablesDB } = createAdminClient();
// tenant_settings satırını bul
const result = await tablesDB.listRows({
databaseId: DATABASE_ID,
tableId: TABLES.tenantSettings,
queries: [Query.equal("tenantId", tenantId), Query.limit(1)],
});
const row = result.rows[0];
if (!row) throw new Error(`tenant_settings bulunamadı: ${tenantId}`);
const now = new Date();
const expires = new Date(now.getTime() + PRO_VALIDITY_DAYS * 24 * 60 * 60 * 1000);
await tablesDB.updateRow(DATABASE_ID, TABLES.tenantSettings, row.$id, {
plan,
planExpiresAt: expires.toISOString(),
planProvider: provider,
});
}
export async function deactivatePlanInDb(tenantId: string): Promise<void> {
const { tablesDB } = createAdminClient();
const result = await tablesDB.listRows({
databaseId: DATABASE_ID,
tableId: TABLES.tenantSettings,
queries: [Query.equal("tenantId", tenantId), Query.limit(1)],
});
const row = result.rows[0];
if (!row) return;
await tablesDB.updateRow(DATABASE_ID, TABLES.tenantSettings, row.$id, {
plan: "free",
planExpiresAt: null,
planProvider: null,
});
}
// ── Mock checkout (geliştirme ortamı) ─────────────────────────────────────────
export async function startMockCheckoutAction(formData: FormData): Promise<void> {
const plan = String(formData.get("plan") ?? "") as TenantPlan;
if (plan !== "pro") throw new Error("Geçersiz plan.");
const ctx = await requireTenant();
requireRole(ctx, ["owner"]);
// Mock: direkt aktive et
await activatePlanInDb(ctx.tenantId, plan, "mock");
redirect("/settings/billing?upgraded=1");
}
// ── Shopier checkout ───────────────────────────────────────────────────────────
export async function startShopierCheckoutAction(formData: FormData): Promise<void> {
const plan = String(formData.get("plan") ?? "") as TenantPlan;
if (plan !== "pro") throw new Error("Geçersiz plan.");
const ctx = await requireTenant();
requireRole(ctx, ["owner"]);
const storeUrl = getShopierPlanUrl(plan);
if (!storeUrl) throw new Error("Shopier mağaza URL'i ayarlanmamış.");
// Shopier mağaza sayfasına yönlendir — ödeme tamamlanınca webhook gelir
redirect(storeUrl);
}
// ── Polar checkout ─────────────────────────────────────────────────────────────
export async function startPolarCheckoutAction(formData: FormData): Promise<void> {
const plan = String(formData.get("plan") ?? "") as TenantPlan;
if (plan !== "pro") throw new Error("Geçersiz plan.");
const ctx = await requireTenant();
requireRole(ctx, ["owner"]);
const catalog = PLAN_CATALOG[plan];
const orderId = generateOrderId();
const appUrl = process.env.APP_URL?.replace(/\/$/, "") ?? "http://localhost:3001";
const checkout = await createPolarCheckout({
orderId,
tenantId: ctx.tenantId,
userEmail: ctx.user.email,
successUrl: `${appUrl}/settings/billing?upgraded=1`,
});
// orderId'yi tenant_settings'e geçici olarak kaydedebiliriz ama
// minimal yaklaşımda metadata.tenant_id yeterli — webhook okur
void catalog; // fiyat bilgisi ileride log için kullanılabilir
redirect(checkout.url);
}
// ── Unified entry point ────────────────────────────────────────────────────────
export async function startCheckoutAction(formData: FormData): Promise<void> {
if (isPolarEnabled()) return startPolarCheckoutAction(formData);
if (isShopierEnabled()) return startShopierCheckoutAction(formData);
return startMockCheckoutAction(formData);
}
// ── Downgrade ──────────────────────────────────────────────────────────────────
export async function downgradeToFreeAction(): Promise<void> {
const ctx = await requireTenant();
requireRole(ctx, ["owner"]);
await deactivatePlanInDb(ctx.tenantId);
redirect("/settings/billing?downgraded=1");
}
// ── Mevcut plan bilgisi ────────────────────────────────────────────────────────
export async function getCurrentPlanAction(): Promise<{
plan: TenantPlan;
expiresAt: string | null;
provider: string | null;
}> {
const ctx = await requireTenant();
const plan = getEffectivePlan(ctx);
return {
plan,
expiresAt: ctx.settings?.planExpiresAt ?? null,
provider: ctx.settings?.planProvider ?? null,
};
}
+43
View File
@@ -0,0 +1,43 @@
import type { TenantPlan } from "./schema";
export type PlanCatalogEntry = {
id: TenantPlan;
name: string;
price: number;
currency: string;
description: string;
features: string[];
};
export const PLAN_CATALOG: Record<TenantPlan, PlanCatalogEntry> = {
free: {
id: "free",
name: "Ücretsiz",
price: 0,
currency: "TRY",
description: "Küçük ofisler ve deneme için.",
features: [
"5 ilan",
"10 müşteri",
"3 sunum",
"2 ekip üyesi",
"Temel destek",
],
},
pro: {
id: "pro",
name: "Pro",
price: 499,
currency: "TRY",
description: "Büyüyen emlak ofisleri için sınırsız kullanım.",
features: [
"Sınırsız ilan",
"Sınırsız müşteri",
"Sınırsız sunum",
"Sınırsız ekip üyesi",
"Otomatik eşleştirme",
"Yatırımcı portalı",
"Öncelikli destek",
],
},
};