release / release (release) Canceled after 0s
- currency.ts: formatCurrency/formatPrice/formatBalance/getCurrencySymbol 默认币种 USD→CNY,Intl locale en-US→zh-CN,显示为 ¥ 而非 CN¥ - 平台配置与管理后台默认 currency: USD→CNY - 硬编码 $ 符号处(商品价格/余额表/充值弹窗/控制台营收/主题 Demo)全部改 ¥ - i18n: 'Minimum recharge amount is $1' key 改为 ¥1(中英同步) - 说明: 易支付 exchange_rate=1,余额数值即人民币,仅改显示符号不改数值
223 lines
6.9 KiB
TypeScript
223 lines
6.9 KiB
TypeScript
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<RechargeModalProps> = ({
|
|
isOpen,
|
|
onClose,
|
|
}) => {
|
|
const { t } = useTranslation();
|
|
const { paymentChannels } = usePlatformConfig();
|
|
const [amount, setAmount] = useState<string>("");
|
|
const [error, setError] = useState<string>("");
|
|
const [selectedPaymentChannel, setSelectedPaymentChannel] = useState<string>(
|
|
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<HTMLInputElement>) => {
|
|
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 (
|
|
<>
|
|
<Modal isOpen={isOpen} onClose={handleClose} size="sm">
|
|
<ModalContent>
|
|
{() => (
|
|
<>
|
|
<ModalHeader className="flex flex-col gap-1">
|
|
{t("Recharge Account")}
|
|
</ModalHeader>
|
|
<ModalBody>
|
|
<Input
|
|
autoFocus
|
|
label={t("Amount")}
|
|
placeholder="0.00"
|
|
value={amount}
|
|
onChange={handleAmountChange}
|
|
startContent={
|
|
<div className="pointer-events-none flex items-center">
|
|
<span className="text-default-400 text-small">
|
|
{currencySymbol}
|
|
</span>
|
|
</div>
|
|
}
|
|
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 && (
|
|
<div className="space-y-2">
|
|
<label className="text-sm font-medium text-default-700">
|
|
{t("Payment Method")}
|
|
</label>
|
|
<RadioGroup
|
|
value={effectiveChannel}
|
|
onValueChange={setSelectedPaymentChannel}
|
|
isInvalid={!!error && error.includes("payment method")}
|
|
errorMessage={
|
|
error && error.includes("payment method") ? error : ""
|
|
}
|
|
orientation="horizontal"
|
|
description={t(
|
|
"Funds will be added to your account balance"
|
|
)}
|
|
>
|
|
{paymentChannels.map((channel) => (
|
|
<Radio key={channel.id} value={channel.id}>
|
|
{channelLabel(channel.id, channel.name)}
|
|
</Radio>
|
|
))}
|
|
</RadioGroup>
|
|
</div>
|
|
)}
|
|
|
|
{effectiveChannel === "epay" && (
|
|
<p className="text-xs text-default-500">
|
|
{t(
|
|
"Billed in CNY via your epay gateway (Alipay / WeChat Pay), credited to your balance 1:1"
|
|
)}
|
|
</p>
|
|
)}
|
|
</ModalBody>
|
|
<ModalFooter>
|
|
<Button variant="light" onPress={handleClose}>
|
|
{t("Cancel")}
|
|
</Button>
|
|
<Button
|
|
color="primary"
|
|
onPress={handlePurchase}
|
|
isDisabled={!effectiveChannel || !amount}
|
|
>
|
|
{t("Confirm")}
|
|
</Button>
|
|
</ModalFooter>
|
|
</>
|
|
)}
|
|
</ModalContent>
|
|
</Modal>
|
|
|
|
{/**Confirm Purchase Modal */}
|
|
<ConfirmPurchaseModal
|
|
isOpen={isConfirmOpen}
|
|
onClose={() => setIsConfirmOpen(false)}
|
|
onConfirm={handleConfirmPurchase}
|
|
paymentId={paymentId}
|
|
/>
|
|
</>
|
|
);
|
|
};
|