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 cron/withdrawal again fr (#3021)

Steven Teyfa53bf4bab60
diff --git a/apps/web/app/(ee)/api/cron/trigger-withdrawal/route.ts b/apps/web/app/(ee)/api/cron/trigger-withdrawal/route.ts
index 9ecbf2c73b..a6aa2a7571 100644
--- a/apps/web/app/(ee)/api/cron/trigger-withdrawal/route.ts
+++ b/apps/web/app/(ee)/api/cron/trigger-withdrawal/route.ts
@@ -15,37 +15,64 @@ export async function GET(req: Request) {
try {
await verifyVercelSignature(req);
- const [stripeBalanceData, processingInvoices, processedPayouts] =
- await Promise.all([
- stripe.balance.retrieve(),
- prisma.invoice.aggregate({
- where: {
- status: "processing",
- createdAt: {
- // Why we're doing this: ACH payments usually take up to 4 business days to settle
- // but in case it settles earlier, we'd want to keep the balance on Stripe for payouts
- // So, we're including "processing" invoices created more than 3 days ago in the reserve balance as well
- lt: new Date(new Date().setDate(new Date().getDate() - 3)),
- },
- },
- _sum: {
- amount: true,
+ const THREE_DAYS_AGO = new Date(
+ new Date().setDate(new Date().getDate() - 3),
+ );
+ const ONE_DAY_AGO = new Date(new Date().setDate(new Date().getDate() - 1));
+
+ const [
+ stripeBalanceData,
+ processingInvoicesDirectDebit,
+ processingInvoicesDirectDebitFast,
+ processedPayouts,
+ ] = await Promise.all([
+ stripe.balance.retrieve(),
+ prisma.invoice.aggregate({
+ where: {
+ status: "processing",
+ paymentMethod: {
+ in: ["ach", "sepa", "acss"],
},
- }),
- prisma.payout.aggregate({
- where: {
- status: "processed",
+ createdAt: {
+ // Why we're doing this: Direct debit payments usually take up to 4 business days to settle
+ // but in case it settles earlier, we'd want to keep the balance on Stripe for payouts
+ // So, we're including "processing" invoices created more than 3 days ago in the reserve balance as well
+ lt: THREE_DAYS_AGO,
},
- _sum: {
- amount: true,
+ },
+ _sum: {
+ amount: true,
+ },
+ }),
+ prisma.invoice.aggregate({
+ where: {
+ status: "processing",
+ paymentMethod: "ach_fast",
+ createdAt: {
+ // Same reasoning as above, but fast ACH can settle in 2 business days instead of 4
+ lt: ONE_DAY_AGO,
},
- }),
- ]);
+ },
+ _sum: {
+ amount: true,
+ },
+ }),
+ prisma.payout.aggregate({
+ where: {
+ status: "processed",
+ },
+ _sum: {
+ amount: true,
+ },
+ }),
+ ]);
const currentAvailableBalance = stripeBalanceData.available[0].amount; // available to withdraw
const currentPendingBalance = stripeBalanceData.pending[0].amount; // balance waiting to settle
- const processingInvoicesAmount = processingInvoices._sum.amount ?? 0;
+ const processingInvoicesAmount =
+ (processingInvoicesDirectDebit._sum.amount ?? 0) +
+ (processingInvoicesDirectDebitFast._sum.amount ?? 0);
const processedPayoutsAmount = processedPayouts._sum.amount ?? 0;
const amountToKeepOnStripe =
@@ -54,18 +81,18 @@ export async function GET(req: Request) {
const balanceToWithdraw = currentAvailableBalance - amountToKeepOnStripe;
console.log({
- currentAvailableBalance,
- currentPendingBalance,
- processingInvoicesAmount,
- processedPayoutsAmount,
- amountToKeepOnStripe,
- balanceToWithdraw,
+ currentAvailableBalance: `${currencyFormatter(currentAvailableBalance / 100)}`,
+ currentPendingBalance: `${currencyFormatter(currentPendingBalance / 100)}`,
+ processingInvoicesAmount: `${currencyFormatter(processingInvoicesAmount / 100)}`,
+ processedPayoutsAmount: `${currencyFormatter(processedPayoutsAmount / 100)}`,
+ amountToKeepOnStripe: `${currencyFormatter(amountToKeepOnStripe / 100)}`,
+ balanceToWithdraw: `${currencyFormatter(balanceToWithdraw / 100)}`,
});
const reservedBalance = 50000; // keep at least $500 in the account
if (balanceToWithdraw <= reservedBalance) {
return logAndRespond(
- `Balance to withdraw is less than ${currencyFormatter(reservedBalance / 100, { currency: "usd" })}, skipping...`,
+ `Balance to withdraw is less than ${currencyFormatter(reservedBalance / 100)}, skipping...`,
);
}
@@ -75,7 +102,7 @@ export async function GET(req: Request) {
});
return logAndRespond(
- `Created payout: ${createdPayout.id} (${currencyFormatter(createdPayout.amount / 100, { currency: createdPayout.currency })})`,
+ `Created payout: ${createdPayout.id} (${currencyFormatter(createdPayout.amount / 100)})`,
);
} catch (error) {
return handleAndReturnErrorResponse(error);
diff --git a/apps/web/app/(ee)/api/stripe/webhook/charge-succeeded.ts b/apps/web/app/(ee)/api/stripe/webhook/charge-succeeded.ts
index 4ab5444c53..3b3ff1a4a2 100644
--- a/apps/web/app/(ee)/api/stripe/webhook/charge-succeeded.ts
+++ b/apps/web/app/(ee)/api/stripe/webhook/charge-succeeded.ts
@@ -4,7 +4,7 @@ import { sendBatchEmail } from "@dub/email";
import DomainRenewed from "@dub/email/templates/domain-renewed";
import { prisma } from "@dub/prisma";
import { Invoice } from "@dub/prisma/client";
-import { APP_DOMAIN_WITH_NGROK, log, pluralize } from "@dub/utils";
+import { APP_DOMAIN_WITH_NGROK, pluralize } from "@dub/utils";
import { addDays } from "date-fns";
import Stripe from "stripe";
@@ -70,14 +70,6 @@ async function processPayoutInvoice({ invoice }: { invoice: Invoice }) {
return;
}
- if (invoice.id === "inv_1K858PN9HDXYG47RVC6C6ZT7N") {
- await log({
- message: `Temporarily skipping payouts for invoice ${invoice.id}.`,
- type: "payouts",
- });
- return;
- }
-
const qstashResponse = await qstash.publishJSON({
url: `${APP_DOMAIN_WITH_NGROK}/api/cron/payouts/charge-succeeded`,
body: {