The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Refactor payout fee calculation and update workspace schema
Kiran K721194d87fb1
diff --git a/apps/web/app/(ee)/api/cron/payouts/confirm/confirm-payouts.ts b/apps/web/app/(ee)/api/cron/payouts/confirm/confirm-payouts.tsindex d065ab99b6..9ad2df1fe8 100644--- a/apps/web/app/(ee)/api/cron/payouts/confirm/confirm-payouts.ts+++ b/apps/web/app/(ee)/api/cron/payouts/confirm/confirm-payouts.ts@@ -7,7 +7,7 @@ import {CUTOFF_PERIOD,CUTOFF_PERIOD_TYPES,} from "@/lib/partners/cutoff-period";-import { calculatePayoutFee } from "@/lib/payment-methods";+import { computePayoutFeeForMethod } from "@/lib/payment-methods";import { stripe } from "@/lib/stripe";import { resend } from "@dub/email/resend";import { VARIANT_TO_FROM_MAP } from "@dub/email/resend/constants";@@ -23,7 +23,10 @@ export async function confirmPayouts({paymentMethodId,cutoffPeriod,}: {- workspace: Pick<Project, "id" | "stripeId" | "plan" | "invoicePrefix">;+ workspace: Pick<+ Project,+ "id" | "stripeId" | "plan" | "invoicePrefix" | "payoutFee"+ >;program: Pick<Program, "id" | "name" | "logo" | "minPayoutAmount">;userId: string;paymentMethodId: string;@@ -83,14 +86,17 @@ export async function confirmPayouts({const newInvoice = await prisma.$transaction(async (tx) => {const amount = payouts.reduce((total, payout) => total + payout.amount, 0);- const fee =- amount *- calculatePayoutFee({- paymentMethod: paymentMethod.type,- plan: workspace.plan,- });+ const payoutFee = computePayoutFeeForMethod({+ paymentMethod: paymentMethod.type,+ payoutFee: workspace.payoutFee,+ });++ if (!payoutFee) {+ throw new Error("Failed to calculate payout fee.");+ }- const total = amount + fee;+ const totalFee = amount * payoutFee;+ const total = amount + totalFee;// Generate the next invoice numberconst totalInvoices = await tx.invoice.count({@@ -108,7 +114,7 @@ export async function confirmPayouts({programId: program.id,workspaceId: workspace.id,amount,- fee,+ fee: totalFee,total,},});diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/billing/plan-usage.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/billing/plan-usage.tsxindex a5e2b6d1ec..cd759f8a9b 100644--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/billing/plan-usage.tsx+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/billing/plan-usage.tsx@@ -1,6 +1,5 @@"use client";-import { PAYOUT_FEES } from "@/lib/partners/constants";import usePartnersCount from "@/lib/swr/use-partners-count";import useTagsCount from "@/lib/swr/use-tags-count";import useUsers from "@/lib/swr/use-users";@@ -44,6 +43,7 @@ export default function PlanUsage() {totalLinks,payoutsUsage,payoutsLimit,+ payoutFee,domains,domainsLimit,foldersUsage,@@ -59,10 +59,6 @@ export default function PlanUsage() {status: "approved",});- const payoutFees = plan- ? PAYOUT_FEES[plan.toLowerCase()]?.direct_debit- : null;-const { data: tags } = useTagsCount();const { users } = useUsers();@@ -221,8 +217,8 @@ export default function PlanUsage() {icon={CirclePercentage}usage={plan- ? payoutFees- ? `${Math.round(payoutFees * 100)}%`+ ? payoutFee+ ? `${Math.round(payoutFee * 100)}%`: "-": undefined}diff --git a/apps/web/lib/payment-methods.ts b/apps/web/lib/payment-methods.tsindex bb20e872dc..6184e303eb 100644--- a/apps/web/lib/payment-methods.ts+++ b/apps/web/lib/payment-methods.ts@@ -1,31 +1,22 @@import Stripe from "stripe";-import {- DIRECT_DEBIT_PAYMENT_METHOD_TYPES,- PAYOUT_FEES,-} from "./partners/constants";+import { DIRECT_DEBIT_PAYMENT_METHOD_TYPES } from "./partners/constants";-export const calculatePayoutFee = ({+export const computePayoutFeeForMethod = ({paymentMethod,- plan,+ payoutFee,}: {paymentMethod: Stripe.PaymentMethod.Type;- plan: string | undefined;+ payoutFee: number | undefined;}) => {- if (!paymentMethod) {- return null;- }-- const planType = plan?.split(" ")[0] ?? "business";-- if (!Object.keys(PAYOUT_FEES).includes(planType)) {- return null;+ if (!paymentMethod || !payoutFee) {+ throw new Error("Invalid payment method or payout fee.");}if (["link", "card"].includes(paymentMethod)) {- return PAYOUT_FEES[planType].card;+ return payoutFee + 0.03;}if (DIRECT_DEBIT_PAYMENT_METHOD_TYPES.includes(paymentMethod)) {- return PAYOUT_FEES[planType].direct_debit;+ return payoutFee;}};diff --git a/apps/web/lib/zod/schemas/workspaces.ts b/apps/web/lib/zod/schemas/workspaces.tsindex 8226d7b89d..5e20e7fbca 100644--- a/apps/web/lib/zod/schemas/workspaces.ts+++ b/apps/web/lib/zod/schemas/workspaces.ts@@ -58,6 +58,9 @@ export const WorkspaceSchema = z.describe("The max dollar amount of partner payouts that can be processed within a billing cycle (in cents).",),+ payoutFee: z+ .number()+ .describe("The payout fee Dub Partners takes for direct debit payments."),domainsLimit: z.number().describe("The domains limit of the workspace."),tagsLimit: z.number().describe("The tags limit of the workspace."),foldersUsage: z.number().describe("The folders usage of the workspace."),diff --git a/apps/web/ui/partners/payout-invoice-sheet.tsx b/apps/web/ui/partners/payout-invoice-sheet.tsxindex 297a2ea91e..b9cd92e068 100644--- a/apps/web/ui/partners/payout-invoice-sheet.tsx+++ b/apps/web/ui/partners/payout-invoice-sheet.tsx@@ -4,7 +4,7 @@ import {CUTOFF_PERIOD,CUTOFF_PERIOD_TYPES,} from "@/lib/partners/cutoff-period";-import { calculatePayoutFee } from "@/lib/payment-methods";+import { computePayoutFeeForMethod } 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";@@ -79,7 +79,13 @@ type SelectPaymentMethod =function PayoutInvoiceSheetContent() {const { queryParams } = useRouterStuff();- const { id: workspaceId, slug, plan, defaultProgramId } = useWorkspace();+ const {+ id: workspaceId,+ slug,+ plan,+ defaultProgramId,+ payoutFee,+ } = useWorkspace();const { paymentMethods, loading: paymentMethodsLoading } =usePaymentMethods();@@ -125,9 +131,9 @@ function PayoutInvoiceSheetContent() {const base = {...paymentMethod,id: pm.id,- fee: calculatePayoutFee({+ fee: computePayoutFeeForMethod({paymentMethod: pm.type,- plan,+ payoutFee,}),};diff --git a/packages/prisma/schema/workspace.prisma b/packages/prisma/schema/workspace.prismaindex a5aa05c725..e89cd8dbee 100644--- a/packages/prisma/schema/workspace.prisma+++ b/packages/prisma/schema/workspace.prisma@@ -18,12 +18,13 @@ model Project {totalLinks Int @default(0) // Total number of links in the workspacetotalClicks Int @default(0) // Total number of clicks in the workspace- usage Int @default(0)- usageLimit Int @default(1000)- linksUsage Int @default(0)- linksLimit Int @default(25)- payoutsUsage Int @default(0)- payoutsLimit Int @default(0)+ usage Int @default(0)+ usageLimit Int @default(1000)+ linksUsage Int @default(0)+ linksLimit Int @default(25)+ payoutsUsage Int @default(0)+ payoutsLimit Int @default(0)+ payoutFee Float @default(0.05) // payout fee for direct debit paymentsdomainsLimit Int @default(3)tagsLimit Int @default(5)