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.

Omit tax amount from sale total in Stripe integration (#3030)

Steven Teyda2d4c145012
diff --git a/apps/web/app/(ee)/api/stripe/integration/webhook/checkout-session-completed.ts b/apps/web/app/(ee)/api/stripe/integration/webhook/checkout-session-completed.ts
index fcc8153a77..2c0c82805f 100644
--- a/apps/web/app/(ee)/api/stripe/integration/webhook/checkout-session-completed.ts
+++ b/apps/web/app/(ee)/api/stripe/integration/webhook/checkout-session-completed.ts
@@ -262,7 +262,11 @@ export async function checkoutSessionCompleted(
return "No stripeCustomerId or dubCustomerExternalId found in Stripe checkout session metadata, skipping...";
}
- if (charge.amount_total === 0) {
+ let chargeAmountTotal =
+ (charge.amount_total ?? 0) - (charge.total_details?.amount_tax ?? 0);
+
+ // should never be below 0, but just in case
+ if (chargeAmountTotal <= 0) {
return `Checkout session completed for Stripe customer ${stripeCustomerId} with invoice ID ${invoiceId} but amount is 0, skipping...`;
}
@@ -282,7 +286,7 @@ export async function checkoutSessionCompleted(
invoiceId,
customerId: customer.id,
workspaceId: customer.projectId,
- amount: charge.amount_total,
+ amount: chargeAmountTotal,
currency: charge.currency,
},
{
@@ -300,11 +304,11 @@ export async function checkoutSessionCompleted(
}
}
- if (charge.currency && charge.currency !== "usd" && charge.amount_total) {
+ if (charge.currency && charge.currency !== "usd" && chargeAmountTotal) {
// support for Stripe Adaptive Pricing: https://docs.stripe.com/payments/checkout/adaptive-pricing
if (charge.currency_conversion) {
charge.currency = charge.currency_conversion.source_currency;
- charge.amount_total = charge.currency_conversion.amount_total;
+ chargeAmountTotal = charge.currency_conversion.amount_total;
// if Stripe Adaptive Pricing is not enabled, we convert the amount to USD based on the current FX rate
// TODO: allow custom "defaultCurrency" on workspace table in the future
@@ -312,11 +316,11 @@ export async function checkoutSessionCompleted(
const { currency: convertedCurrency, amount: convertedAmount } =
await convertCurrency({
currency: charge.currency,
- amount: charge.amount_total,
+ amount: chargeAmountTotal,
});
charge.currency = convertedCurrency;
- charge.amount_total = convertedAmount;
+ chargeAmountTotal = convertedAmount;
}
}
@@ -328,7 +332,7 @@ export async function checkoutSessionCompleted(
event_name:
charge.mode === "payment" ? "Purchase" : "Subscription creation",
payment_processor: "stripe",
- amount: charge.amount_total!,
+ amount: chargeAmountTotal,
currency: charge.currency!,
invoice_id: invoiceId || "",
metadata: JSON.stringify({
@@ -374,7 +378,7 @@ export async function checkoutSessionCompleted(
increment: 1,
},
saleAmount: {
- increment: charge.amount_total!,
+ increment: chargeAmountTotal,
},
},
include: includeTags,
@@ -402,7 +406,7 @@ export async function checkoutSessionCompleted(
increment: 1,
},
saleAmount: {
- increment: charge.amount_total!,
+ increment: chargeAmountTotal,
},
},
}),
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 76bb0694d3..b7ecb3f243 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
@@ -66,6 +66,8 @@ 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;
+
// Skip if invoice id is already processed
const ok = await redis.set(
`trackSale:stripe:invoiceId:${invoiceId}`, // here we assume that Stripe's invoice ID is unique across all customers
@@ -77,7 +79,7 @@ export async function invoicePaid(event: Stripe.Event, mode: StripeMode) {
invoiceId,
customerId: customer.id,
workspaceId: customer.projectId,
- amount: invoice.amount_paid,
+ amount: invoiceSaleAmount,
currency: invoice.currency,
},
{
@@ -94,7 +96,7 @@ export async function invoicePaid(event: Stripe.Event, mode: StripeMode) {
return `Invoice with ID ${invoiceId} already processed, skipping...`;
}
- if (invoice.amount_paid === 0) {
+ if (invoiceSaleAmount === 0) {
return `Invoice with ID ${invoiceId} has an amount of 0, skipping...`;
}
@@ -104,11 +106,11 @@ export async function invoicePaid(event: Stripe.Event, mode: StripeMode) {
const { currency: convertedCurrency, amount: convertedAmount } =
await convertCurrency({
currency: invoice.currency,
- amount: invoice.amount_paid,
+ amount: invoiceSaleAmount,
});
invoice.currency = convertedCurrency;
- invoice.amount_paid = convertedAmount;
+ invoiceSaleAmount = convertedAmount;
}
// Find lead
@@ -130,7 +132,7 @@ export async function invoicePaid(event: Stripe.Event, mode: StripeMode) {
event_id: eventId,
event_name: isOneTimePayment ? "Purchase" : "Invoice paid",
payment_processor: "stripe",
- amount: invoice.amount_paid,
+ amount: invoiceSaleAmount,
currency: invoice.currency,
invoice_id: invoiceId,
metadata: JSON.stringify({
@@ -173,7 +175,7 @@ export async function invoicePaid(event: Stripe.Event, mode: StripeMode) {
increment: 1,
},
saleAmount: {
- increment: invoice.amount_paid,
+ increment: invoiceSaleAmount,
},
},
include: includeTags,
@@ -200,7 +202,7 @@ export async function invoicePaid(event: Stripe.Event, mode: StripeMode) {
increment: 1,
},
saleAmount: {
- increment: invoice.amount_paid,
+ increment: invoiceSaleAmount,
},
},
}),