The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Add fast settlement option to payout processing
Kiran Kb5515c53ab84
diff --git a/apps/web/app/(ee)/api/cron/payouts/process/process-payouts.ts b/apps/web/app/(ee)/api/cron/payouts/process/process-payouts.tsindex 8896e06e7a..0ad2c7ecf0 100644--- a/apps/web/app/(ee)/api/cron/payouts/process/process-payouts.ts+++ b/apps/web/app/(ee)/api/cron/payouts/process/process-payouts.ts@@ -33,6 +33,7 @@ export async function processPayouts({paymentMethodId,cutoffPeriod,excludedPayoutIds,+ fastSettlement,}: {workspace: Pick<Project,@@ -50,6 +51,7 @@ export async function processPayouts({paymentMethodId: string;cutoffPeriod?: CUTOFF_PERIOD_TYPES;excludedPayoutIds?: string[];+ fastSettlement: boolean;}) {const cutoffPeriodValue = CUTOFF_PERIOD.find((c) => c.id === cutoffPeriod,@@ -177,6 +179,14 @@ export async function processPayouts({customer: workspace.stripeId!,payment_method_types: [paymentMethod.type],payment_method: paymentMethod.id,+ ...(paymentMethod.type === "us_bank_account" &&+ fastSettlement && {+ payment_method_options: {+ us_bank_account: {+ preferred_settlement_speed: "fastest",+ },+ },+ }),currency,confirmation_method: "automatic",confirm: true,@@ -249,6 +259,7 @@ export async function processPayouts({amount: payout.amount,startDate: payout.periodStart,endDate: payout.periodEnd,+ fastSettlement,},}),})),diff --git a/apps/web/app/(ee)/api/cron/payouts/process/route.ts b/apps/web/app/(ee)/api/cron/payouts/process/route.tsindex cc72cde793..e95bb7e1be 100644--- a/apps/web/app/(ee)/api/cron/payouts/process/route.ts+++ b/apps/web/app/(ee)/api/cron/payouts/process/route.ts@@ -17,6 +17,7 @@ const processPayoutsCronSchema = z.object({paymentMethodId: z.string(),cutoffPeriod: CUTOFF_PERIOD_ENUM,excludedPayoutIds: z.array(z.string()).optional(),+ fastSettlement: z.boolean(),});// POST /api/cron/payouts/process@@ -35,6 +36,7 @@ export async function POST(req: Request) {paymentMethodId,cutoffPeriod,excludedPayoutIds,+ fastSettlement,} = processPayoutsCronSchema.parse(JSON.parse(rawBody));const workspace = await prisma.project.findUniqueOrThrow({@@ -65,6 +67,7 @@ export async function POST(req: Request) {paymentMethodId,cutoffPeriod,excludedPayoutIds,+ fastSettlement,});return new Response(`Payouts confirmed for program ${program.name}.`);diff --git a/apps/web/lib/actions/partners/confirm-payouts.ts b/apps/web/lib/actions/partners/confirm-payouts.tsindex dca623b3d7..5f5e6c2ad2 100644--- a/apps/web/lib/actions/partners/confirm-payouts.ts+++ b/apps/web/lib/actions/partners/confirm-payouts.ts@@ -16,6 +16,7 @@ const confirmPayoutsSchema = z.object({paymentMethodId: z.string(),cutoffPeriod: CUTOFF_PERIOD_ENUM,excludedPayoutIds: z.array(z.string()).optional(),+ fastSettlement: z.boolean().optional().default(false),amount: z.number(),fee: z.number(),total: z.number(),@@ -30,6 +31,7 @@ export const confirmPayoutsAction = authActionClientpaymentMethodId,cutoffPeriod,excludedPayoutIds,+ fastSettlement,amount,fee,total,@@ -47,6 +49,12 @@ export const confirmPayoutsAction = authActionClientthrow new Error("Workspace does not have a valid Stripe ID.");}+ if (fastSettlement && !workspace.fasterAchPayouts) {+ throw new Error(+ "Fast settlement is not enabled for this program. Contact sales to enable it.",+ );+ }+// if workspace's payouts usage + the current invoice amount// is greater than the workspace's payouts limit, throw an errorif (workspace.payoutsUsage + amount > workspace.payoutsLimit) {@@ -79,6 +87,10 @@ export const confirmPayoutsAction = authActionClient);}+ if (fastSettlement && paymentMethod.type !== "us_bank_account") {+ throw new Error("Fast settlement is only supported for ACH payment.");+ }+const invoice = await prisma.$transaction(async (tx) => {// Generate the next invoice number by counting the number of invoices for the workspaceconst totalInvoices = await tx.invoice.count({@@ -86,6 +98,7 @@ export const confirmPayoutsAction = authActionClientworkspaceId: workspace.id,},});+const paddedNumber = String(totalInvoices + 1).padStart(4, "0");const invoiceNumber = `${workspace.invoicePrefix}-${paddedNumber}`;@@ -101,6 +114,7 @@ export const confirmPayoutsAction = authActionClientamount,fee,total,+ fastSettlement,},});});@@ -115,6 +129,7 @@ export const confirmPayoutsAction = authActionClientpaymentMethodId,cutoffPeriod,excludedPayoutIds,+ fastSettlement,},});diff --git a/apps/web/lib/zod/schemas/workspaces.ts b/apps/web/lib/zod/schemas/workspaces.tsindex c31c91e028..d64c4ca289 100644--- a/apps/web/lib/zod/schemas/workspaces.ts+++ b/apps/web/lib/zod/schemas/workspaces.ts@@ -168,6 +168,7 @@ export const WorkspaceSchemaExtended = WorkspaceSchema.extend({),publishableKey: z.string().nullable(),ssoEmailDomain: z.string().nullable(),+ fasterAchPayouts: z.boolean().nullable().default(false),});export const OnboardingUsageSchema = z.object({diff --git a/packages/email/src/templates/partner-payout-confirmed.tsx b/packages/email/src/templates/partner-payout-confirmed.tsxindex 538a974173..c1ecf8963b 100644--- a/packages/email/src/templates/partner-payout-confirmed.tsx+++ b/packages/email/src/templates/partner-payout-confirmed.tsx@@ -27,6 +27,7 @@ export default function PartnerPayoutConfirmed({amount: 490,startDate: new Date("2024-11-01"),endDate: new Date("2024-11-30"),+ fastSettlement: true,},}: {email: string;@@ -40,6 +41,7 @@ export default function PartnerPayoutConfirmed({amount: number;startDate?: Date | null;endDate?: Date | null;+ fastSettlement?: boolean;};}) {const saleAmountInDollars = currencyFormatter(payout.amount / 100);@@ -100,8 +102,13 @@ export default function PartnerPayoutConfirmed({<Text className="text-sm leading-6 text-neutral-600">The payout is currently being processed and is expected to be- credited to your account within 5 business days (excluding- weekends and public holidays).+ credited to your account within+ <strong>+ {payout.fastSettlement+ ? " 2 business days"+ : " 5 business days"}+ </strong>{" "}+ (excluding weekends and public holidays).</Text><Section className="mb-12 mt-8">diff --git a/packages/prisma/schema/invoice.prisma b/packages/prisma/schema/invoice.prismaindex 6e199b29b9..8da45ad281 100644--- a/packages/prisma/schema/invoice.prisma+++ b/packages/prisma/schema/invoice.prisma@@ -13,9 +13,10 @@ model Invoice {id String @id @default(cuid())programId String?workspaceId String- number String? @unique // This starts with the customer’s unique invoicePrefix+ number String? @unique // This starts with the customer's unique invoicePrefixstatus InvoiceStatus @default(processing)type InvoiceType @default(partnerPayout)+ fastSettlement Boolean?amount Int @default(0) // amount in usd centsfee Int @default(0) // fee in usd centstotal Int @default(0) // amount + fee in usd centsdiff --git a/packages/prisma/schema/workspace.prisma b/packages/prisma/schema/workspace.prismaindex a6188646de..e28d5a437b 100644--- a/packages/prisma/schema/workspace.prisma+++ b/packages/prisma/schema/workspace.prisma@@ -41,11 +41,12 @@ model Project {allowedHostnames Json?publishableKey String? @unique // for the client-side publishable key- conversionEnabled Boolean @default(false) // Whether to enable conversion tracking for links by default- webhookEnabled Boolean @default(false)- ssoEnabled Boolean @default(false) // TODO: this is not used- dotLinkClaimed Boolean @default(false)- ssoEmailDomain String? @unique+ conversionEnabled Boolean @default(false) // Whether to enable conversion tracking for links by default+ webhookEnabled Boolean @default(false)+ ssoEnabled Boolean @default(false) // TODO: this is not used+ dotLinkClaimed Boolean @default(false)+ ssoEmailDomain String? @unique+ fasterAchPayouts Boolean?createdAt DateTime @default(now())updatedAt DateTime @updatedAt