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.

add calculatePayoutFee

Kiran Ka5b1e7f35823
diff --git a/apps/web/lib/actions/partners/confirm-payouts.ts b/apps/web/lib/actions/partners/confirm-payouts.ts
index c03f62f2d9..b60f371027 100644
--- a/apps/web/lib/actions/partners/confirm-payouts.ts
+++ b/apps/web/lib/actions/partners/confirm-payouts.ts
@@ -3,7 +3,11 @@
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 { PAYOUT_FEES } from "@/lib/partners/constants";
+import {
+ calculatePayoutFee,
+ PARTNER_PAYOUT_METHOD,
+ PARTNER_PAYOUT_METHODS,
+} from "@/lib/payment-methods";
import { stripe } from "@/lib/stripe";
import { sendEmail } from "@dub/email";
import PartnerPayoutConfirmed from "@dub/email/templates/partner-payout-confirmed";
@@ -17,7 +21,7 @@ const confirmPayoutsSchema = z.object({
paymentMethodId: z.string(),
});
-const allowedPaymentMethods = ["us_bank_account", "card", "link"];
+const allowedPaymentMethods = ["card", "link", ...PARTNER_PAYOUT_METHODS];
// Confirm payouts
export const confirmPayoutsAction = authActionClient
@@ -46,7 +50,9 @@ export const confirmPayoutsAction = authActionClient
if (!allowedPaymentMethods.includes(paymentMethod.type)) {
throw new Error(
- `We only support ACH and Card for now. Please update your payout method to one of these.`,
+ `We only support ${PARTNER_PAYOUT_METHODS.join(
+ ", ",
+ )} for now. Please update your payout method to one of these.`,
);
}
@@ -96,11 +102,7 @@ export const confirmPayoutsAction = authActionClient
);
const fee =
- amount *
- PAYOUT_FEES[workspace.plan?.split(" ")[0] ?? "business"][
- paymentMethod.type === "us_bank_account" ? "ach" : "card"
- ];
-
+ amount * calculatePayoutFee(paymentMethod.type, workspace.plan);
const total = amount + fee;
// Generate the next invoice number
@@ -161,7 +163,12 @@ export const confirmPayoutsAction = authActionClient
(async () => {
// Send emails to all the partners involved in the payouts if the payout method is ACH
// ACH takes 4 business days to process
- if (newInvoice && paymentMethod.type === "us_bank_account") {
+ if (
+ newInvoice &&
+ PARTNER_PAYOUT_METHODS.includes(
+ paymentMethod.type as PARTNER_PAYOUT_METHOD,
+ )
+ ) {
await Promise.all(
payouts
.filter((payout) => payout.partner.email)
diff --git a/apps/web/lib/payment-methods.ts b/apps/web/lib/payment-methods.ts
index 67d006919b..2502383682 100644
--- a/apps/web/lib/payment-methods.ts
+++ b/apps/web/lib/payment-methods.ts
@@ -1,3 +1,6 @@
+import Stripe from "stripe";
+import { PAYOUT_FEES } from "./partners/constants";
+
export const PARTNER_PAYOUT_METHODS = [
"us_bank_account",
"acss_debit",
@@ -5,3 +8,22 @@ export const PARTNER_PAYOUT_METHODS = [
] as const;
export type PARTNER_PAYOUT_METHOD = (typeof PARTNER_PAYOUT_METHODS)[number];
+
+export const calculatePayoutFee = (
+ paymentMethod: Stripe.PaymentMethod.Type,
+ plan: string | undefined,
+) => {
+ if (!paymentMethod) {
+ return null;
+ }
+
+ const planType = plan?.split(" ")[0] ?? "business";
+
+ if (["link", "card"].includes(paymentMethod)) {
+ return PAYOUT_FEES[planType].card;
+ }
+
+ if (["us_bank_account", "acss_debit", "sepa_debit"].includes(paymentMethod)) {
+ return PAYOUT_FEES[planType].ach;
+ }
+};
diff --git a/apps/web/ui/partners/payout-invoice-sheet.tsx b/apps/web/ui/partners/payout-invoice-sheet.tsx
index d801fdda0c..8209fdd2f1 100644
--- a/apps/web/ui/partners/payout-invoice-sheet.tsx
+++ b/apps/web/ui/partners/payout-invoice-sheet.tsx
@@ -1,5 +1,5 @@
import { confirmPayoutsAction } from "@/lib/actions/partners/confirm-payouts";
-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";
@@ -34,7 +34,6 @@ import {
Dispatch,
Fragment,
SetStateAction,
- useCallback,
useEffect,
useMemo,
useState,
@@ -142,29 +141,6 @@ function PayoutInvoiceSheetContent({ setIsOpen }: PayoutInvoiceSheetProps) {
}
}, [paymentMethods, selectedPaymentMethod]);
- const calculateFee = useCallback(
- (paymentMethod: PAYMENT_METHOD | null) => {
- if (!paymentMethod) {
- return null;
- }
-
- const planType = plan?.split(" ")[0] ?? "business";
-
- if (["link", "card"].includes(paymentMethod.type)) {
- return PAYOUT_FEES[planType].card;
- }
-
- if (
- ["us_bank_account", "acss_debit", "sepa_debit"].includes(
- paymentMethod.type,
- )
- ) {
- return PAYOUT_FEES[planType].ach;
- }
- },
- [plan],
- );
-
const finalPaymentMethods = useMemo(
() =>
paymentMethods?.map((pm) => {
@@ -173,7 +149,7 @@ function PayoutInvoiceSheetContent({ setIsOpen }: PayoutInvoiceSheetProps) {
const base = {
...paymentMethod,
id: pm.id,
- fee: calculateFee(paymentMethod),
+ fee: calculatePayoutFee(pm.type, plan),
};
if (pm.link) {