"use client" import type React from "react" import { useState } from "react" import { X, CheckCircle, Bitcoin, Globe, Copy, Check } from "lucide-react" import { Button } from "@/components/ui/button" import { useStore } from "@/lib/store" const WechatIcon = () => ( ) const AlipayIcon = () => ( ) type PaymentMethod = "wechat" | "alipay" | "usdt" | "paypal" 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("wechat") const [isProcessing, setIsProcessing] = useState(false) const [isSuccess, setIsSuccess] = useState(false) const [showPaymentDetails, setShowPaymentDetails] = useState(false) const [copied, setCopied] = useState(false) const { purchaseSection, purchaseFullBook, user, settings } = useStore() const paymentConfig = settings?.paymentMethods || { wechat: { enabled: true, qrCode: "", account: "" }, 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 () => { if (paymentMethod === "usdt" || paymentMethod === "paypal") { setShowPaymentDetails(true) return } setIsProcessing(true) await new Promise((resolve) => setTimeout(resolve, 1500)) 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) setTimeout(() => { onSuccess() onClose() setIsSuccess(false) }, 1500) } } const confirmCryptoPayment = 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) setShowPaymentDetails(false) if (success) { setIsSuccess(true) setTimeout(() => { onSuccess() onClose() setIsSuccess(false) }, 1500) } } 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, }, { id: "alipay", name: "支付宝", icon: , color: "bg-[#1677FF]", enabled: paymentConfig.alipay.enabled, }, { id: "usdt", name: `USDT (${paymentConfig.usdt.network || "TRC20"})`, icon: , color: "bg-[#26A17B]", enabled: paymentConfig.usdt.enabled, extra: `≈ $${usdtAmount}`, }, { id: "paypal", name: "PayPal", icon: , color: "bg-[#003087]", enabled: paymentConfig.paypal.enabled, extra: `≈ $${paypalAmount}`, }, ] const availableMethods = paymentMethods.filter((m) => m.enabled) // Crypto payment details view if (showPaymentDetails) { const isCrypto = paymentMethod === "usdt" const address = isCrypto ? paymentConfig.usdt.address : paymentConfig.paypal.email const displayAmount = isCrypto ? `$${usdtAmount} USDT` : `$${paypalAmount} USD` return (

{isCrypto ? "USDT支付" : "PayPal支付"}

支付金额

{displayAmount}

≈ ¥{amount.toFixed(2)}

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

{address || (isCrypto ? "请联系客服获取地址" : "请联系客服获取账户")}

{address && ( )}

请在转账完成后点击"已完成支付"按钮,系统将在1-24小时内确认到账并开通权限。

) } return (
{isSuccess ? (

支付成功

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

) : ( <>

确认支付

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

支付金额

¥{amount.toFixed(2)}

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

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

)} {user?.referredBy && (

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

)}

选择支付方式

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

暂无可用支付方式

}

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

)}
) }