@foldspace-fe/casdoor-next-auth-kit 0.1.20 → 0.1.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -1
- package/dist/billing/index.d.ts +32 -3
- package/dist/billing/index.js +3 -1
- package/dist/{chunk-NWYEHQNK.js → chunk-PILMR42A.js} +40 -1
- package/dist/chunk-PILMR42A.js.map +1 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +3 -1
- package/dist/skills/casdoor-next-auth-kit/SKILL.md +5 -1
- package/package.json +1 -1
- package/dist/chunk-NWYEHQNK.js.map +0 -1
package/README.md
CHANGED
|
@@ -51,10 +51,12 @@ import {
|
|
|
51
51
|
### Billing runtime
|
|
52
52
|
|
|
53
53
|
```ts
|
|
54
|
-
import
|
|
54
|
+
import { buildBillingSubscriptionCatalog } from '@foldspace-fe/casdoor-next-auth-kit/billing';
|
|
55
55
|
import { BillingProvider } from '@foldspace-fe/casdoor-next-auth-kit/react';
|
|
56
56
|
```
|
|
57
57
|
|
|
58
|
+
如果宿主已经有自己的会员计划 rows,先用 `buildBillingSubscriptionCatalog()` 生成 subscription catalog,再把结果交给 `BillingProvider`。商品项仍然可以单独拼进同一个 catalog,但订阅和商品要保持语义分离。
|
|
59
|
+
|
|
58
60
|
### Billing 动作
|
|
59
61
|
|
|
60
62
|
```tsx
|
package/dist/billing/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { B as BillingActionPayload, b as BillingRuntimeConfig, a7 as BillingPaymentCallbackContext, aq as BillingPurchaseRequest, m as BillingCreditsState, j as BillingProductState, h as BillingSubscriptionState, n as BillingEntitlementState, a as BillingItem, c as BillingCatalogConfig, p as BillingPurchaseStatus, k as BillingOrderHistoryItem, l as BillingPaymentHistoryItem, t as BillingCasdoorOrderDetail, x as BillingCasdoorSubscriptionDetail, i as BillingSubscriptionHistoryItem, a4 as BillingInterval, r as BillingProductSnapshot, ak as BillingPurchasableEntry, M as BillingCasdoorBuyProductRequest, w as BillingCasdoorProductDetail, N as BillingCasdoorBuyProductResponse, z as BillingActionExecutionResult, af as BillingPaymentSuccessRouteOptions, aa as BillingPaymentFinishedRouteOptions } from '../types-oVIQpMut.js';
|
|
2
|
-
export { f as BillingActionExecutor, A as BillingActionKind, d as BillingApiClient, C as BillingCasdoorAccountDetail, D as BillingCasdoorAccountMultiFactorAuthDetail, E as BillingCasdoorAccountResponse, F as BillingCasdoorApiResponse, G as BillingCasdoorApplicationDetail, H as BillingCasdoorApplicationProviderDetail, I as BillingCasdoorApplicationResponse, J as BillingCasdoorApplicationSigninItemDetail, K as BillingCasdoorApplicationSigninMethodDetail, L as BillingCasdoorApplicationSignupItemDetail, O as BillingCasdoorErrorDetail, P as BillingCasdoorErrorPayload, Q as BillingCasdoorOrderResponse, R as BillingCasdoorOrdersResponse, S as BillingCasdoorOrganizationDetail, T as BillingCasdoorOrganizationNamesResponse, U as BillingCasdoorOrganizationOption, V as BillingCasdoorPaymentDetail, W as BillingCasdoorPaymentResponse, u as BillingCasdoorPlanDetail, X as BillingCasdoorPlanResponse, v as BillingCasdoorPricingDetail, Y as BillingCasdoorPricingResponse, Z as BillingCasdoorProductResponse, _ as BillingCasdoorProviderDetail, $ as BillingCasdoorProviderOption, s as BillingCasdoorQueryState, a0 as BillingCasdoorSubscriptionResponse, a1 as BillingCasdoorSubscriptionsResponse, a2 as BillingConversionRule, y as BillingCoreContextValue, a3 as BillingCreditsContextValue,
|
|
1
|
+
import { B as BillingActionPayload, b as BillingRuntimeConfig, a7 as BillingPaymentCallbackContext, aq as BillingPurchaseRequest, m as BillingCreditsState, j as BillingProductState, h as BillingSubscriptionState, n as BillingEntitlementState, a as BillingItem, c as BillingCatalogConfig, p as BillingPurchaseStatus, k as BillingOrderHistoryItem, l as BillingPaymentHistoryItem, t as BillingCasdoorOrderDetail, x as BillingCasdoorSubscriptionDetail, i as BillingSubscriptionHistoryItem, a4 as BillingInterval, r as BillingProductSnapshot, ak as BillingPurchasableEntry, g as BillingDefaults, M as BillingCasdoorBuyProductRequest, w as BillingCasdoorProductDetail, N as BillingCasdoorBuyProductResponse, z as BillingActionExecutionResult, af as BillingPaymentSuccessRouteOptions, aa as BillingPaymentFinishedRouteOptions } from '../types-oVIQpMut.js';
|
|
2
|
+
export { f as BillingActionExecutor, A as BillingActionKind, d as BillingApiClient, C as BillingCasdoorAccountDetail, D as BillingCasdoorAccountMultiFactorAuthDetail, E as BillingCasdoorAccountResponse, F as BillingCasdoorApiResponse, G as BillingCasdoorApplicationDetail, H as BillingCasdoorApplicationProviderDetail, I as BillingCasdoorApplicationResponse, J as BillingCasdoorApplicationSigninItemDetail, K as BillingCasdoorApplicationSigninMethodDetail, L as BillingCasdoorApplicationSignupItemDetail, O as BillingCasdoorErrorDetail, P as BillingCasdoorErrorPayload, Q as BillingCasdoorOrderResponse, R as BillingCasdoorOrdersResponse, S as BillingCasdoorOrganizationDetail, T as BillingCasdoorOrganizationNamesResponse, U as BillingCasdoorOrganizationOption, V as BillingCasdoorPaymentDetail, W as BillingCasdoorPaymentResponse, u as BillingCasdoorPlanDetail, X as BillingCasdoorPlanResponse, v as BillingCasdoorPricingDetail, Y as BillingCasdoorPricingResponse, Z as BillingCasdoorProductResponse, _ as BillingCasdoorProviderDetail, $ as BillingCasdoorProviderOption, s as BillingCasdoorQueryState, a0 as BillingCasdoorSubscriptionResponse, a1 as BillingCasdoorSubscriptionsResponse, a2 as BillingConversionRule, y as BillingCoreContextValue, a3 as BillingCreditsContextValue, a5 as BillingItemKind, e as BillingLoaders, a6 as BillingOrderCreatedContext, a8 as BillingPaymentFinishedContext, a9 as BillingPaymentFinishedHandler, ab as BillingPaymentRouteBaseOptions, ac as BillingPaymentSuccessContext, ad as BillingPaymentSuccessHandler, ae as BillingPaymentSuccessHandlerResult, ag as BillingProductContextValue, ah as BillingProductDetailState, ai as BillingProductPurchasableEntry, aj as BillingProductPurchaseConfig, al as BillingPurchasableEntryBase, am as BillingPurchasableKind, an as BillingPurchasableWhitelist, ao as BillingPurchaseCompleteContext, ap as BillingPurchaseErrorContext, q as BillingPurchaseHooks, o as BillingStatusState, ar as BillingSubscriptionContextValue, as as BillingSubscriptionPurchasableEntry, at as BillingSubscriptionPurchaseConfig } from '../types-oVIQpMut.js';
|
|
3
3
|
|
|
4
4
|
declare function normalizeBillingRuntimeConfig(config?: Partial<BillingRuntimeConfig> | null): BillingRuntimeConfig;
|
|
5
5
|
declare function normalizeBillingCatalogConfig(config?: Partial<BillingCatalogConfig> | null): BillingCatalogConfig;
|
|
@@ -33,6 +33,35 @@ declare function normalizeBillingPurchaseStatus(status?: Partial<BillingPurchase
|
|
|
33
33
|
declare function resolveBillingInterval(interval?: BillingInterval | null): BillingInterval | undefined;
|
|
34
34
|
declare function buildBillingActionPayload(payload: BillingActionPayload, runtimeConfig?: BillingRuntimeConfig | null): BillingActionPayload;
|
|
35
35
|
|
|
36
|
+
interface BillingSubscriptionCatalogItemInput<TSource = unknown> {
|
|
37
|
+
source: TSource;
|
|
38
|
+
key: string;
|
|
39
|
+
title: string;
|
|
40
|
+
description?: string;
|
|
41
|
+
productId: string;
|
|
42
|
+
planId?: string;
|
|
43
|
+
priceId?: string;
|
|
44
|
+
interval?: BillingInterval;
|
|
45
|
+
featured?: boolean;
|
|
46
|
+
badge?: string;
|
|
47
|
+
priceLabel?: string;
|
|
48
|
+
priceValue?: number;
|
|
49
|
+
features?: string[];
|
|
50
|
+
metadata?: Record<string, string>;
|
|
51
|
+
}
|
|
52
|
+
interface BillingSubscriptionCatalogBuilderOptions<TSource = unknown> {
|
|
53
|
+
catalogKey: string;
|
|
54
|
+
title?: string;
|
|
55
|
+
description?: string;
|
|
56
|
+
portalPath?: string;
|
|
57
|
+
successPath?: string;
|
|
58
|
+
cancelPath?: string;
|
|
59
|
+
purchasableIds?: string[];
|
|
60
|
+
defaults?: BillingDefaults;
|
|
61
|
+
mapPlan: (plan: TSource, index: number) => BillingSubscriptionCatalogItemInput<TSource>;
|
|
62
|
+
}
|
|
63
|
+
declare function buildBillingSubscriptionCatalog<TSource>(plans: readonly TSource[], options: BillingSubscriptionCatalogBuilderOptions<TSource>): BillingCatalogConfig;
|
|
64
|
+
|
|
36
65
|
interface NormalizedCasdoorProductId {
|
|
37
66
|
owner: string;
|
|
38
67
|
name: string;
|
|
@@ -50,4 +79,4 @@ declare function createBillingPaymentSuccessRouteHandler(options?: BillingPaymen
|
|
|
50
79
|
declare function createBillingPaymentFinishedResponse(request: Request, options?: BillingPaymentFinishedRouteOptions): Promise<Response>;
|
|
51
80
|
declare function createBillingPaymentFinishedRouteHandler(options?: BillingPaymentFinishedRouteOptions): (request: Request) => Promise<Response>;
|
|
52
81
|
|
|
53
|
-
export { BillingActionExecutionResult, BillingActionPayload, BillingCasdoorBuyProductRequest, BillingCasdoorBuyProductResponse, BillingCasdoorOrderDetail, BillingCasdoorProductDetail, BillingCasdoorSubscriptionDetail, BillingCatalogConfig, BillingCreditsState, BillingEntitlementState, BillingInterval, BillingItem, BillingOrderHistoryItem, BillingPaymentCallbackContext, BillingPaymentFinishedRouteOptions, BillingPaymentHistoryItem, BillingPaymentSuccessRouteOptions, BillingProductSnapshot, BillingProductState, BillingPurchasableEntry, BillingPurchaseRequest, BillingPurchaseStatus, BillingRuntimeConfig, BillingSubscriptionHistoryItem, BillingSubscriptionState, type NormalizedCasdoorProductId, buildBillingActionPayload, buildBillingPaymentCallbackContext, buildBillingPurchaseRequest, buildCasdoorBuyProductParams, buildCasdoorBuyProductRequest, chooseCasdoorProviderName, createBillingPaymentFinishedResponse, createBillingPaymentFinishedRouteHandler, createBillingPaymentSuccessResponse, createBillingPaymentSuccessRouteHandler, deriveBillingCreditsState, deriveBillingEntitlements, filterBillingPurchasableItems, normalizeBillingCatalogConfig, normalizeBillingPurchaseStatus, normalizeBillingRuntimeConfig, normalizeCasdoorBuyProductResponse, normalizeCasdoorOrderHistoryItem, normalizeCasdoorPaymentHistoryItem, normalizeCasdoorProductId, normalizeCasdoorSubscriptionDetail, normalizeCasdoorSubscriptionHistoryItem, readBuyProductRedirectTo, resolveBillingInterval, resolveBillingItem, resolveBillingProductSnapshot, resolveBillingPurchasable, resolveBillingSubscriptionProduct };
|
|
82
|
+
export { BillingActionExecutionResult, BillingActionPayload, BillingCasdoorBuyProductRequest, BillingCasdoorBuyProductResponse, BillingCasdoorOrderDetail, BillingCasdoorProductDetail, BillingCasdoorSubscriptionDetail, BillingCatalogConfig, BillingCreditsState, BillingDefaults, BillingEntitlementState, BillingInterval, BillingItem, BillingOrderHistoryItem, BillingPaymentCallbackContext, BillingPaymentFinishedRouteOptions, BillingPaymentHistoryItem, BillingPaymentSuccessRouteOptions, BillingProductSnapshot, BillingProductState, BillingPurchasableEntry, BillingPurchaseRequest, BillingPurchaseStatus, BillingRuntimeConfig, type BillingSubscriptionCatalogBuilderOptions, type BillingSubscriptionCatalogItemInput, BillingSubscriptionHistoryItem, BillingSubscriptionState, type NormalizedCasdoorProductId, buildBillingActionPayload, buildBillingPaymentCallbackContext, buildBillingPurchaseRequest, buildBillingSubscriptionCatalog, buildCasdoorBuyProductParams, buildCasdoorBuyProductRequest, chooseCasdoorProviderName, createBillingPaymentFinishedResponse, createBillingPaymentFinishedRouteHandler, createBillingPaymentSuccessResponse, createBillingPaymentSuccessRouteHandler, deriveBillingCreditsState, deriveBillingEntitlements, filterBillingPurchasableItems, normalizeBillingCatalogConfig, normalizeBillingPurchaseStatus, normalizeBillingRuntimeConfig, normalizeCasdoorBuyProductResponse, normalizeCasdoorOrderHistoryItem, normalizeCasdoorPaymentHistoryItem, normalizeCasdoorProductId, normalizeCasdoorSubscriptionDetail, normalizeCasdoorSubscriptionHistoryItem, readBuyProductRedirectTo, resolveBillingInterval, resolveBillingItem, resolveBillingProductSnapshot, resolveBillingPurchasable, resolveBillingSubscriptionProduct };
|
package/dist/billing/index.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import {
|
|
2
|
+
buildBillingSubscriptionCatalog,
|
|
2
3
|
createBillingPaymentFinishedResponse,
|
|
3
4
|
createBillingPaymentFinishedRouteHandler,
|
|
4
5
|
createBillingPaymentSuccessResponse,
|
|
5
6
|
createBillingPaymentSuccessRouteHandler
|
|
6
|
-
} from "../chunk-
|
|
7
|
+
} from "../chunk-PILMR42A.js";
|
|
7
8
|
import {
|
|
8
9
|
buildBillingActionPayload,
|
|
9
10
|
buildBillingPaymentCallbackContext,
|
|
@@ -34,6 +35,7 @@ export {
|
|
|
34
35
|
buildBillingActionPayload,
|
|
35
36
|
buildBillingPaymentCallbackContext,
|
|
36
37
|
buildBillingPurchaseRequest,
|
|
38
|
+
buildBillingSubscriptionCatalog,
|
|
37
39
|
buildCasdoorBuyProductParams,
|
|
38
40
|
buildCasdoorBuyProductRequest,
|
|
39
41
|
chooseCasdoorProviderName,
|
|
@@ -2,6 +2,44 @@ import {
|
|
|
2
2
|
buildBillingPaymentCallbackContext
|
|
3
3
|
} from "./chunk-FL6LOXEG.js";
|
|
4
4
|
|
|
5
|
+
// src/billing/subscription-catalog.ts
|
|
6
|
+
function buildBillingSubscriptionCatalog(plans, options) {
|
|
7
|
+
const items = plans.map((plan, index) => {
|
|
8
|
+
const mapped = options.mapPlan(plan, index);
|
|
9
|
+
return {
|
|
10
|
+
key: mapped.key,
|
|
11
|
+
kind: "subscription",
|
|
12
|
+
title: mapped.title,
|
|
13
|
+
description: mapped.description,
|
|
14
|
+
featured: mapped.featured,
|
|
15
|
+
badge: mapped.badge,
|
|
16
|
+
priceLabel: mapped.priceLabel,
|
|
17
|
+
priceValue: mapped.priceValue,
|
|
18
|
+
interval: mapped.interval,
|
|
19
|
+
features: mapped.features,
|
|
20
|
+
backendRef: {
|
|
21
|
+
productId: mapped.productId,
|
|
22
|
+
planId: mapped.planId,
|
|
23
|
+
priceId: mapped.priceId
|
|
24
|
+
},
|
|
25
|
+
metadata: mapped.metadata
|
|
26
|
+
};
|
|
27
|
+
});
|
|
28
|
+
return {
|
|
29
|
+
catalogKey: options.catalogKey,
|
|
30
|
+
title: options.title,
|
|
31
|
+
description: options.description,
|
|
32
|
+
portalPath: options.portalPath,
|
|
33
|
+
successPath: options.successPath,
|
|
34
|
+
cancelPath: options.cancelPath,
|
|
35
|
+
purchasableIds: options.purchasableIds ?? items.map((item) => item.key),
|
|
36
|
+
purchasables: [],
|
|
37
|
+
conversionRules: [],
|
|
38
|
+
defaults: options.defaults ?? {},
|
|
39
|
+
items
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
5
43
|
// src/billing/payment-route.ts
|
|
6
44
|
import { NextResponse } from "next/server";
|
|
7
45
|
|
|
@@ -134,9 +172,10 @@ function createBillingPaymentFinishedRouteHandler(options = {}) {
|
|
|
134
172
|
}
|
|
135
173
|
|
|
136
174
|
export {
|
|
175
|
+
buildBillingSubscriptionCatalog,
|
|
137
176
|
createBillingPaymentSuccessResponse,
|
|
138
177
|
createBillingPaymentSuccessRouteHandler,
|
|
139
178
|
createBillingPaymentFinishedResponse,
|
|
140
179
|
createBillingPaymentFinishedRouteHandler
|
|
141
180
|
};
|
|
142
|
-
//# sourceMappingURL=chunk-
|
|
181
|
+
//# sourceMappingURL=chunk-PILMR42A.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/billing/subscription-catalog.ts","../src/billing/payment-route.ts","../src/core/origin.ts","../src/billing/payment-success.ts","../src/billing/payment-finished.ts"],"sourcesContent":["import type {\n BillingCatalogConfig,\n BillingDefaults,\n BillingInterval,\n BillingItem,\n} from './types';\n\nexport interface BillingSubscriptionCatalogItemInput<TSource = unknown> {\n source: TSource;\n key: string;\n title: string;\n description?: string;\n productId: string;\n planId?: string;\n priceId?: string;\n interval?: BillingInterval;\n featured?: boolean;\n badge?: string;\n priceLabel?: string;\n priceValue?: number;\n features?: string[];\n metadata?: Record<string, string>;\n}\n\nexport interface BillingSubscriptionCatalogBuilderOptions<TSource = unknown> {\n catalogKey: string;\n title?: string;\n description?: string;\n portalPath?: string;\n successPath?: string;\n cancelPath?: string;\n purchasableIds?: string[];\n defaults?: BillingDefaults;\n mapPlan: (plan: TSource, index: number) => BillingSubscriptionCatalogItemInput<TSource>;\n}\n\nexport function buildBillingSubscriptionCatalog<TSource>(\n plans: readonly TSource[],\n options: BillingSubscriptionCatalogBuilderOptions<TSource>,\n): BillingCatalogConfig {\n const items = plans.map((plan, index) => {\n const mapped = options.mapPlan(plan, index);\n\n return {\n key: mapped.key,\n kind: 'subscription',\n title: mapped.title,\n description: mapped.description,\n featured: mapped.featured,\n badge: mapped.badge,\n priceLabel: mapped.priceLabel,\n priceValue: mapped.priceValue,\n interval: mapped.interval,\n features: mapped.features,\n backendRef: {\n productId: mapped.productId,\n planId: mapped.planId,\n priceId: mapped.priceId,\n },\n metadata: mapped.metadata,\n } satisfies BillingItem;\n });\n\n return {\n catalogKey: options.catalogKey,\n title: options.title,\n description: options.description,\n portalPath: options.portalPath,\n successPath: options.successPath,\n cancelPath: options.cancelPath,\n purchasableIds: options.purchasableIds ?? items.map((item) => item.key),\n purchasables: [],\n conversionRules: [],\n defaults: options.defaults ?? {},\n items,\n };\n}\n","import { NextResponse } from 'next/server';\n\nimport { resolvePublicOrigin } from '../core/origin';\nimport { buildBillingPaymentCallbackContext } from './runtime';\nimport type {\n BillingPaymentRouteBaseOptions,\n BillingPaymentFinishedRouteOptions,\n BillingPaymentFinishedContext,\n BillingPaymentFinishedHandler,\n BillingPaymentSuccessContext,\n BillingPaymentSuccessHandlerResult,\n BillingPaymentSuccessRouteOptions,\n} from './types';\n\nexport interface BillingPaymentRouteOptions extends BillingPaymentRouteBaseOptions {\n routePath: string;\n missingHandlerFile: string;\n handler?: BillingPaymentSuccessRouteOptions['handler'] | BillingPaymentFinishedRouteOptions['handler'];\n phase?: 'success' | 'failure' | 'finished';\n}\n\nfunction sanitizeRedirectPath(value: string | null | undefined, fallback: string): string {\n if (!value || !value.startsWith('/') || value.startsWith('//')) {\n return fallback;\n }\n\n return value;\n}\n\nfunction isDebugEnabled(): boolean {\n const value = process.env.BILLING_PAYMENT_SUCCESS_DEBUG;\n if (!value) {\n return false;\n }\n\n return ['1', 'true', 'yes', 'on'].includes(value.toLowerCase());\n}\n\nfunction resolveRedirectTarget(\n result: BillingPaymentSuccessHandlerResult | undefined,\n context: BillingPaymentSuccessContext,\n fallbackRedirect: string,\n): string {\n if (typeof result === 'string') {\n return sanitizeRedirectPath(result, fallbackRedirect);\n }\n\n if (result && typeof result === 'object' && 'redirectTo' in result) {\n return sanitizeRedirectPath(result.redirectTo ?? null, fallbackRedirect);\n }\n\n return sanitizeRedirectPath(context.redirectTo, fallbackRedirect);\n}\n\nfunction resolveFallbackTarget(fallbackRedirect: string): string {\n return sanitizeRedirectPath(fallbackRedirect, '/');\n}\n\nfunction logMissingPaymentHandler(\n context: BillingPaymentSuccessContext,\n fallbackRedirect: string,\n missingHandlerFile: string,\n routePath: string,\n): void {\n console.warn(\n `[casdoor-next-auth-kit] default billing handler at ${missingHandlerFile} has no business logic. ` +\n `Edit this file to handle ${routePath} with order/payment enrichment before redirecting. ` +\n `Falling back to ${fallbackRedirect}.`,\n {\n paymentOwner: context.paymentOwner,\n paymentName: context.paymentName,\n paymentId: context.paymentId,\n orderId: context.orderId,\n params: context.params,\n },\n );\n}\n\nexport async function createBillingPaymentRouteResponse(\n request: Request,\n options: BillingPaymentRouteOptions,\n) {\n const origin = resolvePublicOrigin(request, options.appUrl);\n const fallbackRedirect = options.fallbackRedirect ?? '/';\n const context = await buildBillingPaymentCallbackContext(request, options.phase);\n\n if (isDebugEnabled()) {\n console.info(`[casdoor-next-auth-kit] ${options.routePath} request`, {\n method: request.method,\n path: context.url.pathname,\n query: context.params,\n body: context.body,\n });\n }\n\n try {\n if (options.handler) {\n const handler = options.handler;\n const result =\n options.phase === 'finished'\n ? await (handler as BillingPaymentFinishedHandler)(context as BillingPaymentFinishedContext)\n : await (handler as NonNullable<BillingPaymentSuccessRouteOptions['handler']>)(context);\n if (result instanceof Response) {\n return result;\n }\n\n const target = resolveRedirectTarget(result, context, fallbackRedirect);\n return NextResponse.redirect(new URL(target, origin), 307);\n }\n\n logMissingPaymentHandler(context, fallbackRedirect, options.missingHandlerFile, options.routePath);\n const target = resolveFallbackTarget(fallbackRedirect);\n return NextResponse.redirect(new URL(target, origin), 307);\n } catch (error) {\n console.error(`[casdoor-next-auth-kit] ${options.routePath} handler failed:`, error);\n return new NextResponse('Billing payment handler failed', { status: 500 });\n }\n}\n","export function normalizeOrigin(value: string | null | undefined): string | null {\n if (!value) return null;\n try {\n return new URL(value).origin;\n } catch {\n return null;\n }\n}\n\nexport function getRequestOrigin(request: Request, appUrl?: string): string {\n const configured = normalizeOrigin(appUrl);\n if (configured) return configured;\n\n const referer = normalizeOrigin(request.headers.get('referer'));\n if (referer) return referer;\n\n const origin = normalizeOrigin(request.headers.get('origin'));\n if (origin) return origin;\n\n const forwardedProto = request.headers.get('x-forwarded-proto')?.split(',')[0]?.trim();\n const forwardedHost = request.headers.get('x-forwarded-host')?.split(',')[0]?.trim();\n if (forwardedProto && forwardedHost) {\n return forwardedProto + '://' + forwardedHost;\n }\n\n return new URL(request.url).origin;\n}\n\nexport function resolvePublicOrigin(request: Request, appUrl?: string): string {\n return getRequestOrigin(request, appUrl);\n}\n","import type { BillingPaymentSuccessHandler, BillingPaymentSuccessRouteOptions } from './types';\nimport { createBillingPaymentRouteResponse } from './payment-route';\n\nexport async function createBillingPaymentSuccessResponse(\n request: Request,\n options: BillingPaymentSuccessRouteOptions = {},\n) {\n return createBillingPaymentRouteResponse(request, {\n ...options,\n routePath: '/auth/payment/success',\n missingHandlerFile: 'lib/billing/payment-success.ts',\n fallbackRedirect: options.fallbackRedirect ?? '/auth/payment/finished',\n phase: options.phase ?? 'success',\n });\n}\n\nexport function createBillingPaymentSuccessRouteHandler(options: BillingPaymentSuccessRouteOptions = {}) {\n return async function GET(request: Request) {\n return createBillingPaymentSuccessResponse(request, options);\n };\n}\n\nexport type { BillingPaymentSuccessHandler };\n","import type { BillingPaymentFinishedHandler, BillingPaymentFinishedRouteOptions } from './types';\nimport { createBillingPaymentRouteResponse } from './payment-route';\n\nexport async function createBillingPaymentFinishedResponse(\n request: Request,\n options: BillingPaymentFinishedRouteOptions = {},\n) {\n return createBillingPaymentRouteResponse(request, {\n ...options,\n routePath: '/auth/payment/finished',\n missingHandlerFile: 'lib/billing/payment-finished.ts',\n fallbackRedirect: options.fallbackRedirect ?? '/',\n phase: options.phase ?? 'finished',\n });\n}\n\nexport function createBillingPaymentFinishedRouteHandler(options: BillingPaymentFinishedRouteOptions = {}) {\n return async function GET(request: Request) {\n return createBillingPaymentFinishedResponse(request, options);\n };\n}\n\nexport type { BillingPaymentFinishedHandler };\n"],"mappings":";;;;;AAoCO,SAAS,gCACd,OACA,SACsB;AACtB,QAAM,QAAQ,MAAM,IAAI,CAAC,MAAM,UAAU;AACvC,UAAM,SAAS,QAAQ,QAAQ,MAAM,KAAK;AAE1C,WAAO;AAAA,MACL,KAAK,OAAO;AAAA,MACZ,MAAM;AAAA,MACN,OAAO,OAAO;AAAA,MACd,aAAa,OAAO;AAAA,MACpB,UAAU,OAAO;AAAA,MACjB,OAAO,OAAO;AAAA,MACd,YAAY,OAAO;AAAA,MACnB,YAAY,OAAO;AAAA,MACnB,UAAU,OAAO;AAAA,MACjB,UAAU,OAAO;AAAA,MACjB,YAAY;AAAA,QACV,WAAW,OAAO;AAAA,QAClB,QAAQ,OAAO;AAAA,QACf,SAAS,OAAO;AAAA,MAClB;AAAA,MACA,UAAU,OAAO;AAAA,IACnB;AAAA,EACF,CAAC;AAED,SAAO;AAAA,IACL,YAAY,QAAQ;AAAA,IACpB,OAAO,QAAQ;AAAA,IACf,aAAa,QAAQ;AAAA,IACrB,YAAY,QAAQ;AAAA,IACpB,aAAa,QAAQ;AAAA,IACrB,YAAY,QAAQ;AAAA,IACpB,gBAAgB,QAAQ,kBAAkB,MAAM,IAAI,CAAC,SAAS,KAAK,GAAG;AAAA,IACtE,cAAc,CAAC;AAAA,IACf,iBAAi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;AACtE,aAAO,aAAa,SAAS,IAAI,IAAIA,SAAQ,MAAM,GAAG,GAAG;AAAA,IAC3D;AAEA,6BAAyB,SAAS,kBAAkB,QAAQ,oBAAoB,QAAQ,SAAS;AACjG,UAAM,SAAS,sBAAsB,gBAAgB;AACrD,WAAO,aAAa,SAAS,IAAI,IAAI,QAAQ,MAAM,GAAG,GAAG;AAAA,EAC3D,SAAS,OAAO;AACd,YAAQ,MAAM,2BAA2B,QAAQ,SAAS,oBAAoB,KAAK;AACnF,WAAO,IAAI,aAAa,kCAAkC,EAAE,QAAQ,IAAI,CAAC;AAAA,EAC3E;AACF;;;AElHA,eAAsB,oCACpB,SACA,UAA6C,CAAC,GAC9C;AACA,SAAO,kCAAkC,SAAS;AAAA,IAChD,GAAG;AAAA,IACH,WAAW;AAAA,IACX,oBAAoB;AAAA,IACpB,kBAAkB,QAAQ,oBAAoB;AAAA,IAC9C,OAAO,QAAQ,SAAS;AAAA,EAC1B,CAAC;AACH;AAEO,SAAS,wCAAwC,UAA6C,CAAC,GAAG;AACvG,SAAO,eAAe,IAAI,SAAkB;AAC1C,WAAO,oCAAoC,SAAS,OAAO;AAAA,EAC7D;AACF;;;ACjBA,eAAsB,qCACpB,SACA,UAA8C,CAAC,GAC/C;AACA,SAAO,kCAAkC,SAAS;AAAA,IAChD,GAAG;AAAA,IACH,WAAW;AAAA,IACX,oBAAoB;AAAA,IACpB,kBAAkB,QAAQ,oBAAoB;AAAA,IAC9C,OAAO,QAAQ,SAAS;AAAA,EAC1B,CAAC;AACH;AAEO,SAAS,yCAAyC,UAA8C,CAAC,GAAG;AACzG,SAAO,eAAe,IAAI,SAAkB;AAC1C,WAAO,qCAAqC,SAAS,OAAO;AAAA,EAC9D;AACF;","names":["target"]}
|
package/dist/index.d.ts
CHANGED
|
@@ -6,7 +6,7 @@ export { C as CallbackHandlerOptions, c as createCallbackHandler, a as createCal
|
|
|
6
6
|
export { createAuthorizeRouteHandler, createLoginRouteHandler, createLogoutHandler, createSignupRouteHandler } from './next/index.js';
|
|
7
7
|
export { A as AuthSession, a as AuthSessionUser, b as AuthTokenPayload, N as NextAuthRouteOptions, c as createNextAuthOptions, d as createNextAuthRouteHandler } from './options-D2YQdRWu.js';
|
|
8
8
|
export { z as BillingActionExecutionResult, f as BillingActionExecutor, A as BillingActionKind, B as BillingActionPayload, d as BillingApiClient, C as BillingCasdoorAccountDetail, D as BillingCasdoorAccountMultiFactorAuthDetail, E as BillingCasdoorAccountResponse, F as BillingCasdoorApiResponse, G as BillingCasdoorApplicationDetail, H as BillingCasdoorApplicationProviderDetail, I as BillingCasdoorApplicationResponse, J as BillingCasdoorApplicationSigninItemDetail, K as BillingCasdoorApplicationSigninMethodDetail, L as BillingCasdoorApplicationSignupItemDetail, M as BillingCasdoorBuyProductRequest, N as BillingCasdoorBuyProductResponse, O as BillingCasdoorErrorDetail, P as BillingCasdoorErrorPayload, t as BillingCasdoorOrderDetail, Q as BillingCasdoorOrderResponse, R as BillingCasdoorOrdersResponse, S as BillingCasdoorOrganizationDetail, T as BillingCasdoorOrganizationNamesResponse, U as BillingCasdoorOrganizationOption, V as BillingCasdoorPaymentDetail, W as BillingCasdoorPaymentResponse, u as BillingCasdoorPlanDetail, X as BillingCasdoorPlanResponse, v as BillingCasdoorPricingDetail, Y as BillingCasdoorPricingResponse, w as BillingCasdoorProductDetail, Z as BillingCasdoorProductResponse, _ as BillingCasdoorProviderDetail, $ as BillingCasdoorProviderOption, s as BillingCasdoorQueryState, x as BillingCasdoorSubscriptionDetail, a0 as BillingCasdoorSubscriptionResponse, a1 as BillingCasdoorSubscriptionsResponse, c as BillingCatalogConfig, a2 as BillingConversionRule, y as BillingCoreContextValue, a3 as BillingCreditsContextValue, m as BillingCreditsState, g as BillingDefaults, n as BillingEntitlementState, a4 as BillingInterval, a as BillingItem, a5 as BillingItemKind, e as BillingLoaders, a6 as BillingOrderCreatedContext, k as BillingOrderHistoryItem, a7 as BillingPaymentCallbackContext, a8 as BillingPaymentFinishedContext, a9 as BillingPaymentFinishedHandler, aa as BillingPaymentFinishedRouteOptions, l as BillingPaymentHistoryItem, ab as BillingPaymentRouteBaseOptions, ac as BillingPaymentSuccessContext, ad as BillingPaymentSuccessHandler, ae as BillingPaymentSuccessHandlerResult, af as BillingPaymentSuccessRouteOptions, ag as BillingProductContextValue, ah as BillingProductDetailState, ai as BillingProductPurchasableEntry, aj as BillingProductPurchaseConfig, r as BillingProductSnapshot, j as BillingProductState, ak as BillingPurchasableEntry, al as BillingPurchasableEntryBase, am as BillingPurchasableKind, an as BillingPurchasableWhitelist, ao as BillingPurchaseCompleteContext, ap as BillingPurchaseErrorContext, q as BillingPurchaseHooks, aq as BillingPurchaseRequest, p as BillingPurchaseStatus, b as BillingRuntimeConfig, o as BillingStatusState, ar as BillingSubscriptionContextValue, i as BillingSubscriptionHistoryItem, as as BillingSubscriptionPurchasableEntry, at as BillingSubscriptionPurchaseConfig, h as BillingSubscriptionState } from './types-oVIQpMut.js';
|
|
9
|
-
export { NormalizedCasdoorProductId, buildBillingActionPayload, buildBillingPaymentCallbackContext, buildBillingPurchaseRequest, buildCasdoorBuyProductParams, buildCasdoorBuyProductRequest, chooseCasdoorProviderName, createBillingPaymentFinishedResponse, createBillingPaymentFinishedRouteHandler, createBillingPaymentSuccessResponse, createBillingPaymentSuccessRouteHandler, deriveBillingCreditsState, deriveBillingEntitlements, filterBillingPurchasableItems, normalizeBillingCatalogConfig, normalizeBillingPurchaseStatus, normalizeBillingRuntimeConfig, normalizeCasdoorBuyProductResponse, normalizeCasdoorOrderHistoryItem, normalizeCasdoorPaymentHistoryItem, normalizeCasdoorProductId, normalizeCasdoorSubscriptionDetail, normalizeCasdoorSubscriptionHistoryItem, readBuyProductRedirectTo, resolveBillingInterval, resolveBillingItem, resolveBillingProductSnapshot, resolveBillingPurchasable, resolveBillingSubscriptionProduct } from './billing/index.js';
|
|
9
|
+
export { BillingSubscriptionCatalogBuilderOptions, BillingSubscriptionCatalogItemInput, NormalizedCasdoorProductId, buildBillingActionPayload, buildBillingPaymentCallbackContext, buildBillingPurchaseRequest, buildBillingSubscriptionCatalog, buildCasdoorBuyProductParams, buildCasdoorBuyProductRequest, chooseCasdoorProviderName, createBillingPaymentFinishedResponse, createBillingPaymentFinishedRouteHandler, createBillingPaymentSuccessResponse, createBillingPaymentSuccessRouteHandler, deriveBillingCreditsState, deriveBillingEntitlements, filterBillingPurchasableItems, normalizeBillingCatalogConfig, normalizeBillingPurchaseStatus, normalizeBillingRuntimeConfig, normalizeCasdoorBuyProductResponse, normalizeCasdoorOrderHistoryItem, normalizeCasdoorPaymentHistoryItem, normalizeCasdoorProductId, normalizeCasdoorSubscriptionDetail, normalizeCasdoorSubscriptionHistoryItem, readBuyProductRedirectTo, resolveBillingInterval, resolveBillingItem, resolveBillingProductSnapshot, resolveBillingPurchasable, resolveBillingSubscriptionProduct } from './billing/index.js';
|
|
10
10
|
import 'next/server';
|
|
11
11
|
import 'next-auth';
|
|
12
12
|
|
package/dist/index.js
CHANGED
|
@@ -10,11 +10,12 @@ import {
|
|
|
10
10
|
sanitizeExistingEnvContent
|
|
11
11
|
} from "./chunk-FW4WDHNS.js";
|
|
12
12
|
import {
|
|
13
|
+
buildBillingSubscriptionCatalog,
|
|
13
14
|
createBillingPaymentFinishedResponse,
|
|
14
15
|
createBillingPaymentFinishedRouteHandler,
|
|
15
16
|
createBillingPaymentSuccessResponse,
|
|
16
17
|
createBillingPaymentSuccessRouteHandler
|
|
17
|
-
} from "./chunk-
|
|
18
|
+
} from "./chunk-PILMR42A.js";
|
|
18
19
|
import {
|
|
19
20
|
createCasdoorApiProxyHandler,
|
|
20
21
|
createCasdoorCommerceProxyHandler,
|
|
@@ -108,6 +109,7 @@ export {
|
|
|
108
109
|
buildBillingActionPayload,
|
|
109
110
|
buildBillingPaymentCallbackContext,
|
|
110
111
|
buildBillingPurchaseRequest,
|
|
112
|
+
buildBillingSubscriptionCatalog,
|
|
111
113
|
buildCasdoorBuyProductParams,
|
|
112
114
|
buildCasdoorBuyProductRequest,
|
|
113
115
|
buildManagedEnvTemplate,
|
|
@@ -82,6 +82,7 @@ npx @foldspace-fe/casdoor-next-auth-kit@latest check
|
|
|
82
82
|
- 数据库契约和同步接口 — 定义宿主项目必须实现的用户数据字段和同步行为
|
|
83
83
|
- `BillingProvider` / `BillingCoreProvider` — headless billing runtime 的根 Provider,支持 `purchasableIds` 白名单、`purchasables` 显式条目、Casdoor 商品购买适配器和购买回调 hooks
|
|
84
84
|
- `SubscriptionProvider` / `ProductProvider` / `CreditsProvider` — 分域 billing Provider,按需注入订阅、商品和额度状态
|
|
85
|
+
- `buildBillingSubscriptionCatalog` — 把宿主已有的会员计划 rows 转成 auth-kit 的 subscription catalog,适合 membership 这种“订阅为主、商品为辅”的接入场景
|
|
85
86
|
- `useBillingAvailablePlans` / `useBillingAvailableProducts` — 从配置或注入的 catalog 中读取可订阅套餐和可购买商品列表,并自动过滤白名单外条目
|
|
86
87
|
- `useBillingPricing` / `useBillingPlan` / `useBillingPricingPlans` / `useBillingSubscriptionPurchaseOptions` — 读取订阅定价、单个计划、计划组合和订阅购买选项
|
|
87
88
|
- `useBillingSubscription` / `useBillingSubscriptionHistory` / `useBillingSubscriptionRecord` / `useBillingSubscriptions` / `useBillingSubscriptionProduct` — 查看当前订阅、订阅历史、订阅记录、订阅列表和当前订阅对应产品
|
|
@@ -90,6 +91,9 @@ npx @foldspace-fe/casdoor-next-auth-kit@latest check
|
|
|
90
91
|
- `useSubscribePlan` / `usePurchaseProduct` — 发起订阅和商品购买动作,单次购买只针对一个具体条目;订阅购买会先走定价和计划查询,商品购买会先走商品详情解析,再组装 Casdoor 的下单参数
|
|
91
92
|
- `useBillingRefresh` / `useBillingPipeline` — 刷新 billing 状态和编排动作执行链路
|
|
92
93
|
|
|
94
|
+
订阅域和商品域要保持分离:`kind: 'subscription'` 的 catalog 条目只负责定价、计划和订阅状态,`kind: 'product'` 的 catalog 条目只负责商品、订单和支付状态。两类条目可以共存于同一个 catalog,但不要把它们压成一套无差别的购买对象。
|
|
95
|
+
这条约定已经由 `packages/auth-kit/test/billing-subscription-domain.test.ts` 锁住;后续如果调整 catalog、购买 payload 或订阅购买路径,优先更新这个回归测试,确保订阅和商品仍然是两条独立链路。
|
|
96
|
+
|
|
93
97
|
## 路由模型
|
|
94
98
|
|
|
95
99
|
认证套件采用同源路由架构,所有认证相关页面均在宿主应用域名下呈现:
|
|
@@ -113,7 +117,7 @@ Billing 的购买白名单可以通过 `BillingCatalogConfig.purchasableIds` 或
|
|
|
113
117
|
|
|
114
118
|
商品购买的包内适配器会优先读取 Casdoor 商品详情,再按 `owner/name` 解析商品 ID,并自动选择可用 provider 后调用 `buy-product` 兼容接口;宿主只需要提供允许购买的商品 id 和相应的 Casdoor 接口 loader。loader 约定使用 Casdoor 的标准响应 envelope,然后从 `data` 中取出商品、组织、账号、应用或支付记录。`buy-product` 如果返回 `status: "error"`,包内会把 `msg` 里的错误信息和错误码透传到宿主的 `onPurchaseError` / `onPurchaseComplete`。`useBillingProductDetail` 会把商品详情里的 `providers` 和 `providerObjs` 暴露给宿主,`useBillingProductPurchaseOptions` 可以直接拿到商品详情、当前 provider 选择、当前选中 provider 对象和 setter,适合商品详情页按支付方式展示不同购买参数;宿主选中的 `providerName` 也可以直接传给 `purchaseProduct.run({ key, providerName })`,让包内适配器按这个 provider 下单。这个 hook 只是给单选场景提供默认态,如果宿主想同时渲染两个不同的支付入口,直接遍历 `providerObjs` 就行,`selectedProvider` 不会限制 UI 结构。`productId` 推荐写成 `owner/name` 形式,例如 `qixiaoju/创小剧积分包-50`,和 `GET /api/get-product?id=qixiaoju/创小剧积分包-50` 的查询值保持一致。支付结果轮询和 `get-account` / `get-application` / `get-payment` 这类浏览器侧查询,优先请求 `/auth/api/*` 同域代理;只有服务端或明确启用 CORS 的特殊场景,才考虑直接连 `NEXT_PUBLIC_CASDOOR_SERVER_URL` origin 的 `/api/*`。
|
|
115
119
|
|
|
116
|
-
SaaS 订阅状态建议直接接 Casdoor 的 `get-pricing` / `get-plan` / `get-subscription` / `get-subscriptions`,产品购买后的订单列表和订单状态建议直接接 Casdoor 的 `get-order` / `get-orders` / `get-payment`。宿主本地的 `BillingSubscriptionState`、`BillingOrderHistoryItem`、`BillingPurchaseStatus` 只应该是归一化展示层,不应该成为真相源;如果宿主需要展示订阅计划价格、计划列表、订单详情或支付明细,优先从这些 Casdoor 查询 loader 里取 `data
|
|
120
|
+
SaaS 订阅状态建议直接接 Casdoor 的 `get-pricing` / `get-plan` / `get-subscription` / `get-subscriptions`,产品购买后的订单列表和订单状态建议直接接 Casdoor 的 `get-order` / `get-orders` / `get-payment`。宿主本地的 `BillingSubscriptionState`、`BillingOrderHistoryItem`、`BillingPurchaseStatus` 只应该是归一化展示层,不应该成为真相源;如果宿主需要展示订阅计划价格、计划列表、订单详情或支付明细,优先从这些 Casdoor 查询 loader 里取 `data`。订阅 catalog 条目和商品 catalog 条目可以共存,但 UI 和购买参数生成要保持两个分支各自独立。
|
|
117
121
|
|
|
118
122
|
## 宿主工程 `proxy.ts` 配置要求
|
|
119
123
|
|
package/package.json
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/billing/payment-route.ts","../src/core/origin.ts","../src/billing/payment-success.ts","../src/billing/payment-finished.ts"],"sourcesContent":["import { NextResponse } from 'next/server';\n\nimport { resolvePublicOrigin } from '../core/origin';\nimport { buildBillingPaymentCallbackContext } from './runtime';\nimport type {\n BillingPaymentRouteBaseOptions,\n BillingPaymentFinishedRouteOptions,\n BillingPaymentFinishedContext,\n BillingPaymentFinishedHandler,\n BillingPaymentSuccessContext,\n BillingPaymentSuccessHandlerResult,\n BillingPaymentSuccessRouteOptions,\n} from './types';\n\nexport interface BillingPaymentRouteOptions extends BillingPaymentRouteBaseOptions {\n routePath: string;\n missingHandlerFile: string;\n handler?: BillingPaymentSuccessRouteOptions['handler'] | BillingPaymentFinishedRouteOptions['handler'];\n phase?: 'success' | 'failure' | 'finished';\n}\n\nfunction sanitizeRedirectPath(value: string | null | undefined, fallback: string): string {\n if (!value || !value.startsWith('/') || value.startsWith('//')) {\n return fallback;\n }\n\n return value;\n}\n\nfunction isDebugEnabled(): boolean {\n const value = process.env.BILLING_PAYMENT_SUCCESS_DEBUG;\n if (!value) {\n return false;\n }\n\n return ['1', 'true', 'yes', 'on'].includes(value.toLowerCase());\n}\n\nfunction resolveRedirectTarget(\n result: BillingPaymentSuccessHandlerResult | undefined,\n context: BillingPaymentSuccessContext,\n fallbackRedirect: string,\n): string {\n if (typeof result === 'string') {\n return sanitizeRedirectPath(result, fallbackRedirect);\n }\n\n if (result && typeof result === 'object' && 'redirectTo' in result) {\n return sanitizeRedirectPath(result.redirectTo ?? null, fallbackRedirect);\n }\n\n return sanitizeRedirectPath(context.redirectTo, fallbackRedirect);\n}\n\nfunction resolveFallbackTarget(fallbackRedirect: string): string {\n return sanitizeRedirectPath(fallbackRedirect, '/');\n}\n\nfunction logMissingPaymentHandler(\n context: BillingPaymentSuccessContext,\n fallbackRedirect: string,\n missingHandlerFile: string,\n routePath: string,\n): void {\n console.warn(\n `[casdoor-next-auth-kit] default billing handler at ${missingHandlerFile} has no business logic. ` +\n `Edit this file to handle ${routePath} with order/payment enrichment before redirecting. ` +\n `Falling back to ${fallbackRedirect}.`,\n {\n paymentOwner: context.paymentOwner,\n paymentName: context.paymentName,\n paymentId: context.paymentId,\n orderId: context.orderId,\n params: context.params,\n },\n );\n}\n\nexport async function createBillingPaymentRouteResponse(\n request: Request,\n options: BillingPaymentRouteOptions,\n) {\n const origin = resolvePublicOrigin(request, options.appUrl);\n const fallbackRedirect = options.fallbackRedirect ?? '/';\n const context = await buildBillingPaymentCallbackContext(request, options.phase);\n\n if (isDebugEnabled()) {\n console.info(`[casdoor-next-auth-kit] ${options.routePath} request`, {\n method: request.method,\n path: context.url.pathname,\n query: context.params,\n body: context.body,\n });\n }\n\n try {\n if (options.handler) {\n const handler = options.handler;\n const result =\n options.phase === 'finished'\n ? await (handler as BillingPaymentFinishedHandler)(context as BillingPaymentFinishedContext)\n : await (handler as NonNullable<BillingPaymentSuccessRouteOptions['handler']>)(context);\n if (result instanceof Response) {\n return result;\n }\n\n const target = resolveRedirectTarget(result, context, fallbackRedirect);\n return NextResponse.redirect(new URL(target, origin), 307);\n }\n\n logMissingPaymentHandler(context, fallbackRedirect, options.missingHandlerFile, options.routePath);\n const target = resolveFallbackTarget(fallbackRedirect);\n return NextResponse.redirect(new URL(target, origin), 307);\n } catch (error) {\n console.error(`[casdoor-next-auth-kit] ${options.routePath} handler failed:`, error);\n return new NextResponse('Billing payment handler failed', { status: 500 });\n }\n}\n","export function normalizeOrigin(value: string | null | undefined): string | null {\n if (!value) return null;\n try {\n return new URL(value).origin;\n } catch {\n return null;\n }\n}\n\nexport function getRequestOrigin(request: Request, appUrl?: string): string {\n const configured = normalizeOrigin(appUrl);\n if (configured) return configured;\n\n const referer = normalizeOrigin(request.headers.get('referer'));\n if (referer) return referer;\n\n const origin = normalizeOrigin(request.headers.get('origin'));\n if (origin) return origin;\n\n const forwardedProto = request.headers.get('x-forwarded-proto')?.split(',')[0]?.trim();\n const forwardedHost = request.headers.get('x-forwarded-host')?.split(',')[0]?.trim();\n if (forwardedProto && forwardedHost) {\n return forwardedProto + '://' + forwardedHost;\n }\n\n return new URL(request.url).origin;\n}\n\nexport function resolvePublicOrigin(request: Request, appUrl?: string): string {\n return getRequestOrigin(request, appUrl);\n}\n","import type { BillingPaymentSuccessHandler, BillingPaymentSuccessRouteOptions } from './types';\nimport { createBillingPaymentRouteResponse } from './payment-route';\n\nexport async function createBillingPaymentSuccessResponse(\n request: Request,\n options: BillingPaymentSuccessRouteOptions = {},\n) {\n return createBillingPaymentRouteResponse(request, {\n ...options,\n routePath: '/auth/payment/success',\n missingHandlerFile: 'lib/billing/payment-success.ts',\n fallbackRedirect: options.fallbackRedirect ?? '/auth/payment/finished',\n phase: options.phase ?? 'success',\n });\n}\n\nexport function createBillingPaymentSuccessRouteHandler(options: BillingPaymentSuccessRouteOptions = {}) {\n return async function GET(request: Request) {\n return createBillingPaymentSuccessResponse(request, options);\n };\n}\n\nexport type { BillingPaymentSuccessHandler };\n","import type { BillingPaymentFinishedHandler, BillingPaymentFinishedRouteOptions } from './types';\nimport { createBillingPaymentRouteResponse } from './payment-route';\n\nexport async function createBillingPaymentFinishedResponse(\n request: Request,\n options: BillingPaymentFinishedRouteOptions = {},\n) {\n return createBillingPaymentRouteResponse(request, {\n ...options,\n routePath: '/auth/payment/finished',\n missingHandlerFile: 'lib/billing/payment-finished.ts',\n fallbackRedirect: options.fallbackRedirect ?? '/',\n phase: options.phase ?? 'finished',\n });\n}\n\nexport function createBillingPaymentFinishedRouteHandler(options: BillingPaymentFinishedRouteOptions = {}) {\n return async function GET(request: Request) {\n return createBillingPaymentFinishedResponse(request, options);\n };\n}\n\nexport type { BillingPaymentFinishedHandler };\n"],"mappings":";;;;;AAAA,SAAS,oBAAoB;;;ACAtB,SAAS,gBAAgB,OAAiD;AAC/E,MAAI,CAAC,MAAO,QAAO;AACnB,MAAI;AACF,WAAO,IAAI,IAAI,KAAK,EAAE;AAAA,EACxB,QAAQ;AACN,WAAO;AAAA,EACT;AACF;AAEO,SAAS,iBAAiB,SAAkB,QAAyB;AAC1E,QAAM,aAAa,gBAAgB,MAAM;AACzC,MAAI,WAAY,QAAO;AAEvB,QAAM,UAAU,gBAAgB,QAAQ,QAAQ,IAAI,SAAS,CAAC;AAC9D,MAAI,QAAS,QAAO;AAEpB,QAAM,SAAS,gBAAgB,QAAQ,QAAQ,IAAI,QAAQ,CAAC;AAC5D,MAAI,OAAQ,QAAO;AAEnB,QAAM,iBAAiB,QAAQ,QAAQ,IAAI,mBAAmB,GAAG,MAAM,GAAG,EAAE,CAAC,GAAG,KAAK;AACrF,QAAM,gBAAgB,QAAQ,QAAQ,IAAI,kBAAkB,GAAG,MAAM,GAAG,EAAE,CAAC,GAAG,KAAK;AACnF,MAAI,kBAAkB,eAAe;AACnC,WAAO,iBAAiB,QAAQ;AAAA,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