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.

Rearrange early return logic for `createStablecoinPayout` and `createStripeTransfer`

Steven Tey88dbdd63b9a0
diff --git a/apps/web/lib/partners/create-stablecoin-payout.ts b/apps/web/lib/partners/create-stablecoin-payout.ts
index 94f2c595e7..abb5cfc320 100644
--- a/apps/web/lib/partners/create-stablecoin-payout.ts
+++ b/apps/web/lib/partners/create-stablecoin-payout.ts
@@ -53,18 +53,6 @@ export const createStablecoinPayout = async ({
},
});
- if (!partner.payoutsEnabledAt) {
- console.warn(`Partner ${partner.email} does not have payouts enabled.`);
- return;
- }
-
- if (!partner.stripeRecipientId) {
- console.warn(
- `Partner ${partner.email} does not have a stripeRecipientId set.`,
- );
- return;
- }
-
const commonInclude: Prisma.PayoutInclude = {
program: {
select: {
@@ -124,60 +112,15 @@ export const createStablecoinPayout = async ({
return;
}
- const idempotencyKey = createPayoutsIdempotencyKey({
- partnerId: partner.id,
- invoiceId,
- payoutIds: allPayouts.map((p) => p.id),
- });
-
- let totalTransferableAmount = allPayouts.reduce(
- (acc, payout) => acc + payout.amount,
- 0,
- );
-
- // 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)}).`,
+ // should never happen, but just in case
+ if (!partner.payoutsEnabledAt || !partner.stripeRecipientId) {
+ await markPayoutsAsProcessed(currentInvoicePayouts);
+ console.warn(
+ `Partner ${partner.email} does not have a stripeRecipientId set.`,
);
+ return;
}
- let withdrawalFee = 0;
-
- // If the total transferable amount is less than the minimum withdrawal amount
- if (totalTransferableAmount < MIN_WITHDRAWAL_AMOUNT_CENTS) {
- // if we're forcing a withdrawal, we need to charge a withdrawal fee
- if (forceWithdrawal) {
- withdrawalFee = BELOW_MIN_WITHDRAWAL_FEE_CENTS;
- // else, we will just update current invoice payouts to "processed" status
- } else {
- await markPayoutsAsProcessed(currentInvoicePayouts);
-
- 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;
- }
- }
-
- // remove the stablecoin payout fee (0.5%) and withdrawal fee (if applicable) from the total amount
- totalTransferableAmount -=
- totalTransferableAmount * STABLECOIN_PAYOUT_FEE_RATE + withdrawalFee;
-
- // Round down to the nearest integer
- totalTransferableAmount = Math.floor(totalTransferableAmount);
-
const stripeRecipientAccount = await getStripeRecipientAccount(
partner.stripeRecipientId,
);
@@ -203,31 +146,26 @@ export const createStablecoinPayout = async ({
return;
}
- const cryptoWalletsActive =
- stripeRecipientAccount.configuration?.recipient?.capabilities
- ?.crypto_wallets?.status === "active";
-
// Identity verification restricts crypto_wallets until Stripe finishes review.
// Keep payouts enabled and defer the transfer so it can be sent once the
// capability is active again.
- if (
- !cryptoWalletsActive &&
- isRecipientPendingIdVerification(stripeRecipientAccount)
- ) {
+ if (isRecipientPendingIdVerification(stripeRecipientAccount)) {
await markPayoutsAsProcessed(currentInvoicePayouts);
- const message = `Stripe recipient account for partner ${partner.email} is restricted while identity verification is in progress. Skipping payout until verification is complete.`;
+ const message = `Stripe recipient account for partner ${partner.email} is restricted while identity verification is in progress.`;
if (forceWithdrawal) {
- throw new Error(
- "Your payout account is temporarily restricted while Stripe verifies your identity. Please try again once verification is complete.",
- );
+ throw new Error(message);
+ } else {
+ console.warn(message);
+ return;
}
-
- console.warn(message);
- return;
}
+ const cryptoWalletsActive =
+ stripeRecipientAccount.configuration?.recipient?.capabilities
+ ?.crypto_wallets?.status === "active";
+
// Stripe recipient account does not have crypto wallet capabilities
if (!cryptoWalletsActive) {
await prisma.partner.update({
@@ -277,6 +215,60 @@ export const createStablecoinPayout = async ({
}
}
+ const idempotencyKey = createPayoutsIdempotencyKey({
+ partnerId: partner.id,
+ invoiceId,
+ payoutIds: allPayouts.map((p) => p.id),
+ });
+
+ let totalTransferableAmount = allPayouts.reduce(
+ (acc, payout) => acc + payout.amount,
+ 0,
+ );
+
+ // 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;
+
+ // If the total transferable amount is less than the minimum withdrawal amount
+ if (totalTransferableAmount < MIN_WITHDRAWAL_AMOUNT_CENTS) {
+ // if we're forcing a withdrawal, we need to charge a withdrawal fee
+ if (forceWithdrawal) {
+ withdrawalFee = BELOW_MIN_WITHDRAWAL_FEE_CENTS;
+ // else, we will just update current invoice payouts to "processed" status
+ } else {
+ await markPayoutsAsProcessed(currentInvoicePayouts);
+
+ 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;
+ }
+ }
+
+ // remove the stablecoin payout fee (0.5%) and withdrawal fee (if applicable) from the total amount
+ totalTransferableAmount -=
+ totalTransferableAmount * STABLECOIN_PAYOUT_FEE_RATE + withdrawalFee;
+
+ // Round down to the nearest integer
+ totalTransferableAmount = Math.floor(totalTransferableAmount);
+
const allPayoutsProgramNames = [
...new Set(allPayouts.map((p) => p.program.name)),
];
diff --git a/apps/web/lib/partners/create-stripe-transfer.ts b/apps/web/lib/partners/create-stripe-transfer.ts
index f0b88adea3..83ecb246f6 100644
--- a/apps/web/lib/partners/create-stripe-transfer.ts
+++ b/apps/web/lib/partners/create-stripe-transfer.ts
@@ -46,14 +46,6 @@ export const createStripeTransfer = async ({
},
});
- // should never happen, but just in case
- if (!partner.stripeConnectId || !partner.payoutsEnabledAt) {
- console.warn(
- `Partner ${partner.email} does not have an active payout account.`,
- );
- return;
- }
-
const commonInclude: Prisma.PayoutInclude = {
program: {
select: {
@@ -107,6 +99,48 @@ export const createStripeTransfer = async ({
return;
}
+ // should never happen, but just in case
+ if (!partner.stripeConnectId || !partner.payoutsEnabledAt) {
+ await markPayoutsAsProcessed(currentInvoicePayouts);
+ console.warn(
+ `Partner ${partner.email} does not have an active payout account.`,
+ );
+ return;
+ }
+
+ const stripeConnectAccount = await stripe.accounts.retrieve(
+ partner.stripeConnectId,
+ );
+
+ if (
+ !stripeConnectAccount.payouts_enabled ||
+ !stripeConnectAccount.capabilities?.transfers ||
+ stripeConnectAccount.capabilities.transfers === "inactive"
+ ) {
+ await prisma.partner.update({
+ where: {
+ id: partner.id,
+ },
+ data: {
+ payoutsEnabledAt: null,
+ defaultPayoutMethod: null,
+ },
+ });
+
+ console.log(`Updated partner ${partner.email} with payoutsEnabledAt null`);
+
+ await markPayoutsAsProcessed(currentInvoicePayouts);
+
+ const message = `Partner's Stripe Express account (${partner.stripeConnectId}) is not configured to receive transfers`;
+
+ if (forceWithdrawal) {
+ throw new Error(message);
+ } else {
+ console.warn(message);
+ return;
+ }
+ }
+
// total transferable amount is the sum of all previously processed payouts (but not sent yet) and the current invoice payouts
const totalTransferableAmount = allPayouts.reduce(
(acc, payout) => acc + payout.amount,
@@ -157,39 +191,6 @@ export const createStripeTransfer = async ({
...new Set(allPayouts.map((p) => p.program.name)), // deduplicate program names
];
- const stripeConnectAccount = await stripe.accounts.retrieve(
- partner.stripeConnectId,
- );
-
- if (
- !stripeConnectAccount.payouts_enabled ||
- !stripeConnectAccount.capabilities?.transfers ||
- stripeConnectAccount.capabilities.transfers === "inactive"
- ) {
- await prisma.partner.update({
- where: {
- id: partner.id,
- },
- data: {
- payoutsEnabledAt: null,
- defaultPayoutMethod: null,
- },
- });
-
- console.log(`Updated partner ${partner.email} with payoutsEnabledAt null`);
-
- await markPayoutsAsProcessed(currentInvoicePayouts);
-
- const message = `Partner's Stripe Express account (${partner.stripeConnectId}) is not configured to receive transfers`;
-
- if (forceWithdrawal) {
- throw new Error(message);
- } else {
- console.warn(message);
- return;
- }
- }
-
// will be used for transfer_group
const finalPayoutInvoiceId = allPayouts[allPayouts.length - 1].invoiceId;