The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Improve calculatePayoutFeeWithWaiver logic (#4046)
Steven Teyb0933e4bffc1
diff --git a/apps/web/app/(ee)/api/cron/payouts/process/process-payouts.ts b/apps/web/app/(ee)/api/cron/payouts/process/process-payouts.tsindex ea16b427ca..7c2fdadccb 100644--- a/apps/web/app/(ee)/api/cron/payouts/process/process-payouts.ts+++ b/apps/web/app/(ee)/api/cron/payouts/process/process-payouts.ts@@ -1,6 +1,6 @@import { getPayoutEligibilityFilter } from "@/lib/api/payouts/payout-eligibility-filter";import { payoutIdSelectionWhere } from "@/lib/api/payouts/payout-id-selection-where";-import { FAST_ACH_FEE_CENTS, FOREX_MARKUP_RATE } from "@/lib/constants/payouts";+import { FOREX_MARKUP_RATE } from "@/lib/constants/payouts";import { qstash } from "@/lib/cron";import { calculatePayoutFeeWithWaiver } from "@/lib/partners/calculate-payout-fee-with-waiver";import {@@ -165,7 +165,7 @@ export async function processPayouts({payoutFee,payoutFeeWaiverLimit: workspace.payoutFeeWaiverLimit,payoutFeeWaiverUsage: workspace.payoutFeeWaiverUsage,- fastAchFee: invoice.paymentMethod === "ach_fast" ? FAST_ACH_FEE_CENTS : 0,+ paymentMethod: invoice.paymentMethod!,});const invoiceTotal = totalPayoutAmount + invoiceFee;diff --git a/apps/web/lib/constants/payouts.ts b/apps/web/lib/constants/payouts.tsindex 038827ef13..6ecd43c60b 100644--- a/apps/web/lib/constants/payouts.ts+++ b/apps/web/lib/constants/payouts.ts@@ -9,6 +9,7 @@ export const PAYOUTS_SHEET_ITEMS_LIMIT = 10;export const ELIGIBLE_PAYOUTS_MAX_PAGE_SIZE = 500;export const CUTOFF_PERIOD_MAX_PAYOUTS = 1000;+export const CARD_PAYOUT_HARD_COST_RATE = 0.03; // 3% Stripe card processing cost (non-waivable)export const STABLECOIN_PAYOUT_FEE_RATE = 0.005; // 0.5%export const STABLECOIN_PAYOUT_FIXED_FEE_CENTS = 50; // $0.50export const FAST_ACH_FEE_CENTS = 2500; // $25diff --git a/apps/web/lib/partners/calculate-payout-fee-with-waiver.ts b/apps/web/lib/partners/calculate-payout-fee-with-waiver.tsindex 75ba1c14cd..617162eaf2 100644--- a/apps/web/lib/partners/calculate-payout-fee-with-waiver.ts+++ b/apps/web/lib/partners/calculate-payout-fee-with-waiver.ts@@ -1,9 +1,15 @@+import {+ CARD_PAYOUT_HARD_COST_RATE,+ FAST_ACH_FEE_CENTS,+} from "@/lib/constants/payouts";+import { PaymentMethod } from "@dub/prisma/client";+export interface PayoutFeeWithWaiverParams {payoutAmount: number;payoutFee: number;payoutFeeWaiverLimit: number;payoutFeeWaiverUsage: number;- fastAchFee?: number;+ paymentMethod: PaymentMethod;}// Calculates payout fee with tiered waiver logic@@ -12,27 +18,37 @@ export function calculatePayoutFeeWithWaiver({payoutFee,payoutFeeWaiverLimit,payoutFeeWaiverUsage,- fastAchFee = 0,+ paymentMethod,}: PayoutFeeWithWaiverParams) {+ const nonWaivableFeeRate =+ paymentMethod === "card" ? CARD_PAYOUT_HARD_COST_RATE : 0;+ const waivableFeeRate = payoutFee - nonWaivableFeeRate;+ const fastAchFee = paymentMethod === "ach_fast" ? FAST_ACH_FEE_CENTS : 0;++ let feeWaiverRemaining: number;+ let feeFreeAmount: number;+ let feeChargedAmount: number;+if (payoutFeeWaiverLimit === 0) {- return {- fee: Math.round(payoutAmount * payoutFee) + fastAchFee,- feeFreeAmount: 0,- feeChargedAmount: payoutAmount,- feeWaiverRemaining: 0,- };+ feeWaiverRemaining = 0;+ feeFreeAmount = 0;+ feeChargedAmount = payoutAmount;+ } else {+ feeWaiverRemaining = Math.max(+ 0,+ payoutFeeWaiverLimit - payoutFeeWaiverUsage,+ );+ feeFreeAmount = Math.min(payoutAmount, feeWaiverRemaining);+ feeChargedAmount = payoutAmount - feeFreeAmount;}- // Split the payout amount between free tier (0% fee) and fee charged (normal rate)- const feeWaiverRemaining = Math.max(- 0,- payoutFeeWaiverLimit - payoutFeeWaiverUsage,- );- const feeFreeAmount = Math.min(payoutAmount, feeWaiverRemaining);- const feeChargedAmount = payoutAmount - feeFreeAmount;+ const fee =+ Math.round(payoutAmount * nonWaivableFeeRate) ++ Math.round(feeChargedAmount * waivableFeeRate) ++ fastAchFee;return {- fee: Math.round(feeChargedAmount * payoutFee) + fastAchFee,+ fee,feeFreeAmount,feeChargedAmount,feeWaiverRemaining,diff --git a/apps/web/lib/stripe/payment-methods.ts b/apps/web/lib/stripe/payment-methods.tsindex dcead78273..a39c7c29f1 100644--- a/apps/web/lib/stripe/payment-methods.ts+++ b/apps/web/lib/stripe/payment-methods.ts@@ -1,4 +1,7 @@-import { DIRECT_DEBIT_PAYMENT_METHOD_TYPES } from "@/lib/constants/payouts";+import {+ CARD_PAYOUT_HARD_COST_RATE,+ DIRECT_DEBIT_PAYMENT_METHOD_TYPES,+} from "@/lib/constants/payouts";import { CreditCard, GreekTemple } from "@dub/ui";import Stripe from "stripe";@@ -14,7 +17,7 @@ export const calculatePayoutFeeForMethod = ({}if (["link", "card"].includes(paymentMethod)) {- return payoutFee + 0.03;+ return payoutFee + CARD_PAYOUT_HARD_COST_RATE;}if (DIRECT_DEBIT_PAYMENT_METHOD_TYPES.includes(paymentMethod)) {diff --git a/apps/web/tests/misc/calculate-payout-fee-with-waiver.test.ts b/apps/web/tests/misc/calculate-payout-fee-with-waiver.test.tsindex 5546a75d8e..e2d05491f1 100644--- a/apps/web/tests/misc/calculate-payout-fee-with-waiver.test.ts+++ b/apps/web/tests/misc/calculate-payout-fee-with-waiver.test.ts@@ -1,8 +1,13 @@+import {+ CARD_PAYOUT_HARD_COST_RATE,+ FAST_ACH_FEE_CENTS,+} from "@/lib/constants/payouts";import { calculatePayoutFeeWithWaiver } from "@/lib/partners/calculate-payout-fee-with-waiver";import { describe, expect, it } from "vitest";describe("calculatePayoutFeeWithWaiver", () => {const payoutFee = 0.03; // 3% fee+ const cardPayoutFee = payoutFee + CARD_PAYOUT_HARD_COST_RATE;it("zero waiver limit (backward compatibility)", () => {const result = calculatePayoutFeeWithWaiver({@@ -10,6 +15,7 @@ describe("calculatePayoutFeeWithWaiver", () => {payoutFee,payoutFeeWaiverLimit: 0,payoutFeeWaiverUsage: 0,+ paymentMethod: "ach",});expect(result).toEqual({@@ -26,6 +32,7 @@ describe("calculatePayoutFeeWithWaiver", () => {payoutFee,payoutFeeWaiverLimit: 50000, // $500.00 limitpayoutFeeWaiverUsage: 0, // nothing used yet+ paymentMethod: "ach",});expect(result).toEqual({@@ -42,6 +49,7 @@ describe("calculatePayoutFeeWithWaiver", () => {payoutFee,payoutFeeWaiverLimit: 50000, // $500.00 limitpayoutFeeWaiverUsage: 45000, // $450.00 already used+ paymentMethod: "ach",});expect(result).toEqual({@@ -58,6 +66,7 @@ describe("calculatePayoutFeeWithWaiver", () => {payoutFee,payoutFeeWaiverLimit: 50000, // $500.00 limitpayoutFeeWaiverUsage: 50000, // fully used+ paymentMethod: "ach",});expect(result).toEqual({@@ -68,20 +77,71 @@ describe("calculatePayoutFeeWithWaiver", () => {});});- it("includes fastAchFee when provided", () => {+ it("includes fast ACH fee for ach_fast payment method", () => {const result = calculatePayoutFeeWithWaiver({payoutAmount: 10000,payoutFeeWaiverLimit: 50000,payoutFeeWaiverUsage: 45000,payoutFee,- fastAchFee: 50,+ paymentMethod: "ach_fast",});expect(result).toEqual({- fee: 200, // 3% of $50.00 + $0.50 fast ACH fee+ fee: 150 + FAST_ACH_FEE_CENTS, // 3% of $50.00 + fast ACH feefeeFreeAmount: 5000,feeChargedAmount: 5000,feeWaiverRemaining: 5000,});});++ it("card payment fully within waiver still charges hard cost", () => {+ const result = calculatePayoutFeeWithWaiver({+ payoutAmount: 10000, // $100.00+ payoutFee: cardPayoutFee,+ payoutFeeWaiverLimit: 50000,+ payoutFeeWaiverUsage: 0,+ paymentMethod: "card",+ });++ expect(result).toEqual({+ fee: 300, // 3% card hard cost on full amount+ feeFreeAmount: 10000,+ feeChargedAmount: 0,+ feeWaiverRemaining: 50000,+ });+ });++ it("card payment partially within waiver charges hard cost on full amount", () => {+ const result = calculatePayoutFeeWithWaiver({+ payoutAmount: 10000, // $100.00+ payoutFee: cardPayoutFee,+ payoutFeeWaiverLimit: 50000,+ payoutFeeWaiverUsage: 45000,+ paymentMethod: "card",+ });++ expect(result).toEqual({+ fee: 450, // 3% of $100.00 + 3% of $50.00+ feeFreeAmount: 5000,+ feeChargedAmount: 5000,+ feeWaiverRemaining: 5000,+ });+ });++ it("card payment with exhausted waiver charges full fee", () => {+ const result = calculatePayoutFeeWithWaiver({+ payoutAmount: 10000,+ payoutFee: cardPayoutFee,+ payoutFeeWaiverLimit: 50000,+ payoutFeeWaiverUsage: 50000,+ paymentMethod: "card",+ });++ expect(result).toEqual({+ fee: 600, // 6% of $100.00+ feeFreeAmount: 0,+ feeChargedAmount: 10000,+ feeWaiverRemaining: 0,+ });+ });});diff --git a/apps/web/ui/partners/confirm-payouts-sheet.tsx b/apps/web/ui/partners/confirm-payouts-sheet.tsxindex 0c6eec2c62..ef7dcd859e 100644--- a/apps/web/ui/partners/confirm-payouts-sheet.tsx+++ b/apps/web/ui/partners/confirm-payouts-sheet.tsx@@ -1,11 +1,13 @@import { confirmPayoutsAction } from "@/lib/actions/partners/confirm-payouts";import { clientAccessCheck } from "@/lib/client-access-check";import {+ CARD_PAYOUT_HARD_COST_RATE,CUTOFF_PERIOD_MAX_PAYOUTS,DIRECT_DEBIT_PAYMENT_METHOD_TYPES,ELIGIBLE_PAYOUTS_MAX_PAGE_SIZE,FAST_ACH_FEE_CENTS,INVOICE_MIN_PAYOUT_AMOUNT_CENTS,+ STRIPE_PAYMENT_METHOD_NORMALIZATION,} from "@/lib/constants/payouts";import { exceededLimitError } from "@/lib/exceeded-limit-error";import { calculatePayoutFeeWithWaiver } from "@/lib/partners/calculate-payout-fee-with-waiver";@@ -446,16 +448,19 @@ function ConfirmPayoutsSheetContent() {0,);- const fastAchFee = selectedPaymentMethod.fastSettlement- ? FAST_ACH_FEE_CENTS- : 0;+ const invoicePaymentMethod = selectedPaymentMethod.fastSettlement+ ? "ach_fast"+ : STRIPE_PAYMENT_METHOD_NORMALIZATION[selectedPaymentMethod.type];++ const fastAchFee =+ invoicePaymentMethod === "ach_fast" ? FAST_ACH_FEE_CENTS : 0;const { fee } = calculatePayoutFeeWithWaiver({payoutAmount: amount,payoutFeeWaiverLimit: payoutFeeWaiverLimit ?? 0,payoutFeeWaiverUsage: payoutFeeWaiverUsage ?? 0,payoutFee: selectedPaymentMethod.fee,- fastAchFee,+ paymentMethod: invoicePaymentMethod,});const total = amount + fee;@@ -1223,12 +1228,6 @@ function buildPayoutFeeTooltip({const fastAchFeeText =fastAchFee > 0 ? ` + ${currencyFormatter(fastAchFee)} Fast ACH fee` : "";- if (isWithinWaiver) {- const waiverLimitFormatted = nFormatter(payoutFeeWaiverLimit / 100);-- return `0% processing fee for the first $${waiverLimitFormatted} payouts, then ${feePercentage}%${fastAchFeeText}. [Learn more](https://d.to/payouts)`;- }-const isDirectDebit = DIRECT_DEBIT_PAYMENT_METHOD_TYPES.includes(selectedPaymentMethod.type as any,);@@ -1237,5 +1236,14 @@ function buildPayoutFeeTooltip({? "": " Switch to Direct Debit for a reduced fee.";- return `${feePercentage}% processing fee${fastAchFeeText}. ${directDebitSuggestion} [Learn more](https://d.to/payouts)`;+ if (isWithinWaiver) {+ const effectiveFeePercentage = isDirectDebit+ ? 0+ : CARD_PAYOUT_HARD_COST_RATE * 100;+ const waiverLimitFormatted = nFormatter(payoutFeeWaiverLimit / 100);++ return `${effectiveFeePercentage}% processing fee for the first $${waiverLimitFormatted} payouts, then ${feePercentage}%${fastAchFeeText}.${directDebitSuggestion} [Learn more](https://d.to/payouts)`;+ }++ return `${feePercentage}% processing fee${fastAchFeeText}.${directDebitSuggestion} [Learn more](https://d.to/payouts)`;}