import React from "react"; import { useNavigate } from "react-router-dom"; import { useTranslations } from "@/hooks/translations"; import { Elements } from "@stripe/react-stripe-js"; import { Stripe, loadStripe } from "@stripe/stripe-js"; import "./discount-screen.css"; import routes from "@/routes"; import { useApi } from "@/api"; import { useAuth } from "@/auth"; import HeaderLogo from "@/components/palmistry/header-logo/header-logo"; import CheckoutForm from "@/components/PaymentPage/methods/CheckoutForm"; import { ResponseGet } from "@/api/resources/SinglePayment"; import Loader, { LoaderColor } from "@/components/Loader"; const currentProductKey = "skip.trial.subscription.aura"; const returnUrl = `${window.location.host}/palmistry/premium-bundle`; export default function DiscountScreen() { const navigate = useNavigate(); const api = useApi(); const { token, user } = useAuth(); const { i18n } = useTranslations(); const locale = i18n.language; const [price, setPrice] = React.useState(""); const [isSuccess] = React.useState(false); const [stripePromise, setStripePromise] = React.useState | null>(null); const [product, setProduct] = React.useState(); const [clientSecret, setClientSecret] = React.useState(null); const [stripePublicKey, setStripePublicKey] = React.useState(""); const [isLoading, setIsLoading] = React.useState(false); const goPremiumBundle = () => { navigate(routes.client.palmistryPremiumBundle()); }; React.useEffect(() => { (async () => { const { sub_plans } = await api.getSubscriptionPlans({ locale }); const plan = sub_plans.find((plan) => plan.id === "stripe.40"); if (!plan?.price_cents) return; setPrice((plan?.price_cents / 100).toFixed(2)); })(); // eslint-disable-next-line react-hooks/exhaustive-deps }, []); React.useEffect(() => { (async () => { const products = await api.getSinglePaymentProducts({ token }); const product = products.find( (product) => product.key === currentProductKey ); if (product) { setProduct(product); } })(); // eslint-disable-next-line react-hooks/exhaustive-deps }, []); React.useEffect(() => { if (!stripePublicKey) return; setStripePromise(loadStripe(stripePublicKey)); }, [stripePublicKey]); const buy = async () => { if (!user?.id || !product) return; const { _id, key } = product; const paymentInfo = { productId: _id, key, }; try { setIsLoading(true); const response = await api.createSinglePayment({ token: token, data: { user: { id: user.id, email: user.email, name: user.username || "", sign: user.profile?.sign?.sign || "", age: user.profile.age?.years || 0, }, partner: { sign: "", age: 0, }, paymentInfo, return_url: returnUrl, }, }); if ( ("paymentIntent" in response && response.paymentIntent.status === "paid") || ("payment" in response && response.payment.status === "paid") || ("invoiceId" in response && response.invoiceId === "paid") ) { goPremiumBundle(); } else if ("paymentIntent" in response) { setClientSecret(response.paymentIntent.data.client_secret); setStripePublicKey(response.paymentIntent.data.public_key); } } catch (error) { console.log("Error: ", error); } finally { setIsLoading(false); } }; return (
Not planning on looking back?
$19 for
1-week plan
Total savings $0
7-day trial yes
save 33%
${price} for
1-week plan
Total savings $6.27
3-day trial no
{stripePromise && clientSecret && (
{isSuccess && (
Payment success
)}
)}
); }