import React, { useState, useMemo, useEffect } from "react"; import { useSubscription } from "../../hooks/useAuth"; import { useParams } from "react-router-dom"; import { useCreateTenant, useIndustries } from "../../hooks/useTenant"; import { formatCurrency, formatFigure, frequencyLabel, } from "../../utils/appUtils"; import { formatUTCToLocalTime } from "../../utils/dateUtils"; import { PaymentRepository } from "../../repositories/PaymentRepository"; import { useMakePayment } from "../../hooks/usePayment"; import { useDispatch, useSelector } from "react-redux"; import { setSelfTenant } from "../../slices/localVariablesSlice"; import { unblockUI } from "../../utils/blockUI"; import showToast from "../../services/toastService"; import SelectedPlanSkeleton from "./SelectedPlanSkeleton"; const ProcessedPayment = ({ onNext, resetPaymentStep, setCurrentStep, setStepStatus, resetFormStep, }) => { const { planName } = useParams(); const { details: client, planId: selectedPlanId, frequency, } = useSelector((store) => store.localVariables.selfTenant); const [selectedPlan, setSelectedPlan] = useState(null); const [currentPlan, setCurrentPlan] = useState(null); const [failPayment, setFailPayment] = useState(null); const { data: plans, isError: isPlanError, isLoading, isError, isRefetching, refetch, } = useSubscription(frequency); useEffect(() => { if (!plans || !selectedPlanId) return; const selected = plans.find((p) => p.id === selectedPlanId); setSelectedPlan(selected); }, [plans, selectedPlanId]); const loadScript = (src) => new Promise((resolve) => { const script = document.createElement("script"); script.src = src; script.onload = () => resolve(true); script.onerror = () => resolve(false); document.body.appendChild(script); }); const { mutate: MakePayment, isPending } = useMakePayment( (response) => { unblockUI(); onNext(response); }, (fail) => { unblockUI(); setFailPayment(fail); onNext(fail); }, currentPlan ); const ProcessToPayment = async () => { setStepStatus((prev) => ({ ...prev, 3: "success" })); setCurrentStep(4); const res = await loadScript( "https://checkout.razorpay.com/v1/checkout.js" ); if (!res) { alert("Failed to load Razorpay SDK"); return; } let price = 0; price = frequencyLabel(selectedPlan?.frequency, true, true)?.planDurationInInt * selectedPlan?.price; MakePayment({ amount: price }); }; const handleRetry = () => { setFailPayment(null); if (typeof resetPaymentStep === "function") resetPaymentStep(); }; const handlPrevious = () => { setCurrentStep, setStepStatus((prev) => ({ ...prev, 2: "pending", 3: "pending" })); setCurrentStep(2); }; // useEffect(() => { // if (!client || Object.keys(client).length === 0) { // setFailPayment(null); // if (typeof resetFormStep === "function") { // resetFormStep(); // } // } // }, [client]); if (failPayment) { return (

Payment Failed!

Unfortunately, your payment could not be completed. Please try again or use a different payment method.

Go Back to Dashboard
{failPayment?.error && (
Error Details:
                {JSON.stringify(failPayment.error, null, 2)}
              
)}
); } return (

You’ve Selected the Perfect Plan for Your Organization

Great choice! This plan is tailored to meet your team’s needs and help you maximize productivity.

{isError && (

{error?.message}

{error?.name} {isRefetching ? ( <> {" "} Retrying... ) : ( "Try to refetch" )}
)} {isLoading ? ( ) : ( <> {selectedPlan && (
{selectedPlan?.description}
Price -{" "} {selectedPlan.currency?.symbol} {selectedPlan.price}{" "} per {frequencyLabel(frequency)}
{selectedPlan?.planName} billed {frequencyLabel(frequency, true)}
)} {selectedPlan && (
{(() => { const { planName, description, price, frequency, trialDays, maxUser, maxStorage, currency, features, } = selectedPlan; return ( <>
Max Users: {maxUser}
Max Storage: {maxStorage} MB
Trial Days: {trialDays}
Included Features
{features && Object.entries(features?.modules || {}) .filter(([key]) => key !== "id") .map(([key, mod]) => (
{mod.name}
))}
Support
    {features?.supports?.emailSupport && (
  • Email Support
  • )} {features?.supports?.phoneSupport && (
  • Phone Support
  • )} {features?.supports?.prioritySupport && (
  • Priority Support
  • )}

Duration
{frequencyLabel( selectedPlan?.frequency, true )}
Total Price
{formatFigure( frequencyLabel( selectedPlan?.frequency, true, true )?.planDurationInInt * price, { type: "currency", currency: selectedPlan?.currency.currencyCode, } )}
); })()}
)} )}
{client && (
Confirm your organization details.
Name:
{client.firstName} {client.lastName}
Email:
{client.email}
Contact Number:
{client.contactNumber}
Organization Name:
{client.organizationName}
Onboarding Date:
{formatUTCToLocalTime(client.onBoardingDate)}
Billing Address:
{client.billingAddress}
Industry :
{client?.industry?.name}
)}
); }; export default ProcessedPayment;