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.

Fix payouts limit

Steven Tey1c0c23543b4f
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.ts
index 3bd382351b..7776036576 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
@@ -1,4 +1,5 @@
import { createId } from "@/lib/api/create-id";
+import { exceededLimitError } from "@/lib/api/errors";
import {
DIRECT_DEBIT_PAYMENT_METHOD_TYPES,
FOREX_MARKUP_RATE,
@@ -10,6 +11,7 @@ import {
import { calculatePayoutFeeForMethod } from "@/lib/payment-methods";
import { stripe } from "@/lib/stripe";
import { createFxQuote } from "@/lib/stripe/create-fx-quote";
+import { PlanProps } from "@/lib/types";
import { resend } from "@dub/email/resend";
import { VARIANT_TO_FROM_MAP } from "@dub/email/resend/constants";
import PartnerPayoutConfirmed from "@dub/email/templates/partner-payout-confirmed";
@@ -31,7 +33,13 @@ export async function confirmPayouts({
}: {
workspace: Pick<
Project,
- "id" | "stripeId" | "plan" | "invoicePrefix" | "payoutFee"
+ | "id"
+ | "stripeId"
+ | "plan"
+ | "invoicePrefix"
+ | "payoutsUsage"
+ | "payoutsLimit"
+ | "payoutFee"
>;
program: Pick<Program, "id" | "name" | "logo" | "minPayoutAmount">;
userId: string;
@@ -86,12 +94,25 @@ export async function confirmPayouts({
return;
}
+ const payoutAmount = payouts.reduce(
+ (total, payout) => total + payout.amount,
+ 0,
+ );
+
+ if (workspace.payoutsUsage + payoutAmount > workspace.payoutsLimit) {
+ throw new Error(
+ exceededLimitError({
+ plan: workspace.plan as PlanProps,
+ limit: workspace.payoutsLimit,
+ type: "payouts",
+ }),
+ );
+ }
+
const paymentMethod = await stripe.paymentMethods.retrieve(paymentMethodId);
// Create the invoice for the payouts
const newInvoice = await prisma.$transaction(async (tx) => {
- const amount = payouts.reduce((total, payout) => total + payout.amount, 0);
-
const payoutFee = calculatePayoutFeeForMethod({
paymentMethod: paymentMethod.type,
payoutFee: workspace.payoutFee,
@@ -106,8 +127,8 @@ export async function confirmPayouts({
);
const currency = paymentMethodToCurrency[paymentMethod.type] || "usd";
- const totalFee = amount * payoutFee;
- const total = amount + totalFee;
+ const totalFee = payoutAmount * payoutFee;
+ const total = payoutAmount + totalFee;
let convertedTotal = total;
// convert the amount to EUR/CAD if the payment method is sepa_debit or acss_debit
@@ -150,7 +171,7 @@ export async function confirmPayouts({
number: invoiceNumber,
programId: program.id,
workspaceId: workspace.id,
- amount,
+ amount: payoutAmount,
fee: totalFee,
total,
},
@@ -194,7 +215,7 @@ export async function confirmPayouts({
},
data: {
payoutsUsage: {
- increment: amount,
+ increment: payoutAmount,
},
},
});
diff --git a/apps/web/lib/actions/partners/confirm-payouts.ts b/apps/web/lib/actions/partners/confirm-payouts.ts
index 69893fd39c..39e357f5cc 100644
--- a/apps/web/lib/actions/partners/confirm-payouts.ts
+++ b/apps/web/lib/actions/partners/confirm-payouts.ts
@@ -1,5 +1,6 @@
"use server";
+import { exceededLimitError } from "@/lib/api/errors";
import { qstash } from "@/lib/cron";
import { PAYMENT_METHOD_TYPES } from "@/lib/partners/constants";
import { CUTOFF_PERIOD_ENUM } from "@/lib/partners/cutoff-period";
@@ -25,8 +26,17 @@ export const confirmPayoutsAction = authActionClient
throw new Error("Workspace does not have a valid Stripe ID.");
}
- if (workspace.payoutsUsage >= workspace.payoutsLimit) {
- throw new Error("Payouts limit exceeded.");
+ if (
+ workspace.payoutsUsage + workspace.payoutsLimit >
+ workspace.payoutsLimit
+ ) {
+ throw new Error(
+ exceededLimitError({
+ plan: workspace.plan,
+ limit: workspace.payoutsLimit,
+ type: "payouts",
+ }),
+ );
}
const paymentMethod = await stripe.paymentMethods.retrieve(paymentMethodId);
diff --git a/apps/web/lib/api/errors.ts b/apps/web/lib/api/errors.ts
index 71358f0fa8..b9bb94f48a 100644
--- a/apps/web/lib/api/errors.ts
+++ b/apps/web/lib/api/errors.ts
@@ -1,5 +1,5 @@
import z from "@/lib/zod";
-import { capitalize } from "@dub/utils";
+import { capitalize, currencyFormatter } from "@dub/utils";
import { NextResponse } from "next/server";
import { ZodError } from "zod";
import { generateErrorMessage } from "zod-error";
@@ -233,11 +233,21 @@ export const exceededLimitError = ({
}: {
plan: PlanProps;
limit: number;
- type: "clicks" | "links" | "AI" | "domains" | "tags" | "users" | "folders";
+ type:
+ | "clicks"
+ | "links"
+ | "AI"
+ | "domains"
+ | "tags"
+ | "users"
+ | "folders"
+ | "payouts";
}) => {
return `You've reached your ${
- type === "links" || type === "AI" ? "monthly" : ""
- } limit of ${limit} ${
- limit === 1 ? type.slice(0, -1) : type
- } on the ${capitalize(plan)} plan. Please upgrade to add more ${type}.`;
+ ["links", "AI", "payouts"].includes(type) ? "monthly" : ""
+ } limit of ${
+ type === "payouts"
+ ? currencyFormatter(limit / 100)
+ : `${limit} ${limit === 1 ? type.slice(0, -1) : type}`
+ } on the ${capitalize(plan)} plan. Please upgrade for higher limits.`;
};
diff --git a/apps/web/ui/partners/payout-invoice-sheet.tsx b/apps/web/ui/partners/payout-invoice-sheet.tsx
index be2fde5eef..adebaa25a1 100644
--- a/apps/web/ui/partners/payout-invoice-sheet.tsx
+++ b/apps/web/ui/partners/payout-invoice-sheet.tsx
@@ -1,4 +1,5 @@
import { confirmPayoutsAction } from "@/lib/actions/partners/confirm-payouts";
+import { exceededLimitError } from "@/lib/api/errors";
import { DIRECT_DEBIT_PAYMENT_METHOD_TYPES } from "@/lib/partners/constants";
import {
CUTOFF_PERIOD,
@@ -8,7 +9,7 @@ import { calculatePayoutFeeForMethod } 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";
-import { PayoutResponse } from "@/lib/types";
+import { PayoutResponse, PlanProps } from "@/lib/types";
import { X } from "@/ui/shared/icons";
import {
Button,
@@ -20,6 +21,7 @@ import {
Sheet,
SimpleTooltipContent,
Table,
+ TooltipContent,
useRouterStuff,
useTable,
} from "@dub/ui";
@@ -84,6 +86,8 @@ function PayoutInvoiceSheetContent() {
slug,
plan,
defaultProgramId,
+ payoutsUsage,
+ payoutsLimit,
payoutFee,
} = useWorkspace();
@@ -411,9 +415,6 @@ function PayoutInvoiceSheetContent() {
type="button"
variant="primary"
loading={isPending}
- disabled={
- eligiblePayoutsLoading || !selectedPaymentMethod || amount === 0
- }
onClick={async () => {
if (!workspaceId || !selectedPaymentMethod) {
return;
@@ -433,6 +434,25 @@ function PayoutInvoiceSheetContent() {
})} payout`
: "Confirm payout"
}
+ disabled={
+ eligiblePayoutsLoading || !selectedPaymentMethod || amount === 0
+ }
+ disabledTooltip={
+ payoutsUsage &&
+ payoutsLimit &&
+ amount &&
+ payoutsUsage + amount > payoutsLimit && (
+ <TooltipContent
+ title={exceededLimitError({
+ plan: plan as PlanProps,
+ limit: payoutsLimit,
+ type: "payouts",
+ })}
+ cta="Upgrade"
+ href={`/${slug}/settings/billing/upgrade`}
+ />
+ )
+ }
/>
</div>
</div>