import {
Box,
Collapse,
Flex,
Heading,
HStack,
Icon,
Input,
Popover,
PopoverArrow,
PopoverBody,
PopoverContent,
PopoverTrigger,
Select,
Skeleton,
Switch,
Text,
Textarea,
useColorModeValue,
VStack
} from "@chakra-ui/react";
import { __ } from "@wordpress/i18n";
import React, { useEffect, useState } from "react";
import {
apiGet,
CurrencyData,
PaymentSettingsResponse
} from "../../api/gettingStartedApi";
import { PaymentSettings } from "../../context/Gettingstartedcontext";
import { useStateValue } from "../../context/StateProvider";
interface PaymentGatewayData {
id: string;
label: string;
description: string;
enabled: boolean;
configured: boolean;
settings_url: string;
paypal_mode?: string;
paypal_test_email?: string;
paypal_test_client_id?: string;
paypal_test_client_secret?: string;
paypal_production_email?: string;
paypal_production_client_id?: string;
paypal_production_client_secret?: string;
is_new_installation?: boolean;
bank_details?: string;
stripe_test_mode?: boolean;
stripe_test_publishable_key?: string;
stripe_test_secret_key?: string;
stripe_live_publishable_key?: string;
stripe_live_secret_key?: string;
}
const InfoIcon: React.FC = () => (
);
interface CustomTooltipProps {
label: string;
children?: React.ReactNode;
}
const CustomTooltip: React.FC = ({ label }) => {
return (
{label}
);
};
// Consistent width for all labels
const LABEL_WIDTH = "200px";
// Nested labels are smaller to compensate for border offset (ml={1} + pl={4} = ~20px)
const NESTED_LABEL_WIDTH = "180px";
const CONTENT_FLEX = "1";
interface PaymentOptionProps {
label: string;
isChecked: boolean;
onChange: (checked: boolean) => void;
children?: React.ReactNode;
}
const PaymentOption: React.FC = ({
label,
isChecked,
onChange,
children
}) => {
const textColor = useColorModeValue("gray.800", "white");
return (
{label}
onChange(e.target.checked)}
colorScheme="blue"
size="sm"
sx={{
"& .chakra-switch__track[data-checked]": {
bg: "#475BB2"
}
}}
/>
{children}
);
};
interface FieldRowProps {
label: string;
tooltip?: string;
children: React.ReactNode;
alignItems?: string;
isNested?: boolean;
isRequired?: boolean;
}
const FieldRow: React.FC = ({
label,
tooltip,
children,
alignItems = "center",
isNested = true,
isRequired = false
}) => {
const mutedColor = useColorModeValue("#383838", "gray.300");
const labelW = isNested ? NESTED_LABEL_WIDTH : LABEL_WIDTH;
return (
{label}
{isRequired && (
*
)}
{tooltip && }
{children}
);
};
const PaymentStep: React.FC = () => {
const { state, dispatch } = useStateValue();
const { paymentSettings } = state;
const [isLoadingSettings, setIsLoadingSettings] = useState(false);
const [currencies, setCurrencies] = useState([]);
const [isNewInstallation, setIsNewInstallation] = useState(true);
const textColor = useColorModeValue("gray.800", "white");
const subtextColor = useColorModeValue("gray.600", "gray.300");
const inputBg = useColorModeValue("white", "gray.700");
const inputBorder = useColorModeValue("gray.300", "gray.600");
useEffect(() => {
const loadPaymentSettings = async () => {
try {
setIsLoadingSettings(true);
const response =
await apiGet("/payments");
if (response.currencies) {
setCurrencies(response.currencies);
}
if (response.currency) {
dispatch({
type: "SET_PAYMENT_SETTING",
payload: {
key: "currency",
value: response.currency
}
});
}
if (response.payment_gateways) {
const gateways =
response.payment_gateways as PaymentGatewayData[];
gateways.forEach((gateway) => {
if (gateway.id === "offline_payment") {
dispatch({
type: "SET_PAYMENT_SETTING",
payload: {
key: "offlinePayment",
value: gateway.enabled
}
});
if (gateway.bank_details) {
dispatch({
type: "SET_PAYMENT_SETTING",
payload: {
key: "bankDetails",
value: gateway.bank_details
}
});
}
} else if (gateway.id === "paypal") {
dispatch({
type: "SET_PAYMENT_SETTING",
payload: {
key: "paypal",
value: gateway.enabled
}
});
if (gateway.paypal_mode) {
dispatch({
type: "SET_PAYMENT_SETTING",
payload: {
key: "paypalMode",
value: gateway.paypal_mode
}
});
}
if (gateway.paypal_test_email) {
dispatch({
type: "SET_PAYMENT_SETTING",
payload: {
key: "paypalTestEmail",
value: gateway.paypal_test_email
}
});
}
if (gateway.paypal_test_client_id) {
dispatch({
type: "SET_PAYMENT_SETTING",
payload: {
key: "paypalTestClientId",
value: gateway.paypal_test_client_id
}
});
}
if (gateway.paypal_test_client_secret) {
dispatch({
type: "SET_PAYMENT_SETTING",
payload: {
key: "paypalTestClientSecret",
value: gateway.paypal_test_client_secret
}
});
}
if (gateway.paypal_production_email) {
dispatch({
type: "SET_PAYMENT_SETTING",
payload: {
key: "paypalProductionEmail",
value: gateway.paypal_production_email
}
});
}
if (gateway.paypal_production_client_id) {
dispatch({
type: "SET_PAYMENT_SETTING",
payload: {
key: "paypalProductionClientId",
value: gateway.paypal_production_client_id
}
});
}
if (gateway.paypal_production_client_secret) {
dispatch({
type: "SET_PAYMENT_SETTING",
payload: {
key: "paypalProductionClientSecret",
value: gateway.paypal_production_client_secret
}
});
}
if (typeof gateway.is_new_installation === "boolean") {
setIsNewInstallation(gateway.is_new_installation);
}
} else if (gateway.id === "stripe") {
dispatch({
type: "SET_PAYMENT_SETTING",
payload: {
key: "stripe",
value: gateway.enabled
}
});
if (typeof gateway.stripe_test_mode === "boolean") {
dispatch({
type: "SET_PAYMENT_SETTING",
payload: {
key: "stripeTestMode",
value: gateway.stripe_test_mode
}
});
}
if (gateway.stripe_test_publishable_key) {
dispatch({
type: "SET_PAYMENT_SETTING",
payload: {
key: "stripeTestPublishableKey",
value: gateway.stripe_test_publishable_key
}
});
}
if (gateway.stripe_test_secret_key) {
dispatch({
type: "SET_PAYMENT_SETTING",
payload: {
key: "stripeTestSecretKey",
value: gateway.stripe_test_secret_key
}
});
}
if (gateway.stripe_live_publishable_key) {
dispatch({
type: "SET_PAYMENT_SETTING",
payload: {
key: "stripeLivePublishableKey",
value: gateway.stripe_live_publishable_key
}
});
}
if (gateway.stripe_live_secret_key) {
dispatch({
type: "SET_PAYMENT_SETTING",
payload: {
key: "stripeLiveSecretKey",
value: gateway.stripe_live_secret_key
}
});
}
}
});
}
} catch (e) {
console.error("Failed to load payment settings:", e);
} finally {
setIsLoadingSettings(false);
}
};
loadPaymentSettings();
}, [dispatch]);
const handlePaymentSettingChange = (
key: keyof PaymentSettings,
value: boolean | string
) => {
dispatch({
type: "SET_PAYMENT_SETTING",
payload: { key, value }
});
};
const inputStyles = {
bg: inputBg,
borderColor: inputBorder,
fontSize: "sm",
borderRadius: "4px",
_focus: {
borderColor: "#475BB2",
boxShadow: "0 0 0 1px #475BB2"
}
};
return (
<>
{__("Payments", "user-registration")}
{__(
"Set up payment options for your membership plans. Choose currency and enable payment gateways. You can edit this anytime.",
"user-registration"
)}
{/* Currency Row */}
{__("Currency", "user-registration")}
{isLoadingSettings ? (
) : (
)}
{/* Offline Payment */}
handlePaymentSettingChange("offlinePayment", checked)
}
>
{/* PayPal */}
handlePaymentSettingChange("paypal", checked)
}
>
{paymentSettings.paypalMode === "test" ? (
<>
{!isNewInstallation && paymentSettings.paypalTestEmail && (
handlePaymentSettingChange(
"paypalTestEmail",
e.target.value
)
}
{...inputStyles}
/>
)}
handlePaymentSettingChange(
"paypalTestClientId",
e.target.value
)
}
{...inputStyles}
/>
handlePaymentSettingChange(
"paypalTestClientSecret",
e.target.value
)
}
{...inputStyles}
/>
>
) : (
<>
{!isNewInstallation && paymentSettings.paypalProductionEmail && (
handlePaymentSettingChange(
"paypalProductionEmail",
e.target.value
)
}
{...inputStyles}
/>
)}
handlePaymentSettingChange(
"paypalProductionClientId",
e.target.value
)
}
{...inputStyles}
/>
handlePaymentSettingChange(
"paypalProductionClientSecret",
e.target.value
)
}
{...inputStyles}
/>
>
)}
{/* Stripe */}
handlePaymentSettingChange("stripe", checked)
}
>
handlePaymentSettingChange(
"stripeTestMode",
e.target.checked
)
}
colorScheme="blue"
size="sm"
sx={{
"& .chakra-switch__track[data-checked]": {
bg: "#475BB2"
}
}}
/>
{paymentSettings.stripeTestMode ? (
<>
handlePaymentSettingChange(
"stripeTestPublishableKey",
e.target.value
)
}
{...inputStyles}
/>
handlePaymentSettingChange(
"stripeTestSecretKey",
e.target.value
)
}
{...inputStyles}
/>
>
) : (
<>
handlePaymentSettingChange(
"stripeLivePublishableKey",
e.target.value
)
}
{...inputStyles}
/>
handlePaymentSettingChange(
"stripeLiveSecretKey",
e.target.value
)
}
{...inputStyles}
/>
>
)}
>
);
};
export default PaymentStep;