diff --git a/components/Pricing.tsx b/components/Pricing.tsx index 0dd1bee..0cc3930 100644 --- a/components/Pricing.tsx +++ b/components/Pricing.tsx @@ -18,16 +18,6 @@ const Pricing = (): JSX.Element => { const [customerId, setCustomerId] = useState(null); const [sub, setSub] = useState(false); - const portal = () => { - axios - .post('/api/stripe/customer-portal', { - customerId, - }) - .then((result) => { - router.push(result.data.url); - }); - }; - useEffect(() => { if (user) { getSub().then((result) => setSub(result)); @@ -45,16 +35,25 @@ const Pricing = (): JSX.Element => { const handleSubmit = async (e: React.SyntheticEvent, priceId: string) => { e.preventDefault(); // Create a Checkout Session. This will redirect the user to the Stripe website for the payment. - axios - .post('/api/stripe/create-checkout-session', { - priceId, - email: user.email, - customerId, - userId: user.id, - tokenId: session.access_token, - pay_mode: 'subscription', - }) - .then((result) => router.push(result.data.url)); + if (sub) { + axios + .post('/api/stripe/customer-portal', { + customerId, + }) + .then((result) => { + router.push(result.data.url); + }); + } else + axios + .post('/api/stripe/create-checkout-session', { + priceId, + email: user.email, + customerId, + userId: user.id, + tokenId: session.access_token, + pay_mode: 'subscription', + }) + .then((result) => router.push(result.data.url)); }; return (
@@ -104,7 +103,7 @@ const Pricing = (): JSX.Element => { handleSubmit(e, 'price_1JtHhaDMjD0UnVmM5uCyyrWn'); }} > - Subscribe + {sub ? 'Handle subscription' : 'Subscribe'} ) : ( + ) : (