import Loader from '@/components/Loader'; import styles from './styles.module.scss'; import cn from 'classnames'; import { EPlacementKeys, IPaywallProduct } from '@/api/resources/Paywall'; import { useNavigate } from 'react-router-dom'; import { Dispatch, LegacyRef, SetStateAction, useEffect, useMemo, useRef, useState } from 'react'; import { loadStripe, Stripe } from '@stripe/stripe-js'; import { usePaywall } from '@/hooks/paywall/usePaywall'; import { useMakePayment } from '@/hooks/payment/useMakePayment'; import { getFormattedPrice } from '@/utils/price.utils'; import routes from '@/routes'; import Title from '@/components/Title'; import { Elements } from '@stripe/react-stripe-js'; import ExpressCheckoutStripe from '@/components/PaymentPage/methods/ExpressCheckoutStripe'; import SecurityPayments from '@/components/pages/TrialPayment/components/SecurityPayments'; import PaymentCardModal from '@/components/PaymentModalNew/PaymentCardModal'; import CreditCardIcon from '@/components/PaymentModalNew/PaymentCardModal/CreditCardIcon'; interface IPaymentModalNewProps { returnUrl: string; placementKey: EPlacementKeys; activeProduct: IPaywallProduct; setHeight?: Dispatch>; } export default function PaymentModalNew({ returnUrl, activeProduct, placementKey, setHeight, }: IPaymentModalNewProps) { const navigate = useNavigate(); const ref = useRef(); const [stripePromise, setStripePromise] = useState | null>(null); const {products, placementId, paywallId} = usePaywall({ placementKey, }); const [isOpenCardModal, setIsOpenCardModal] = useState(false); const { paymentIntentId, clientSecret, returnUrl: checkoutUrl, paymentType, publicKey, isLoading: isLoadingPayment, error, } = useMakePayment({ productId: activeProduct?._id || '', placementId, paywallId, returnPaidUrl: returnUrl, }); const [isLoadingExpressCheckout, setIsLoadingExpressCheckout] = useState(true); const isLoading = useMemo(() => { return isLoadingPayment || isLoadingExpressCheckout; }, [isLoadingPayment, isLoadingExpressCheckout]); if (checkoutUrl?.length) { window.location.href = checkoutUrl; } useEffect(() => { (async () => { if (!products?.length || !publicKey) return; setStripePromise(loadStripe(publicKey)); const isActiveProduct = products.find( (product) => product._id === activeProduct?._id, ); if (!activeProduct || !isActiveProduct) { navigate(routes.client.trialChoice()); } })(); }, [activeProduct, navigate, products, publicKey]); const resizeHandler = () => { setHeight?.(ref?.current?.clientHeight || 32); }; if (error?.length) { setTimeout(resizeHandler, 300); return (
} className={styles['payment-modal']}> Something went wrong
); } return (
} className={cn(styles.paymentModalContainer, isLoading && styles.paymentModalContainerLoading)}> {isLoading &&
}
Total due today: ${getFormattedPrice(activeProduct.trialPrice)}
{!isLoadingPayment && <> {!isLoading &&
setIsOpenCardModal(true)}>
Credit / Debit Card
} { setIsLoadingExpressCheckout(isLoading); setTimeout(resizeHandler, 300); } } /> {!isLoading && <>

1123 Rimer Dr Moraga, California 94556

} }
); }