From 6848d16da4ab5fc9050ed0e8fe7dde4fe0ae9388 Mon Sep 17 00:00:00 2001 From: vincanger <70215737+vincanger@users.noreply.github.com> Date: Mon, 15 Apr 2024 17:30:20 +0200 Subject: [PATCH] move customer portal url to .env.client --- app/.env.client.example | 7 ++++-- app/src/client/app/AccountPage.tsx | 10 ++++++-- app/src/client/app/PricingPage.tsx | 23 +++++++++++++++---- app/src/shared/constants.ts | 37 ------------------------------ app/tsconfig.json | 1 + 5 files changed, 33 insertions(+), 45 deletions(-) diff --git a/app/.env.client.example b/app/.env.client.example index 95b14ca..87702c9 100644 --- a/app/.env.client.example +++ b/app/.env.client.example @@ -1,2 +1,5 @@ -# learn more about client side env vars https://wasp-lang.dev/docs/project/env-vars -REACT_APP_SOME_VAR_NAME=foo \ No newline at end of file +# All client-side env vars must start with REACT_APP_ https://wasp-lang.dev/docs/project/env-vars + +# Find your test url at https://dashboard.stripe.com/test/settings/billing/portal +# Remember to replace the test url with the live url before deploying to production +REACT_APP_STRIPE_CUSTOMER_PORTAL= \ No newline at end of file diff --git a/app/src/client/app/AccountPage.tsx b/app/src/client/app/AccountPage.tsx index 8db115b..3cfd18e 100644 --- a/app/src/client/app/AccountPage.tsx +++ b/app/src/client/app/AccountPage.tsx @@ -1,8 +1,8 @@ import { Link } from 'wasp/client/router'; import { type User } from 'wasp/entities'; import { logout } from 'wasp/client/auth'; -import { STRIPE_CUSTOMER_PORTAL_LINK } from '../../shared/constants'; import { TierIds } from '../../shared/constants'; +import { z } from 'zod'; export default function AccountPage({ user }: { user: User }) { return ( @@ -82,7 +82,13 @@ function BuyMoreButton() { function CustomerPortalButton() { const handleClick = () => { - window.open(STRIPE_CUSTOMER_PORTAL_LINK, '_blank'); + try { + const schema = z.string().url(); + const customerPortalUrl = schema.parse(import.meta.env.REACT_APP_STRIPE_CUSTOMER_PORTAL); + window.open(customerPortalUrl, '_blank'); + } catch (err) { + console.error(err) + } }; return ( diff --git a/app/src/client/app/PricingPage.tsx b/app/src/client/app/PricingPage.tsx index bcf651f..a48ae85 100644 --- a/app/src/client/app/PricingPage.tsx +++ b/app/src/client/app/PricingPage.tsx @@ -1,10 +1,11 @@ import { useAuth } from 'wasp/client/auth'; import { stripePayment } from 'wasp/client/operations'; -import { TierIds, STRIPE_CUSTOMER_PORTAL_LINK } from '../../shared/constants'; +import { TierIds } from '../../shared/constants'; import { AiFillCheckCircle } from 'react-icons/ai'; import { useState } from 'react'; import { useHistory } from 'react-router-dom'; import { cn } from '../../shared/utils'; +import { z } from 'zod'; export const tiers = [ { @@ -57,6 +58,20 @@ const PricingPage = () => { } } + const handleCustomerPortalClick = () => { + if (!user) { + history.push('/login'); + return; + } + try { + const schema = z.string().url(); + const customerPortalUrl = schema.parse(import.meta.env.REACT_APP_STRIPE_CUSTOMER_PORTAL); + window.open(customerPortalUrl, '_blank'); + } catch (err) { + console.error(err); + } + }; + return (
@@ -115,8 +130,8 @@ const PricingPage = () => {
{!!user && user.hasPaid ? ( - { )} > Manage Subscription - + ) : (