/* Copyright (C) 2023-2026 QuantumNous This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . For commercial licensing, please contact support@quantumnous.com */ import * as React from 'react' import * as z from 'zod' import { useForm } from 'react-hook-form' import { zodResolver } from '@hookform/resolvers/zod' import { useMutation, useQueryClient } from '@tanstack/react-query' import { Code2, Eye, ShieldAlert } from 'lucide-react' import { useTranslation } from 'react-i18next' import { toast } from 'sonner' import { cn } from '@/lib/utils' import { Alert, AlertAction, AlertDescription, AlertTitle, } from '@/components/ui/alert' import { Button } from '@/components/ui/button' import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, } from '@/components/ui/form' import { Input } from '@/components/ui/input' import { Separator } from '@/components/ui/separator' import { Switch } from '@/components/ui/switch' import { Textarea } from '@/components/ui/textarea' import { RiskAcknowledgementDialog } from '@/components/risk-acknowledgement-dialog' import { confirmPaymentCompliance } from '../api' import { SettingsSection } from '../components/settings-section' import { useUpdateOption } from '../hooks/use-update-option' import { AmountDiscountVisualEditor } from './amount-discount-visual-editor' import { AmountOptionsVisualEditor } from './amount-options-visual-editor' import { CreemProductsVisualEditor } from './creem-products-visual-editor' import { PaymentMethodsVisualEditor } from './payment-methods-visual-editor' import { formatJsonForEditor, getJsonError, normalizeJsonForComparison, removeTrailingSlash, } from './utils' import { WaffoPancakeSettingsSection, type WaffoPancakeSettingsValues, } from './waffo-pancake-settings-section' import { WaffoSettingsSection, type WaffoSettingsValues, } from './waffo-settings-section' const paymentSchema = z.object({ PayAddress: z.string().refine((value) => { const trimmed = value.trim() if (!trimmed) return true return /^https?:\/\//.test(trimmed) }, 'Provide a valid callback URL starting with http:// or https://'), EpayId: z.string(), EpayKey: z.string(), Price: z.coerce.number().min(0), MinTopUp: z.coerce.number().min(0), CustomCallbackAddress: z.string().refine((value) => { const trimmed = value.trim() if (!trimmed) return true return /^https?:\/\//.test(trimmed) }, 'Provide a valid URL starting with http:// or https://'), PayMethods: z.string().superRefine((value, ctx) => { const error = getJsonError(value) if (error) { ctx.addIssue({ code: z.ZodIssueCode.custom, message: error, }) } }), AmountOptions: z.string().superRefine((value, ctx) => { const error = getJsonError(value, (parsed) => Array.isArray(parsed)) if (error) { ctx.addIssue({ code: z.ZodIssueCode.custom, message: error, }) } }), AmountDiscount: z.string().superRefine((value, ctx) => { const error = getJsonError( value, (parsed) => !!parsed && typeof parsed === 'object' && !Array.isArray(parsed) ) if (error) { ctx.addIssue({ code: z.ZodIssueCode.custom, message: error, }) } }), StripeApiSecret: z.string(), StripeWebhookSecret: z.string(), StripePriceId: z.string(), StripeUnitPrice: z.coerce.number().min(0), StripeMinTopUp: z.coerce.number().min(0), StripePromotionCodesEnabled: z.boolean(), CreemApiKey: z.string(), CreemWebhookSecret: z.string(), CreemTestMode: z.boolean(), CreemProducts: z.string().superRefine((value, ctx) => { const error = getJsonError(value, (parsed) => Array.isArray(parsed)) if (error) { ctx.addIssue({ code: z.ZodIssueCode.custom, message: error, }) } }), }) type PaymentFormValues = z.infer const CURRENT_COMPLIANCE_TERMS_VERSION = 'v1' type PaymentComplianceDefaults = { confirmed: boolean termsVersion: string confirmedAt: number confirmedBy: number } type PaymentSettingsSectionProps = { defaultValues: PaymentFormValues waffoDefaultValues: WaffoSettingsValues waffoPancakeDefaultValues: WaffoPancakeSettingsValues waffoPancakeProvisionedStoreID?: string waffoPancakeProvisionedProductID?: string complianceDefaults: PaymentComplianceDefaults } export function PaymentSettingsSection({ defaultValues, waffoDefaultValues, waffoPancakeDefaultValues, waffoPancakeProvisionedStoreID, waffoPancakeProvisionedProductID, complianceDefaults, }: PaymentSettingsSectionProps) { const { t } = useTranslation() const queryClient = useQueryClient() const updateOption = useUpdateOption() const initialRef = React.useRef(defaultValues) const defaultsSignature = React.useMemo( () => JSON.stringify(defaultValues), [defaultValues] ) const [payMethodsVisualMode, setPayMethodsVisualMode] = React.useState(true) const [amountOptionsVisualMode, setAmountOptionsVisualMode] = React.useState(true) const [amountDiscountVisualMode, setAmountDiscountVisualMode] = React.useState(true) const [creemProductsVisualMode, setCreemProductsVisualMode] = React.useState(true) const [showComplianceDialog, setShowComplianceDialog] = React.useState(false) const complianceStatements = React.useMemo( () => [ t( 'You have legally obtained authorization for the connected model APIs, accounts, keys, and quotas.' ), t( 'You commit to using upstream APIs, accounts, keys, quotas, and service capabilities only within the scope of lawful authorization obtained from upstream service providers, model service providers, or relevant rights holders, and will not conduct unauthorized resale, trafficking, distribution, or other non-compliant commercialization.' ), t( 'If you provide generative AI services to the public in mainland China, you will fulfill legal obligations including filing, security assessment, content safety, complaint handling, generated content labeling, log retention, and personal information protection.' ), t( 'You commit not to use this system to implement, assist with, or indirectly implement acts that violate applicable laws and regulations, regulatory requirements, platform rules, public interests, or the lawful rights and interests of third parties.' ), t( 'You understand and independently bear legal responsibility arising from deployment, operation, and charging behavior.' ), t( 'You understand this compliance reminder is only for risk notice and does not constitute legal advice, a compliance review conclusion, or a guarantee of the legality of your use of this system; you should consult professional legal or compliance advisors based on your actual business scenario.' ), ], [t] ) const complianceRequiredText = t( 'I have read and understood the above compliance reminder, acknowledge the related legal risks, and confirm that I bear legal responsibility arising from deployment, operation, and charging behavior.' ) const complianceRequiredTextParts = React.useMemo( () => [ { type: 'input' as const, text: t('I have read and understood the above compliance reminder'), }, { type: 'static' as const, text: t(',') }, { type: 'input' as const, text: t('acknowledge the related legal risks'), }, { type: 'static' as const, text: t(',and ') }, { type: 'input' as const, text: t( 'confirm that I bear legal responsibility arising from deployment' ), }, { type: 'static' as const, text: t('、') }, { type: 'input' as const, text: t('operation and charging behavior'), }, ], [t] ) const complianceConfirmed = complianceDefaults.confirmed && complianceDefaults.termsVersion === CURRENT_COMPLIANCE_TERMS_VERSION const confirmComplianceMutation = useMutation({ mutationFn: confirmPaymentCompliance, onSuccess: (data) => { if (data.success) { toast.success(t('Compliance confirmed successfully')) setShowComplianceDialog(false) queryClient.invalidateQueries({ queryKey: ['system-options'] }) } else { toast.error(data.message || t('Failed to confirm compliance')) } }, onError: (error: Error) => { toast.error(error.message || t('Failed to confirm compliance')) }, }) const form = useForm({ resolver: zodResolver(paymentSchema), mode: 'onChange', // Enable real-time validation defaultValues: { ...defaultValues, PayMethods: formatJsonForEditor(defaultValues.PayMethods), AmountOptions: formatJsonForEditor(defaultValues.AmountOptions), AmountDiscount: formatJsonForEditor(defaultValues.AmountDiscount), CreemProducts: formatJsonForEditor(defaultValues.CreemProducts), }, }) React.useEffect(() => { const parsedDefaults = JSON.parse(defaultsSignature) as PaymentFormValues initialRef.current = parsedDefaults form.reset({ ...parsedDefaults, PayMethods: formatJsonForEditor(parsedDefaults.PayMethods), AmountOptions: formatJsonForEditor(parsedDefaults.AmountOptions), AmountDiscount: formatJsonForEditor(parsedDefaults.AmountDiscount), CreemProducts: formatJsonForEditor(parsedDefaults.CreemProducts), }) }, [defaultsSignature, form]) const saveGeneralSettings = async () => { const values = form.getValues() const sanitized = { Price: values.Price as number, MinTopUp: values.MinTopUp as number, PayMethods: values.PayMethods.trim(), AmountOptions: values.AmountOptions.trim(), AmountDiscount: values.AmountDiscount.trim(), } const initial = { Price: initialRef.current.Price, MinTopUp: initialRef.current.MinTopUp, PayMethods: initialRef.current.PayMethods.trim(), AmountOptions: initialRef.current.AmountOptions.trim(), AmountDiscount: initialRef.current.AmountDiscount.trim(), } const updates: Array<{ key: string; value: string | number }> = [] if (sanitized.Price !== initial.Price) { updates.push({ key: 'Price', value: sanitized.Price }) } if (sanitized.MinTopUp !== initial.MinTopUp) { updates.push({ key: 'MinTopUp', value: sanitized.MinTopUp }) } if ( normalizeJsonForComparison(sanitized.PayMethods) !== normalizeJsonForComparison(initial.PayMethods) ) { updates.push({ key: 'PayMethods', value: sanitized.PayMethods }) } if ( normalizeJsonForComparison(sanitized.AmountOptions) !== normalizeJsonForComparison(initial.AmountOptions) ) { updates.push({ key: 'payment_setting.amount_options', value: sanitized.AmountOptions, }) } if ( normalizeJsonForComparison(sanitized.AmountDiscount) !== normalizeJsonForComparison(initial.AmountDiscount) ) { updates.push({ key: 'payment_setting.amount_discount', value: sanitized.AmountDiscount, }) } if (updates.length === 0) { return } for (const update of updates) { await updateOption.mutateAsync(update) } } const saveEpaySettings = async () => { const values = form.getValues() const sanitized = { PayAddress: removeTrailingSlash(values.PayAddress), EpayId: values.EpayId.trim(), EpayKey: values.EpayKey.trim(), CustomCallbackAddress: removeTrailingSlash(values.CustomCallbackAddress), } const initial = { PayAddress: removeTrailingSlash(initialRef.current.PayAddress), EpayId: initialRef.current.EpayId.trim(), EpayKey: initialRef.current.EpayKey.trim(), CustomCallbackAddress: removeTrailingSlash( initialRef.current.CustomCallbackAddress ), } const updates: Array<{ key: string; value: string }> = [] if (sanitized.PayAddress !== initial.PayAddress) { updates.push({ key: 'PayAddress', value: sanitized.PayAddress }) } if (sanitized.EpayId !== initial.EpayId) { updates.push({ key: 'EpayId', value: sanitized.EpayId }) } if (sanitized.EpayKey && sanitized.EpayKey !== initial.EpayKey) { updates.push({ key: 'EpayKey', value: sanitized.EpayKey }) } if (sanitized.CustomCallbackAddress !== initial.CustomCallbackAddress) { updates.push({ key: 'CustomCallbackAddress', value: sanitized.CustomCallbackAddress, }) } if (updates.length === 0) { return } for (const update of updates) { await updateOption.mutateAsync(update) } } const saveStripeSettings = async () => { const values = form.getValues() const sanitized = { StripeApiSecret: values.StripeApiSecret.trim(), StripeWebhookSecret: values.StripeWebhookSecret.trim(), StripePriceId: values.StripePriceId.trim(), StripeUnitPrice: values.StripeUnitPrice as number, StripeMinTopUp: values.StripeMinTopUp as number, StripePromotionCodesEnabled: values.StripePromotionCodesEnabled as boolean, } const initial = { StripeApiSecret: initialRef.current.StripeApiSecret.trim(), StripeWebhookSecret: initialRef.current.StripeWebhookSecret.trim(), StripePriceId: initialRef.current.StripePriceId.trim(), StripeUnitPrice: initialRef.current.StripeUnitPrice, StripeMinTopUp: initialRef.current.StripeMinTopUp, StripePromotionCodesEnabled: initialRef.current.StripePromotionCodesEnabled, } const updates: Array<{ key: string; value: string | number | boolean }> = [] if ( sanitized.StripeApiSecret && sanitized.StripeApiSecret !== initial.StripeApiSecret ) { updates.push({ key: 'StripeApiSecret', value: sanitized.StripeApiSecret }) } if ( sanitized.StripeWebhookSecret && sanitized.StripeWebhookSecret !== initial.StripeWebhookSecret ) { updates.push({ key: 'StripeWebhookSecret', value: sanitized.StripeWebhookSecret, }) } if (sanitized.StripePriceId !== initial.StripePriceId) { updates.push({ key: 'StripePriceId', value: sanitized.StripePriceId }) } if (sanitized.StripeUnitPrice !== initial.StripeUnitPrice) { updates.push({ key: 'StripeUnitPrice', value: sanitized.StripeUnitPrice }) } if (sanitized.StripeMinTopUp !== initial.StripeMinTopUp) { updates.push({ key: 'StripeMinTopUp', value: sanitized.StripeMinTopUp }) } if ( sanitized.StripePromotionCodesEnabled !== initial.StripePromotionCodesEnabled ) { updates.push({ key: 'StripePromotionCodesEnabled', value: sanitized.StripePromotionCodesEnabled, }) } if (updates.length === 0) { return } for (const update of updates) { await updateOption.mutateAsync(update) } } const saveCreemSettings = async () => { const values = form.getValues() const sanitized = { CreemApiKey: values.CreemApiKey.trim(), CreemWebhookSecret: values.CreemWebhookSecret.trim(), CreemTestMode: values.CreemTestMode as boolean, CreemProducts: values.CreemProducts.trim(), } const initial = { CreemApiKey: initialRef.current.CreemApiKey.trim(), CreemWebhookSecret: initialRef.current.CreemWebhookSecret.trim(), CreemTestMode: initialRef.current.CreemTestMode, CreemProducts: initialRef.current.CreemProducts.trim(), } const updates: Array<{ key: string; value: string | boolean }> = [] if ( sanitized.CreemApiKey && sanitized.CreemApiKey !== initial.CreemApiKey ) { updates.push({ key: 'CreemApiKey', value: sanitized.CreemApiKey }) } if ( sanitized.CreemWebhookSecret && sanitized.CreemWebhookSecret !== initial.CreemWebhookSecret ) { updates.push({ key: 'CreemWebhookSecret', value: sanitized.CreemWebhookSecret, }) } if (sanitized.CreemTestMode !== initial.CreemTestMode) { updates.push({ key: 'CreemTestMode', value: sanitized.CreemTestMode }) } if ( normalizeJsonForComparison(sanitized.CreemProducts) !== normalizeJsonForComparison(initial.CreemProducts) ) { updates.push({ key: 'CreemProducts', value: sanitized.CreemProducts }) } if (updates.length === 0) { return } for (const update of updates) { await updateOption.mutateAsync(update) } } const onSubmit = async (values: PaymentFormValues) => { const sanitized = { PayAddress: removeTrailingSlash(values.PayAddress), EpayId: values.EpayId.trim(), EpayKey: values.EpayKey.trim(), Price: values.Price, MinTopUp: values.MinTopUp, CustomCallbackAddress: removeTrailingSlash(values.CustomCallbackAddress), PayMethods: values.PayMethods.trim(), AmountOptions: values.AmountOptions.trim(), AmountDiscount: values.AmountDiscount.trim(), StripeApiSecret: values.StripeApiSecret.trim(), StripeWebhookSecret: values.StripeWebhookSecret.trim(), StripePriceId: values.StripePriceId.trim(), StripeUnitPrice: values.StripeUnitPrice, StripeMinTopUp: values.StripeMinTopUp, StripePromotionCodesEnabled: values.StripePromotionCodesEnabled, } const initial = { PayAddress: removeTrailingSlash(initialRef.current.PayAddress), EpayId: initialRef.current.EpayId.trim(), EpayKey: initialRef.current.EpayKey.trim(), Price: initialRef.current.Price, MinTopUp: initialRef.current.MinTopUp, CustomCallbackAddress: removeTrailingSlash( initialRef.current.CustomCallbackAddress ), PayMethods: initialRef.current.PayMethods.trim(), AmountOptions: initialRef.current.AmountOptions.trim(), AmountDiscount: initialRef.current.AmountDiscount.trim(), StripeApiSecret: initialRef.current.StripeApiSecret.trim(), StripeWebhookSecret: initialRef.current.StripeWebhookSecret.trim(), StripePriceId: initialRef.current.StripePriceId.trim(), StripeUnitPrice: initialRef.current.StripeUnitPrice, StripeMinTopUp: initialRef.current.StripeMinTopUp, StripePromotionCodesEnabled: initialRef.current.StripePromotionCodesEnabled, } const updates: Array<{ key: string; value: string | number | boolean }> = [] if (sanitized.PayAddress !== initial.PayAddress) { updates.push({ key: 'PayAddress', value: sanitized.PayAddress }) } if (sanitized.EpayId !== initial.EpayId) { updates.push({ key: 'EpayId', value: sanitized.EpayId }) } if (sanitized.EpayKey && sanitized.EpayKey !== initial.EpayKey) { updates.push({ key: 'EpayKey', value: sanitized.EpayKey }) } if (sanitized.Price !== initial.Price) { updates.push({ key: 'Price', value: sanitized.Price }) } if (sanitized.MinTopUp !== initial.MinTopUp) { updates.push({ key: 'MinTopUp', value: sanitized.MinTopUp }) } if (sanitized.CustomCallbackAddress !== initial.CustomCallbackAddress) { updates.push({ key: 'CustomCallbackAddress', value: sanitized.CustomCallbackAddress, }) } if ( normalizeJsonForComparison(sanitized.PayMethods) !== normalizeJsonForComparison(initial.PayMethods) ) { updates.push({ key: 'PayMethods', value: sanitized.PayMethods }) } if ( normalizeJsonForComparison(sanitized.AmountOptions) !== normalizeJsonForComparison(initial.AmountOptions) ) { updates.push({ key: 'payment_setting.amount_options', value: sanitized.AmountOptions, }) } if ( normalizeJsonForComparison(sanitized.AmountDiscount) !== normalizeJsonForComparison(initial.AmountDiscount) ) { updates.push({ key: 'payment_setting.amount_discount', value: sanitized.AmountDiscount, }) } if ( sanitized.StripeApiSecret && sanitized.StripeApiSecret !== initial.StripeApiSecret ) { updates.push({ key: 'StripeApiSecret', value: sanitized.StripeApiSecret }) } if ( sanitized.StripeWebhookSecret && sanitized.StripeWebhookSecret !== initial.StripeWebhookSecret ) { updates.push({ key: 'StripeWebhookSecret', value: sanitized.StripeWebhookSecret, }) } if (sanitized.StripePriceId !== initial.StripePriceId) { updates.push({ key: 'StripePriceId', value: sanitized.StripePriceId }) } if (sanitized.StripeUnitPrice !== initial.StripeUnitPrice) { updates.push({ key: 'StripeUnitPrice', value: sanitized.StripeUnitPrice }) } if (sanitized.StripeMinTopUp !== initial.StripeMinTopUp) { updates.push({ key: 'StripeMinTopUp', value: sanitized.StripeMinTopUp }) } if ( sanitized.StripePromotionCodesEnabled !== initial.StripePromotionCodesEnabled ) { updates.push({ key: 'StripePromotionCodesEnabled', value: sanitized.StripePromotionCodesEnabled, }) } for (const update of updates) { await updateOption.mutateAsync(update) } } return ( {!complianceConfirmed ? ( {t('Compliance confirmation required')}

{t( 'Payment, redemption codes, subscription plans, and invitation rewards are locked until the root administrator confirms the compliance terms.' )}

    {complianceStatements.map((statement) => (
  1. {statement}
  2. ))}
) : ( {t('Compliance confirmed')} {t('Confirmed at {{time}} by user #{{userId}}', { time: complianceDefaults.confirmedAt ? new Date( complianceDefaults.confirmedAt * 1000 ).toLocaleString() : '-', userId: complianceDefaults.confirmedBy || '-', })} )} confirmComplianceMutation.mutate()} /> {/* eslint-disable react-hooks/refs */}

{t('General Settings')}

{t('Shared configuration for all payment gateways')}

( {t('Price (local currency / USD)')} field.onChange(event.target.valueAsNumber) } /> {t( 'How much to charge for each US dollar of balance (Epay)' )} )} /> ( {t('Minimum top-up (USD)')} field.onChange(event.target.valueAsNumber) } /> {t('Smallest USD amount users can recharge (Epay)')} )} />
(
{t('Payment methods')}
{payMethodsVisualMode ? ( ) : (