From 35d7a5955cf0a8cabfaf2936f22fbc8d15047e32 Mon Sep 17 00:00:00 2001 From: vincanger <70215737+vincanger@users.noreply.github.com> Date: Wed, 10 Jan 2024 16:54:29 -0500 Subject: [PATCH] update stripe customer portal link validation --- app/main.wasp | 3 ++- app/src/shared/constants.ts | 48 ++++++++++++++++++++++++++++--------- 2 files changed, 39 insertions(+), 12 deletions(-) diff --git a/app/main.wasp b/app/main.wasp index f0ce5b1..b1c7370 100644 --- a/app/main.wasp +++ b/app/main.wasp @@ -85,7 +85,8 @@ app SaaSTemplate { ("openai", "^4.24.1"), ("lucide-react", "0.306.0"), ("prettier", "3.1.1"), - ("prettier-plugin-tailwindcss", "0.5.11") + ("prettier-plugin-tailwindcss", "0.5.11"), + ("zod", "3.22.4") ], } diff --git a/app/src/shared/constants.ts b/app/src/shared/constants.ts index 6d6b0bd..06dd614 100644 --- a/app/src/shared/constants.ts +++ b/app/src/shared/constants.ts @@ -1,3 +1,5 @@ +import { z } from 'zod'; + export enum TierIds { HOBBY = 'hobby-tier', PRO = 'pro-tier', @@ -7,18 +9,42 @@ export enum TierIds { export const DOCS_URL = 'https://docs.opensaas.sh'; export const BLOG_URL = 'https://docs.opensaas.sh/blog'; -//get this link at https://dashboard.stripe.com/test/settings/billing/portal -const isDev = process.env.NODE_ENV !== 'production'; -const customerPortalTestUrl = ''; -const customerPortalProdUrl = undefined; // TODO: add before deploying to production +const isDevEnv = process.env.NODE_ENV !== 'production'; +const customerPortalTestUrl = 'https://cool.com'; // TODO: find your test url at https://dashboard.stripe.com/test/settings/billing/portal +const customerPortalProdUrl = 'https://cool.com'; // TODO: add before deploying to production -export const STRIPE_CUSTOMER_PORTAL_LINK = isDev ? customerPortalTestUrl : customerPortalProdUrl; +export const STRIPE_CUSTOMER_PORTAL_LINK = isDevEnv + ? customerPortalTestUrl + : customerPortalProdUrl; -checkStripePortalLinkExists(STRIPE_CUSTOMER_PORTAL_LINK); +checkStripePortalLinksExist({ customerPortalTestUrl, customerPortalProdUrl }); -function checkStripePortalLinkExists(link: string | undefined) { - if (!link) { - if (isDev) console.warn('\x1b[31m%s\x1b[0m', '⚠️ STRIPE_CUSTOMER_PORTAL_LINK is not defined.'); - if (!isDev) throw new Error('🚫 STRIPE_CUSTOMER_PORTAL_LINK is not defined'); - } +type StripePortalUrls = { + customerPortalTestUrl: string | undefined; + customerPortalProdUrl: string | undefined; +}; + +function checkStripePortalLinksExist(links: StripePortalUrls) { + const schema = z.string().url(); + const testResult = schema.safeParse(links.customerPortalTestUrl); + const prodResult = schema.safeParse(links.customerPortalProdUrl); + let consoleMsg = { + color: '\x1b[33m%s\x1b[0m', + msg: '', + }; + + if (testResult.success && prodResult.success) { + consoleMsg.color = '\x1b[32m%s\x1b[0m'; + consoleMsg.msg = + '✅ Both STRIPE_CUSTOMER_PORTAL_LINK links defined'; + } else if (!testResult.success && !prodResult.success) { + consoleMsg.msg = + '⛔️ STRIPE_CUSTOMER_PORTAL_LINK is not defined'; + } else if (!testResult.success) { + consoleMsg.msg = '⛔️ STRIPE_CUSTOMER_PORTAL_LINK is not defined for test env'; + } else { + consoleMsg.msg = + '⛔️ STRIPE_CUSTOMER_PORTAL_LINK is not defined for prod env'; + } + console.log(consoleMsg.color, consoleMsg.msg); }