Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
import { useEffect, useMemo, useState } from "react";
import { ChevronLeftIcon } from "lucide-react";
import { createNotification } from "@app/components/notifications";
import { Badge, Button, SheetFooter, SheetHeader, SheetTitle } from "@app/components/v3";
import { cn } from "@app/components/v3/utils";
import {
BillingV2Cadence,
BillingV2CatalogProduct,
BillingV2Dim,
BillingV2ErrorCode,
BillingV2Plan,
useBuyBillingV2Product,
usePreviewBillingV2Change
} from "@app/hooks/api";
import { billingV2ErrorCode, fmtMoney } from "../billing-v2-format";
import { ChargeBreakdown } from "./ChargeBreakdown";
import { CommitmentTerms } from "./CommitmentTerms";
import {
CostSummary,
CostSummaryRow,
PaymentActionRequiredNotice,
PaymentApprovalNotice,
ProductIcon,
Stepper
} from "./shared";
type Props = {
orgId: string;
prod: BillingV2CatalogProduct;
plan: BillingV2Plan;
hasActiveSubscription: boolean;
returnPath: string;
renewsOn: string | null;
// false for an enterprise-managed org: the activate button is disabled (a mutation would 403).
selfServe: boolean;
// This plan offers an unused self-serve trial. In the monthly cadence the primary CTA becomes
// "Start free trial" (which opens the confirmation dialog) instead of a paid activation.
trialAvailable: boolean;
onStartTrial: () => void;
onBack: () => void;
onDone: () => void;
};
const isCommittable = (dim: BillingV2Dim): boolean =>
dim.annual > 0 && dim.monthly > 0 && !dim.meteredAnnual;
// The plan's monthly headline price (base fee, else the first priced dimension's monthly rate).
const monthlyHeadline = (plan: BillingV2Plan): number => {
if (plan.base) {
return plan.base.monthly;
}
const dim = plan.dims.find((d) => d.monthly > 0);
return dim ? dim.monthly : 0;
};
// The plan's annual headline price (base fee, else the first priced dimension's annual rate).
const annualHeadline = (plan: BillingV2Plan): number => {
if (plan.base) {
return plan.base.annual;
}
const dim = plan.dims.find((d) => d.annual > 0);
return dim ? dim.annual : 0;
};
type CadenceOptionProps = {
active: boolean;
title: string;
subtitle: string;
badge?: string;
onClick: () => void;
};
const CadenceOption = ({ active, title, subtitle, badge, onClick }: CadenceOptionProps) => (
<button
type="button"
onClick={onClick}
className={cn(
"flex flex-1 flex-col gap-1 rounded-lg border p-4 text-left transition-colors",
active ? "border-org bg-org/5" : "border-border hover:border-foreground/20"
)}
>
<div className="flex items-center gap-2">
<span className="text-sm font-medium text-foreground">{title}</span>
{badge && <Badge variant="success">{badge}</Badge>}
</div>
<span className="text-xs text-muted">{subtitle}</span>
</button>
);
export const ActivateView = ({
orgId,
prod,
plan,
hasActiveSubscription,
returnPath,
renewsOn,
selfServe,
trialAvailable,
onStartTrial,
onBack,
onDone
}: Props) => {
const committable = useMemo(() => plan.dims.filter(isCommittable), [plan.dims]);
const supportsAnnual =
plan.dims.some((d) => d.annual > 0) || Boolean(plan.base && plan.base.annual > 0);
const supportsMonthly =
plan.dims.some((d) => d.monthly > 0) || Boolean(plan.base && plan.base.monthly > 0);
const [cadence, setCadence] = useState<BillingV2Cadence>(() => {
if (plan.trialable && supportsMonthly) {
return "monthly";
}
return supportsAnnual ? "annual" : "monthly";
});
const [commitments, setCommitments] = useState<Record<string, number>>(() =>
Object.fromEntries(committable.map((dim) => [dim.key, Math.max(dim.included, 1)]))
);
const [acknowledged, setAcknowledged] = useState(false);
const needsCommitmentAck = cadence === "annual" && committable.length > 0;
// A trial is monthly/usage-based, so it only applies to the monthly cadence. Switching to yearly is
// a paid annual purchase, not a trial, so the CTA flips back to Pay.
const trialFlow = trialAvailable && cadence === "monthly";
const preview = usePreviewBillingV2Change();
const buyProduct = useBuyBillingV2Product();
// Yearly sends the buyer-chosen commitment quantities; monthly sends nothing (the server seeds the
// recurring quantities from present usage).
const quantitiesForCadence = cadence === "annual" ? commitments : undefined;
const quantitiesKey = JSON.stringify(quantitiesForCadence ?? {});
// Keyed to the selection that was declined, so changing cadence or quantities clears the notice.
const selectionKey = `${cadence}:${quantitiesKey}`;
const [declinedSelectionKey, setDeclinedSelectionKey] = useState<string | null>(null);
const showPaymentActionRequired = declinedSelectionKey === selectionKey;
const [approval, setApproval] = useState<{ selectionKey: string; paymentUrl: string } | null>(
null
);
const approvalUrl = approval?.selectionKey === selectionKey ? approval.paymentUrl : null;
// The plan's headline price for the chosen cadence, shown on the static product row.
const localRecurring = cadence === "annual" ? annualHeadline(plan) : monthlyHeadline(plan);
// The preview prices the purchase for the current cadence + quantities whether or not a live
// subscription exists (it prices a first purchase too), so every activation waits on it. Until it is
// fresh for the current selection the cost rows render as skeletons.
const previewFresh =
!preview.isPending &&
Boolean(preview.data) &&
preview.variables?.cadence === cadence &&
JSON.stringify(preview.variables?.quantities ?? {}) === quantitiesKey;
const isCalculating = !previewFresh;
// Debounced re-price on cadence/quantity changes so a burst of stepper clicks makes one request;
// skipped when the current selection is already priced.
useEffect(() => {
if (previewFresh) {
return undefined;
}
const timeout = setTimeout(() => {
preview.mutate({
orgId,
addProductId: prod.id,
plan: plan.tier,
cadence,
quantities: quantitiesForCadence
});
}, 400);
return () => clearTimeout(timeout);
}, [orgId, prod.id, plan.tier, cadence, quantitiesKey]);
// Both figures come from the preview. "Charged today" differs by path: adding to a live subscription
// is invoice-now, so the card is charged totalDueNow (this change plus any earlier unbilled mid-cycle
// changes it settles); a first purchase goes through Stripe Checkout, which collects the whole first
// invoice up front (nextInvoiceTotal) — for a yearly commitment that is the full annual amount, not
// the $0 prorationAmount a first purchase reports.
// A trial collects nothing today (the card, if any, is only set up), so due-today is 0 regardless of
// the previewed purchase amount.
let dueToday = 0;
if (!trialFlow && preview.data) {
dueToday = hasActiveSubscription
? Math.max(preview.data.totalDueNow, 0)
: preview.data.nextInvoiceTotal;
}
const recurring = preview.data?.nextRecurringTotal ?? 0;
// Only the invoice-now (existing-subscription) path settles earlier pending charges; disclose the
// split when there are any. A first purchase via Checkout has none.
const additionalCharges = preview.data?.additionalCharges ?? 0;
const showChargeBreakdown = hasActiveSubscription && !isCalculating && additionalCharges !== 0;
const period = cadence === "annual" ? "year" : "month";
const pending = buyProduct.isPending;
let chargedNote = "Calculating…";
if (trialFlow) {
chargedNote = "Free during your trial";
} else if (!isCalculating) {
chargedNote = hasActiveSubscription
? "Prorated onto your shared billing date"
: "Due today at checkout";
}
// The paid CTA waits until the preview has priced the current selection, and an annual commitment
// also requires the customer to acknowledge the terms. The trial CTA has nothing to price, so it
// only waits on selfServe. An enterprise-managed org (selfServe false) can never submit either.
const primaryDisabled = trialFlow
? !selfServe
: !selfServe || pending || isCalculating || (needsCommitmentAck && !acknowledged);
// Savings badge on the yearly option: how much cheaper the annual rate is vs paying monthly.
const savingsPct = useMemo(() => {
const monthly = monthlyHeadline(plan);
const annual = annualHeadline(plan);
if (monthly <= 0 || annual <= 0) {
return null;
}
const pct = Math.round((1 - annual / (monthly * 12)) * 100);
return pct > 0 ? pct : null;
}, [plan]);
const handleActivate = async () => {
setDeclinedSelectionKey(null);
setApproval(null);
try {
const result = await buyProduct.mutateAsync({
orgId,
productId: prod.id,
plan: plan.tier,
cadence,
quantities: quantitiesForCadence,
returnPath
});
if (result.outcome === "payment_action_required" && result.paymentUrl) {
setApproval({ selectionKey, paymentUrl: result.paymentUrl });
return;
}
if (result.outcome === "subscription_updated") {
createNotification({
type: "success",
text: `${prod.name} activated. It may take a moment to update here.`
});
onDone();
return;
}
if (result.checkoutUrl) {
window.location.href = result.checkoutUrl;
return;
}
createNotification({ type: "error", text: "Failed to start checkout." });
} catch (error) {
// The backend's message is surfaced by the global mutation error handler (reactQuery.tsx).
if (billingV2ErrorCode(error) === BillingV2ErrorCode.PaymentActionRequired) {
setDeclinedSelectionKey(selectionKey);
}
}
};
let primaryLabel = "Start a free trial";
if (!trialFlow) {
primaryLabel = isCalculating
? "Activate"
: `Activate · pay ${fmtMoney(dueToday, dueToday ? 2 : 0)} today`;
}
let recurringNote: string | undefined;
if (trialFlow) {
recurringNote = "Billed monthly after your trial ends";
} else if (renewsOn) {
recurringNote = `Billed on ${renewsOn}`;
}
return (
<>
<SheetHeader className="flex-row items-center gap-3.5 border-b pr-12">
<ProductIcon product={prod} size={40} />
<div className="min-w-0 flex-1">
<SheetTitle className="text-base">Activate {prod.name}</SheetTitle>
<p className="mt-1 text-sm text-muted">
Choose monthly or yearly billing, set your units, and see the cost before you confirm.
</p>
</div>
</SheetHeader>
<div className="flex flex-col gap-4 overflow-y-auto p-5">
{supportsMonthly && supportsAnnual && (
<div className="flex flex-col gap-2">
<span className="text-sm font-medium text-foreground">Billing cadence</span>
<div className="flex gap-3">
<CadenceOption
active={cadence === "monthly"}
title="Monthly"
subtitle="Usage-based"
onClick={() => setCadence("monthly")}
/>
<CadenceOption
active={cadence === "annual"}
title="Yearly"
subtitle="Commit & save"
badge={savingsPct ? `SAVE ~${savingsPct}%` : undefined}
onClick={() => setCadence("annual")}
/>
</div>
<span className="text-xs text-muted">
{cadence === "monthly"
? "Pay for what you provision each month. No commitment, scale up or down freely."
: "Pre-buy units for the year at the annual rate. Usage above your commitment is billed monthly on-demand."}
</span>
</div>
)}
{cadence === "annual" && committable.length > 0
? committable.map((dim) => (
<div
key={dim.key}
className="flex items-center justify-between gap-4 rounded-lg border border-border bg-card p-4"
>
<div className="min-w-0">
<div className="text-sm font-medium text-foreground">{dim.label}</div>
<div className="mt-0.5 text-xs text-muted">
{fmtMoney(dim.annual / 12, 2)} / {dim.noun} / month · billed annually
</div>
</div>
<Stepper
value={commitments[dim.key] ?? 0}
min={0}
onChange={(value) => setCommitments((prev) => ({ ...prev, [dim.key]: value }))}
/>
</div>
))
: null}
{(cadence === "monthly" || committable.length === 0) && (
<div className="flex items-center justify-between gap-4 rounded-lg border border-border bg-card p-4">
<div className="flex min-w-0 items-center gap-3">
<ProductIcon product={prod} size={36} />
<div className="min-w-0">
<div className="text-sm font-medium text-foreground">
{prod.name} · {plan.name}
</div>
{plan.feature && <div className="mt-0.5 text-xs text-muted">{plan.feature}</div>}
</div>
</div>
<div className="flex shrink-0 flex-col items-end">
<span className="text-sm font-semibold text-foreground tabular-nums">
{fmtMoney(cadence === "annual" ? localRecurring / 12 : localRecurring)} / mo
</span>
{cadence === "annual" && (
<span className="text-[11px] text-muted">billed annually</span>
)}
</div>
</div>
)}
<CostSummary>
<CostSummaryRow
label={trialFlow ? "Due today" : "Charged today"}
note={chargedNote}
value={fmtMoney(dueToday, dueToday ? 2 : 0)}
isCalculating={!trialFlow && isCalculating}
valueClassName="text-base"
/>
<CostSummaryRow
label={cadence === "annual" ? "New yearly total" : "New monthly total"}
note={recurringNote}
value={`${fmtMoney(recurring)} / ${period}`}
isCalculating={isCalculating}
/>
</CostSummary>
{showChargeBreakdown && (
<ChargeBreakdown
prorationAmount={preview.data?.prorationAmount ?? 0}
additionalCharges={additionalCharges}
totalDueNow={preview.data?.totalDueNow ?? 0}
/>
)}
{needsCommitmentAck && (
<CommitmentTerms acknowledged={acknowledged} onAcknowledgedChange={setAcknowledged} />
)}
{showPaymentActionRequired && <PaymentActionRequiredNotice orgId={orgId} />}
{approvalUrl && <PaymentApprovalNotice paymentUrl={approvalUrl} />}
</div>
<SheetFooter className="flex-row justify-between border-t">
<Button variant="ghost" onClick={onBack} isDisabled={pending}>
<ChevronLeftIcon />
Back to plans
</Button>
<Button
variant="org"
onClick={trialFlow ? onStartTrial : handleActivate}
isDisabled={primaryDisabled}
isPending={pending}
>
{primaryLabel}
</Button>
</SheetFooter>
</>
);
};