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.

Let companies choose between the their existing payment methods.

Kiran Kf87bf8928edd
diff --git a/apps/web/lib/actions/partners/confirm-payouts.ts b/apps/web/lib/actions/partners/confirm-payouts.ts
index 4ca34ce3e5..5bcabb9fa8 100644
--- a/apps/web/lib/actions/partners/confirm-payouts.ts
+++ b/apps/web/lib/actions/partners/confirm-payouts.ts
@@ -14,6 +14,7 @@ import { authActionClient } from "../safe-action";
const confirmPayoutsSchema = z.object({
workspaceId: z.string(),
programId: z.string(),
+ payoutMethodId: z.string(),
payoutIds: z.array(z.string()).min(1),
});
@@ -22,7 +23,7 @@ export const confirmPayoutsAction = authActionClient
.schema(confirmPayoutsSchema)
.action(async ({ parsedInput, ctx }) => {
const { workspace } = ctx;
- const { programId, payoutIds } = parsedInput;
+ const { programId, payoutMethodId, payoutIds } = parsedInput;
await getProgramOrThrow({
workspaceId: workspace.id,
@@ -33,8 +34,11 @@ export const confirmPayoutsAction = authActionClient
throw new Error("Workspace does not have a valid Stripe ID.");
}
- if (!workspace.payoutMethodId) {
- throw new Error("Workspace does not have a valid payout method.");
+ // Check the payout method is valid
+ const payoutMethod = await stripe.paymentMethods.retrieve(payoutMethodId);
+
+ if (payoutMethod.customer !== workspace.stripeId) {
+ throw new Error("Invalid payout method.");
}
const payouts = await prisma.payout.findMany({
diff --git a/apps/web/ui/partners/payout-invoice-sheet.tsx b/apps/web/ui/partners/payout-invoice-sheet.tsx
index 63f15722ad..1ab82d8cc0 100644
--- a/apps/web/ui/partners/payout-invoice-sheet.tsx
+++ b/apps/web/ui/partners/payout-invoice-sheet.tsx
@@ -3,17 +3,37 @@ import {
DUB_PARTNERS_PAYOUT_FEE,
MIN_PAYOUT_AMOUNT,
} from "@/lib/partners/constants";
+import usePaymentMethods from "@/lib/swr/use-payment-methods";
import usePayouts from "@/lib/swr/use-payouts";
import useWorkspace from "@/lib/swr/use-workspace";
import { PayoutResponse } from "@/lib/types";
import { X } from "@/ui/shared/icons";
-import { Button, Sheet, Table, useRouterStuff, useTable } from "@dub/ui";
+import {
+ Button,
+ buttonVariants,
+ CreditCard,
+ Gear,
+ GreekTemple,
+ Sheet,
+ Table,
+ useRouterStuff,
+ useTable,
+} from "@dub/ui";
import { cn, currencyFormatter, DICEBEAR_AVATAR_URL } from "@dub/utils";
+import { capitalize } from "@dub/utils/src/functions";
import { Row } from "@tanstack/react-table";
import { useAction } from "next-safe-action/hooks";
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 Stripe from "stripe";
interface PayoutInvoiceSheetProps {
setIsOpen: Dispatch<SetStateAction<boolean>>;
@@ -22,7 +42,10 @@ interface PayoutInvoiceSheetProps {
function PayoutInvoiceSheetContent({ setIsOpen }: PayoutInvoiceSheetProps) {
const { id: workspaceId, slug } = useWorkspace();
const { programId } = useParams<{ programId: string }>();
+ const { paymentMethods, loading: paymentMethodsLoading } =
+ usePaymentMethods();
const [selectedPayouts, setSelectedPayouts] = useState<PayoutResponse[]>([]);
+ const [payoutMethodId, setPayoutMethodId] = useState<string | null>(null);
const {
payouts,
@@ -34,6 +57,12 @@ function PayoutInvoiceSheetContent({ setIsOpen }: PayoutInvoiceSheetProps) {
},
});
+ useEffect(() => {
+ if (paymentMethods && paymentMethods.length > 0 && !payoutMethodId) {
+ setPayoutMethodId(paymentMethods[0].id);
+ }
+ }, [paymentMethods, payoutMethodId]);
+
const { executeAsync, isExecuting } = useAction(confirmPayoutsAction, {
onSuccess: async () => {
toast.success(
@@ -59,8 +88,35 @@ function PayoutInvoiceSheetContent({ setIsOpen }: PayoutInvoiceSheetProps) {
);
const invoiceData = useMemo(() => {
+ const paymentMethodOption = (paymentMethod: Stripe.PaymentMethod) => {
+ const paymentMethods = {
+ card: {
+ title: `${capitalize(paymentMethod.card?.brand)} **** ${paymentMethod.card?.last4}`,
+ icon: CreditCard,
+ },
+ us_bank_account: {
+ title: `ACH **** ${paymentMethod.us_bank_account?.last4}`,
+ icon: GreekTemple,
+ },
+ };
+
+ const { title, icon: Icon } = paymentMethods[paymentMethod.type];
+
+ return (
+ <div className="flex items-center gap-2">
+ <span className="text-xs text-neutral-500">
+ <Icon className="size-4" />
+ </span>
+ <span>{title}</span>
+ </div>
+ );
+ };
+
if (!selectedPayouts) {
return {
+ Method: (
+ <div className="h-4 w-24 animate-pulse rounded-md bg-neutral-200" />
+ ),
Amount: (
<div className="h-4 w-24 animate-pulse rounded-md bg-neutral-200" />
),
@@ -81,6 +137,36 @@ function PayoutInvoiceSheetContent({ setIsOpen }: PayoutInvoiceSheetProps) {
const total = amount + fee;
return {
+ Method: (
+ <div className="flex items-center gap-2 pr-6">
+ {paymentMethodsLoading ? (
+ <div className="h-[30px] w-full animate-pulse rounded-md bg-neutral-200" />
+ ) : (
+ <select
+ className="h-auto flex-1 rounded-md border border-neutral-200 py-1.5 text-xs focus:border-neutral-600 focus:ring-neutral-600"
+ value={payoutMethodId || ""}
+ onChange={(e) => setPayoutMethodId(e.target.value)}
+ >
+ {paymentMethods?.map((pm) => (
+ <option key={pm.id} value={pm.id}>
+ {paymentMethodOption(pm)}
+ </option>
+ ))}
+ </select>
+ )}
+ <a
+ href={`/${slug}/settings/billing`}
+ className={cn(
+ buttonVariants({ variant: "secondary" }),
+ "flex items-center rounded-md border border-neutral-200 px-2 py-1.5 text-sm",
+ )}
+ target="_blank"
+ >
+ <Gear className="size-4" />
+ </a>
+ </div>
+ ),
+
Amount: currencyFormatter(amount / 100, {
minimumFractionDigits: 2,
maximumFractionDigits: 2,
@@ -96,7 +182,7 @@ function PayoutInvoiceSheetContent({ setIsOpen }: PayoutInvoiceSheetProps) {
maximumFractionDigits: 2,
}),
};
- }, [selectedPayouts]);
+ }, [selectedPayouts, paymentMethods]);
const table = useTable({
data: pendingPayouts || [],
@@ -160,14 +246,9 @@ function PayoutInvoiceSheetContent({ setIsOpen }: PayoutInvoiceSheetProps) {
error: payoutsError
? "Failed to load payouts for this invoice."
: undefined,
-
getRowId: (originalRow: PayoutResponse) => originalRow.id,
onRowSelectionChange: (rows: Row<PayoutResponse>[]) =>
setSelectedPayouts(rows.map((row) => row.original)),
- selectedRows: selectedPayouts?.reduce((acc, payout) => {
- acc[payout.id] = true;
- return acc;
- }, {}),
} as any);
return (
@@ -219,13 +300,19 @@ function PayoutInvoiceSheetContent({ setIsOpen }: PayoutInvoiceSheetProps) {
variant="primary"
loading={isExecuting}
onClick={async () => {
- if (!workspaceId || !programId || !selectedPayouts.length) {
+ if (
+ !workspaceId ||
+ !programId ||
+ !selectedPayouts.length ||
+ !payoutMethodId
+ ) {
return;
}
await executeAsync({
workspaceId,
programId,
+ payoutMethodId,
payoutIds: selectedPayouts.map((p) => p.id),
});
}}