Skip to content

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

Add payment method selection in pay summary panel

Joshen Lim13c514961929
diff --git a/studio/components/interfaces/Billing/PaymentSummaryPanel.tsx b/studio/components/interfaces/Billing/PaymentSummaryPanel.tsx
index a70ccf46a4d..9afe0b5ee4b 100644
--- a/studio/components/interfaces/Billing/PaymentSummaryPanel.tsx
+++ b/studio/components/interfaces/Billing/PaymentSummaryPanel.tsx
@@ -1,13 +1,19 @@
import { FC } from 'react'
+import { Listbox, IconLoader } from '@supabase/ui'
+
import { StripeSubscription } from '.'
import { BillingPlan } from './PlanSelection/Plans/Plans.types'
interface Props {
currentPlan: StripeSubscription
- selectedPlan?: BillingPlan
currentComputeSize?: any
+ selectedPlan?: BillingPlan
selectedComputeSize: any
isOverageEnabled: boolean
+ paymentMethods?: any
+ selectedPaymentMethod: any
+ isLoadingPaymentMethods: boolean
+ onSelectPaymentMethod: (method: any) => void
}
// Use case of this panel is actually only for upgrading from Free to Pro
@@ -23,6 +29,10 @@ const PaymentSummaryPanel: FC<Props> = ({
isOverageEnabled,
currentComputeSize,
selectedComputeSize,
+ paymentMethods,
+ selectedPaymentMethod,
+ isLoadingPaymentMethods,
+ onSelectPaymentMethod,
}) => {
const isChangingPlan = selectedPlan !== undefined
const totalMonthlyCost = (selectedPlan?.price ?? 0) + selectedComputeSize.price
@@ -85,6 +95,45 @@ const PaymentSummaryPanel: FC<Props> = ({
<p className="text-2xl">0</p>
</div>
</div>
+
+ {/* Payment method selection */}
+ {isLoadingPaymentMethods ? (
+ <div className="flex items-center space-x-4">
+ <IconLoader className="animate-spin" size={14} />
+ <p className="text-sm">Retrieving payment methods</p>
+ </div>
+ ) : paymentMethods.length === 0 ? (
+ <div className="text-scale-1100">No methods</div>
+ ) : (
+ <Listbox
+ label="Select payment method"
+ value={selectedPaymentMethod?.id}
+ onChange={onSelectPaymentMethod}
+ >
+ {paymentMethods.map((method: any) => {
+ const label = `•••• •••• •••• ${method.card.last4}`
+ return (
+ <Listbox.Option
+ key={method.id}
+ label={label}
+ value={method.id}
+ addOnBefore={() => {
+ return (
+ <img
+ src={`/img/payment-methods/${method.card.brand
+ .replace(' ', '-')
+ .toLowerCase()}.png`}
+ width="32"
+ />
+ )
+ }}
+ >
+ <div>{label}</div>
+ </Listbox.Option>
+ )
+ })}
+ </Listbox>
+ )}
</div>
)
}
diff --git a/studio/components/interfaces/Billing/PlanSelection/Plans/Plans.tsx b/studio/components/interfaces/Billing/PlanSelection/Plans/Plans.tsx
index ab3ca84e31c..6f2a65ee259 100644
--- a/studio/components/interfaces/Billing/PlanSelection/Plans/Plans.tsx
+++ b/studio/components/interfaces/Billing/PlanSelection/Plans/Plans.tsx
@@ -68,8 +68,8 @@ const Plans: FC<Props> = ({ plans, currentPlan, onSelectPlan }) => {
)}
</div>
<ul className="space-y-4">
- {plan.pointers.map((pointer: string) => (
- <li className="text-sm flex">
+ {plan.pointers.map((pointer: string, idx: number) => (
+ <li key={`pointer-${idx}`} className="text-sm flex">
<div className="w-[15%]">
<svg
className={`h-5 w-5 text-green-900`}
diff --git a/studio/components/interfaces/Billing/ProUpgrade.tsx b/studio/components/interfaces/Billing/ProUpgrade.tsx
index 75de767aeca..ab7f04652a4 100644
--- a/studio/components/interfaces/Billing/ProUpgrade.tsx
+++ b/studio/components/interfaces/Billing/ProUpgrade.tsx
@@ -1,4 +1,4 @@
-import { FC, useState } from 'react'
+import { FC, useEffect, useState } from 'react'
import { Transition } from '@headlessui/react'
import { Badge, Button, IconArrowLeft, IconHelpCircle, Toggle } from '@supabase/ui'
@@ -11,17 +11,35 @@ interface Props {
visible: boolean
currentPlan: StripeSubscription
selectedPlan?: BillingPlan
+ paymentMethods?: any[]
onSelectBack: () => void
+
+ isLoadingPaymentMethods: boolean
}
-const ProUpgrade: FC<Props> = ({ visible, currentPlan, selectedPlan, onSelectBack }) => {
+const ProUpgrade: FC<Props> = ({
+ visible,
+ currentPlan,
+ selectedPlan,
+ paymentMethods,
+ onSelectBack,
+ isLoadingPaymentMethods,
+}) => {
const [isOverageEnabled, setIsOverageEnabled] = useState(false)
const [selectedComputeSize, setSelectedComputeSize] = useState<any>(COMPUTE_SIZES[0])
+ const [selectedPaymentMethod, setSelectedPaymentMethod] = useState<any>()
const onSelectComputeSizeOption = (option: any) => {
setSelectedComputeSize(option)
}
+ useEffect(() => {
+ if (!isLoadingPaymentMethods && paymentMethods && paymentMethods.length > 0) {
+ // [TODO] Figure out how to get the DEFAULT payment method
+ setSelectedPaymentMethod(paymentMethods[0])
+ }
+ }, [isLoadingPaymentMethods])
+
return (
<Transition
show={visible}
@@ -92,6 +110,10 @@ const ProUpgrade: FC<Props> = ({ visible, currentPlan, selectedPlan, onSelectBac
selectedPlan={selectedPlan}
isOverageEnabled={isOverageEnabled}
selectedComputeSize={selectedComputeSize}
+ paymentMethods={paymentMethods}
+ isLoadingPaymentMethods={isLoadingPaymentMethods}
+ selectedPaymentMethod={selectedPaymentMethod}
+ onSelectPaymentMethod={setSelectedPaymentMethod}
/>
</div>
</>
diff --git a/studio/components/layouts/BillingLayout.tsx b/studio/components/layouts/BillingLayout.tsx
index 8ed36e1a46d..2e84a2fc94e 100644
--- a/studio/components/layouts/BillingLayout.tsx
+++ b/studio/components/layouts/BillingLayout.tsx
@@ -44,7 +44,6 @@ const BillingLayout: FC<Props> = ({ children }) => {
</div>
</div>
<div className="overflow-auto">
- {/* <section className="mx-auto max-w-4xl relative my-10">{children}</section> */}
<section className="relative">{children}</section>
</div>
</div>
diff --git a/studio/pages/project/[ref]/settings/billing/update.tsx b/studio/pages/project/[ref]/settings/billing/update.tsx
index 7ceb7444b84..b673196ed9c 100644
--- a/studio/pages/project/[ref]/settings/billing/update.tsx
+++ b/studio/pages/project/[ref]/settings/billing/update.tsx
@@ -22,16 +22,19 @@ import { BILLING_PLANS } from 'components/interfaces/Billing/PlanSelection/Plans
const BillingUpdate: NextPage = () => {
const { ui } = useStore()
const projectRef = ui.selectedProject?.ref
+ const stripeCustomerId = ui.selectedOrganization?.stripe_customer_id
const subscriptionStats = useSubscriptionStats()
const freeProjectsLimit = ui?.profile?.free_project_limit ?? DEFAULT_FREE_PROJECTS_LIMIT
const freeProjectsOwned = subscriptionStats.total_free_projects ?? 0
- const [loading, setLoading] = useState(false)
+ const [isLoadingSubscription, setIsLoadingSubscription] = useState(false)
+ const [isLoadingPaymentMethods, setIsLoadingPaymentMethods] = useState(false)
const [showConfirmDowngrade, setShowConfirmDowngrade] = useState(false)
const [showDowngradeError, setShowDowngradeError] = useState(false)
const [subscription, setSubscription] = useState<StripeSubscription>()
+ const [paymentMethods, setPaymentMethods] = useState<any>()
const [selectedPlan, setSelectedPlan] = useState<BillingPlan>()
const showProPlanPage =
@@ -42,9 +45,31 @@ const BillingUpdate: NextPage = () => {
if (projectRef) getSubscription()
}, [projectRef])
+ useEffect(() => {
+ if (stripeCustomerId) getStripeAccount()
+ }, [stripeCustomerId])
+
+ const getStripeAccount = async () => {
+ try {
+ setIsLoadingPaymentMethods(true)
+ const { paymentMethods, error: customerError } = await post(`${API_URL}/stripe/customer`, {
+ stripe_customer_id: stripeCustomerId,
+ })
+ if (customerError) throw customerError
+ setIsLoadingPaymentMethods(false)
+ setPaymentMethods(paymentMethods)
+ } catch (error: any) {
+ ui.setNotification({
+ error,
+ category: 'error',
+ message: `Failed to get subscription: ${error.message}`,
+ })
+ }
+ }
+
const getSubscription = async () => {
try {
- setLoading(true)
+ setIsLoadingSubscription(true)
if (!ui.selectedProject?.subscription_id) {
throw new Error('Unable to get subscription ID of project')
@@ -64,7 +89,7 @@ const BillingUpdate: NextPage = () => {
message: `Failed to get subscription: ${error.message}`,
})
} finally {
- setLoading(false)
+ setIsLoadingSubscription(false)
}
}
@@ -107,6 +132,8 @@ const BillingUpdate: NextPage = () => {
visible={showProPlanPage}
currentPlan={subscription}
selectedPlan={selectedPlan}
+ isLoadingPaymentMethods={isLoadingPaymentMethods}
+ paymentMethods={paymentMethods?.data ?? []}
onSelectBack={() => setSelectedPlan(undefined)}
/>
)}