54 lines
1.4 KiB
TypeScript
54 lines
1.4 KiB
TypeScript
import { getApiStripePlans } from '@repo/loomart-sdk';
|
|
import { loomartClient } from './loomart-client';
|
|
|
|
export interface CreditPlan {
|
|
name: string;
|
|
recurring?: {
|
|
interval: string;
|
|
};
|
|
credits?: number;
|
|
price_id?: string;
|
|
product_id?: string;
|
|
metered_price_id?: string;
|
|
}
|
|
|
|
export interface StripePricingTableResponse {
|
|
pricing_table_items: CreditPlan[];
|
|
}
|
|
|
|
export const getStripePlans = async () => {
|
|
try {
|
|
const { data } = await getApiStripePlans({
|
|
client: loomartClient,
|
|
});
|
|
|
|
// 后端返回 { success: true, data: pricingTable }
|
|
// SDK 包装为 { data: { success: true, data: pricingTable } }
|
|
// 需要解包嵌套的 data
|
|
if (data && typeof data === 'object' && 'success' in data && data.success && 'data' in data) {
|
|
return {
|
|
success: true,
|
|
data: data.data,
|
|
};
|
|
}
|
|
|
|
return {
|
|
success: false,
|
|
message: 'Invalid response format',
|
|
};
|
|
} catch (error) {
|
|
console.error('Failed to fetch Stripe plans:', error);
|
|
return {
|
|
success: false,
|
|
message: error instanceof Error ? error.message : 'Unknown error',
|
|
};
|
|
}
|
|
};
|
|
|
|
export const getPlanNames = (pricingData?: StripePricingTableResponse): string[] => {
|
|
if (!pricingData?.pricing_table_items) return [];
|
|
return pricingData.pricing_table_items
|
|
.filter((item) => item.recurring?.interval === 'month')
|
|
.map((item) => item.name || 'basic');
|
|
};
|