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.

Merge branch 'main' into bank-debits

Kiran Ka50132a7f391
diff --cc apps/web/app/(ee)/api/cron/payouts/confirm/confirm-payouts.ts
index 6bbadfcc7e,030f4bb1bf..80caf8e342
--- a/apps/web/app/(ee)/api/cron/payouts/confirm/confirm-payouts.ts
+++ b/apps/web/app/(ee)/api/cron/payouts/confirm/confirm-payouts.ts
@@@ -1,5 -1,9 +1,9 @@@
import { createId } from "@/lib/api/create-id";
-import { PAYOUT_FEES } from "@/lib/partners/constants";
+import { PAYMENT_METHOD_TYPES, PAYOUT_FEES } from "@/lib/partners/constants";
+ import {
+ CUTOFF_PERIOD,
+ CUTOFF_PERIOD_TYPES,
+ } from "@/lib/partners/cutoff-period";
import { stripe } from "@/lib/stripe";
import { sendEmail } from "@dub/email";
import PartnerPayoutConfirmed from "@dub/email/templates/partner-payout-confirmed";
diff --cc apps/web/lib/actions/partners/confirm-payouts.ts
index 789d394dd7,c42034d0dd..28c459bc55
--- a/apps/web/lib/actions/partners/confirm-payouts.ts
+++ b/apps/web/lib/actions/partners/confirm-payouts.ts
@@@ -2,7 -2,7 +2,8 @@@
import { getDefaultProgramIdOrThrow } from "@/lib/api/programs/get-default-program-id-or-throw";
import { qstash } from "@/lib/cron";
+import { PAYMENT_METHOD_TYPES } from "@/lib/partners/constants";
+ import { CUTOFF_PERIOD_ENUM } from "@/lib/partners/cutoff-period";
import { stripe } from "@/lib/stripe";
import { APP_DOMAIN_WITH_NGROK } from "@dub/utils";
import z from "zod";
@@@ -11,9 -11,11 +12,9 @@@ import { authActionClient } from "../sa
const confirmPayoutsSchema = z.object({
workspaceId: z.string(),
paymentMethodId: z.string(),
- excludeCurrentMonth: z.boolean().optional().default(false),
+ cutoffPeriod: CUTOFF_PERIOD_ENUM,
});
-const allowedPaymentMethods = ["us_bank_account", "card", "link"];
-
// Confirm payouts
export const confirmPayoutsAction = authActionClient
.schema(confirmPayoutsSchema)
diff --cc apps/web/ui/partners/payout-invoice-sheet.tsx
index 4a186167ac,49b8ef6624..e891bbe4cb
--- a/apps/web/ui/partners/payout-invoice-sheet.tsx
+++ b/apps/web/ui/partners/payout-invoice-sheet.tsx
@@@ -1,6 -1,9 +1,10 @@@
import { confirmPayoutsAction } from "@/lib/actions/partners/confirm-payouts";
-import { PAYOUT_FEES } from "@/lib/partners/constants";
+import { DIRECT_DEBIT_PAYMENT_METHOD_TYPES } from "@/lib/partners/constants";
+ import {
+ CUTOFF_PERIOD,
+ CUTOFF_PERIOD_TYPES,
+ } from "@/lib/partners/cutoff-period";
+import { calculatePayoutFee } from "@/lib/payment-methods";
import { mutatePrefix } from "@/lib/swr/mutate";
import usePaymentMethods from "@/lib/swr/use-payment-methods";
import useWorkspace from "@/lib/swr/use-workspace";
@@@ -31,59 -34,49 +35,61 @@@ import
import { useAction } from "next-safe-action/hooks";
import { Fragment, useEffect, useMemo, useState } from "react";
import { toast } from "sonner";
+import Stripe from "stripe";
import useSWR from "swr";
+const PAYMENT_METHODS = Object.freeze({
+ link: {
+ label: "link",
+ type: "link",
+ icon: CreditCard,
+ duration: "Instantly",
+ },
+ card: {
+ label: "card",
+ type: "card",
+ icon: CreditCard,
+ duration: "Instantly",
+ },
+ us_bank_account: {
+ label: "ACH",
+ type: "us_bank_account",
+ icon: GreekTemple,
+ duration: "4 business days",
+ },
+ acss_debit: {
+ label: "ACSS Debit",
+ type: "acss_debit",
+ icon: GreekTemple,
+ duration: "5 business days",
+ },
+ sepa_debit: {
+ label: "SEPA Debit",
+ type: "sepa_debit",
+ icon: GreekTemple,
+ duration: "5 business days",
+ },
+});
+
+type SelectPaymentMethod =
+ (typeof PAYMENT_METHODS)[keyof typeof PAYMENT_METHODS] & {
+ id: string;
+ fee: number;
+ };
+
function PayoutInvoiceSheetContent() {
- const { id: workspaceId, slug, plan, defaultProgramId } = useWorkspace();
const { queryParams } = useRouterStuff();
- const [excludeCurrentMonth, setExcludeCurrentMonth] = useState(false);
+ const { id: workspaceId, slug, plan, defaultProgramId } = useWorkspace();
+
const { paymentMethods, loading: paymentMethodsLoading } =
usePaymentMethods();
- const paymentMethodsTypes = Object.freeze({
- link: {
- label: "link",
- type: "link",
- icon: CreditCard,
- fee: PAYOUT_FEES[plan?.split(" ")[0] ?? "business"].card,
- duration: "Instantly",
- },
- card: {
- label: "card",
- type: "card",
- icon: CreditCard,
- fee: PAYOUT_FEES[plan?.split(" ")[0] ?? "business"].card,
- duration: "Instantly",
- },
- us_bank_account: {
- label: "ACH",
- type: "us_bank_account",
- icon: GreekTemple,
- fee: PAYOUT_FEES[plan?.split(" ")[0] ?? "business"].ach,
- duration: "4 business days",
- },
- });
-
- type PaymentMethodWithFee =
- (typeof paymentMethodsTypes)[keyof typeof paymentMethodsTypes] & {
- id: string;
- };
-
const [selectedPaymentMethod, setSelectedPaymentMethod] =
- useState<PaymentMethodWithFee | null>(null);
+ useState<SelectPaymentMethod | null>(null);
+ const [cutoffPeriod, setCutoffPeriod] =
+ useState<CUTOFF_PERIOD_TYPES>("today");
+
const {
data: eligiblePayouts,
error: eligiblePayoutsError,
@@@ -111,54 -104,44 +117,58 @@@
},
});
- // Set the first payment method as the selected payment method
- useEffect(() => {
- if (!paymentMethods || !paymentMethods.length) {
- return;
- }
+ const finalPaymentMethods = useMemo(
+ () =>
+ paymentMethods?.map((pm) => {
+ const paymentMethod = PAYMENT_METHODS[pm.type];
- if (!selectedPaymentMethod) {
- const firstPaymentMethod = paymentMethods[0];
- setSelectedPaymentMethod({
- ...paymentMethodsTypes[firstPaymentMethod.type],
- id: firstPaymentMethod.id,
- });
- }
- }, [paymentMethods, selectedPaymentMethod]);
+ const base = {
+ ...paymentMethod,
+ id: pm.id,
+ fee: calculatePayoutFee(pm.type, plan),
+ };
- const paymentMethodsWithFee = useMemo(
- () =>
- paymentMethods?.map((pm) => ({
- ...paymentMethodsTypes[pm.type],
- id: pm.id,
- title: pm.link
- ? `Link – ${truncate(pm.link.email, 24)}`
- : pm.card
- ? `${capitalize(pm.card?.brand)} **** ${pm.card?.last4}`
- : `ACH **** ${pm.us_bank_account?.last4}`,
- })),
- [paymentMethods],
+ if (pm.link) {
+ return {
+ ...base,
+ title: `Link – ${truncate(pm.link.email, 16)}`,
+ };
+ }
+
+ if (pm.card) {
+ return {
+ ...base,
+ title: `${capitalize(pm.card.brand)} **** ${pm.card.last4}`,
+ };
+ }
+
+ return {
+ ...base,
+ title: `${paymentMethod.label} **** ${pm[paymentMethod.type]?.last4}`,
+ };
+ }),
+ [paymentMethods, plan],
);
+ useEffect(() => {
+ if (
+ !selectedPaymentMethod &&
+ finalPaymentMethods &&
+ finalPaymentMethods.length > 0
+ ) {
+ setSelectedPaymentMethod(finalPaymentMethods[0]);
+ }
+ }, [finalPaymentMethods, selectedPaymentMethod]);
+
- const invoiceData = useMemo(() => {
- const amount = eligiblePayouts?.reduce((acc, payout) => {
- return acc + payout.amount;
- }, 0);
+ const amount = useMemo(
+ () =>
+ eligiblePayouts?.reduce((acc, payout) => {
+ return acc + payout.amount;
+ }, 0),
+ [eligiblePayouts],
+ );
+ const invoiceData = useMemo(() => {
const fee =
amount === undefined
? undefined
@@@ -166,100 -149,116 +176,116 @@@
const total =
amount !== undefined && fee !== undefined ? amount + fee : undefined;
- 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={selectedPaymentMethod?.id || ""}
- onChange={(e) =>
- setSelectedPaymentMethod(
- finalPaymentMethods?.find((pm) => pm.id === e.target.value) ||
- null,
- )
- }
- >
- {finalPaymentMethods?.map(({ id, title }) => (
- <option key={id} value={id}>
- {title}
- </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",
+ return [
+ {
+ key: "Method",
+ value: (
+ <div className="flex items-center gap-2 pr-6">
+ {paymentMethodsLoading ? (
+ <div className="h-[26px] w-40 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={selectedPaymentMethod?.id || ""}
+ onChange={(e) =>
+ setSelectedPaymentMethod(
- paymentMethodsWithFee?.find(
++ finalPaymentMethods?.find(
+ (pm) => pm.id === e.target.value,
+ ) || null,
+ )
+ }
+ >
- {paymentMethodsWithFee?.map(({ id, title }) => (
++ {finalPaymentMethods?.map(({ id, title }) => (
+ <option key={id} value={id}>
+ {title}
+ </option>
+ ))}
+ </select>
)}
- target="_blank"
- >
- <Gear className="size-4" />
- </a>
- </div>
- ),
-
- Amount:
- amount === undefined ? (
- <div className="h-4 w-24 animate-pulse rounded-md bg-neutral-200" />
- ) : (
- currencyFormatter(amount / 100, {
- minimumFractionDigits: 2,
- maximumFractionDigits: 2,
- })
+ <a
+ href={`/${slug}/settings/billing`}
+ className={cn(
+ buttonVariants({ variant: "secondary" }),
+ "flex items-center rounded-md border border-neutral-200 p-1.5 text-sm",
+ )}
+ target="_blank"
+ >
+ <Gear className="size-4" />
+ </a>
+ </div>
),
-
- Fee:
- selectedPaymentMethod && fee !== undefined ? (
- <Tooltip
- content={
- <SimpleTooltipContent
- title={`${Math.round(selectedPaymentMethod.fee * 100)}% processing fee. ${!DIRECT_DEBIT_PAYMENT_METHOD_TYPES.includes(selectedPaymentMethod.type as Stripe.PaymentMethod.Type) ? " Switch to Direct Debit for a reduced fee." : ""}`}
- cta="Learn more"
- href="https://d.to/payouts"
- />
- }
+ },
+ {
+ key: "Cutoff Period",
+ value: (
+ <select
+ value={cutoffPeriod}
+ className="h-auto w-fit rounded-md border border-neutral-200 py-1 text-xs focus:border-neutral-600 focus:ring-neutral-600"
+ onChange={(e) => setCutoffPeriod(e.target.value)}
>
- <span className="underline decoration-dotted underline-offset-2">
- {currencyFormatter(fee / 100, {
- minimumFractionDigits: 2,
- maximumFractionDigits: 2,
- })}
- </span>
- </Tooltip>
- ) : (
- <div className="h-4 w-24 animate-pulse rounded-md bg-neutral-200" />
+ {CUTOFF_PERIOD.map(({ id, label, value }) => (
+ <option key={id} value={id}>
+ {label} ({formatDate(value)})
+ </option>
+ ))}
+ </select>
),
-
- "Transfer Time": (
- <div>
- {selectedPaymentMethod ? (
- selectedPaymentMethod.duration
+ tooltipContent:
+ "Cutoff period in UTC. If set, only commissions accrued up to the cutoff period will be included in the payout invoice.",
+ },
+ {
+ key: "Amount",
+ value:
+ amount === undefined ? (
+ <div className="h-4 w-24 animate-pulse rounded-md bg-neutral-200" />
+ ) : (
+ currencyFormatter(amount / 100, {
+ minimumFractionDigits: 2,
+ maximumFractionDigits: 2,
+ })
+ ),
+ },
+ {
+ key: "Fee",
+ value:
+ selectedPaymentMethod && fee !== undefined ? (
+ currencyFormatter(fee / 100, {
+ minimumFractionDigits: 2,
+ maximumFractionDigits: 2,
+ })
) : (
<div className="h-4 w-24 animate-pulse rounded-md bg-neutral-200" />
- )}
- </div>
- ),
-
- Total:
- total === undefined ? (
- <div className="h-4 w-24 animate-pulse rounded-md bg-neutral-200" />
+ ),
+ tooltipContent: selectedPaymentMethod ? (
+ <SimpleTooltipContent
- title={`${Math.round(selectedPaymentMethod.fee * 100)}% processing fee.${selectedPaymentMethod.type !== "us_bank_account" ? " Switch to ACH for a reduced fee." : ""}`}
++ title={`${Math.round(selectedPaymentMethod.fee * 100)}% processing fee. ${!DIRECT_DEBIT_PAYMENT_METHOD_TYPES.includes(selectedPaymentMethod.type as Stripe.PaymentMethod.Type) ? " Switch to Direct Debit for a reduced fee." : ""}`}
+ cta="Learn more"
+ href="https://d.to/payouts"
+ />
+ ) : undefined,
+ },
+ {
+ key: "Transfer Time",
+ value: selectedPaymentMethod ? (
+ selectedPaymentMethod.duration
) : (
- currencyFormatter(total / 100, {
- minimumFractionDigits: 2,
- maximumFractionDigits: 2,
- })
+ <div className="h-4 w-24 animate-pulse rounded-md bg-neutral-200" />
),
- };
- }, [
- eligiblePayouts,
- paymentMethods,
- selectedPaymentMethod,
- excludeCurrentMonth,
- ]);
+ },
+ {
+ key: "Total",
+ value:
+ total === undefined ? (
+ <div className="h-4 w-24 animate-pulse rounded-md bg-neutral-200" />
+ ) : (
+ currencyFormatter(total / 100, {
+ minimumFractionDigits: 2,
+ maximumFractionDigits: 2,
+ })
+ ),
+ },
+ ];
+ }, [amount, paymentMethods, selectedPaymentMethod, cutoffPeriod]);
const table = useTable({
data: eligiblePayouts || [],