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.

Improve `invoice.paid` webhook to handle upsells (#3608)

Steven Tey78aa21202053
diff --git a/apps/web/app/(ee)/api/cron/send-batch-email/route.ts b/apps/web/app/(ee)/api/cron/send-batch-email/route.ts
index 1c4120aa07..9aa59c08d2 100644
--- a/apps/web/app/(ee)/api/cron/send-batch-email/route.ts
+++ b/apps/web/app/(ee)/api/cron/send-batch-email/route.ts
@@ -16,7 +16,7 @@ const batchEmailPayloadSchema = z.array(
Object.keys(EMAIL_TEMPLATES_MAP) as [string, ...string[]],
),
templateProps: z.record(z.string(), z.any()),
- to: z.email(),
+ to: z.string(),
from: z.string().optional(),
subject: z.string(),
bcc: z.union([z.string(), z.array(z.string())]).optional(),
diff --git a/apps/web/app/(ee)/api/stripe/integration/webhook/invoice-paid.ts b/apps/web/app/(ee)/api/stripe/integration/webhook/invoice-paid.ts
index 1ce63a3c55..71918cd034 100644
--- a/apps/web/app/(ee)/api/stripe/integration/webhook/invoice-paid.ts
+++ b/apps/web/app/(ee)/api/stripe/integration/webhook/invoice-paid.ts
@@ -69,7 +69,12 @@ export async function invoicePaid(event: Stripe.Event, mode: StripeMode) {
return `Customer with stripeCustomerId ${stripeCustomerId} not found on Dub (nor does the connected customer ${stripeCustomerId} have a valid dubCustomerExternalId), skipping...`;
}
- let invoiceSaleAmount = invoice.total_excluding_tax ?? invoice.amount_paid;
+ // Sale amount excluding tax: use total_excluding_tax only when invoice was paid in full
+ // (amount_paid === total); otherwise use amount_paid (e.g. credits applied, upsells, etc.).
+ let invoiceSaleAmount =
+ invoice.amount_paid === invoice.total && invoice.total_excluding_tax != null
+ ? invoice.total_excluding_tax
+ : invoice.amount_paid;
// Skip if invoice id is already processed
const ok = await redis.set(