Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

update payment methods

Kiran K455177d54733
diff --git a/apps/web/app/api/workspaces/[idOrSlug]/billing/payment-methods/route.ts b/apps/web/app/api/workspaces/[idOrSlug]/billing/payment-methods/route.ts
index 28a0ebb07f..b6f435f11d 100644
--- a/apps/web/app/api/workspaces/[idOrSlug]/billing/payment-methods/route.ts
+++ b/apps/web/app/api/workspaces/[idOrSlug]/billing/payment-methods/route.ts
@@ -8,13 +8,7 @@ import { z } from "zod";
const addPaymentMethodSchema = z.object({
method: z
- .enum([
- "card",
- "us_bank_account",
- "acss_debit",
- "sepa_debit",
- "au_becs_debit",
- ])
+ .enum(["card", "us_bank_account", "acss_debit", "sepa_debit"])
.optional(),
});
@@ -28,7 +22,6 @@ const stripePaymentMethodOptions = {
},
},
sepa_debit: {},
- au_becs_debit: {},
};
// GET /api/workspaces/[idOrSlug]/billing/payment-methods - get all payment methods
@@ -84,6 +77,9 @@ export const POST = withWorkspace(async ({ workspace, req }) => {
mode: "setup",
customer: workspace.stripeId,
payment_method_types: [method],
+ payment_method_options: {
+ [method]: stripePaymentMethodOptions[method],
+ },
currency: "usd",
success_url: `${APP_DOMAIN}/${workspace.slug}/settings/billing`,
cancel_url: `${APP_DOMAIN}/${workspace.slug}/settings/billing`,
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/billing/payment-methods.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/billing/payment-methods.tsx
index 2b047ccd03..b3702eea76 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/billing/payment-methods.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/billing/payment-methods.tsx
@@ -14,6 +14,7 @@ import { PaymentMethodTypesList } from "./payment-method-types";
export default function PaymentMethods() {
const router = useRouter();
+ const [isLoading, setIsLoading] = useState(false);
const { slug, stripeId, partnersEnabled, plan } = useWorkspace();
const { paymentMethods } = usePaymentMethods();
@@ -25,8 +26,6 @@ export default function PaymentMethods() {
(pm) => pm.type === "us_bank_account",
);
- const [isLoading, setIsLoading] = useState(false);
-
const managePaymentMethods = async () => {
setIsLoading(true);
const { url } = await fetch(
@@ -123,9 +122,6 @@ const PaymentMethodCard = ({
paymentMethod?: Stripe.PaymentMethod;
forPayouts?: boolean;
}) => {
- const router = useRouter();
- const { slug } = useWorkspace();
- const [isLoading, setIsLoading] = useState(false);
const { setShowAddPayoutMethodModal, AddPayoutMethodModal } =
useAddPayoutMethodModal();
@@ -142,19 +138,6 @@ const PaymentMethodCard = ({
description: "Not connected",
};
- const addPaymentMethod = async (method: string) => {
- setIsLoading(true);
- const { url } = await fetch(
- `/api/workspaces/${slug}/billing/payment-methods`,
- {
- method: "POST",
- body: JSON.stringify({ method }),
- },
- ).then((res) => res.json());
-
- router.push(url);
- };
-
return (
<>
{AddPayoutMethodModal}
@@ -189,7 +172,6 @@ const PaymentMethodCard = ({
className="h-9 w-fit"
text="Connect"
onClick={() => setShowAddPayoutMethodModal(true)}
- loading={isLoading}
/>
)}
</div>
diff --git a/apps/web/ui/modals/add-payout-method-modal.tsx b/apps/web/ui/modals/add-payout-method-modal.tsx
index c4ecb31102..49b13d552d 100644
--- a/apps/web/ui/modals/add-payout-method-modal.tsx
+++ b/apps/web/ui/modals/add-payout-method-modal.tsx
@@ -1,10 +1,24 @@
"use client";
+import useWorkspace from "@/lib/swr/use-workspace";
import { X } from "@/ui/shared/icons";
import { AnimatedSizeContainer, GreekTemple, Modal } from "@dub/ui";
+import { useRouter } from "next/navigation";
import { CSSProperties, Dispatch, SetStateAction, useState } from "react";
+import { toast } from "sonner";
-const PAYMENT_PROCESSORS = [
+type PaymentMethods =
+ | "us_bank_account"
+ | "acss_debit"
+ | "sepa_debit"
+ | "au_becs_debit";
+
+const PAYMENT_METHODS: {
+ id: PaymentMethods;
+ location: string;
+ method: string;
+ icon: string;
+}[] = [
{
id: "us_bank_account",
location: "US",
@@ -21,14 +35,14 @@ const PAYMENT_PROCESSORS = [
id: "sepa_debit",
location: "EU",
method: "SEPA Debit",
- icon: "https://flag.vercel.app/m/US.svg",
- },
- {
- id: "au_becs_debit",
- location: "AUS",
- method: "AU BECS Debit",
- icon: "https://flag.vercel.app/m/AU.svg",
+ icon: "https://flag.vercel.app/m/US.svg", // TODO: update the flag icon
},
+ // {
+ // id: "au_becs_debit",
+ // location: "AUS",
+ // method: "AU BECS Debit",
+ // icon: "https://flag.vercel.app/m/AU.svg",
+ // },
];
function AddPayoutMethodModal({
@@ -42,7 +56,7 @@ function AddPayoutMethodModal({
<Modal
showModal={showAddPayoutMethodModal}
setShowModal={setShowAddPayoutMethodModal}
- className="max-h-[calc(100dvh-100px)] max-w-2xl"
+ className="max-h-[calc(100dvh-100px)] max-w-xl"
>
<AddPayoutMethodModalInner
setShowAddPayoutMethodModal={setShowAddPayoutMethodModal}
@@ -56,9 +70,31 @@ function AddPayoutMethodModalInner({
}: {
setShowAddPayoutMethodModal: Dispatch<SetStateAction<boolean>>;
}) {
- const [paymentProcessorIndex, setPaymentProcessorIndex] = useState<
- number | null
- >(null);
+ const router = useRouter();
+ const { slug } = useWorkspace();
+ const [isLoading, setIsLoading] = useState(false);
+
+ const addPaymentMethod = async (method: PaymentMethods) => {
+ setIsLoading(true);
+
+ const response = await fetch(
+ `/api/workspaces/${slug}/billing/payment-methods`,
+ {
+ method: "POST",
+ body: JSON.stringify({ method }),
+ },
+ );
+
+ if (!response.ok) {
+ setIsLoading(false);
+ toast.error("Failed to add payment method. Please try again.");
+ return;
+ }
+
+ const data = (await response.json()) as { url: string };
+
+ router.push(data.url);
+ };
return (
<AnimatedSizeContainer
@@ -92,19 +128,18 @@ function AddPayoutMethodModalInner({
className="grid grid-cols-2 gap-4 sm:grid-cols-[repeat(var(--cols),minmax(0,1fr))]"
style={
{
- "--cols": PAYMENT_PROCESSORS.length,
+ "--cols": PAYMENT_METHODS.length,
} as CSSProperties
}
>
- {PAYMENT_PROCESSORS.map(
- ({ icon: Icon, location, method }, index) => (
+ {PAYMENT_METHODS.map(
+ ({ id, icon: Icon, location, method }, index) => (
<button
key={index}
type="button"
className="group flex flex-col items-center gap-4 rounded-lg bg-neutral-200/40 p-8 px-2 py-4 transition-colors duration-100 hover:bg-neutral-200/60"
- onClick={() => {
- setPaymentProcessorIndex(index);
- }}
+ onClick={() => addPaymentMethod(id)}
+ disabled={isLoading}
>
<img
src={Icon}