Initial commit of Descrybe v2 without local scratch artifacts.
Drop one-shot tmp/axe scripts and agent i18n scratch so the Gitea tree is deployable.
This commit is contained in:
commit
8580c996c3
1285 files changed
+325780
No files matched your search
@@ -0,0 +1,110 @@
|
||||
import { api, ApiError } from "$lib/api";
|
||||
import {
|
||||
buildCreditPackEcommerce,
|
||||
buildSubscriptionEcommerce,
|
||||
trackEcommerceEvent,
|
||||
trackEvent
|
||||
} from "$lib/analytics";
|
||||
import { CONTACT_SALES_HREF, SALES_CALENDLY_URL } from "$lib/components/pricing/pricing-data";
|
||||
import { CREDIT_PACKS } from "$lib/components/pricing/credit-packs";
|
||||
import { isSelfServeCheckoutPlan } from "$lib/billing-display";
|
||||
|
||||
export type StripeStatus = {
|
||||
configured: boolean;
|
||||
mock: boolean;
|
||||
has_customer: boolean;
|
||||
has_subscription: boolean;
|
||||
customer_id?: string;
|
||||
subscription_id?: string;
|
||||
subscription_status?: string;
|
||||
};
|
||||
|
||||
export type CheckoutResult = {
|
||||
url: string;
|
||||
mock?: boolean;
|
||||
applied?: boolean;
|
||||
message?: string;
|
||||
};
|
||||
|
||||
export type PortalResult = {
|
||||
url: string;
|
||||
mock?: boolean;
|
||||
};
|
||||
|
||||
export { isSelfServeCheckoutPlan, SALES_CALENDLY_URL, CONTACT_SALES_HREF };
|
||||
|
||||
export async function fetchStripeStatus(): Promise<StripeStatus | null> {
|
||||
try {
|
||||
return await api<StripeStatus>("/api/billing/stripe");
|
||||
} catch (err) {
|
||||
if (err instanceof ApiError && (err.status === 404 || err.status === 501)) return null;
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
export async function startCheckout(
|
||||
plan: string,
|
||||
term: "monthly" | "yearly" = "monthly"
|
||||
): Promise<CheckoutResult> {
|
||||
const key = plan.trim().toLowerCase();
|
||||
if (!isSelfServeCheckoutPlan(key)) {
|
||||
throw new Error(
|
||||
"This plan is not available for self-serve checkout. Contact sales for Enterprise."
|
||||
);
|
||||
}
|
||||
const { ecommerce, extra } = buildSubscriptionEcommerce(key, term);
|
||||
trackEcommerceEvent("begin_checkout", ecommerce, extra);
|
||||
return api<CheckoutResult>("/api/billing/checkout", {
|
||||
method: "POST",
|
||||
body: { plan: key, term }
|
||||
});
|
||||
}
|
||||
|
||||
export type CreditPack = {
|
||||
id: string;
|
||||
name: string;
|
||||
description: string;
|
||||
credits: number;
|
||||
price_usd: number;
|
||||
ai_products: number;
|
||||
};
|
||||
|
||||
export async function fetchCreditPacks(): Promise<CreditPack[]> {
|
||||
try {
|
||||
const res = await api<{ packs?: CreditPack[] }>("/api/billing/credit-packs");
|
||||
return Array.isArray(res?.packs) ? res.packs : [];
|
||||
} catch (err) {
|
||||
if (err instanceof ApiError && (err.status === 404 || err.status === 501)) return [];
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
/** One-time AI credit top-up via Stripe Checkout (mode=payment). */
|
||||
export async function startCreditPackCheckout(packId: string): Promise<CheckoutResult> {
|
||||
const pack = packId.trim().toLowerCase();
|
||||
if (!pack) {
|
||||
throw new Error("Choose a credit pack.");
|
||||
}
|
||||
const marketing = CREDIT_PACKS.find((p) => p.id === pack);
|
||||
const { ecommerce, extra } = buildCreditPackEcommerce(pack, {
|
||||
priceUsd: marketing?.priceUSD,
|
||||
itemName: marketing?.name
|
||||
});
|
||||
trackEcommerceEvent("begin_checkout", ecommerce, extra);
|
||||
return api<CheckoutResult>("/api/billing/checkout", {
|
||||
method: "POST",
|
||||
body: { pack }
|
||||
});
|
||||
}
|
||||
|
||||
export async function openBillingPortal(): Promise<PortalResult> {
|
||||
trackEvent("billing_portal_opened");
|
||||
return api<PortalResult>("/api/billing/portal", { method: "POST", body: {} });
|
||||
}
|
||||
|
||||
/** Navigate to checkout URL (or mock success redirect). */
|
||||
export function redirectToCheckout(result: CheckoutResult): void {
|
||||
if (result.url) {
|
||||
window.location.assign(result.url);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user