"use client" import type React from "react" import { useState, useEffect } from "react" import { X, CheckCircle, Bitcoin, Globe, Copy, Check, QrCode } from "lucide-react" import { Button } from "@/components/ui/button" import { useStore } from "@/lib/store" const WechatIcon = () => ( ) const AlipayIcon = () => ( ) type PaymentMethod = "wechat" | "alipay" | "usdt" | "paypal" | "stripe" | "bank" interface PaymentModalProps { isOpen: boolean onClose: () => void type: "section" | "fullbook" sectionId?: string sectionTitle?: string amount: number onSuccess: () => void } export function PaymentModal({ isOpen, onClose, type, sectionId, sectionTitle, amount, onSuccess }: PaymentModalProps) { const [paymentMethod, setPaymentMethod] = useState("alipay") const [isProcessing, setIsProcessing] = useState(false) const [isSuccess, setIsSuccess] = useState(false) const [showQRCode, setShowQRCode] = useState(false) const [copied, setCopied] = useState(false) const { purchaseSection, purchaseFullBook, user, settings } = useStore() useEffect(() => { if (isOpen) { setShowQRCode(false) setIsSuccess(false) setIsProcessing(false) } }, [isOpen]) const paymentConfig = settings?.paymentMethods || { wechat: { enabled: true, qrCode: "", account: "", groupQrCode: "" }, alipay: { enabled: true, qrCode: "", account: "" }, usdt: { enabled: true, network: "TRC20", address: "", exchangeRate: 7.2 }, paypal: { enabled: false, email: "", exchangeRate: 7.2 }, } const usdtAmount = (amount / (paymentConfig.usdt?.exchangeRate || 7.2)).toFixed(2) const paypalAmount = (amount / (paymentConfig.paypal?.exchangeRate || 7.2)).toFixed(2) const handleCopyAddress = (address: string) => { navigator.clipboard.writeText(address) setCopied(true) setTimeout(() => setCopied(false), 2000) } const handlePayment = async () => { // 直接显示支付二维码/详情页面 setShowQRCode(true) // 如果有跳转链接,尝试打开 if (paymentMethod === "wechat" && paymentConfig.wechat?.qrCode) { const link = paymentConfig.wechat.qrCode if (link.startsWith("http") || link.startsWith("weixin://")) { window.open(link, "_blank") } } else if (paymentMethod === "alipay" && paymentConfig.alipay?.qrCode) { const link = paymentConfig.alipay.qrCode if (link.startsWith("http") || link.startsWith("alipays://")) { window.open(link, "_blank") } } } const confirmPayment = async () => { setIsProcessing(true) await new Promise((resolve) => setTimeout(resolve, 1000)) let success = false if (type === "section" && sectionId) { success = await purchaseSection(sectionId, sectionTitle, paymentMethod) } else if (type === "fullbook") { success = await purchaseFullBook(paymentMethod) } setIsProcessing(false) if (success) { setIsSuccess(true) // 支付成功后跳转微信群 const groupUrl = paymentConfig.wechat?.groupQrCode if (groupUrl) { setTimeout(() => { window.open(groupUrl, "_blank") }, 800) } setTimeout(() => { onSuccess() onClose() setIsSuccess(false) setShowQRCode(false) }, 2500) } } if (!isOpen) return null const paymentMethods: { id: PaymentMethod name: string icon: React.ReactNode color: string enabled: boolean extra?: string }[] = [ { id: "wechat", name: "微信支付", icon: , color: "bg-[#07C160]", enabled: paymentConfig.wechat?.enabled ?? true, }, { id: "alipay", name: "支付宝", icon: , color: "bg-[#1677FF]", enabled: paymentConfig.alipay?.enabled ?? true, }, { id: "usdt", name: `USDT (${paymentConfig.usdt?.network || "TRC20"})`, icon: , color: "bg-[#26A17B]", enabled: paymentConfig.usdt?.enabled ?? true, extra: `≈ $${usdtAmount}`, }, { id: "paypal", name: "PayPal", icon: , color: "bg-[#003087]", enabled: paymentConfig.paypal?.enabled ?? false, extra: `≈ $${paypalAmount}`, }, ] const availableMethods = paymentMethods.filter((m) => m.enabled) // 二维码/详情页面 if (showQRCode) { const isCrypto = paymentMethod === "usdt" const isPaypal = paymentMethod === "paypal" const isWechat = paymentMethod === "wechat" const isAlipay = paymentMethod === "alipay" let address = "" let displayAmount = `¥${amount.toFixed(2)}` let title = "扫码支付" let hint = "支付完成后,请点击下方已完成支付按钮" let qrCodeUrl = "" if (isCrypto) { address = paymentConfig.usdt?.address || "" displayAmount = `$${usdtAmount} USDT` title = "USDT支付" hint = "请转账到以下地址,完成后点击确认" } else if (isPaypal) { address = paymentConfig.paypal?.email || "" displayAmount = `$${paypalAmount} USD` title = "PayPal支付" hint = "请转账到以下PayPal账户" } else if (isWechat) { title = "微信支付" qrCodeUrl = paymentConfig.wechat?.qrCode || "" hint = "请使用微信扫码支付" } else if (isAlipay) { title = "支付宝支付" qrCodeUrl = paymentConfig.alipay?.qrCode || "" hint = "请使用支付宝扫码支付" } return (
{/* 标题 */}

{title}

{/* 金额显示 */}

{displayAmount}

{/* QR Code Display */} {(isWechat || isAlipay) && (
{qrCodeUrl ? ( 支付二维码 ) : (
请在后台配置收款码
)}

{hint}

)} {/* Crypto/PayPal Address */} {(isCrypto || isPaypal) && (

{isCrypto ? `收款地址 (${paymentConfig.usdt?.network})` : "PayPal账户"}

{address || "请在后台配置收款地址"}

{address && ( )}

{hint}

)} {/* 提示信息 */}

支付完成后,系统将自动开通阅读权限

{/* Action Buttons */}
) } // 支付成功页面 if (isSuccess) { return (

支付成功

{type === "fullbook" ? "您已解锁全部内容" : "您已解锁本节内容"}

{paymentConfig.wechat?.groupQrCode &&

正在跳转到微信群...

}
) } // 主支付选择页面 return (
{/* Header */}

确认支付

{type === "fullbook" ? "购买整本书,解锁全部内容" : `购买: ${sectionTitle}`}

{/* Amount */}

支付金额

¥{amount.toFixed(2)}

{(paymentMethod === "usdt" || paymentMethod === "paypal") && (

≈ ${paymentMethod === "usdt" ? usdtAmount : paypalAmount} USD

)} {user?.referredBy && (

通过邀请注册,{settings?.distributorShare || 90}%将返还给推荐人

)}
{/* Payment Methods */}

选择支付方式

{availableMethods.map((method) => ( ))} {availableMethods.length === 0 &&

暂无可用支付方式

}
{/* Submit Button */}

支付即表示同意《用户协议》和《隐私政策》

) }