Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Update project billing page to direct to new billing portal

Joshen Lim9cdc753bdf58
diff --git a/studio/components/interfaces/Billing/Subscription/Subscription.tsx b/studio/components/interfaces/Billing/Subscription/Subscription.tsx
index e7061a576c1..77a2fe7bdec 100644
--- a/studio/components/interfaces/Billing/Subscription/Subscription.tsx
+++ b/studio/components/interfaces/Billing/Subscription/Subscription.tsx
@@ -1,9 +1,9 @@
import { FC } from 'react'
import dayjs from 'dayjs'
import { sum } from 'lodash'
-import { Loading } from '@supabase/ui'
+import { Loading, Button } from '@supabase/ui'
+import { useRouter } from 'next/router'
-import UpgradeButton from './UpgradeButton'
import CostBreakdownRow from './CostBreakdownRow'
import { StripeSubscription } from './Subscription.types'
import { deriveFeatureCost, deriveProductCost } from '../PAYGUsage/PAYGUsage.utils'
@@ -11,12 +11,11 @@ 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 } from 'hooks'
+import { SubscriptionStats, useStore } from 'hooks'
interface Props {
project: any
subscription: StripeSubscription
- subscriptionStats: SubscriptionStats
paygStats?: Dictionary<number>
loading?: boolean
showProjectName?: boolean
@@ -27,13 +26,16 @@ interface Props {
const Subscription: FC<Props> = ({
project,
subscription,
- subscriptionStats,
paygStats,
loading = false,
showProjectName = false,
currentPeriodStart,
currentPeriodEnd,
}) => {
+ const router = useRouter()
+ const { ui } = useStore()
+ const isOrgOwner = ui.selectedOrganization?.is_owner
+
const isPayg = subscription?.tier.prod_id === STRIPE_PRODUCT_IDS.PAYG
const addOns = subscription?.addons ?? []
const paid = subscription && subscription.tier.unit_amount > 0
@@ -57,11 +59,21 @@ const Subscription: FC<Props> = ({
<p className="text-sm">{showProjectName ? project.name : 'Current subscription'}</p>
<h3 className="text-xl mb-0">{subscription?.tier.name ?? '-'}</h3>
</div>
- <UpgradeButton
- paid={paid}
- projectRef={project.ref}
- subscriptionStats={subscriptionStats}
- />
+ {/* <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>
</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.tsx
deleted file mode 100644
index 5be0c0793b1..00000000000
--- a/studio/components/interfaces/Billing/Subscription/UpgradeButton.tsx
+++ /dev/null
@@ -1,253 +0,0 @@
-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.
- *
- * @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 >= freeProjectsLimit) {
- 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/pages/project/[ref]/settings/billing/index.tsx b/studio/pages/project/[ref]/settings/billing/index.tsx
index 02162eb8933..3fbdd3c89d8 100644
--- a/studio/pages/project/[ref]/settings/billing/index.tsx
+++ b/studio/pages/project/[ref]/settings/billing/index.tsx
@@ -1,7 +1,7 @@
import dayjs from 'dayjs'
import { FC, useEffect, useState } from 'react'
import { observer } from 'mobx-react-lite'
-import { Typography, Loading, IconArrowRight } from '@supabase/ui'
+import { Typography, Loading, IconArrowRight, Button } from '@supabase/ui'
import { get as _get, maxBy } from 'lodash'
import { Dictionary } from '@supabase/grid'
@@ -123,7 +123,6 @@ const Settings: FC<SettingsProps> = ({ project }) => {
project={project}
subscription={subscription}
paygStats={paygStats}
- subscriptionStats={subscriptionStats}
currentPeriodStart={subscription?.billing.current_period_start}
currentPeriodEnd={subscription?.billing.current_period_end}
/>