From 3022a497d5e4aedabbfea9c8ddf6f31765efa142 Mon Sep 17 00:00:00 2001 From: Zixin Zhou Date: Tue, 9 Sep 2025 21:14:06 +0800 Subject: [PATCH] adds alipay --- components/layout/top-bar.tsx | 115 ++++++++++++++++++++++++++++++---- lib/stripe.ts | 32 ++++++++++ 2 files changed, 136 insertions(+), 11 deletions(-) diff --git a/components/layout/top-bar.tsx b/components/layout/top-bar.tsx index 2516a30..49a438e 100644 --- a/components/layout/top-bar.tsx +++ b/components/layout/top-bar.tsx @@ -23,6 +23,7 @@ import { createPortalSession, redirectToPortal, getUserSubscriptionInfo, + buyTokens, } from "@/lib/stripe"; import UserCard from "@/components/common/userCard"; import { showInsufficientPointsNotification } from "@/utils/notifications"; @@ -51,6 +52,8 @@ export function TopBar({ collapsed, isDesktop=true }: { collapsed: boolean, isDe const [subscriptionStatus, setSubscriptionStatus] = useState(""); const [credits, setCredits] = useState(0); const [isLoadingSubscription, setIsLoadingSubscription] = useState(false); + const [isBuyingTokens, setIsBuyingTokens] = useState(false); + const [customAmount, setCustomAmount] = useState(""); // 获取用户订阅信息 const fetchSubscriptionInfo = async () => { @@ -77,6 +80,49 @@ export function TopBar({ collapsed, isDesktop=true }: { collapsed: boolean, isDe } }; + // 处理Token购买 + const handleBuyTokens = async (tokenAmount: number) => { + if (!currentUser?.id) { + console.error("用户未登录"); + return; + } + + if (tokenAmount <= 0) { + console.error("Token数量必须大于0"); + return; + } + + setIsBuyingTokens(true); + try { + const response = await buyTokens({ + token_amount: tokenAmount, + package_type: "basic" + }); + + if (response.successful && response.data?.checkout_url) { + // 跳转到Stripe支付页面 + window.location.href = response.data.checkout_url; + } else { + console.error("创建Token购买失败:", response.message); + } + } catch (error) { + console.error("Token购买失败:", error); + } finally { + setIsBuyingTokens(false); + } + }; + + // 处理自定义金额购买 + const handleCustomAmountBuy = async () => { + const amount = parseInt(customAmount); + if (isNaN(amount) || amount <= 0) { + console.error("请输入有效的Token数量"); + return; + } + await handleBuyTokens(amount); + setCustomAmount(""); // 清空输入框 + }; + useEffect(() => { const token = localStorage.getItem("token"); if (token) { @@ -299,12 +345,12 @@ export function TopBar({ collapsed, isDesktop=true }: { collapsed: boolean, isDe right: "1rem", zIndex: 9999, }} - className="overflow-hidden rounded-xl" + className="overflow-hidden rounded-xl max-h-[90vh]" data-alt="user-menu-dropdown" onClick={(e) => e.stopPropagation()} > -
+
{/* 顶部用户信息 */}
@@ -323,19 +369,66 @@ export function TopBar({ collapsed, isDesktop=true }: { collapsed: boolean, isDe
{/* AI 积分 */} -
-
- +
+
+
+ +
+ + {isLoadingSubscription + ? "Loading..." + : `${credits} credits`} + +
+ + {/* Purchase Credits 按钮 */} +
+ + + +
+ + {/* 自定义金额输入 */} +
+ setCustomAmount(e.target.value)} + placeholder="Custom amount" + className="flex-1 px-2 py-1 text-xs bg-white/10 text-white placeholder-white/60 border border-white/20 rounded focus:outline-none focus:border-blue-400" + min="1" + disabled={isBuyingTokens} + /> +
- - {isLoadingSubscription - ? "Loading..." - : `${credits} credits`} -
{/* 操作按钮区域 */} -
+