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.

resolve partner promo codes from invoice line-item discounts

Pedro Ladeira3b4fc1eb8765
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 9cae9a5037..b10a67a567 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
@@ -438,18 +438,30 @@ async function resolvePromotionCodeIdFromInvoice({
> {
const stripe = stripeAppClient({ mode });
+ type ExpandedDiscount = {
+ promotion_code: Stripe.PromotionCode | null;
+ };
+
const expandedInvoice = (await stripe.invoices.retrieve(
invoiceId,
{
- expand: ["discounts", "discounts.promotion_code"],
+ expand: [
+ "discounts",
+ "discounts.promotion_code",
+ "lines.data.discounts",
+ "lines.data.discounts.promotion_code",
+ ],
},
{
stripeAccount: stripeAccountId,
},
- )) as Omit<Stripe.Invoice, "discounts"> & {
- discounts: {
- promotion_code: Stripe.PromotionCode | null;
- }[];
+ )) as Omit<Stripe.Invoice, "discounts" | "lines"> & {
+ discounts: ExpandedDiscount[];
+ lines: {
+ data: {
+ discounts: (string | ExpandedDiscount)[];
+ }[];
+ };
};
if (!expandedInvoice) {
@@ -459,14 +471,24 @@ async function resolvePromotionCodeIdFromInvoice({
};
}
- if (!expandedInvoice.discounts || expandedInvoice.discounts.length === 0) {
+ const invoiceDiscounts = expandedInvoice.discounts ?? [];
+ const lineItemDiscounts = (expandedInvoice.lines?.data ?? []).flatMap(
+ (line) =>
+ (line.discounts ?? []).filter(
+ (discount): discount is ExpandedDiscount =>
+ typeof discount === "object" && discount !== null,
+ ),
+ );
+ const discounts = [...invoiceDiscounts, ...lineItemDiscounts];
+
+ if (discounts.length === 0) {
return {
promotionCodeId: null,
resolvePromotionCodeError: "No discounts found on invoice",
};
}
- const discountWithPromotionCode = expandedInvoice.discounts.find((discount) =>
+ const discountWithPromotionCode = discounts.find((discount) =>
Boolean(discount?.promotion_code?.id),
);