"use client" import * as React from "react" import { useState } from "react" import { toast } from "sonner" import { Power, PowerOff } from "lucide-react" import { Button } from "@/components/ui/button" import { Spinner } from "@/components/ui/spinner" import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, AlertDialogTrigger, } from "@/components/ui/alert-dialog" import type { MerchantAPIKey, UpdateAPIKeyRequest } from "@/lib/services" interface TestModeToggleProps { /** API Key */ apiKey: MerchantAPIKey /** 更新回调 */ onUpdate?: (updatedKey: MerchantAPIKey) => void /** 更新 API Key */ updateAPIKey?: (id: string, data: UpdateAPIKeyRequest) => Promise } /** * 测试模式切换组件 * 提供测试模式开关按钮,带有详细的说明提示 */ export function TestModeToggle({ apiKey, onUpdate, updateAPIKey }: TestModeToggleProps) { const [open, setOpen] = useState(false) const [processing, setProcessing] = useState(false) const [pendingValue, setPendingValue] = useState(false) const handleToggle = async () => { try { setProcessing(true) const updateData = { app_name: apiKey.app_name, app_homepage_url: apiKey.app_homepage_url, app_description: apiKey.app_description, redirect_uri: apiKey.redirect_uri, notify_url: apiKey.notify_url, test_mode: pendingValue } if (updateAPIKey) { await updateAPIKey(apiKey.id, updateData) } else { const services = await import("@/lib/services") await services.default.merchant.updateAPIKey(apiKey.id, updateData) } toast.success(pendingValue ? '测试模式已开启' : '测试模式已关闭', { description: pendingValue ? '现在订单不会真实扣费,仅用于测试调试' : '现在订单将正常扣费,可以接收真实付款' }) const updatedKey = { ...apiKey, test_mode: pendingValue } onUpdate?.(updatedKey) setOpen(false) } catch (error) { const errorMessage = (error as Error).message || '更新测试模式失败' toast.error('更新失败', { description: errorMessage }) } finally { setProcessing(false) } } const handleClick = (e: React.MouseEvent) => { e.preventDefault() setPendingValue(!apiKey.test_mode) setOpen(true) } return ( {pendingValue ? ( <> 开启测试模式 ) : ( <> 关闭测试模式 )}
{pendingValue ? ( <>

测试模式开启后:

  • 订单不会真实扣费:用户余额不会扣减
  • 商户不会收款:你的余额不会增加
  • 仅限自己测试:只有你可以支付测试订单
  • 订单标记明显:测试订单会显示警告标识

使用场景:在正式上线前测试支付流程、回调通知等功能

) : ( <>

测试模式关闭后:

  • 订单真实扣费:用户余额会正常扣减
  • 商户正常收款:你的余额会正常增加
  • 其他用户可支付:任何人都可以支付订单
  • 正式生产环境:可以接收真实的业务订单

注意:关闭后应用将进入生产模式,请确保已完成测试

)}
取消 { e.preventDefault() handleToggle() }} disabled={processing} className={pendingValue ? "bg-orange-600 hover:bg-orange-700" : "bg-green-600 hover:bg-green-700"} > {processing ? ( <> {pendingValue ? '开启中' : '关闭中'} ) : ( pendingValue ? '确认开启' : '确认关闭' )}
) }