import React, { useEffect, useState } from "react"; import { Modal, ModalContent, ModalHeader, ModalBody, ModalFooter, Button, Input, RadioGroup, Radio, } from "@nextui-org/react"; import { useTranslation } from "@/shared/lib/useTranslation"; import { fetchAPI } from "@/shared/rpc/common-function"; import toast from "react-hot-toast"; import { ConfirmPurchaseModal } from "./ConfirmPurchaseModal"; import { usePlatformConfig } from "@/shared/contexts/PlatformConfigContext"; interface RechargeModalProps { isOpen: boolean; onClose: () => void; } export const RechargeModal: React.FC = ({ isOpen, onClose, }) => { const { t } = useTranslation(); const { paymentChannels } = usePlatformConfig(); const [amount, setAmount] = useState(""); const [error, setError] = useState(""); const [selectedPaymentChannel, setSelectedPaymentChannel] = useState( paymentChannels[0]?.id || "" ); // payment channels arrive asynchronously -- fall back to the first one so the // confirm button is not stuck disabled on a modal opened before they load. const effectiveChannel = selectedPaymentChannel || paymentChannels[0]?.id || ""; const currencySymbol = effectiveChannel === "epay" ? "¥" : "¥"; const channelLabel = (id: string, name: string) => { if (id === "epay") return t("Epay (Alipay / WeChat)"); if (id === "stripe") return name || t("Credit Card"); return name || id; }; const [paymentId, setPaymentId] = useState(""); const [isConfirmOpen, setIsConfirmOpen] = useState(false); const handlePurchase = async () => { const numAmount = parseFloat(amount); if (isNaN(numAmount) || numAmount <= 0) { setError(t("Please enter a valid amount")); return; } if (numAmount < 1) { setError(t("Minimum recharge amount is ¥1")); return; } if (!effectiveChannel) { setError(t("Please select a payment method")); return; } const response = await fetchAPI("/api/payment/create_payment_link", { method: "POST", body: { amount: numAmount, currency: effectiveChannel === "epay" ? "cny" : "usd", // channel id doubles as the payment method the backend switches on payment_method: effectiveChannel, success_url: window.location.origin + "/pay_success", } as unknown as BodyInit, }); if (!response.success) { toast.error( t(`Purchase Error:{{error_msg}}`, { error_msg: response.error_message, }), { duration: 3000, className: "max-w-fit", } ); return; } const purchaseWindow = window.open( response.data.pay_url, "PurchaseWindow", "width=600,height=600" ); setPaymentId(response.data.payment_id); if (!purchaseWindow) { alert("The purchase window was blocked by the browser."); return; } setIsConfirmOpen(true); }; const handleConfirmPurchase = async (confirmed: boolean = false) => { if (confirmed) { //auto refresh user info onClose(); } setIsConfirmOpen(false); }; const handleAmountChange = (e: React.ChangeEvent) => { const value = e.target.value; // only allow input numbers and decimal points if (/^\d*\.?\d*$/.test(value) || value === "") { setAmount(value); setError(""); } }; const handleClose = () => { setAmount(""); setError(""); setSelectedPaymentChannel(paymentChannels[0]?.id || ""); onClose(); }; return ( <> {() => ( <> {t("Recharge Account")} {currencySymbol} } type="text" variant="bordered" isInvalid={!!error && !error.includes("payment method")} errorMessage={ error && !error.includes("payment method") ? error : "" } labelPlacement="outside" onKeyDown={(e) => { if (e.key === "Enter") { handlePurchase(); } }} /> {paymentChannels.length > 1 && (
{paymentChannels.map((channel) => ( {channelLabel(channel.id, channel.name)} ))}
)} {effectiveChannel === "epay" && (

{t( "Billed in CNY via your epay gateway (Alipay / WeChat Pay), credited to your balance 1:1" )}

)}
)}
{/**Confirm Purchase Modal */} setIsConfirmOpen(false)} onConfirm={handleConfirmPurchase} paymentId={paymentId} /> ); };