feat(subscription): load vip and credit plans from api
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import type { PaymentPlan } from "@/data/dto/payment";
|
||||
|
||||
import type { CoinsOfferPlanView } from "./components/subscription-coins-offer-section";
|
||||
import type { SubscriptionPlanView } from "./components/subscription-plan-card";
|
||||
import type { VipOfferPlanView } from "./components/subscription-vip-offer-section";
|
||||
|
||||
export type SubscriptionType = "vip" | "voice";
|
||||
|
||||
@@ -9,17 +11,29 @@ export const SUBSCRIPTION_BANNER_TITLES: Record<SubscriptionType, string> = {
|
||||
voice: "Buy Voice Message Package",
|
||||
};
|
||||
|
||||
function isVipPlan(plan: PaymentPlan): boolean {
|
||||
export function isVipPlan(plan: PaymentPlan): boolean {
|
||||
return plan.orderType.startsWith("vip_") || plan.planId.startsWith("vip_");
|
||||
}
|
||||
|
||||
export function isCreditPlan(plan: PaymentPlan): boolean {
|
||||
const orderType = plan.orderType.toLowerCase();
|
||||
const planId = plan.planId.toLowerCase();
|
||||
return (
|
||||
plan.dolAmount !== null ||
|
||||
orderType.startsWith("dol_") ||
|
||||
orderType.startsWith("credit_") ||
|
||||
planId.startsWith("dol_") ||
|
||||
planId.startsWith("credit_") ||
|
||||
orderType.includes("credit") ||
|
||||
planId.includes("credit")
|
||||
);
|
||||
}
|
||||
|
||||
function isVoicePackagePlan(plan: PaymentPlan): boolean {
|
||||
const orderType = plan.orderType.toLowerCase();
|
||||
const planId = plan.planId.toLowerCase();
|
||||
return (
|
||||
plan.dolAmount !== null ||
|
||||
orderType.startsWith("dol_") ||
|
||||
planId.startsWith("dol_") ||
|
||||
isCreditPlan(plan) ||
|
||||
orderType.includes("voice") ||
|
||||
planId.includes("voice")
|
||||
);
|
||||
@@ -35,8 +49,9 @@ export function isPlanForType(
|
||||
}
|
||||
|
||||
function currencySymbol(currency: string): string {
|
||||
if (currency === "CNY") return "¥";
|
||||
if (currency === "USD") return "$";
|
||||
const normalized = currency.toUpperCase();
|
||||
if (normalized === "CNY") return "¥";
|
||||
if (normalized === "USD") return "$";
|
||||
return `${currency} `;
|
||||
}
|
||||
|
||||
@@ -46,6 +61,33 @@ function formatAmount(amountCents: number | null): string | null {
|
||||
return (amountCents / 100).toFixed(2).replace(/\.?0+$/, "");
|
||||
}
|
||||
|
||||
function formatOfferAmount(amountCents: number | null): string {
|
||||
return (formatAmount(amountCents) ?? "").replace(".", ",");
|
||||
}
|
||||
|
||||
function formatOfferCurrency(currency: string): string {
|
||||
const normalized = currency.toUpperCase();
|
||||
if (normalized === "USD") return "usd";
|
||||
return currency.toLowerCase();
|
||||
}
|
||||
|
||||
function formatCoinCurrency(currency: string): string {
|
||||
const normalized = currency.toUpperCase();
|
||||
if (normalized === "USD") return "US$";
|
||||
if (normalized === "CNY") return "¥";
|
||||
return `${currency} `;
|
||||
}
|
||||
|
||||
function vipOfferTitle(plan: PaymentPlan): string {
|
||||
const key = `${plan.planId} ${plan.orderType} ${plan.planName}`.toLowerCase();
|
||||
if (key.includes("year")) return "连续包年";
|
||||
if (key.includes("quarter")) return "连续包季";
|
||||
if (key.includes("month")) return "连续包月";
|
||||
if (plan.vipDays !== null && plan.vipDays >= 300) return "连续包年";
|
||||
if (plan.vipDays !== null && plan.vipDays >= 80) return "连续包季";
|
||||
return plan.planName;
|
||||
}
|
||||
|
||||
function planCaption(
|
||||
plan: PaymentPlan,
|
||||
subscriptionType: SubscriptionType,
|
||||
@@ -79,3 +121,22 @@ export function toPlanView(
|
||||
currencySymbol: currencySymbol(plan.currency),
|
||||
};
|
||||
}
|
||||
|
||||
export function toVipOfferPlanView(plan: PaymentPlan): VipOfferPlanView {
|
||||
return {
|
||||
id: plan.planId,
|
||||
title: vipOfferTitle(plan),
|
||||
price: formatOfferAmount(plan.amountCents),
|
||||
currency: formatOfferCurrency(plan.currency),
|
||||
originalPrice: formatOfferAmount(plan.originalAmountCents),
|
||||
};
|
||||
}
|
||||
|
||||
export function toCoinsOfferPlanView(plan: PaymentPlan): CoinsOfferPlanView {
|
||||
return {
|
||||
id: plan.planId,
|
||||
coins: plan.dolAmount ?? 0,
|
||||
price: formatOfferAmount(plan.amountCents),
|
||||
currency: formatCoinCurrency(plan.currency),
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user