import * as creem_models_components from 'creem/models/components'; import * as zod from 'zod'; import * as better_auth from 'better-auth'; import { BetterAuthPluginDBSchema } from '@better-auth/core/db'; import { NormalizedCheckoutEntity, NormalizedRefundEntity, NormalizedDisputeEntity, NormalizedSubscriptionEntity } from '@creem_io/webhook-types'; export { DiscountDuration, DiscountEntity, DiscountStatus, DiscountType } from '@creem_io/webhook-types'; export { C as CheckboxFieldConfig, a as CheckoutCustomer, b as CreateCheckoutInput, c as CreateCheckoutResponse, d as CreatePortalInput, e as CreatePortalResponse, f as CustomFieldInput, R as RetrieveSubscriptionInput, S as SearchTransactionsInput, g as SearchTransactionsResponse, h as SubscriptionData, T as TextFieldConfig, i as TransactionData } from './search-transactions-types-DJyPIg0H.js'; export { C as CancelSubscriptionInput, a as CancelSubscriptionResponse, H as HasAccessGrantedResponse } from './has-active-subscription-types-D3Rjv4Qe.js'; export { CreemServerConfig, cancelSubscription, checkSubscriptionAccess, createCheckout, createCreemClient, createPortal, formatCreemDate, getActiveSubscriptions, getDaysUntilRenewal, isActiveSubscription, retrieveSubscription, searchTransactions, validateWebhookSignature } from './creem-server.js'; import 'creem'; /** * Flattened checkout.completed callback parameter. * All properties are at the top level for easy destructuring. */ type FlatCheckoutCompleted = { /** Webhook event type identifier */ webhookEventType: "checkout.completed"; /** Unique webhook event ID */ webhookId: string; /** Webhook event creation timestamp */ webhookCreatedAt: number; } & NormalizedCheckoutEntity; /** * Flattened refund.created callback parameter. * All properties are at the top level for easy destructuring. */ type FlatRefundCreated = { /** Webhook event type identifier */ webhookEventType: "refund.created"; /** Unique webhook event ID */ webhookId: string; /** Webhook event creation timestamp */ webhookCreatedAt: number; } & NormalizedRefundEntity; /** * Flattened dispute.created callback parameter. * All properties are at the top level for easy destructuring. */ type FlatDisputeCreated = { /** Webhook event type identifier */ webhookEventType: "dispute.created"; /** Unique webhook event ID */ webhookId: string; /** Webhook event creation timestamp */ webhookCreatedAt: number; } & NormalizedDisputeEntity; /** * Flattened subscription event callback parameter. * All properties are at the top level for easy destructuring. */ type FlatSubscriptionEvent = { /** Webhook event type identifier */ webhookEventType: T; /** Unique webhook event ID */ webhookId: string; /** Webhook event creation timestamp */ webhookCreatedAt: number; } & NormalizedSubscriptionEntity; type GrantAccessReason = "subscription_active" | "subscription_trialing" | "subscription_paid"; type RevokeAccessReason = "subscription_paused" | "subscription_expired"; /** * Context passed to onGrantAccess callback. * All subscription properties are flattened for easy destructuring. */ type GrantAccessContext = { /** The reason for granting access */ reason: GrantAccessReason; } & NormalizedSubscriptionEntity; /** * Context passed to onRevokeAccess callback. * All subscription properties are flattened for easy destructuring. */ type RevokeAccessContext = { /** The reason for revoking access */ reason: RevokeAccessReason; } & NormalizedSubscriptionEntity; interface CreemOptions { /** * Creem API Key */ apiKey: string; /** * Creem Webhook Secret (for signature verification) */ webhookSecret?: string; /** * Test mode */ testMode?: boolean; /** * Default success URL */ defaultSuccessUrl?: string; /** * Whether to persist subscription data to the database. * When enabled (default: true), the plugin will: * - Create/update subscription records in the database * - Add subscription and user schema tables * - Automatically sync subscription data from Creem webhooks * * When disabled (false): * - No database operations will be performed * - No schema tables will be created * - You must handle all subscription data management yourself * * @default true */ persistSubscriptions?: boolean; schema?: BetterAuthPluginDBSchema; /** * Called when a checkout is completed. * All properties are flattened for easy destructuring: * { webhookEventType, webhookId, webhookCreatedAt, order, product, customer, subscription, status, ... } * * @example * onCheckoutCompleted: async ({ webhookEventType, product, customer, order, subscription }) => { * console.log(`Checkout completed: ${customer?.email} purchased ${product.name}`); * } */ onCheckoutCompleted?: (data: FlatCheckoutCompleted) => void | Promise; /** * Called when a refund is created. * All properties are flattened for easy destructuring. */ onRefundCreated?: (data: FlatRefundCreated) => void | Promise; /** * Called when a dispute is created. * All properties are flattened for easy destructuring. */ onDisputeCreated?: (data: FlatDisputeCreated) => void | Promise; /** * Called when a subscription becomes active. * All properties are flattened for easy destructuring: * { webhookEventType, webhookId, webhookCreatedAt, product, customer, status, metadata, ... } * * @example * onSubscriptionActive: async ({ product, customer, status }) => { * console.log(`${customer.email} subscribed to ${product.name}`); * } */ onSubscriptionActive?: (data: FlatSubscriptionEvent<"subscription.active">) => void | Promise; /** * Called when a subscription is in trialing state. * All properties are flattened for easy destructuring. */ onSubscriptionTrialing?: (data: FlatSubscriptionEvent<"subscription.trialing">) => void | Promise; /** * Called when a subscription is canceled. * All properties are flattened for easy destructuring. */ onSubscriptionCanceled?: (data: FlatSubscriptionEvent<"subscription.canceled">) => void | Promise; /** * Called when a subscription is paid. * All properties are flattened for easy destructuring. */ onSubscriptionPaid?: (data: FlatSubscriptionEvent<"subscription.paid">) => void | Promise; /** * Called when a subscription has expired. * All properties are flattened for easy destructuring. */ onSubscriptionExpired?: (data: FlatSubscriptionEvent<"subscription.expired">) => void | Promise; /** * Called when a subscription is unpaid. * All properties are flattened for easy destructuring. */ onSubscriptionUnpaid?: (data: FlatSubscriptionEvent<"subscription.unpaid">) => void | Promise; /** * Called when a subscription is updated. * All properties are flattened for easy destructuring. */ onSubscriptionUpdate?: (data: FlatSubscriptionEvent<"subscription.update">) => void | Promise; /** * Called when a subscription is past due. * All properties are flattened for easy destructuring. */ onSubscriptionPastDue?: (data: FlatSubscriptionEvent<"subscription.past_due">) => void | Promise; /** * Called when a subscription is paused. * All properties are flattened for easy destructuring. */ onSubscriptionPaused?: (data: FlatSubscriptionEvent<"subscription.paused">) => void | Promise; /** * Called when a user should be granted access to the platform. * This is triggered for: active, trialing, and paid subscriptions. * * All subscription properties are flattened for easy destructuring: * { reason, product, customer, status, metadata, current_period_end_date, ... } * * NOTE: This may be called multiple times for the same user/subscription. * Implement this as an idempotent operation (safe to call repeatedly). * * @example * onGrantAccess: async ({ reason, product, customer, metadata }) => { * const userId = metadata?.referenceId as string; * console.log(`Granting ${reason} to ${customer.email} for ${product.name}`); * // Your database logic here * } */ onGrantAccess?: (context: GrantAccessContext) => void | Promise; /** * Called when a user's access should be revoked. * This is triggered for: paused, expired, and canceled (after period ends) subscriptions. * * All subscription properties are flattened for easy destructuring: * { reason, product, customer, status, metadata, ... } * * NOTE: This may be called multiple times for the same user/subscription. * Implement this as an idempotent operation (safe to call repeatedly). * * @example * onRevokeAccess: async ({ reason, product, customer, metadata }) => { * const userId = metadata?.referenceId as string; * console.log(`Revoking access (${reason}) from ${customer.email}`); * // Your database logic here * } */ onRevokeAccess?: (context: RevokeAccessContext) => void | Promise; } /** * Creem Better-Auth plugin for payment and subscription management. * * Provides endpoints for: * - `createCheckout` - Create a checkout session for a product * - `createPortal` - Create a customer portal session * - `cancelSubscription` - Cancel an active subscription * - `retrieveSubscription` - Get subscription details * - `searchTransactions` - Search transaction history * - `hasAccessGranted` - Check if user has an active subscription * * @param options - Plugin configuration options * @returns BetterAuth plugin configuration * * @example * ```typescript * import { creem } from "@creem_io/better-auth"; * * export const auth = betterAuth({ * plugins: [ * creem({ * apiKey: process.env.CREEM_API_KEY!, * testMode: true, * defaultSuccessUrl: "/success", * onGrantAccess: async ({ customer, product, metadata }) => { * // Grant user access to your platform * }, * onRevokeAccess: async ({ customer, product, metadata }) => { * // Revoke user access * } * }) * ] * }); * ``` */ declare const creem: (options: CreemOptions) => { id: "creem"; endpoints: { creemWebhook?: better_auth.StrictEndpoint<"/creem/webhook", { method: "POST"; requireRequest: true; requireHeaders: true; disableBody: true; }, { error: string; } | { message: string; }> | undefined; createCheckout: better_auth.StrictEndpoint<"/creem/create-checkout", { method: "POST"; body: zod.ZodObject<{ productId: zod.ZodString; requestId: zod.ZodOptional; units: zod.ZodOptional; discountCode: zod.ZodOptional; customer: zod.ZodOptional; }, "strip", zod.ZodTypeAny, { email?: string | undefined; }, { email?: string | undefined; }>>; customFields: zod.ZodOptional; key: zod.ZodString; label: zod.ZodString; optional: zod.ZodOptional; text: zod.ZodOptional; minLength: zod.ZodOptional; }, "strip", zod.ZodTypeAny, { maxLength?: number | undefined; minLength?: number | undefined; }, { maxLength?: number | undefined; minLength?: number | undefined; }>>; checkbox: zod.ZodOptional; }, "strip", zod.ZodTypeAny, { label?: string | undefined; }, { label?: string | undefined; }>>; }, "strip", zod.ZodTypeAny, { type: "text" | "checkbox"; key: string; label: string; text?: { maxLength?: number | undefined; minLength?: number | undefined; } | undefined; checkbox?: { label?: string | undefined; } | undefined; optional?: boolean | undefined; }, { type: "text" | "checkbox"; key: string; label: string; text?: { maxLength?: number | undefined; minLength?: number | undefined; } | undefined; checkbox?: { label?: string | undefined; } | undefined; optional?: boolean | undefined; }>, "many">>; customField: zod.ZodOptional; key: zod.ZodString; label: zod.ZodString; optional: zod.ZodOptional; text: zod.ZodOptional; minLength: zod.ZodOptional; }, "strip", zod.ZodTypeAny, { maxLength?: number | undefined; minLength?: number | undefined; }, { maxLength?: number | undefined; minLength?: number | undefined; }>>; checkbox: zod.ZodOptional; }, "strip", zod.ZodTypeAny, { label?: string | undefined; }, { label?: string | undefined; }>>; }, "strip", zod.ZodTypeAny, { type: "text" | "checkbox"; key: string; label: string; text?: { maxLength?: number | undefined; minLength?: number | undefined; } | undefined; checkbox?: { label?: string | undefined; } | undefined; optional?: boolean | undefined; }, { type: "text" | "checkbox"; key: string; label: string; text?: { maxLength?: number | undefined; minLength?: number | undefined; } | undefined; checkbox?: { label?: string | undefined; } | undefined; optional?: boolean | undefined; }>, "many">>; successUrl: zod.ZodOptional; metadata: zod.ZodOptional>; }, "strip", zod.ZodTypeAny, { productId: string; requestId?: string | undefined; units?: number | undefined; discountCode?: string | undefined; customer?: { email?: string | undefined; } | undefined; customFields?: { type: "text" | "checkbox"; key: string; label: string; text?: { maxLength?: number | undefined; minLength?: number | undefined; } | undefined; checkbox?: { label?: string | undefined; } | undefined; optional?: boolean | undefined; }[] | undefined; customField?: { type: "text" | "checkbox"; key: string; label: string; text?: { maxLength?: number | undefined; minLength?: number | undefined; } | undefined; checkbox?: { label?: string | undefined; } | undefined; optional?: boolean | undefined; }[] | undefined; successUrl?: string | undefined; metadata?: Record | undefined; }, { productId: string; requestId?: string | undefined; units?: number | undefined; discountCode?: string | undefined; customer?: { email?: string | undefined; } | undefined; customFields?: { type: "text" | "checkbox"; key: string; label: string; text?: { maxLength?: number | undefined; minLength?: number | undefined; } | undefined; checkbox?: { label?: string | undefined; } | undefined; optional?: boolean | undefined; }[] | undefined; customField?: { type: "text" | "checkbox"; key: string; label: string; text?: { maxLength?: number | undefined; minLength?: number | undefined; } | undefined; checkbox?: { label?: string | undefined; } | undefined; optional?: boolean | undefined; }[] | undefined; successUrl?: string | undefined; metadata?: Record | undefined; }>; }, { error: string; } | { url: string | undefined; redirect: boolean; }>; createPortal: better_auth.StrictEndpoint<"/creem/create-portal", { method: "POST"; body: zod.ZodObject<{ customerId: zod.ZodOptional; }, "strip", zod.ZodTypeAny, { customerId?: string | undefined; }, { customerId?: string | undefined; }>; }, { error: string; } | { url: string; redirect: boolean; }>; cancelSubscription: better_auth.StrictEndpoint<"/creem/cancel-subscription", { method: "POST"; body: zod.ZodObject<{ id: zod.ZodOptional; }, "strip", zod.ZodTypeAny, { id?: string | undefined; }, { id?: string | undefined; }>; }, { error: string; } | { success: boolean; message: string; }>; retrieveSubscription: better_auth.StrictEndpoint<"/creem/retrieve-subscription", { method: "POST"; body: zod.ZodObject<{ id: zod.ZodOptional; }, "strip", zod.ZodTypeAny, { id?: string | undefined; }, { id?: string | undefined; }>; }, { error: string; } | creem_models_components.SubscriptionEntity>; searchTransactions: better_auth.StrictEndpoint<"/creem/search-transactions", { method: "POST"; body: zod.ZodObject<{ customerId: zod.ZodOptional; pageNumber: zod.ZodOptional; pageSize: zod.ZodOptional; productId: zod.ZodOptional; orderId: zod.ZodOptional; }, "strip", zod.ZodTypeAny, { productId?: string | undefined; customerId?: string | undefined; pageNumber?: number | undefined; pageSize?: number | undefined; orderId?: string | undefined; }, { productId?: string | undefined; customerId?: string | undefined; pageNumber?: number | undefined; pageSize?: number | undefined; orderId?: string | undefined; }>; }, { error: string; } | creem_models_components.TransactionListEntity>; hasAccessGranted: better_auth.StrictEndpoint<"/creem/has-access-granted", { method: "GET"; }, { hasAccessGranted: undefined; message: string; } | { hasAccessGranted: boolean; message: string; } | { hasAccessGranted: boolean; subscription: { id: string; status: string; productId: string; periodEnd: Date | undefined; }; }>; }; schema: {}; }; export { type CreemOptions, type FlatCheckoutCompleted, type FlatDisputeCreated, type FlatRefundCreated, type FlatSubscriptionEvent, type GrantAccessContext, type GrantAccessReason, type RevokeAccessContext, type RevokeAccessReason, creem };