The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Improve payout flow to handle platform-level clawbacks
Steven Tey7d9d3af43a0d
diff --git a/apps/web/lib/constants/misc.ts b/apps/web/lib/constants/misc.tsindex d11edddcf9..268e347f09 100644--- a/apps/web/lib/constants/misc.ts+++ b/apps/web/lib/constants/misc.ts@@ -7,3 +7,28 @@ export const SLACK_SUPPORT_INVITE_MAX_EMAILS = 5;export const VITEST_POLL_INTERVAL_MS = 5000; // 5 secondsexport const VITEST_TEST_TIMEOUT_MS = 90_000; // 90 seconds++// TODO: Remove after everything looks good+export const PARTNER_IDS_TO_LOG_PAYOUTS_FOR = [+ "pn_1KA455P5Z1KZGAQWEXYRPEDE1",+ "pn_1JRC1B0F9T3G9CV4Y7SCJTMVT",+ "pn_1K9NSA0XZJT5379AWPJMSEKAJ",+ "pn_1K7163Q3M6QT2RV5G6X7VYMN6",+ "pn_1K30PXMNKW5Y94Q726MET9T6J",+ "pn_1K564YVV7RKE9JPTJWRACJ559",+ "pn_1K717SGE359QA4PNKE637WW4J",+ "pn_1KE72H473V7ZDAASPC2KPH120",+ "pn_1KR4SHW7A9Z8STZ3009V65E5G",+ "pn_1KTN301HYD69FTJJFZ68E0P4V",+ "pn_1KPQ307QQ7FT8240GHF3TJ011",+ "pn_1KA4SY9AGM1072BBD3MAN9K34",+ "pn_1KP9560JT042WGSY8B8C5K61T",+ "pn_1K7183R8NPWMDN0QPAGGBZBE7",+ "pn_1KAH7V6W86Q8DA5BP8KV0GBV6",+ "pn_1K7EJC5C47YSWB567VN6Z7SVV",+ "pn_1KCFT9F8VEKENVVPVAQT9GQXH",+ "pn_1K86AKF4629KA4DXMNN3ZFD45",+ "pn_1KAGYC6EZC3G5S9BTW11SHYHN",+ "pn_1K3M1JXQTBQ50XXKVDSFZXE7G",+ "pn_1KAHF27R2QNKV83R3S10KERXK",+];diff --git a/apps/web/lib/partners/create-stablecoin-payout.ts b/apps/web/lib/partners/create-stablecoin-payout.tsindex 1b201f3f6b..120cd009c1 100644--- a/apps/web/lib/partners/create-stablecoin-payout.ts+++ b/apps/web/lib/partners/create-stablecoin-payout.ts@@ -8,10 +8,12 @@ import {chunk,currencyFormatter,log,+ pluralize,prettyPrint,} from "@dub/utils";import { PartnerPayoutMethod, Prisma } from "@prisma/client";import { waitUntil } from "@vercel/functions";+import { PARTNER_IDS_TO_LOG_PAYOUTS_FOR } from "../constants/misc";import {BELOW_MIN_WITHDRAWAL_FEE_CENTS,MIN_FORCE_WITHDRAWAL_AMOUNT_CENTS,@@ -130,17 +132,14 @@ export const createStablecoinPayout = async ({0,);- if (totalTransferableAmount < MIN_FORCE_WITHDRAWAL_AMOUNT_CENTS) {- const message = `Total transferable amount (${currencyFormatter(totalTransferableAmount)}) is less than the minimum amount required for withdrawal (${currencyFormatter(MIN_FORCE_WITHDRAWAL_AMOUNT_CENTS)}).`;-- // For force-withdrawal action, throw so the error surfaces back to partners.- // Otherwise (e.g. cron-driven payouts) just log and skip silently.- if (forceWithdrawal) {- throw new Error(message);- } else {- console.warn(message);- return;- }+ // For force-withdrawals, if amount is less than the minimum force withdrawal amount, throw an error+ if (+ forceWithdrawal &&+ totalTransferableAmount < MIN_FORCE_WITHDRAWAL_AMOUNT_CENTS+ ) {+ throw new Error(+ `Total transferable amount (${currencyFormatter(totalTransferableAmount)}) is less than the minimum amount required for withdrawal (${currencyFormatter(MIN_FORCE_WITHDRAWAL_AMOUNT_CENTS)}).`,+ );}let withdrawalFee = 0;@@ -154,9 +153,16 @@ export const createStablecoinPayout = async ({} else {await markPayoutsAsProcessed(currentInvoicePayouts);- console.log(- `Total processed payouts (${currencyFormatter(totalTransferableAmount)}) for partner ${partner.id} are below ${currencyFormatter(MIN_WITHDRAWAL_AMOUNT_CENTS)}, skipping...`,- );+ const message = `Total processed payouts (${currencyFormatter(totalTransferableAmount)}) for partner ${partner.id} are below ${currencyFormatter(MIN_WITHDRAWAL_AMOUNT_CENTS)}, skipping...`;+ console.log(message);++ if (PARTNER_IDS_TO_LOG_PAYOUTS_FOR.includes(partner.id)) {+ await log({+ message,+ type: "alerts",+ mention: true,+ });+ }return;}@@ -246,6 +252,21 @@ export const createStablecoinPayout = async ({return;}+ const message = `Transfer of ${currencyFormatter(totalTransferableAmount)} (${outboundPayment.id}) created for partner ${partner.id} for ${pluralize(+ "payout",+ allPayouts.length,+ )} ${allPayouts.map((p) => p.id).join(", ")}`;++ console.log(message);++ if (PARTNER_IDS_TO_LOG_PAYOUTS_FOR.includes(partner.id)) {+ await log({+ message,+ type: "alerts",+ mention: true,+ });+ }+const payoutIds = allPayouts.map((p) => p.id);const commissions = await prisma.commission.findMany({diff --git a/apps/web/lib/partners/create-stripe-transfer.ts b/apps/web/lib/partners/create-stripe-transfer.tsindex 714680b8de..d58a12078e 100644--- a/apps/web/lib/partners/create-stripe-transfer.ts+++ b/apps/web/lib/partners/create-stripe-transfer.ts@@ -18,6 +18,7 @@ import {} from "@dub/utils";import { Prisma } from "@prisma/client";import { waitUntil } from "@vercel/functions";+import { PARTNER_IDS_TO_LOG_PAYOUTS_FOR } from "../constants/misc";import { enqueueBatchJobs } from "../cron/enqueue-batch-jobs";import { createPayoutsIdempotencyKey } from "../payouts/create-payouts-idempotency-key";import { markPayoutsAsProcessed } from "../payouts/mark-payouts-as-processed";@@ -111,17 +112,14 @@ export const createStripeTransfer = async ({0,);- if (totalTransferableAmount < MIN_FORCE_WITHDRAWAL_AMOUNT_CENTS) {- const message = `Total transferable amount (${currencyFormatter(totalTransferableAmount)}) is less than the minimum amount required for withdrawal (${currencyFormatter(MIN_FORCE_WITHDRAWAL_AMOUNT_CENTS)}).`;-- // For force-withdrawal action, throw so the error surfaces back to partners.- // Otherwise (e.g. cron-driven payouts) just log and skip silently.- if (forceWithdrawal) {- throw new Error(message);- } else {- console.warn(message);- return;- }+ // For force-withdrawals, if amount is less than the minimum force withdrawal amount, throw an error+ if (+ forceWithdrawal &&+ totalTransferableAmount < MIN_FORCE_WITHDRAWAL_AMOUNT_CENTS+ ) {+ throw new Error(+ `Total transferable amount (${currencyFormatter(totalTransferableAmount)}) is less than the minimum amount required for withdrawal (${currencyFormatter(MIN_FORCE_WITHDRAWAL_AMOUNT_CENTS)}).`,+ );}let withdrawalFee = 0;@@ -135,9 +133,16 @@ export const createStripeTransfer = async ({} else {await markPayoutsAsProcessed(currentInvoicePayouts);- console.log(- `Total processed payouts (${currencyFormatter(totalTransferableAmount)}) for partner ${partner.id} are below ${currencyFormatter(MIN_WITHDRAWAL_AMOUNT_CENTS)}, skipping...`,- );+ const message = `Total processed payouts (${currencyFormatter(totalTransferableAmount)}) for partner ${partner.id} are below ${currencyFormatter(MIN_WITHDRAWAL_AMOUNT_CENTS)}, skipping...`;+ console.log(message);++ if (PARTNER_IDS_TO_LOG_PAYOUTS_FOR.includes(partner.id)) {+ await log({+ message,+ type: "alerts",+ mention: true,+ });+ }// skip creating a transferreturn;@@ -210,12 +215,20 @@ export const createStripeTransfer = async ({},);- console.log(- `Transfer of ${currencyFormatter(finalTransferableAmount)} (${transfer.id}) created for partner ${partner.id} for ${pluralize(- "payout",- allPayouts.length,- )} ${allPayouts.map((p) => p.id).join(", ")}`,- );+ const message = `Transfer of ${currencyFormatter(finalTransferableAmount)} (${transfer.id}) created for partner ${partner.id} for ${pluralize(+ "payout",+ allPayouts.length,+ )} ${allPayouts.map((p) => p.id).join(", ")}`;++ console.log(message);++ if (PARTNER_IDS_TO_LOG_PAYOUTS_FOR.includes(partner.id)) {+ await log({+ message,+ type: "alerts",+ mention: true,+ });+ }const payoutIds = allPayouts.map((p) => p.id);