The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
Hook up subscription preview API and change enabling overages toggle to enabling spend caps
Joshen Lim7409efc92f32
diff --git a/studio/components/interfaces/Billing/AddOns/AddOns.constant.ts b/studio/components/interfaces/Billing/AddOns/AddOns.constant.tsindex ec516b17645..73d71e95349 100644--- a/studio/components/interfaces/Billing/AddOns/AddOns.constant.ts+++ b/studio/components/interfaces/Billing/AddOns/AddOns.constant.ts@@ -1,35 +1,37 @@+import { STRIPE_COMPUTE_SIZE_ADD_ON_PRICE_IDS } from 'lib/constants'+// Temp hard-code, should fetch all available add ons from APIexport const COMPUTE_SIZES = [{- id: 1,+ id: STRIPE_COMPUTE_SIZE_ADD_ON_PRICE_IDS.SMALL,name: 'Small',description: 'Great for personal projects and experiments',specs: '2 CPUs • 1GB memory • 2,085Mbps Disk IO',price: 0,},{- id: 'prod_KrzaPTzPj1KAYB',+ id: STRIPE_COMPUTE_SIZE_ADD_ON_PRICE_IDS.MEDIUM,name: 'Medium',description: 'Great for projects that are starting to gain traction',specs: '2 CPUs • 4GB memory • 2,085Mbps Disk IO',price: 50,},{- id: 'prod_KrzZKxuF2YNsjR',+ id: STRIPE_COMPUTE_SIZE_ADD_ON_PRICE_IDS.LARGE,name: 'Large',description: 'For a large project that receives a decent amount of traffic',specs: '2 CPUs • 8GB memory • 4,750Mbps Disk IO',price: 100,},{- id: 'prod_KF94pLcOx5PSlp',+ id: STRIPE_COMPUTE_SIZE_ADD_ON_PRICE_IDS.XLARGE,name: 'X-Large',description: 'For a large project that receives a sizeable amount of traffic',specs: '4 CPUs • 16GB memory • 4,750Mbps Disk IO',price: 200,},{- id: 'prod_KrzapWYxUYmve5',+ id: STRIPE_COMPUTE_SIZE_ADD_ON_PRICE_IDS.XXLARGE,name: 'XX-Large',description: 'For a huge project that receives heavy amount of traffic',specs: '8 CPUs • 32GB memory • 4,750Mbps Disk IO',diff --git a/studio/components/interfaces/Billing/Billing.types.ts b/studio/components/interfaces/Billing/Billing.types.tsnew file mode 100644index 00000000000..9d37321db29--- /dev/null+++ b/studio/components/interfaces/Billing/Billing.types.ts@@ -0,0 +1,23 @@+// [TODO] I'm guessing this should come from the API?+export interface SubscriptionPreview {+ // Amount due total+ amount_due: number+ // Amount due total - available credit balance+ amount_due_immediately: number+ // Amount of credit the org has left+ available_credit_balance: number+ // Remaining credit balance after deducting to amount due immediately+ remaining_credit_balance: number+ // Amount of credits returned as refund from a downgrade+ returned_credits: number+ // Amount due for each subsequent month+ base_amount_due_next_billing_cycle: number+ // Date for next invoice (Billing cycle resets for every change to a subscription)+ next_billing_cycle: number+ // Remaining number of days to end of current billing cycle+ // This is meant for FE to display the refund specifics+ remaining_days_in_current_billing_cycle: number+ // Total days in current billing cycle+ // This is meant for FE to display the refund specifics+ total_days_in_current_billing_cycle: number+}diff --git a/studio/components/interfaces/Billing/PaymentSummaryPanel.tsx b/studio/components/interfaces/Billing/PaymentSummaryPanel.tsxindex 9456bfc8071..6aaa43a108b 100644--- a/studio/components/interfaces/Billing/PaymentSummaryPanel.tsx+++ b/studio/components/interfaces/Billing/PaymentSummaryPanel.tsx@@ -1,22 +1,26 @@import { FC } from 'react'-import { Listbox, IconLoader, Button, IconPlus } from '@supabase/ui'+import { Listbox, IconLoader, Button, Loading, IconPlus } from '@supabase/ui'import { STRIPE_PRODUCT_IDS } from 'lib/constants'import { StripeProduct } from '.'import { BillingPlan } from './PlanSelection/Plans/Plans.types'+import { SubscriptionPreview } from './Billing.types'interface Props {+ isRefreshingPreview: boolean+ subscriptionPreview?: SubscriptionPreviewcurrentPlan: StripeProductcurrentComputeSize: anyselectedPlan?: BillingPlanselectedComputeSize: any- isOverageEnabled: boolean+ isSpendCapEnabled: booleanpaymentMethods?: anyselectedPaymentMethod: anyisLoadingPaymentMethods: booleanonSelectPaymentMethod: (method: any) => voidonSelectAddNewPaymentMethod: () => voidonConfirmPayment: () => void+ isSubmitting: boolean}// Use case of this panel is actually only for upgrading from Free to Pro@@ -27,9 +31,11 @@ interface Props {// the selectedComputeSize to an addOns array. But for now, we keep it simple, don't over-engineer too earlyconst PaymentSummaryPanel: FC<Props> = ({+ isRefreshingPreview,+ subscriptionPreview,currentPlan,selectedPlan,- isOverageEnabled,+ isSpendCapEnabled,currentComputeSize,selectedComputeSize,paymentMethods,@@ -38,17 +44,18 @@ const PaymentSummaryPanel: FC<Props> = ({onSelectPaymentMethod,onSelectAddNewPaymentMethod,onConfirmPayment,+ isSubmitting,}) => {const isChangingPlan =(currentPlan.prod_id !== STRIPE_PRODUCT_IDS.PAYG && currentPlan.prod_id !== selectedPlan?.id) ||- (currentPlan.prod_id !== STRIPE_PRODUCT_IDS.PAYG && isOverageEnabled) ||- (currentPlan.prod_id === STRIPE_PRODUCT_IDS.PAYG && !isOverageEnabled)+ (currentPlan.prod_id !== STRIPE_PRODUCT_IDS.PAYG && !isSpendCapEnabled) ||+ (currentPlan.prod_id === STRIPE_PRODUCT_IDS.PAYG && isSpendCapEnabled)const isChangingComputeSize = currentComputeSize.id !== selectedComputeSize.idconst hasChangesToPlan = isChangingPlan || isChangingComputeSizeconst totalMonthlyCost = (selectedPlan?.price ?? 0) + selectedComputeSize.priceconst getCurrentPlanName = () => {- if (currentPlan.prod_id === STRIPE_PRODUCT_IDS.PAYG) return 'Pro tier (Overages enabled)'+ if (currentPlan.prod_id === STRIPE_PRODUCT_IDS.PAYG) return 'Pro tier (No spend caps)'else return currentPlan.name}@@ -73,7 +80,7 @@ const PaymentSummaryPanel: FC<Props> = ({{isChangingPlan && (<div className="flex items-center justify-between"><p className="text-sm">- {selectedPlan?.name} tier {isOverageEnabled ? '(Overages enabled)' : ''}+ {selectedPlan?.name} tier {!isSpendCapEnabled ? '(No spend caps)' : ''}</p><p className="text-sm">${selectedPlan?.price}</p></div>@@ -107,24 +114,26 @@ const PaymentSummaryPanel: FC<Props> = ({<div className="h-px w-full bg-scale-600"></div>{/* Payment total */}- <div className="flex items-start justify-between space-x-32">- <div className="space-y-1">- <p>Amount due today</p>- {hasChangesToPlan && (- <p className="text-sm text-scale-1100">- You'll pay a monthly total of{' '}- <span className="text-scale-1200">- ${totalMonthlyCost} {isOverageEnabled && '+ usage fees '}- </span>- starting on 1st February 2022- </p>- )}- </div>- <div className="flex justify-end items-end relative -top-[5px]">- <p className="text-scale-1100 relative -top-[1px]">$</p>- <p className="text-2xl">0</p>+ <Loading active={isRefreshingPreview}>+ <div className="flex items-start justify-between space-x-32">+ <div className="space-y-1">+ <p>Amount due today</p>+ {hasChangesToPlan && (+ <p className="text-sm text-scale-1100">+ You'll pay a monthly total of{' '}+ <span className="text-scale-1200">+ ${totalMonthlyCost} {!isSpendCapEnabled && '+ usage fees '}+ </span>+ starting on 1st February 2022+ </p>+ )}+ </div>+ <div className="flex justify-end items-end relative -top-[5px]">+ <p className="text-scale-1100 relative -top-[1px]">$</p>+ <p className="text-2xl">{(subscriptionPreview?.amount_due_immediately ?? 0) / 100}</p>+ </div></div>- </div>+ </Loading>{/* Payment method selection */}<div className="space-y-2">@@ -177,7 +186,12 @@ const PaymentSummaryPanel: FC<Props> = ({</div><div className="flex items-center justify-end">- <Button type="primary" disabled={!hasChangesToPlan} onClick={() => onConfirmPayment()}>+ <Button+ type="primary"+ loading={isSubmitting}+ disabled={isSubmitting || isLoadingPaymentMethods || !hasChangesToPlan}+ onClick={() => onConfirmPayment()}+ >Confirm payment</Button></div>diff --git a/studio/components/interfaces/Billing/PlanSelection/Plans/Plans.constants.ts b/studio/components/interfaces/Billing/PlanSelection/Plans/Plans.constants.tsindex 352db6a5bbd..114876e83fc 100644--- a/studio/components/interfaces/Billing/PlanSelection/Plans/Plans.constants.ts+++ b/studio/components/interfaces/Billing/PlanSelection/Plans/Plans.constants.ts@@ -1,4 +1,4 @@-import { STRIPE_PRODUCT_IDS } from 'lib/constants'+import { STRIPE_PRODUCT_IDS, STRIPE_TIER_PRICE_IDS } from 'lib/constants'import { BillingPlan } from './Plans.types'// [Joshen] The idea is that API fetch plans will probably receive StripeProduct[]@@ -8,6 +8,7 @@ import { BillingPlan } from './Plans.types'export const BILLING_PLANS: BillingPlan[] = [{id: STRIPE_PRODUCT_IDS.FREE,+ priceId: STRIPE_TIER_PRICE_IDS.FREE,name: 'Free',price: 0,description: 'Zero commitment, perfect for hobby projects and experiments',@@ -20,6 +21,7 @@ export const BILLING_PLANS: BillingPlan[] = [},{id: STRIPE_PRODUCT_IDS.PRO,+ priceId: STRIPE_TIER_PRICE_IDS.PRO,name: 'Pro',price: 25,description: 'Designated support team, account manager and technical specialist',@@ -34,6 +36,7 @@ export const BILLING_PLANS: BillingPlan[] = [},{id: 'Enterprise',+ priceId: '',name: 'Enterprise',price: null,description: 'Custom tailored services to suit your specific business needs',diff --git a/studio/components/interfaces/Billing/PlanSelection/Plans/Plans.types.ts b/studio/components/interfaces/Billing/PlanSelection/Plans/Plans.types.tsindex 158f30e0d6f..4c07e3556b2 100644--- a/studio/components/interfaces/Billing/PlanSelection/Plans/Plans.types.ts+++ b/studio/components/interfaces/Billing/PlanSelection/Plans/Plans.types.ts@@ -1,5 +1,6 @@export interface BillingPlan {id?: string+ priceId?: stringname: stringprice: number | nulldescription: stringdiff --git a/studio/components/interfaces/Billing/ProUpgrade.tsx b/studio/components/interfaces/Billing/ProUpgrade.tsxindex 28ef515f7b8..2a2f634ab6b 100644--- a/studio/components/interfaces/Billing/ProUpgrade.tsx+++ b/studio/components/interfaces/Billing/ProUpgrade.tsx@@ -4,9 +4,9 @@ import { useRouter } from 'next/router'import { Badge, Button, IconArrowLeft, IconHelpCircle, Toggle, Modal } from '@supabase/ui'import { useStore } from 'hooks'-import { getURL } from 'lib/helpers'-import { post } from 'lib/common/fetch'-import { API_URL } from 'lib/constants'+import { getURL, timeout } from 'lib/helpers'+import { post, patch } from 'lib/common/fetch'+import { API_URL, STRIPE_TIER_PRICE_IDS } from 'lib/constants'import Divider from 'components/ui/Divider'import {PaymentSummaryPanel,@@ -17,6 +17,7 @@ import {import { BillingPlan } from './PlanSelection/Plans/Plans.types'import { COMPUTE_SIZES } from './AddOns/AddOns.constant'import { STRIPE_PRODUCT_IDS } from 'lib/constants'+import { SubscriptionPreview } from './Billing.types'interface Props {visible: boolean@@ -25,7 +26,6 @@ interface Props {paymentMethods?: any[]isLoadingPaymentMethods: booleanonSelectBack: () => void- onConfirmPayment: () => void}const ProUpgrade: FC<Props> = ({@@ -35,11 +35,12 @@ const ProUpgrade: FC<Props> = ({paymentMethods,isLoadingPaymentMethods,onSelectBack,- onConfirmPayment,}) => {const { ui } = useStore()const router = useRouter()+ const projectRef = ui.selectedProject?.ref+const currentComputeSize =COMPUTE_SIZES.find((options) => options.id === currentSubscription?.addons[0]?.prod_id) ||COMPUTE_SIZES[0]@@ -48,21 +49,34 @@ const ProUpgrade: FC<Props> = ({currentSubscription.tier.prod_id === STRIPE_PRODUCT_IDS.PRO ||currentSubscription.tier.prod_id === STRIPE_PRODUCT_IDS.PAYG- const [isOverageEnabled, setIsOverageEnabled] = useState(- currentSubscription.tier.prod_id === STRIPE_PRODUCT_IDS.PAYG+ const [isSpendCapEnabled, setIsSpendCapEnabled] = useState(+ currentSubscription.tier.prod_id === STRIPE_PRODUCT_IDS.PRO)+ const [isRefreshingPreview, setIsRefreshingPreview] = useState(false)+ const [isSubmitting, setIsSubmitting] = useState(false)const [showAddPaymentMethodModal, setShowAddPaymentMethodModal] = useState(false)- const [showOveragesHelperModal, setShowOveragesHelperModal] = useState(false)+ const [showSpendCapHelperModal, setShowSpendCapHelperModal] = useState(false)+const [selectedComputeSize, setSelectedComputeSize] = useState<any>(currentComputeSize)const [selectedPaymentMethod, setSelectedPaymentMethod] = useState<any>()+ const [subscriptionPreview, setSubscriptionPreview] = useState<SubscriptionPreview>()useEffect(() => {if (!isLoadingPaymentMethods && paymentMethods && paymentMethods.length > 0) {// [TODO] Figure out how to get the DEFAULT payment methodsetSelectedPaymentMethod(paymentMethods[0])}- }, [isLoadingPaymentMethods])+ }, [isLoadingPaymentMethods, paymentMethods])++ useEffect(() => {+ if (selectedPlan !== undefined) {+ getSubscriptionPreview()+ if (selectedPlan.id === STRIPE_PRODUCT_IDS.PRO) {+ setIsSpendCapEnabled(true)+ }+ }+ }, [selectedPlan, selectedComputeSize, isSpendCapEnabled])const onSelectComputeSizeOption = (option: any) => {setSelectedComputeSize(option)@@ -83,6 +97,42 @@ const ProUpgrade: FC<Props> = ({}}+ const getSubscriptionPreview = async () => {+ const tier = !isSpendCapEnabled ? STRIPE_TIER_PRICE_IDS.PAYG : selectedPlan?.priceId+ // Small currently has no stripe product attached, so FE has a hardcoded ID just for that product+ const addons = selectedComputeSize.name === 'Small' ? [] : [selectedComputeSize.id]+ const proration_date = Math.floor(Date.now() / 1000)++ setIsRefreshingPreview(true)+ const preview = await post(`${API_URL}/projects/${projectRef}/subscription/preview`, {+ tier,+ addons,+ proration_date,+ })+ setIsRefreshingPreview(false)+ console.log('Preview retrieved', preview)+ setSubscriptionPreview(preview)+ }++ const onConfirmPayment = async () => {+ const tier = !isSpendCapEnabled ? STRIPE_TIER_PRICE_IDS.PAYG : selectedPlan?.priceId+ // Small currently has no stripe product attached, so FE has a hardcoded ID just for that product+ const addons = selectedComputeSize.name === 'Small' ? [] : [selectedComputeSize.id]+ const paymentMethod = selectedPaymentMethod.id+ const proration_date = Math.floor(Date.now() / 1000)++ setIsSubmitting(true)+ await timeout(1000)+ setIsSubmitting(false)++ console.log('Confirm payment', {+ tier,+ addons,+ paymentMethod,+ proration_date,+ })+ }+return (<><Transition@@ -129,21 +179,21 @@ const ProUpgrade: FC<Props> = ({<div className="flex items-start justify-between"><div><div className="flex items-center space-x-2">- <p>Enable overages</p>+ <p>Enable spend caps</p><IconHelpCirclesize={16}strokeWidth={1.5}className="cursor-pointer opacity-50 hover:opacity-100 transition"- onClick={() => setShowOveragesHelperModal(true)}+ onClick={() => setShowSpendCapHelperModal(true)}/></div><p className="text-sm text-scale-1100">- Additional resources will be charged on a usage basis+ If disabled, additional resources will be charged on a per-usage basis</p></div><Toggle- checked={isOverageEnabled}- onChange={() => setIsOverageEnabled(!isOverageEnabled)}+ checked={isSpendCapEnabled}+ onChange={() => setIsSpendCapEnabled(!isSpendCapEnabled)}/></div><Divider light />@@ -161,9 +211,11 @@ const ProUpgrade: FC<Props> = ({</div><div className="w-2/5 -mt-10"><PaymentSummaryPanel+ isRefreshingPreview={isRefreshingPreview}+ subscriptionPreview={subscriptionPreview}currentPlan={currentSubscription.tier}selectedPlan={selectedPlan}- isOverageEnabled={isOverageEnabled}+ isSpendCapEnabled={isSpendCapEnabled}currentComputeSize={currentComputeSize}selectedComputeSize={selectedComputeSize}paymentMethods={paymentMethods}@@ -176,6 +228,7 @@ const ProUpgrade: FC<Props> = ({// setShowAddPaymentMethodModal(true)}}onConfirmPayment={onConfirmPayment}+ isSubmitting={isSubmitting}/></div></>@@ -187,13 +240,13 @@ const ProUpgrade: FC<Props> = ({onCancel={() => setShowAddPaymentMethodModal(false)}/>- {/* Overages helper modal */}+ {/* Spend caps helper modal */}<ModalhideFooter- visible={showOveragesHelperModal}+ visible={showSpendCapHelperModal}size="large"- header="Enabling overages"- onCancel={() => setShowOveragesHelperModal(false)}+ header="Enabling spend caps"+ onCancel={() => setShowSpendCapHelperModal(false)}><div className="py-4 space-y-4"><Modal.Content>@@ -251,12 +304,7 @@ const ProUpgrade: FC<Props> = ({<Modal.Seperator /><Modal.Content><div className="flex items-center gap-2">- <Button- block- type="primary"- htmlType="button"- onClick={() => setShowOveragesHelperModal(false)}- >+ <Button block type="primary" onClick={() => setShowSpendCapHelperModal(false)}>Understood</Button></div>diff --git a/studio/lib/constants/infrastructure.ts b/studio/lib/constants/infrastructure.tsindex d15adeb97fe..f383ce83bd2 100644--- a/studio/lib/constants/infrastructure.ts+++ b/studio/lib/constants/infrastructure.ts@@ -99,10 +99,32 @@ export const PROJECT_STATUS = {RESTORING: 'RESTORING',}+// Probably should deprecate to just use price ids?export const STRIPE_PRODUCT_IDS = {- PAYG: process?.env?.NEXT_PUBLIC_STRIPE_PAYG_TIER_ID,- PRO: process?.env?.NEXT_PUBLIC_STRIPE_PRO_TIER_ID,FREE: process?.env?.NEXT_PUBLIC_STRIPE_FREE_TIER_ID,+ PRO: process?.env?.NEXT_PUBLIC_STRIPE_PRO_TIER_ID,+ PAYG: process?.env?.NEXT_PUBLIC_STRIPE_PAYG_TIER_ID,+}++export const STRIPE_TIER_PRICE_IDS = {+ FREE: process?.env.NEXT_PUBLIC_STRIPE_FREE_PRICE_ID ?? 'price_1KCawmJDPojXS6LNahIn31Mr',+ PRO: process?.env.NEXT_PUBLIC_STRIPE_PRO_PRICE_ID ?? 'price_1IdYPAJDPojXS6LNRVCC1LAV',+ PAYG: process?.env.NEXT_PUBLIC_STRIPE_PAYG_PRICE_ID ?? 'price_1KCFZCJDPojXS6LNjTEjcXgU',+}++export const STRIPE_COMPUTE_SIZE_ADD_ON_PRICE_IDS = {+ SMALL: process?.env.NEXT_PUBLIC_STRIPE_COMPUTE_SIZE_SMALL_PRICE_ID ?? 'small',+ MEDIUM:+ process?.env.NEXT_PUBLIC_STRIPE_COMPUTE_SIZE_MEDIUM_PRICE_ID ??+ 'price_1KCFarJDPojXS6LNlS2pAp36',+ LARGE:+ process?.env.NEXT_PUBLIC_STRIPE_COMPUTE_SIZE_LARGE_PRICE_ID ?? 'price_1KCFaNJDPojXS6LNKg8Gy6pS',+ XLARGE:+ process?.env.NEXT_PUBLIC_STRIPE_COMPUTE_SIZE_XLARGE_PRICE_ID ??+ 'price_1Jaem9JDPojXS6LN62rrj5al',+ XXLARGE:+ process?.env.NEXT_PUBLIC_STRIPE_COMPUTE_SIZE_XXLARGE_PRICE_ID ??+ 'price_1KCFbVJDPojXS6LNmlyzusY8',}export const DEFAULT_MINIMUM_PASSWORD_STRENGTH = 4diff --git a/studio/pages/project/[ref]/settings/billing/update.tsx b/studio/pages/project/[ref]/settings/billing/update.tsxindex 34928d7862f..be7dcbf888a 100644--- a/studio/pages/project/[ref]/settings/billing/update.tsx+++ b/studio/pages/project/[ref]/settings/billing/update.tsx@@ -110,10 +110,6 @@ const BillingUpdate: NextPage = () => {setShowConfirmDowngrade(false)}- const onConfirmPayment = () => {- console.log('Confirm payment')- }-return (<BillingLayout><div className="mx-auto max-w-5xl my-10">@@ -140,7 +136,6 @@ const BillingUpdate: NextPage = () => {isLoadingPaymentMethods={isLoadingPaymentMethods}paymentMethods={paymentMethods?.data ?? []}onSelectBack={() => setSelectedPlan(undefined)}- onConfirmPayment={onConfirmPayment}/>)}