The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Add convertCurrency for all track sale endpoints
Steven Tey16d68a3e2c35
diff --git a/apps/web/app/api/stripe/integration/webhook/checkout-session-completed.ts b/apps/web/app/api/stripe/integration/webhook/checkout-session-completed.tsindex 91ea69251e..11a7e0f8d1 100644--- a/apps/web/app/api/stripe/integration/webhook/checkout-session-completed.ts+++ b/apps/web/app/api/stripe/integration/webhook/checkout-session-completed.ts@@ -1,3 +1,4 @@+import { convertCurrency } from "@/lib/analytics/convert-currency";import { createId } from "@/lib/api/create-id";import { includeTags } from "@/lib/api/links/include-tags";import { notifyPartnerSale } from "@/lib/api/partners/notify-partner-sale";@@ -196,10 +197,24 @@ export async function checkoutSessionCompleted(event: Stripe.Event) {}}- // support for Stripe Adaptive Pricing: https://docs.stripe.com/payments/checkout/adaptive-pricing- if (charge.currency !== "usd" && charge.currency_conversion) {- charge.amount_total = charge.currency_conversion.amount_total;- charge.currency = charge.currency_conversion.source_currency;+ if (charge.currency && charge.currency !== "usd" && charge.amount_total) {+ // support for Stripe Adaptive Pricing: https://docs.stripe.com/payments/checkout/adaptive-pricing+ if (charge.currency_conversion) {+ charge.amount_total = charge.currency_conversion.amount_total;+ charge.currency = charge.currency_conversion.source_currency;++ // 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+ } else {+ const { currency: convertedCurrency, amount: convertedAmount } =+ await convertCurrency({+ currency: charge.currency,+ amount: charge.amount_total,+ });++ charge.amount_total = convertedAmount;+ charge.currency = convertedCurrency;+ }}const eventId = nanoid(16);diff --git a/apps/web/app/api/stripe/integration/webhook/invoice-paid.ts b/apps/web/app/api/stripe/integration/webhook/invoice-paid.tsindex 203e92042d..6cf68c9436 100644--- a/apps/web/app/api/stripe/integration/webhook/invoice-paid.ts+++ b/apps/web/app/api/stripe/integration/webhook/invoice-paid.ts@@ -1,3 +1,4 @@+import { convertCurrency } from "@/lib/analytics/convert-currency";import { createId } from "@/lib/api/create-id";import { includeTags } from "@/lib/api/links/include-tags";import { notifyPartnerSale } from "@/lib/api/partners/notify-partner-sale";@@ -48,6 +49,19 @@ export async function invoicePaid(event: Stripe.Event) {return `Invoice with ID ${invoiceId} has an amount of 0, skipping...`;}+ // if currency is not USD, convert it to USD based on the current FX rate+ // TODO: allow custom "defaultCurrency" on workspace table in the future+ if (invoice.currency && invoice.currency !== "usd") {+ const { currency: convertedCurrency, amount: convertedAmount } =+ await convertCurrency({+ currency: invoice.currency,+ amount: invoice.amount_paid,+ });++ invoice.currency = convertedCurrency;+ invoice.amount_paid = convertedAmount;+ }+// Find leadconst leadEvent = await getLeadEvent({ customerId: customer.id });if (!leadEvent || leadEvent.data.length === 0) {diff --git a/apps/web/app/api/track/sale/route.ts b/apps/web/app/api/track/sale/route.tsindex ee04604f70..c7b9195252 100644--- a/apps/web/app/api/track/sale/route.ts+++ b/apps/web/app/api/track/sale/route.ts@@ -1,3 +1,4 @@+import { convertCurrency } from "@/lib/analytics/convert-currency";import { createId } from "@/lib/api/create-id";import { DubApiError } from "@/lib/api/errors";import { includeTags } from "@/lib/api/links/include-tags";@@ -116,13 +117,11 @@ export const POST = withWorkspace(// if currency is not USD, convert it to USD based on the current FX rate// TODO: allow custom "defaultCurrency" on workspace table in the futureif (currency !== "usd") {- const fxRates = await redis.hget("fxRates:usd", currency.toUpperCase()); // e.g. for MYR it'll be around 4.4- if (fxRates) {- currency = "usd";- // convert amount to USD (in cents) based on the current FX rate- // round it to 0 decimal places- amount = Math.round(amount / Number(fxRates));- }+ const { currency: convertedCurrency, amount: convertedAmount } =+ await convertCurrency({ currency, amount });++ currency = convertedCurrency;+ amount = convertedAmount;}const eventId = nanoid(16);diff --git a/apps/web/lib/analytics/convert-currency.ts b/apps/web/lib/analytics/convert-currency.tsnew file mode 100644index 0000000000..b5c3b99323--- /dev/null+++ b/apps/web/lib/analytics/convert-currency.ts@@ -0,0 +1,25 @@+import { redis } from "../upstash/redis";++export const convertCurrency = async ({+ currency,+ amount,+}: {+ currency: string;+ amount: number;+}) => {+ const fxRates = await redis.hget("fxRates:usd", currency.toUpperCase()); // e.g. for MYR it'll be around 4.4++ if (fxRates) {+ return {+ currency: "usd",+ // convert amount to USD (in cents) based on the current FX rate+ // round it to 0 decimal places+ amount: Math.round(amount / Number(fxRates)),+ };+ }++ return {+ currency,+ amount,+ };+};