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.

select payment method

Kiran K624950e14d7a
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/payouts/payout-stats.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/payouts/payout-stats.tsx
index af403a1efa..1713490f77 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/payouts/payout-stats.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/payouts/payout-stats.tsx
@@ -25,6 +25,8 @@ export function PayoutStats() {
(p) => p.status === PayoutStatus.completed,
);
+ const confirmButtonDisabled = pendingPayouts?.amount === 0;
+
return (
<>
{payoutInvoiceSheet}
@@ -38,8 +40,12 @@ export function PayoutStats() {
text="Confirm payouts"
className="h-7 w-fit px-2"
onClick={() => setIsOpen(true)}
- disabled={pendingPayouts?.amount === 0}
- disabledTooltip="You have no pending payouts."
+ disabled={confirmButtonDisabled}
+ disabledTooltip={
+ confirmButtonDisabled
+ ? "You have no pending payouts."
+ : undefined
+ }
/>
</div>
<div className="mt-2 text-2xl text-neutral-800">
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/billing/payment-method-types.ts b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/billing/payment-method-types.ts
new file mode 100644
index 0000000000..29d0b64ae4
--- /dev/null
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/billing/payment-method-types.ts
@@ -0,0 +1,37 @@
+"use client";
+
+import { CreditCard, GreekTemple, StripeLink } from "@dub/ui/icons";
+import { Stripe } from "stripe";
+
+export const PaymentMethodTypesList = (paymentMethod: Stripe.PaymentMethod) =>
+ [
+ {
+ type: "card",
+ title: "Card",
+ icon: CreditCard,
+ description: paymentMethod.card
+ ? `Connected ${paymentMethod.card.brand} ***${paymentMethod.card.last4}`
+ : "No card connected",
+ },
+ {
+ type: "us_bank_account",
+ title: "ACH",
+ icon: GreekTemple,
+ description: paymentMethod.us_bank_account
+ ? `Connected ${paymentMethod.us_bank_account.account_holder_type} account ending in ${paymentMethod.us_bank_account.last4}`
+ : "No ACH Debit connected",
+ },
+ {
+ type: "link",
+ title: "Link",
+ icon: StripeLink,
+ iconBgColor: "bg-green-100",
+ description: `Connected Link account ${paymentMethod.link?.email}`,
+ },
+ ] satisfies {
+ type: Stripe.PaymentMethod.Type;
+ title: string;
+ icon: React.ElementType;
+ description: string;
+ iconBgColor?: string;
+ }[];
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/billing/payment-methods.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/billing/payment-methods.tsx
index 668fbc4488..8d4fb317ef 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/billing/payment-methods.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/billing/payment-methods.tsx
@@ -1,24 +1,21 @@
"use client";
+import usePaymentMethods from "@/lib/swr/use-payment-methods";
import useWorkspace from "@/lib/swr/use-workspace";
import { AnimatedEmptyState } from "@/ui/shared/animated-empty-state";
import { Button, TooltipContent } from "@dub/ui";
-import { CreditCard, GreekTemple, StripeLink } from "@dub/ui/icons";
-import { cn, fetcher } from "@dub/utils";
+import { CreditCard } from "@dub/ui/icons";
+import { cn } from "@dub/utils";
import { useRouter } from "next/navigation";
import { useState } from "react";
import { Stripe } from "stripe";
-import useSWR from "swr";
+import { PaymentMethodTypesList } from "./payment-method-types";
export default function PaymentMethods() {
- const { slug, stripeId } = useWorkspace();
- const { data: paymentMethods } = useSWR<Stripe.PaymentMethod[]>(
- slug && `/api/workspaces/${slug}/billing/payment-methods`,
- fetcher,
- );
-
const router = useRouter();
const [isLoading, setIsLoading] = useState(false);
+ const { slug, stripeId } = useWorkspace();
+ const { paymentMethods } = usePaymentMethods();
const managePaymentMethods = async () => {
setIsLoading(true);
@@ -93,39 +90,6 @@ export default function PaymentMethods() {
);
}
-const PaymentMethodTypesList = (paymentMethod: Stripe.PaymentMethod) =>
- [
- {
- type: "card",
- title: "Card",
- icon: CreditCard,
- description: paymentMethod.card
- ? `Connected ${paymentMethod.card.brand} ***${paymentMethod.card.last4}`
- : "No card connected",
- },
- {
- type: "us_bank_account",
- title: "ACH",
- icon: GreekTemple,
- description: paymentMethod.us_bank_account
- ? `Connected ${paymentMethod.us_bank_account.account_holder_type} account ending in ${paymentMethod.us_bank_account.last4}`
- : "No ACH Debit connected",
- },
- {
- type: "link",
- title: "Link",
- icon: StripeLink,
- iconBgColor: "bg-green-100",
- description: `Connected Link account ${paymentMethod.link?.email}`,
- },
- ] satisfies {
- type: Stripe.PaymentMethod.Type;
- title: string;
- icon: React.ElementType;
- description: string;
- iconBgColor?: string;
- }[];
-
const PaymentMethodCard = ({
paymentMethod,
}: {
diff --git a/apps/web/lib/actions/partners/confirm-payouts.ts b/apps/web/lib/actions/partners/confirm-payouts.ts
index d606be78da..df2eeab6eb 100644
--- a/apps/web/lib/actions/partners/confirm-payouts.ts
+++ b/apps/web/lib/actions/partners/confirm-payouts.ts
@@ -3,19 +3,21 @@
import { getProgramOrThrow } from "@/lib/api/programs/get-program";
import { createId } from "@/lib/api/utils";
import { qstash } from "@/lib/cron";
+import { stripe } from "@/lib/stripe";
import { prisma } from "@dub/prisma";
import { APP_DOMAIN_WITH_NGROK } from "@dub/utils";
import { waitUntil } from "@vercel/functions";
+import Stripe from "stripe";
import z from "zod";
import { authActionClient } from "../safe-action";
// TODO:
-// Fix `paymentMethodId`
// Fix `fee`
const confirmPayoutsSchema = z.object({
workspaceId: z.string(),
programId: z.string(),
+ paymentMethodId: z.string().min(1, "Please select a payment method."),
});
// Confirm payouts
@@ -23,13 +25,25 @@ export const confirmPayoutsAction = authActionClient
.schema(confirmPayoutsSchema)
.action(async ({ parsedInput, ctx }) => {
const { workspace } = ctx;
- const { programId } = parsedInput;
+ const { programId, paymentMethodId } = parsedInput;
await getProgramOrThrow({
workspaceId: workspace.id,
programId,
});
+ // Doing some check to make sure the payment method is valid
+ const paymentMethod = await stripe.paymentMethods.retrieve(
+ paymentMethodId,
+ { expand: ["customer"] },
+ );
+
+ const stripeCustomer = paymentMethod.customer as Stripe.Customer;
+
+ if (stripeCustomer.id !== workspace.stripeId) {
+ throw new Error("The payment method is not valid for this workspace.");
+ }
+
const result = await prisma.$transaction(async (tx) => {
// Find the pending payouts for the program
const payouts = await tx.payout.findMany({
@@ -60,7 +74,7 @@ export const confirmPayoutsAction = authActionClient
data: {
id: createId({ prefix: "inv_" }),
programId,
- paymentMethodId: "pm_1QVOigAlJJEpqkPV6Y3Jc1hs", // TODO: Fix this
+ paymentMethodId,
amount,
fee,
total,
diff --git a/apps/web/lib/swr/use-payment-methods.ts b/apps/web/lib/swr/use-payment-methods.ts
new file mode 100644
index 0000000000..9890cf9ffb
--- /dev/null
+++ b/apps/web/lib/swr/use-payment-methods.ts
@@ -0,0 +1,20 @@
+import { fetcher } from "@dub/utils";
+import Stripe from "stripe";
+import useSWR from "swr";
+import useWorkspace from "./use-workspace";
+
+// Returns the Stripe payment methods for the business
+export default function usePaymentMethods() {
+ const { slug } = useWorkspace();
+
+ const { data, isLoading, error } = useSWR<Stripe.PaymentMethod[]>(
+ slug && `/api/workspaces/${slug}/billing/payment-methods`,
+ fetcher,
+ );
+
+ return {
+ paymentMethods: data || [],
+ error,
+ loading: isLoading && !error && !data,
+ };
+}
diff --git a/apps/web/ui/partners/payout-invoice-sheet.tsx b/apps/web/ui/partners/payout-invoice-sheet.tsx
index fce7b0acdc..c426de3743 100644
--- a/apps/web/ui/partners/payout-invoice-sheet.tsx
+++ b/apps/web/ui/partners/payout-invoice-sheet.tsx
@@ -1,12 +1,15 @@
import { confirmPayoutsAction } from "@/lib/actions/partners/confirm-payouts";
+import usePaymentMethods from "@/lib/swr/use-payment-methods";
import usePayouts from "@/lib/swr/use-payouts";
import useWorkspace from "@/lib/swr/use-workspace";
import { X } from "@/ui/shared/icons";
import {
Button,
buttonVariants,
+ Gear,
Sheet,
Table,
+ Tooltip,
useRouterStuff,
useTable,
} from "@dub/ui";
@@ -14,7 +17,14 @@ import { cn, currencyFormatter, DICEBEAR_AVATAR_URL } from "@dub/utils";
import { useAction } from "next-safe-action/hooks";
import Link from "next/link";
import { useParams } from "next/navigation";
-import { Dispatch, Fragment, SetStateAction, useMemo, useState } from "react";
+import {
+ Dispatch,
+ Fragment,
+ SetStateAction,
+ useEffect,
+ useMemo,
+ useState,
+} from "react";
import { toast } from "sonner";
import { SaleRowMenu } from "./sale-row-menu";
@@ -25,10 +35,12 @@ interface PayoutInvoiceSheetProps {
// TODO:
// Fix the fee calculation
// Fix the table menus + View all
+// Payment method dropdown UI need fixing
function PayoutInvoiceSheetContent({ setIsOpen }: PayoutInvoiceSheetProps) {
const { id: workspaceId, slug } = useWorkspace();
const { programId } = useParams<{ programId: string }>();
+ const [paymentMethodId, setPaymentMethodId] = useState<string | null>(null);
const {
payouts,
@@ -40,6 +52,17 @@ function PayoutInvoiceSheetContent({ setIsOpen }: PayoutInvoiceSheetProps) {
},
});
+ const { paymentMethods, loading: paymentMethodsLoading } =
+ usePaymentMethods();
+
+ useEffect(() => {
+ if (!paymentMethods.length) {
+ return;
+ }
+
+ setPaymentMethodId(paymentMethods[0].id);
+ }, [paymentMethods]);
+
const { executeAsync, isExecuting } = useAction(confirmPayoutsAction, {
onSuccess: async () => {
toast.success("Payouts confirmed successfully! We'll process them soon.");
@@ -57,7 +80,38 @@ function PayoutInvoiceSheetContent({ setIsOpen }: PayoutInvoiceSheetProps) {
const total = amount + fee;
return {
- Method: "-",
+ Method: (
+ <div className="flex items-center gap-2">
+ {paymentMethodsLoading ? (
+ <div className="h-7 w-full animate-pulse rounded-md border-neutral-300 bg-neutral-100"></div>
+ ) : (
+ <select
+ onChange={(e) => setPaymentMethodId(e.target.value)}
+ defaultValue={paymentMethodId || ""}
+ className="block h-8 w-full rounded-md border-neutral-300 text-neutral-900 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm"
+ >
+ {paymentMethods.map((method) => (
+ <option key={method.id} value={method.id}>
+ {method.type}
+ </option>
+ ))}
+ </select>
+ )}
+
+ <Tooltip content="Manage payment methods">
+ <Link
+ href={`/${slug}/settings/billing`}
+ className={cn(
+ buttonVariants({ variant: "secondary" }),
+ "flex h-8 items-center rounded-md border px-2 text-sm",
+ )}
+ target="_blank"
+ >
+ <Gear className="size-4" />
+ </Link>
+ </Tooltip>
+ </div>
+ ),
Amount: currencyFormatter(amount / 100, {
minimumFractionDigits: 2,
@@ -74,7 +128,7 @@ function PayoutInvoiceSheetContent({ setIsOpen }: PayoutInvoiceSheetProps) {
maximumFractionDigits: 2,
}),
};
- }, [payouts]);
+ }, [payouts, paymentMethods, paymentMethodsLoading]);
const table = useTable({
data: payouts || [],
@@ -193,9 +247,15 @@ function PayoutInvoiceSheetContent({ setIsOpen }: PayoutInvoiceSheetProps) {
return;
}
+ if (!paymentMethodId) {
+ toast.error("Please select a payment method.");
+ return;
+ }
+
await executeAsync({
workspaceId,
programId,
+ paymentMethodId,
});
}}
text="Confirm payout"