The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
Hide native billing flow behind feature flag
Joshen Lim6cc490b0f08b
diff --git a/studio/components/interfaces/Billing/AddNewPaymentMethodModal/index.tsx b/studio/components/interfaces/Billing/AddNewPaymentMethodModal/AddNewPaymentMethodModal.tsxsimilarity index 100%rename from studio/components/interfaces/Billing/AddNewPaymentMethodModal/index.tsxrename to studio/components/interfaces/Billing/AddNewPaymentMethodModal/AddNewPaymentMethodModal.tsxdiff --git a/studio/components/interfaces/Billing/PaymentSummaryPanel/index.tsx b/studio/components/interfaces/Billing/PaymentSummaryPanel/PaymentSumaryPanel.tsxsimilarity index 99%rename from studio/components/interfaces/Billing/PaymentSummaryPanel/index.tsxrename to studio/components/interfaces/Billing/PaymentSummaryPanel/PaymentSumaryPanel.tsxindex c209f17f8b3..e1ba0a5dae9 100644--- a/studio/components/interfaces/Billing/PaymentSummaryPanel/index.tsx+++ b/studio/components/interfaces/Billing/PaymentSummaryPanel/PaymentSumaryPanel.tsx@@ -3,7 +3,6 @@ import { Listbox, IconLoader, Button, IconPlus, IconAlertCircle } from '@supabasimport { useStore } from 'hooks'import { STRIPE_PRODUCT_IDS } from 'lib/constants'-import { StripeProduct } from '..'import { SubscriptionPreview } from '../Billing.types'import PaymentTotal from './PaymentTotal'import InformationBox from 'components/ui/InformationBox'@@ -11,7 +10,7 @@ import InformationBox from 'components/ui/InformationBox'interface Props {isRefreshingPreview: booleansubscriptionPreview?: SubscriptionPreview- currentPlan: StripeProduct+ currentPlan: anycurrentComputeSize: anyselectedPlan?: anyselectedComputeSize: anydiff --git a/studio/components/interfaces/Billing/PlanSelection/index.tsx b/studio/components/interfaces/Billing/PlanSelection/PlanSelection.tsxsimilarity index 95%rename from studio/components/interfaces/Billing/PlanSelection/index.tsxrename to studio/components/interfaces/Billing/PlanSelection/PlanSelection.tsxindex 180db2a2708..51167104083 100644--- a/studio/components/interfaces/Billing/PlanSelection/index.tsx+++ b/studio/components/interfaces/Billing/PlanSelection/PlanSelection.tsx@@ -2,14 +2,13 @@ import Link from 'next/link'import { FC } from 'react'import { Transition } from '@headlessui/react'-import { StripeProduct } from '..'import Plans from './Plans/Plans'import { formatTierOptions } from './PlanSelection.utils'interface Props {visible: booleantiers: any[]- currentPlan?: StripeProduct+ currentPlan?: anyonSelectPlan: (plan: any) => void}diff --git a/studio/components/interfaces/Billing/ProUpgrade/index.tsx b/studio/components/interfaces/Billing/ProUpgrade/ProUpgrade.tsxsimilarity index 100%rename from studio/components/interfaces/Billing/ProUpgrade/index.tsxrename to studio/components/interfaces/Billing/ProUpgrade/ProUpgrade.tsxdiff --git a/studio/components/interfaces/Billing/Subscription/Subscription.tsx b/studio/components/interfaces/Billing/Subscription/Subscription.tsxindex 01c3a93c39b..7478c9335f4 100644--- a/studio/components/interfaces/Billing/Subscription/Subscription.tsx+++ b/studio/components/interfaces/Billing/Subscription/Subscription.tsx@@ -1,17 +1,18 @@import { FC } from 'react'import dayjs from 'dayjs'import { sum } from 'lodash'-import { Loading, Button } from '@supabase/ui'import { useRouter } from 'next/router'+import { Loading, Button } from '@supabase/ui'+import { Dictionary } from '@supabase/grid'+import { formatBytes } from 'lib/helpers'+import { STRIPE_PRODUCT_IDS } from 'lib/constants'+import { SubscriptionStats, useStore, useFlag, useSubscriptionStats } from 'hooks'import CostBreakdownRow from './CostBreakdownRow'import { StripeSubscription } from './Subscription.types'import { deriveFeatureCost, deriveProductCost } from '../PAYGUsage/PAYGUsage.utils'import { chargeableProducts } from '../PAYGUsage/PAYGUsage.constants'-import { formatBytes } from 'lib/helpers'-import { STRIPE_PRODUCT_IDS } from 'lib/constants'-import { Dictionary } from '@supabase/grid'-import { SubscriptionStats, useStore } from 'hooks'+import UpgradeButton from './UpgradeButton'interface Props {project: any@@ -36,6 +37,9 @@ const Subscription: FC<Props> = ({const { ui } = useStore()const isOrgOwner = ui.selectedOrganization?.is_owner+ const nativeBilling = useFlag('nativeBilling')+ const subscriptionStats = useSubscriptionStats()+const isPayg = subscription?.tier.prod_id === STRIPE_PRODUCT_IDS.PAYGconst addOns = subscription?.addons ?? []const paid = subscription && subscription.tier.unit_amount > 0@@ -61,21 +65,28 @@ const Subscription: FC<Props> = ({</p><h3 className="text-xl mb-0">{subscription?.tier.name ?? '-'}</h3></div>- {/* <UpgradeButton paid={paid} projectRef={project.ref} /> */}- <div className="flex flex-col items-end space-y-2">- <Button- disabled={!isOrgOwner}- onClick={() => router.push(`/project/${project.ref}/settings/billing/update`)}- type="primary"- >- Change subscription- </Button>- {!isOrgOwner && (- <p className="text-sm text-scale-1100">- Only the organization owner can amend subscriptions- </p>- )}- </div>+ {nativeBilling ? (+ <div className="flex flex-col items-end space-y-2">+ <Button+ disabled={!isOrgOwner}+ onClick={() => router.push(`/project/${project.ref}/settings/billing/update`)}+ type="primary"+ >+ Change subscription+ </Button>+ {!isOrgOwner && (+ <p className="text-sm text-scale-1100">+ Only the organization owner can amend subscriptions+ </p>+ )}+ </div>+ ) : (+ <UpgradeButton+ paid={paid}+ projectRef={project.ref}+ subscriptionStats={subscriptionStats}+ />+ )}</div>{paid && (<div className="px-6 pt-4">diff --git a/studio/components/interfaces/Billing/Subscription/UpgradeButton.tsx b/studio/components/interfaces/Billing/Subscription/UpgradeButton.tsxnew file mode 100644index 00000000000..d39467cfcf3--- /dev/null+++ b/studio/components/interfaces/Billing/Subscription/UpgradeButton.tsx@@ -0,0 +1,254 @@+import { FC, useReducer, useState } from 'react'+import { useRouter } from 'next/router'+import { includes, without } from 'lodash'+import { Button, Modal, Input, Divider, Typography, IconCheckCircle } from '@supabase/ui'++import { SubscriptionStats, useStore } from 'hooks'+import { API_URL, DEFAULT_FREE_PROJECTS_LIMIT } from 'lib/constants'+import { getURL } from 'lib/helpers'+import { post } from 'lib/common/fetch'+import { CancellationReasons } from './SubcriptionCancellation.constants'++/**+ * Generates a Stripe Billing Portal link for a project.+ * To be deprecate after native billing is completely rolled out+ *+ * @param {Object} props.projectRef Project Ref+ * @param {Boolean} props.paid Tier name+ */++interface Props {+ projectRef: string+ paid: boolean+ subscriptionStats: SubscriptionStats+}++const UpgradeButton: FC<Props> = ({ projectRef, paid, subscriptionStats }) => {+ const router = useRouter()+ const { ui, app } = useStore()+ const [loading, setLoading] = useState<boolean>(false)+ const [exitSurveyVisible, setExitSurveyVisible] = useState<boolean>(false)+ const [showFreeProjectLimitWarning, setShowFreeProjectLimitWarning] = useState<boolean>(false)++ const freeProjectsLimit = ui?.profile?.free_project_limit ?? DEFAULT_FREE_PROJECTS_LIMIT+ const freeProjectsOwned = subscriptionStats.total_free_projects ?? 0+ const isOrgOwner = ui.selectedOrganization?.is_owner++ /**+ * Get a link and then redirect them+ */+ const redirectToPortal = async () => {+ try {+ setLoading(true)+ let { subscriptionPortal, billingPortal } = await post(`${API_URL}/stripe/portal`, {+ projectRef,+ returnTo: `${getURL()}${router.asPath}`,+ })+ window.location.replace(subscriptionPortal || billingPortal)+ setExitSurveyVisible(false)+ } catch (error: any) {+ ui.setNotification({ category: 'error', message: `Failed to redirect: ${error.message}` })+ } finally {+ setExitSurveyVisible(false)+ setLoading(false)+ }+ }++ function handleUpgradeButton() {+ if (paid) {+ if (freeProjectsOwned >= DEFAULT_FREE_PROJECTS_LIMIT) {+ setShowFreeProjectLimitWarning(true)+ } else {+ setExitSurveyVisible(true)+ }+ } else {+ redirectToPortal()+ }+ }++ return (+ <>+ <UnsubcribeExitSurvey+ visible={exitSurveyVisible}+ setVisible={setExitSurveyVisible}+ handleDowngrade={redirectToPortal}+ />+ <Modal+ closable+ visible={showFreeProjectLimitWarning}+ onCancel={() => setShowFreeProjectLimitWarning(false)}+ header="Free project limit reached"+ description="Please delete one of your free projects to downgrade this project back to free tier"+ size="tiny"+ showIcon+ layout="vertical"+ customFooter={+ <div className="flex w-full items-end">+ <Button+ block+ icon={<IconCheckCircle strokeWidth={2} />}+ onClick={() => setShowFreeProjectLimitWarning(false)}+ >+ OK+ </Button>+ </div>+ }+ >+ <Modal.Content>+ <p className="text-sm text-scale-1100 py-4">+ Please delete one of your free projects to downgrade this project back to free tier+ </p>+ </Modal.Content>+ </Modal>+ <div className="flex flex-col items-end space-y-2">+ <Button+ disabled={!isOrgOwner}+ onClick={handleUpgradeButton}+ type={paid ? 'secondary' : 'primary'}+ loading={loading}+ >+ {paid ? 'Cancel subscription' : 'Upgrade to Pro'}+ </Button>+ {!isOrgOwner && (+ <Typography.Text type="secondary" small>+ Only the organization owner can amend subscriptions+ </Typography.Text>+ )}+ </div>+ </>+ )+}++export default UpgradeButton++function UnsubcribeExitSurvey({ visible, setVisible, handleDowngrade }: any) {+ const router = useRouter()+ const { ref } = router.query++ const { ui } = useStore()++ const [loading, setLoading] = useState<boolean>(false)+ const [additionalFeedback, setAdditionalFeedback] = useState('')++ const [selectedCancellationReasons, dispatchSelectedCancellationReasons] = useReducer(reducer, [])++ const sendExitSurvey = async () => {+ try {+ const message = `+ # Exit survey\n\n\n++ **Reasons for leaving:**\n+ ${selectedCancellationReasons.map((x) => {+ return `${x}. \n`+ })}++ \n+ Additonal feedback:\n+ ${additionalFeedback}++ `++ setLoading(true)+ await post(`${API_URL}/feedback/send`, {+ projectRef: ref,+ subject: 'Subscription cancellation - Exit survey',+ tags: ['dashboard-exitsurvey'],+ category: 'Billing',+ message,+ })+ setLoading(false)+ await handleDowngrade()+ } catch (error: any) {+ ui.setNotification({ category: 'error', message: `Failed to submit exit survey: ${error}` })+ }+ }++ return (+ <Modal+ header="Reason for cancelling"+ loading={loading}+ visible={visible}+ closable+ contentStyle={{ padding: 0 }}+ hideFooter+ onCancel={() => setVisible(false)}+ >+ <div className="py-4 space-y-4">+ <Modal.Content>+ <div className="">+ <div>+ <p className="text-base text-scale-1200">+ <p>We always strive to improve Supabase as much as we can.</p>+ </p>+ <p className="text-sm text-scale-1100">+ <p>+ please let us know the reasons you are cancelling your subscription so we can+ improve in the future.+ </p>+ </p>+ </div>+ </div>+ </Modal.Content>+ <Modal.Seperator />+ <Modal.Content>+ <div className="flex flex-wrap gap-2" data-toggle="buttons">+ {CancellationReasons.map((option) => {+ const active = selectedCancellationReasons.find((x) => x === option)+ return (+ <label+ key={option}+ className={`+ checkbox-pill text-center rounded-md py-1 pl-2 pr-3 shadow-sm+ transition-all flex items-center space-x-2+ duration-100 cursor-pointer text-sm+ ${+ active+ ? ` opacity-100 bg-white text-scale-100 hover:bg-opacity-75`+ : ` bg-scale-1200 opacity-25 hover:opacity-50 text-scale-100`+ }+ `}+ >+ <input+ type="checkbox"+ name="options"+ value={option}+ className="hidden"+ onClick={dispatchSelectedCancellationReasons}+ />+ <div>{option}</div>+ </label>+ )+ })}+ </div>+ </Modal.Content>+ <Modal.Seperator />+ <Modal.Content>+ <Input.TextArea+ id="feedback"+ onChange={(e) => setAdditionalFeedback(e.target.value)}+ value={additionalFeedback}+ label="Anything else we can improve on?"+ placeholder="Anything else we should know, or could improve on?"+ />+ </Modal.Content>+ <Modal.Seperator />+ <div className="flex gap-2 justify-end px-6 w-full">+ <Button type="default" onClick={() => setVisible(false)}>+ Cancel+ </Button>+ <Button loading={loading} onClick={sendExitSurvey}>+ Proceed to cancel subscription+ </Button>+ </div>+ </div>+ </Modal>+ )+}++function reducer(state: any, action: any) {+ if (includes(state, action.target.value)) {+ return without(state, action.target.value)+ } else {+ return [...state, action.target.value]+ }+}diff --git a/studio/components/interfaces/Billing/index.ts b/studio/components/interfaces/Billing/index.tsindex d942ea1fe77..c71fe6c60f6 100644--- a/studio/components/interfaces/Billing/index.ts+++ b/studio/components/interfaces/Billing/index.ts@@ -1,13 +1,13 @@import PAYGUsage from './PAYGUsage/PAYGUsage'import Subscription from './Subscription/Subscription'-import PlanSelection from './PlanSelection'-import ProUpgrade from './ProUpgrade'+import PlanSelection from './PlanSelection/PlanSelection'+import ProUpgrade from './ProUpgrade/ProUpgrade'import EnterpriseRequest from './EnterpriseRequest'import ExitSurvey from './ExitSurvey/ExitSurvey'-import PaymentSummaryPanel from './PaymentSummaryPanel'+import PaymentSummaryPanel from './PaymentSummaryPanel/PaymentSumaryPanel'import ComputeSizeSelection from './AddOns/ComputeSizeSelection'-import AddNewPaymentMethodModal from './AddNewPaymentMethodModal'+import AddNewPaymentMethodModal from './AddNewPaymentMethodModal/AddNewPaymentMethodModal'import UpdateSuccess from './UpdateSuccess'import Invoices from './Invoices'