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.

Add `source_transaction` to `stripe.transfers.create`

Kiran Kbf6051f8fa60
diff --git a/apps/web/app/(ee)/api/cron/payouts/charge-succeeded/route.ts b/apps/web/app/(ee)/api/cron/payouts/charge-succeeded/route.ts
index 5264ccb421..e9515457a3 100644
--- a/apps/web/app/(ee)/api/cron/payouts/charge-succeeded/route.ts
+++ b/apps/web/app/(ee)/api/cron/payouts/charge-succeeded/route.ts
@@ -12,6 +12,10 @@ const payloadSchema = z.object({
invoiceId: z.string(),
});
+const stripeChargeMetadataSchema = z.object({
+ id: z.string(), // Stripe charge id
+});
+
// POST /api/cron/payouts/charge-succeeded
// This route is used to process the charge-succeeded event from Stripe
// we're intentionally offloading this to a cron job to avoid blocking the main thread
@@ -54,9 +58,15 @@ export async function POST(req: Request) {
);
}
+ // Find the id of the charge that was used to fund the transfer
+ const { id: chargeId } = stripeChargeMetadataSchema.parse(
+ invoice.stripeChargeMetadata,
+ );
+
await Promise.allSettled([
sendStripePayouts({
invoiceId,
+ chargeId,
}),
sendPaypalPayouts({
diff --git a/apps/web/app/(ee)/api/cron/payouts/charge-succeeded/send-stripe-payouts.ts b/apps/web/app/(ee)/api/cron/payouts/charge-succeeded/send-stripe-payouts.ts
index ce333d7e36..b1a7baa841 100644
--- a/apps/web/app/(ee)/api/cron/payouts/charge-succeeded/send-stripe-payouts.ts
+++ b/apps/web/app/(ee)/api/cron/payouts/charge-succeeded/send-stripe-payouts.ts
@@ -4,7 +4,13 @@ import PartnerPayoutProcessed from "@dub/email/templates/partner-payout-processe
import { prisma } from "@dub/prisma";
import { Prisma } from "@prisma/client";
-export async function sendStripePayouts({ invoiceId }: { invoiceId: string }) {
+export async function sendStripePayouts({
+ invoiceId,
+ chargeId,
+}: {
+ invoiceId: string;
+ chargeId: string;
+}) {
const commonInclude = Prisma.validator<Prisma.PayoutInclude>()({
partner: {
select: {
@@ -85,6 +91,7 @@ export async function sendStripePayouts({ invoiceId }: { invoiceId: string }) {
currentInvoicePayouts: partnerPayouts.filter(
(p) => p.invoiceId === invoiceId,
),
+ chargeId,
});
// sleep for 250ms
diff --git a/apps/web/lib/partners/create-stripe-transfer.ts b/apps/web/lib/partners/create-stripe-transfer.ts
index c6f2a49417..d60ae75333 100644
--- a/apps/web/lib/partners/create-stripe-transfer.ts
+++ b/apps/web/lib/partners/create-stripe-transfer.ts
@@ -17,10 +17,12 @@ export const createStripeTransfer = async ({
partner,
previouslyProcessedPayouts,
currentInvoicePayouts,
+ chargeId,
}: {
partner: Pick<Partner, "id" | "minWithdrawalAmount" | "stripeConnectId">;
previouslyProcessedPayouts: PayoutWithProgramName[];
currentInvoicePayouts?: PayoutWithProgramName[];
+ chargeId?: string;
}) => {
// this should never happen since we guard for it outside, but just in case
if (!partner.stripeConnectId) {
@@ -103,6 +105,11 @@ export const createStripeTransfer = async ({
transfer_group: finalPayoutInvoiceId!,
destination: partner.stripeConnectId,
description: `Dub Partners payout ${pluralize("transfer", allPayoutsPrograms.length)} (${allPayoutsPrograms.join(", ")})`,
+ // Omit `source_transaction` if prior processed payouts exist to ensure this transfer
+ // never exceeds the original charge amount.
+ ...(previouslyProcessedPayouts.length === 0 && {
+ source_transaction: chargeId,
+ }),
},
{
idempotencyKey: `${finalPayoutInvoiceId}-${partner.id}`,