The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Merge branch 'main' into bank-debits
Kiran K1e8e3313fba7
diff --cc apps/web/lib/actions/partners/confirm-payouts.tsindex 3a047efff8,27eac10462..e30b584608--- a/apps/web/lib/actions/partners/confirm-payouts.ts+++ b/apps/web/lib/actions/partners/confirm-payouts.ts@@@ -1,29 -1,19 +1,24 @@@"use server";- import { createId } from "@/lib/api/create-id";import { getDefaultProgramIdOrThrow } from "@/lib/api/programs/get-default-program-id-or-throw";- import { getProgramOrThrow } from "@/lib/api/programs/get-program-or-throw";+ import { qstash } from "@/lib/cron";+import { DIRECT_DEBIT_PAYMENT_METHODS } from "@/lib/partners/constants";- import { calculatePayoutFee } from "@/lib/payment-methods";import { stripe } from "@/lib/stripe";- import { DIRECT_DEBIT_PAYMENT_METHOD } from "@/lib/types";- import { sendEmail } from "@dub/email";- import PartnerPayoutConfirmed from "@dub/email/templates/partner-payout-confirmed";- import { prisma } from "@dub/prisma";- import { waitUntil } from "@vercel/functions";+ import { APP_DOMAIN_WITH_NGROK } from "@dub/utils";import z from "zod";import { authActionClient } from "../safe-action";const confirmPayoutsSchema = z.object({workspaceId: z.string(),paymentMethodId: z.string(),+ excludeCurrentMonth: z.boolean().optional().default(false),});-const allowedPaymentMethods = ["us_bank_account", "card", "link"];+const ALLOWED_PAYMENT_METHODS = [+ "card",+ "link",+ ...DIRECT_DEBIT_PAYMENT_METHODS,+];// Confirm payoutsexport const confirmPayoutsAction = authActionClientdiff --cc apps/web/ui/partners/payout-invoice-sheet.tsxindex fe57773c14,b4d8cfb68b..8fc37167f2--- a/apps/web/ui/partners/payout-invoice-sheet.tsx+++ b/apps/web/ui/partners/payout-invoice-sheet.tsx@@@ -1,14 -1,10 +1,10 @@@import { confirmPayoutsAction } from "@/lib/actions/partners/confirm-payouts";- import { DIRECT_DEBIT_PAYMENT_METHODS } from "@/lib/partners/constants";-import { PAYOUT_FEES } from "@/lib/partners/constants";+import { calculatePayoutFee } from "@/lib/payment-methods";import { mutatePrefix } from "@/lib/swr/mutate";import usePaymentMethods from "@/lib/swr/use-payment-methods";- import usePayouts from "@/lib/swr/use-payouts";- import usePayoutsCount from "@/lib/swr/use-payouts-count";import useWorkspace from "@/lib/swr/use-workspace";- import { DIRECT_DEBIT_PAYMENT_METHOD, PayoutsCount } from "@/lib/types";-import { PayoutResponse } from "@/lib/types";++import { DirectDebitPaymentMethod, PayoutResponse } from "@/lib/types";import { X } from "@/ui/shared/icons";- import { PayoutStatus } from "@dub/prisma/client";import {Button,buttonVariants,@@@ -30,88 -28,61 +28,65 @@@ importtruncate,} from "@dub/utils";import { useAction } from "next-safe-action/hooks";- import { useParams } from "next/navigation";- import {- Dispatch,- Fragment,- SetStateAction,- useEffect,- useMemo,- useState,- } from "react";+ import { Fragment, useEffect, useMemo, useState } from "react";import { toast } from "sonner";-- interface PayoutInvoiceSheetProps {- setIsOpen: Dispatch<SetStateAction<boolean>>;- }+ 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 PAYMENT_METHOD = (typeof PAYMENT_METHODS)[keyof typeof PAYMENT_METHODS] & {- id: string;- fee: number;- };-- function PayoutInvoiceSheetContent({ setIsOpen }: PayoutInvoiceSheetProps) {- const { programId } = useParams<{ programId: string }>();- const { id: workspaceId, slug, plan } = useWorkspace();-+ function PayoutInvoiceSheetContent() {+ const { id: workspaceId, slug, plan, defaultProgramId } = useWorkspace();+ const { queryParams } = useRouterStuff();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<PAYMENT_METHOD | null>(null);- useState<PaymentMethodWithFee | null>(null);++ useState<DirectDebitPaymentMethod | null>(null);- const {- payoutsCount: eligiblePayoutsCount,- loading: eligiblePayoutsCountLoading,- } = usePayoutsCount<PayoutsCount[]>({- groupBy: "status",- eligibility: "eligible",- });+ const [excludeCurrentMonth, setExcludeCurrentMonth] = useState(false);const {- payouts: eligiblePayouts,+ data: eligiblePayouts,error: eligiblePayoutsError,- loading: eligiblePayoutsLoading,- } = usePayouts({- query: {- status: "pending",- eligibility: "eligible",- sortBy: "amount",- },- });+ isLoading: eligiblePayoutsLoading,+ } = useSWR<PayoutResponse[]>(+ `/api/programs/${defaultProgramId}/payouts/confirm?${new URLSearchParams({+ workspaceId,+ excludeCurrentMonth: excludeCurrentMonth ? "true" : "false",+ } as Record<string, any>).toString()}`,+ fetcher,+ );const { executeAsync, isPending } = useAction(confirmPayoutsAction, {onSuccess: async () => {@@@ -126,76 -99,46 +103,60 @@@},});- // 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, 16)}`- : 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(() => {- if (eligiblePayoutsCountLoading) {- 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" />- ),- Fee: (- <div className="h-4 w-24 animate-pulse rounded-md bg-neutral-200" />- ),- Duration: (- <div className="h-4 w-24 animate-pulse rounded-md bg-neutral-200" />- ),- Total: (- <div className="h-4 w-24 animate-pulse rounded-md bg-neutral-200" />- ),- };- }+ const amount = eligiblePayouts?.reduce((acc, payout) => {+ return acc + payout.amount;+ }, 0);- const amount =- eligiblePayoutsCount?.find((p) => p.status === PayoutStatus.pending)- ?.amount ?? 0;-- const fee = amount * (selectedPaymentMethod?.fee ?? 0);- const total = amount + fee;+ const fee =+ amount === undefined+ ? undefined+ : amount * (selectedPaymentMethod?.fee ?? 0);+ const total =+ amount !== undefined && fee !== undefined ? amount + fee : undefined;return {Method: (