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.

Update balance-available.ts

Kiran K600f79484ea2
diff --git a/apps/web/app/(ee)/api/stripe/connect/webhook/balance-available.ts b/apps/web/app/(ee)/api/stripe/connect/webhook/balance-available.ts
index 6e8f0f3c8b..a2b831e0ae 100644
--- a/apps/web/app/(ee)/api/stripe/connect/webhook/balance-available.ts
+++ b/apps/web/app/(ee)/api/stripe/connect/webhook/balance-available.ts
@@ -77,6 +77,36 @@ export async function balanceAvailable(event: Stripe.Event) {
return;
}
+ // Subtract the pending/in-transit payouts from the available balance
+ const { data: stripePayouts } = await stripe.payouts.list(
+ {
+ status: "pending",
+ },
+ {
+ stripeAccount,
+ },
+ );
+
+ if (stripePayouts.length > 0) {
+ const pendingOrInTransitPayouts = stripePayouts.filter(
+ ({ status }) => status === "pending" || status === "in_transit",
+ );
+
+ const alreadyPaidOutAmount = pendingOrInTransitPayouts.reduce(
+ (acc, payout) => acc + payout.amount,
+ 0,
+ );
+
+ availableBalance = availableBalance - alreadyPaidOutAmount;
+ }
+
+ if (availableBalance <= 0) {
+ console.log(
+ `The available balance (${currencyFormatter(convertedUsdAmount / 100, { maximumFractionDigits: 2 })}) for partner ${partner.email} (${stripeAccount}) is less than or equal to 0. Skipping...`,
+ );
+ return;
+ }
+
let withdrawalFee = 0;
// Decide if we need to charge a withdrawal fee for the partner