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.

Tapfiliate importer (#4050)

Kiran Kdc3ae58cc86f
diff --git a/apps/web/app/(ee)/api/cron/import/tapfiliate/route.ts b/apps/web/app/(ee)/api/cron/import/tapfiliate/route.ts
new file mode 100644
index 0000000000..e739c51c37
--- /dev/null
+++ b/apps/web/app/(ee)/api/cron/import/tapfiliate/route.ts
@@ -0,0 +1,38 @@
+import { withCron } from "@/lib/cron/with-cron";
+import { cleanupPartners } from "@/lib/tapfiliate/cleanup-partners";
+import { importCommissions } from "@/lib/tapfiliate/import-commissions";
+import { importCustomers } from "@/lib/tapfiliate/import-customers";
+import { importGroups } from "@/lib/tapfiliate/import-groups";
+import { importPartners } from "@/lib/tapfiliate/import-partners";
+import { tapfiliateImportPayloadSchema } from "@/lib/tapfiliate/schemas";
+import { updateStripeCustomers } from "@/lib/tapfiliate/update-stripe-customers";
+import { logAndRespond } from "../../utils";
+
+export const dynamic = "force-dynamic";
+
+export const POST = withCron(async ({ rawBody }) => {
+ const payload = tapfiliateImportPayloadSchema.parse(JSON.parse(rawBody));
+
+ switch (payload.action) {
+ case "import-groups":
+ await importGroups(payload);
+ break;
+ case "import-partners":
+ await importPartners(payload);
+ break;
+ case "import-customers":
+ await importCustomers(payload);
+ break;
+ case "import-commissions":
+ await importCommissions(payload);
+ break;
+ case "update-stripe-customers":
+ await updateStripeCustomers(payload);
+ break;
+ case "cleanup-partners":
+ await cleanupPartners(payload);
+ break;
+ }
+
+ return logAndRespond("OK");
+});
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/partners-menu-popover.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/partners-menu-popover.tsx
index c750da9ce2..ca526a1b76 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/partners-menu-popover.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/partners-menu-popover.tsx
@@ -6,6 +6,7 @@ import { useExportPartnersModal } from "@/ui/modals/export-partners-modal";
import { useImportFirstPromoterModal } from "@/ui/modals/import-firstpromoter-modal";
import { useImportPartnerStackModal } from "@/ui/modals/import-partnerstack-modal";
import { useImportRewardfulModal } from "@/ui/modals/import-rewardful-modal";
+import { useImportTapfiliateModal } from "@/ui/modals/import-tapfiliate-modal";
import { useImportToltModal } from "@/ui/modals/import-tolt-modal";
import { Download, ThreeDots } from "@/ui/shared/icons";
import { Button, IconMenu, Popover } from "@dub/ui";
@@ -21,6 +22,7 @@ export function PartnersMenuPopover() {
const { ImportRewardfulModal } = useImportRewardfulModal();
const { ImportPartnerStackModal } = useImportPartnerStackModal();
const { ImportFirstPromoterModal } = useImportFirstPromoterModal();
+ const { ImportTapfiliateModal } = useImportTapfiliateModal();
const { ExportPartnersModal, setShowExportPartnersModal } =
useExportPartnersModal();
@@ -31,6 +33,7 @@ export function PartnersMenuPopover() {
<ImportRewardfulModal />
<ImportFirstPromoterModal />
<ImportPartnerStackModal />
+ <ImportTapfiliateModal />
<ExportPartnersModal />
<Popover
content={
diff --git a/apps/web/lib/actions/partners/set-tapfiliate-token.ts b/apps/web/lib/actions/partners/set-tapfiliate-token.ts
new file mode 100644
index 0000000000..5f1f7e8b2f
--- /dev/null
+++ b/apps/web/lib/actions/partners/set-tapfiliate-token.ts
@@ -0,0 +1,51 @@
+"use server";
+
+import { TapfiliateApi } from "@/lib/tapfiliate/api";
+import { tapfiliateImporter } from "@/lib/tapfiliate/importer";
+import { TapfiliateProgram } from "@/lib/tapfiliate/types";
+import * as z from "zod/v4";
+import { authActionClient } from "../safe-action";
+import { throwIfNoPermission } from "../throw-if-no-permission";
+
+const schema = z.object({
+ workspaceId: z.string(),
+ apiKey: z.string().trim().min(1),
+});
+
+export const setTapfiliateTokenAction = authActionClient
+ .inputSchema(schema)
+ .action(async ({ parsedInput, ctx }) => {
+ const { workspace } = ctx;
+ const { apiKey } = parsedInput;
+
+ throwIfNoPermission({
+ role: workspace.role,
+ requiredRoles: ["owner", "member"],
+ });
+
+ const tapfiliateApi = new TapfiliateApi({
+ apiKey,
+ });
+
+ // Validate the API key by listing the account's programs
+ let programs: TapfiliateProgram[];
+
+ try {
+ programs = await tapfiliateApi.listPrograms();
+ } catch (error) {
+ console.error(error);
+ throw new Error("Invalid Tapfiliate API key.");
+ }
+
+ if (programs.length === 0) {
+ throw new Error("No programs found in your Tapfiliate account.");
+ }
+
+ await tapfiliateImporter.setCredentials(workspace.id, {
+ apiKey,
+ });
+
+ return {
+ programs,
+ };
+ });
diff --git a/apps/web/lib/actions/partners/start-tapfiliate-import.ts b/apps/web/lib/actions/partners/start-tapfiliate-import.ts
new file mode 100644
index 0000000000..54912fc3c7
--- /dev/null
+++ b/apps/web/lib/actions/partners/start-tapfiliate-import.ts
@@ -0,0 +1,65 @@
+"use server";
+
+import { createId } from "@/lib/api/create-id";
+import { getDefaultProgramIdOrThrow } from "@/lib/api/programs/get-default-program-id-or-throw";
+import { TapfiliateApi } from "@/lib/tapfiliate/api";
+import { tapfiliateImporter } from "@/lib/tapfiliate/importer";
+import * as z from "zod/v4";
+import { getProgramOrThrow } from "../../api/programs/get-program-or-throw";
+import { authActionClient } from "../safe-action";
+import { throwIfNoPermission } from "../throw-if-no-permission";
+
+const schema = z.object({
+ workspaceId: z.string(),
+ tapfiliateProgramId: z.string().trim().min(1),
+});
+
+export const startTapfiliateImportAction = authActionClient
+ .inputSchema(schema)
+ .action(async ({ ctx, parsedInput }) => {
+ const { workspace, user } = ctx;
+ const { tapfiliateProgramId } = parsedInput;
+
+ throwIfNoPermission({
+ role: workspace.role,
+ requiredRoles: ["owner", "member"],
+ });
+
+ const programId = getDefaultProgramIdOrThrow(workspace);
+
+ const program = await getProgramOrThrow({
+ workspaceId: workspace.id,
+ programId,
+ });
+
+ if (!program.domain) {
+ throw new Error("Program domain is not set.");
+ }
+
+ if (!program.url) {
+ throw new Error("Program URL is not set.");
+ }
+
+ const credentials = await tapfiliateImporter.getCredentials(workspace.id);
+
+ const tapfiliateApi = new TapfiliateApi({
+ apiKey: credentials.apiKey,
+ });
+
+ try {
+ await tapfiliateApi.getProgram({
+ programId: tapfiliateProgramId,
+ });
+ } catch (error) {
+ console.error(error);
+ throw new Error("Invalid Tapfiliate program ID.");
+ }
+
+ await tapfiliateImporter.queue({
+ importId: createId({ prefix: "import_" }),
+ userId: user.id,
+ programId: program.id,
+ tapfiliateProgramId,
+ action: "import-groups",
+ });
+ });
diff --git a/apps/web/lib/constants/program.ts b/apps/web/lib/constants/program.ts
index e303669667..c3cb7e2132 100644
--- a/apps/web/lib/constants/program.ts
+++ b/apps/web/lib/constants/program.ts
@@ -31,6 +31,12 @@ export const PROGRAM_IMPORT_SOURCES = [
image: "https://assets.dub.co/misc/icons/firstpromoter.svg",
helpUrl: "https://dub.co/help/article/migrating-from-firstpromoter",
},
+ {
+ id: "tapfiliate",
+ value: "Tapfiliate",
+ image: "https://assets.dub.co/misc/icons/tapfiliate.svg",
+ helpUrl: "https://dub.co/help/article/migrating-from-tapfiliate",
+ },
] as const;
export const PROGRAM_APPLICATION_IMAGE_MAX_FILE_SIZE_MB = 5;
diff --git a/apps/web/lib/tapfiliate/api.ts b/apps/web/lib/tapfiliate/api.ts
new file mode 100644
index 0000000000..9fcd00fdd8
--- /dev/null
+++ b/apps/web/lib/tapfiliate/api.ts
@@ -0,0 +1,102 @@
+import {
+ tapfiliateConversionSchema,
+ tapfiliateCustomerSchema,
+ tapfiliateGroupSchema,
+ tapfiliatePartnerSchema,
+ tapfiliateProgramSchema,
+} from "./schemas";
+
+export class TapfiliateApi {
+ private readonly baseUrl = "https://api.tapfiliate.com/1.6";
+ private readonly apiKey: string;
+
+ constructor({ apiKey }: { apiKey: string }) {
+ this.apiKey = apiKey;
+ }
+
+ private async fetch(path: string) {
+ const controller = new AbortController();
+ const timeout = setTimeout(() => controller.abort(), 15000);
+
+ const response = await fetch(`${this.baseUrl}${path}`, {
+ headers: {
+ "X-Api-Key": this.apiKey,
+ "Content-Type": "application/json",
+ },
+ signal: controller.signal,
+ });
+
+ clearTimeout(timeout);
+
+ if (!response.ok) {
+ const error = await response.text();
+
+ console.error("Tapfiliate API Error:", error);
+
+ throw new Error(
+ `[Tapfiliate API] ${error || `Request to ${path} failed with status ${response.status}.`}`,
+ );
+ }
+
+ return await response.json();
+ }
+
+ async listPrograms() {
+ const data = await this.fetch(`/programs/`);
+
+ return tapfiliateProgramSchema.array().parse(data);
+ }
+
+ async getProgram({ programId }: { programId: string }) {
+ const data = await this.fetch(`/programs/${programId}/`);
+
+ return tapfiliateProgramSchema.parse(data);
+ }
+
+ async listGroups() {
+ const data = await this.fetch(`/affiliate-groups/`);
+
+ return tapfiliateGroupSchema.array().parse(data);
+ }
+
+ async listPartners({ page = 1 }: { page?: number }) {
+ const data = await this.fetch(`/affiliates/?page=${page}`);
+
+ return tapfiliatePartnerSchema.array().parse(data);
+ }
+
+ async listCustomers({
+ programId,
+ page = 1,
+ }: {
+ programId: string;
+ page?: number;
+ }) {
+ const searchParams = new URLSearchParams({
+ program_id: programId,
+ page: page.toString(),
+ });
+
+ const data = await this.fetch(`/customers?${searchParams.toString()}`);
+
+ return tapfiliateCustomerSchema.array().parse(data);
+ }
+
+ // Fetch conversions and commissions
+ async listConversions({
+ programId,
+ page = 1,
+ }: {
+ programId: string;
+ page?: number;
+ }) {
+ const searchParams = new URLSearchParams({
+ program_id: programId,
+ page: page.toString(),
+ });
+
+ const data = await this.fetch(`/conversions/?${searchParams.toString()}`);
+
+ return tapfiliateConversionSchema.array().parse(data);
+ }
+}
diff --git a/apps/web/lib/tapfiliate/cleanup-partners.ts b/apps/web/lib/tapfiliate/cleanup-partners.ts
new file mode 100644
index 0000000000..23e45cada7
--- /dev/null
+++ b/apps/web/lib/tapfiliate/cleanup-partners.ts
@@ -0,0 +1,180 @@
+import { prisma } from "@/lib/prisma";
+import { sendEmail } from "@dub/email";
+import ProgramImported from "@dub/email/templates/program-imported";
+import { tapfiliateImporter } from "./importer";
+import { TapfiliateImportPayload } from "./types";
+
+const PARTNER_IDS_PER_BATCH = 100;
+
+// Remove partners with no leads and clean up orphaned partners
+export async function cleanupPartners(payload: TapfiliateImportPayload) {
+ const { programId, userId, importId } = payload;
+
+ let hasMore = true;
+ let start = 0;
+
+ while (hasMore) {
+ const partnerIds = await tapfiliateImporter.listImportedPartnerIds({
+ programId,
+ start,
+ end: start + PARTNER_IDS_PER_BATCH - 1,
+ });
+
+ if (partnerIds.length === 0) {
+ hasMore = false;
+ break;
+ }
+
+ const links = await prisma.link.groupBy({
+ by: ["programId", "partnerId"],
+ where: {
+ programId,
+ partnerId: {
+ in: partnerIds,
+ },
+ },
+ _sum: {
+ leads: true,
+ },
+ });
+
+ const linksWithNoLeads = links.filter((link) => link._sum.leads === 0);
+
+ const partnerIdsWithNoLeads = linksWithNoLeads
+ .map((link) => link.partnerId)
+ .filter((partnerId): partnerId is string => partnerId !== null);
+
+ if (partnerIdsWithNoLeads.length > 0) {
+ // Remove program enrollments and links for partners with no leads
+ await prisma.$transaction(async (tx) => {
+ await tx.programEnrollment.deleteMany({
+ where: {
+ programId,
+ partnerId: {
+ in: partnerIdsWithNoLeads,
+ },
+ },
+ });
+
+ await tx.link.deleteMany({
+ where: {
+ programId,
+ partnerId: {
+ in: partnerIdsWithNoLeads,
+ },
+ },
+ });
+ });
+
+ // Remove partners that are not enrolled in any other program
+ const otherProgramEnrollments = await prisma.programEnrollment.findMany({
+ where: {
+ partnerId: {
+ in: partnerIdsWithNoLeads,
+ },
+ programId: {
+ not: programId,
+ },
+ },
+ select: {
+ partnerId: true,
+ },
+ });
+
+ const enrolledPartnerIds = otherProgramEnrollments.map(
+ ({ partnerId }) => partnerId,
+ );
+
+ // Find partners that are not enrolled in any other program
+ const removablePartnerIds = partnerIdsWithNoLeads.filter(
+ (partnerId) => !enrolledPartnerIds.includes(partnerId),
+ );
+
+ // Find partners that have no user account
+ if (removablePartnerIds.length > 0) {
+ const partnersWithoutUserAccount = await prisma.partner.findMany({
+ where: {
+ id: {
+ in: removablePartnerIds,
+ },
+ users: {
+ none: {},
+ },
+ },
+ select: {
+ id: true,
+ email: true,
+ },
+ });
+
+ if (partnersWithoutUserAccount.length > 0) {
+ await prisma.partner.deleteMany({
+ where: {
+ id: {
+ in: partnersWithoutUserAccount.map(({ id }) => id),
+ },
+ },
+ });
+ }
+ }
+ }
+
+ start += PARTNER_IDS_PER_BATCH;
+ }
+
+ await tapfiliateImporter.clearImportedPartnerIds(programId);
+
+ // Import is finished, send the email to the workspace user
+ const program = await prisma.program.findUnique({
+ where: {
+ id: programId,
+ },
+ select: {
+ name: true,
+ workspace: {
+ select: {
+ id: true,
+ slug: true,
+ },
+ },
+ },
+ });
+
+ if (!program) {
+ console.error(`Program ${programId} not found.`);
+ return;
+ }
+
+ const workspaceUser = await prisma.projectUsers.findUniqueOrThrow({
+ where: {
+ userId_projectId: {
+ userId,
+ projectId: program.workspace.id,
+ },
+ },
+ select: {
+ user: {
+ select: {
+ email: true,
+ },
+ },
+ },
+ });
+
+ if (workspaceUser && workspaceUser.user.email) {
+ await sendEmail({
+ to: workspaceUser.user.email,
+ subject: "Tapfiliate program imported",
+ react: ProgramImported({
+ email: workspaceUser.user.email,
+ workspace: program.workspace,
+ program,
+ provider: "Tapfiliate",
+ importId,
+ }),
+ headers: {
+ "Idempotency-Key": importId,
+ },
+ });
+ }
+}
diff --git a/apps/web/lib/tapfiliate/import-commissions.ts b/apps/web/lib/tapfiliate/import-commissions.ts
new file mode 100644
index 0000000000..a86b6aae0a
--- /dev/null
+++ b/apps/web/lib/tapfiliate/import-commissions.ts
@@ -0,0 +1,367 @@
+import { prisma } from "@/lib/prisma";
+import { chunk, nanoid } from "@dub/utils";
+import { Customer, Link, Program } from "@prisma/client";
+import { convertCurrencyWithFxRates } from "../analytics/convert-currency";
+import { isFirstConversion } from "../analytics/is-first-conversion";
+import { createId } from "../api/create-id";
+import { updateLinkStatsForImporter } from "../api/links/update-link-stats-for-importer";
+import { syncPartnerLinksStats } from "../api/partners/sync-partner-links-stats";
+import { syncTotalCommissions } from "../api/partners/sync-total-commissions";
+import { getLeadEvents } from "../tinybird/get-lead-events";
+import { logImportError } from "../tinybird/log-import-error";
+import { recordSaleWithTimestamp } from "../tinybird/record-sale";
+import { LeadEventTB } from "../types";
+import { redis } from "../upstash";
+import { clickEventSchemaTB } from "../zod/schemas/clicks";
+import { TapfiliateApi } from "./api";
+import { TAPFILIATE_MAX_BATCHES, tapfiliateImporter } from "./importer";
+import {
+ TapfiliateConversionWithCommission,
+ TapfiliateImportPayload,
+} from "./types";
+
+export async function importCommissions(payload: TapfiliateImportPayload) {
+ const { importId, programId, tapfiliateProgramId, page = 1 } = payload;
+
+ const program = await prisma.program.findUnique({
+ where: {
+ id: programId,
+ },
+ select: {
+ id: true,
+ workspaceId: true,
+ },
+ });
+
+ if (!program) {
+ console.error(`Program ${programId} not found.`);
+ return;
+ }
+
+ const { apiKey } = await tapfiliateImporter.getCredentials(
+ program.workspaceId,
+ );
+
+ const tapfiliateApi = new TapfiliateApi({
+ apiKey,
+ });
+
+ const fxRates = await redis.hgetall<Record<string, string>>("fxRates:usd");
+
+ if (!fxRates) {
+ console.error("No FX rates found, skipping commission import...");
+ return;
+ }
+
+ let currentPage = page;
+ let hasMore = true;
+ let processedBatches = 0;
+
+ while (hasMore && processedBatches < TAPFILIATE_MAX_BATCHES) {
+ let conversions = await tapfiliateApi.listConversions({
+ programId: tapfiliateProgramId,
+ page: currentPage,
+ });
+
+ if (conversions.length === 0) {
+ hasMore = false;
+ break;
+ }
+
+ conversions = conversions.filter(
+ (conversion) => conversion.program?.id === tapfiliateProgramId,
+ );
+
+ const customerExternalIds = conversions
+ .map((conversion) => conversion.customer?.customer_id)
+ .filter((id): id is string => Boolean(id));
+
+ const customersData = await prisma.customer.findMany({
+ where: {
+ projectId: program.workspaceId,
+ externalId: {
+ in: customerExternalIds,
+ },
+ },
+ include: {
+ link: true,
+ },
+ orderBy: {
+ createdAt: "asc",
+ },
+ });
+
+ const customerLeadEvents = await getLeadEvents({
+ customerIds: customersData.map((customer) => customer.id),
+ }).then((res) => res.data);
+
+ const flattenedConversions = conversions.flatMap((conversion) =>
+ (conversion.commissions ?? []).map((commission) => ({
+ ...conversion,
+ commission,
+ })),
+ );
+
+ if (flattenedConversions.length > 0) {
+ const conversionChunks = chunk(flattenedConversions, 10);
+
+ for (const conversionChunk of conversionChunks) {
+ await Promise.all(
+ conversionChunk.map((conversion) =>
+ createCommission({
+ program,
+ conversion,
+ fxRates,
+ importId,
+ customersData,
+ customerLeadEvents,
+ }),
+ ),
+ );
+ }
+ }
+
+ currentPage++;
+ processedBatches++;
+ }
+
+ if (!hasMore) {
+ await tapfiliateImporter.deleteCredentials(program.workspaceId);
+ }
+
+ await tapfiliateImporter.queue({
+ ...payload,
+ action: hasMore ? "import-commissions" : "update-stripe-customers",
+ page: hasMore ? currentPage : undefined,
+ });
+}
+
+async function createCommission({
+ program,
+ conversion,
+ fxRates,
+ importId,
+ customersData,
+ customerLeadEvents,
+}: {
+ program: Pick<Program, "id" | "workspaceId">;
+ conversion: TapfiliateConversionWithCommission;
+ fxRates: Record<string, string> | null;
+ importId: string;
+ customersData: (Customer & { link: Link | null })[];
+ customerLeadEvents: LeadEventTB[];
+}) {
+ const { commission, customer } = conversion;
+
+ const commonImportLogInputs = {
+ workspace_id: program.workspaceId,
+ import_id: importId,
+ source: "tapfiliate",
+ entity: "commission",
+ entity_id: commission.id.toString(),
+ } as const;
+
+ const existingCommission = await prisma.commission.findUnique({
+ where: {
+ invoiceId_programId: {
+ invoiceId: commission.id.toString(),
+ programId: program.id,
+ },
+ },
+ select: {
+ id: true,
+ },
+ });
+
+ if (existingCommission) {
+ console.log(`Commission ${commission.id} already exists, skipping...`);
+ return;
+ }
+
+ if (!customer) {
+ await logImportError({
+ ...commonImportLogInputs,
+ code: "CUSTOMER_NOT_FOUND",
+ message: `A customer is not associated with this commission ${commission.id}, skipping...`,
+ });
+
+ return;
+ }
+
+ const existingCustomer = customersData.find(
+ ({ externalId }) => externalId === customer?.customer_id,
+ );
+
+ if (!existingCustomer) {
+ await logImportError({
+ ...commonImportLogInputs,
+ code: "CUSTOMER_NOT_FOUND",
+ message: `No customer ${customer.customer_id} found for commission ${commission.id}.`,
+ });
+
+ return;
+ }
+
+ let saleAmount = Math.round(commission.conversion_sub_amount * 100);
+ let earnings = Math.round(commission.amount * 100);
+
+ if (commission.currency.toUpperCase() !== "USD" && fxRates) {
+ const { amount: convertedSaleAmount } = convertCurrencyWithFxRates({
+ currency: commission.currency,
+ amount: saleAmount,
+ fxRates,
+ });
+
+ const { amount: convertedEarnings } = convertCurrencyWithFxRates({
+ currency: commission.currency,
+ amount: earnings,
+ fxRates,
+ });
+
+ saleAmount = convertedSaleAmount;
+ earnings = convertedEarnings;
+ }
+
+ if (!existingCustomer.linkId) {
+ await logImportError({
+ ...commonImportLogInputs,
+ code: "LINK_NOT_FOUND",
+ message: `No link found for customer ${existingCustomer.id}.`,
+ });
+
+ return;
+ }
+
+ if (!existingCustomer.clickId) {
+ await logImportError({
+ ...commonImportLogInputs,
+ code: "CLICK_NOT_FOUND",
+ message: `No click found for customer ${existingCustomer.id}.`,
+ });
+
+ return;
+ }
+
+ if (!existingCustomer.link?.partnerId) {
+ await logImportError({
+ ...commonImportLogInputs,
+ code: "PARTNER_NOT_FOUND",
+ message: `No partner found for customer ${existingCustomer.id}.`,
+ });
+
+ return;
+ }
+
+ const leadEvent = customerLeadEvents.find(
+ (event) => event.customer_id === existingCustomer.id,
+ );
+
+ if (!leadEvent) {
+ await logImportError({
+ ...commonImportLogInputs,
+ code: "LEAD_NOT_FOUND",
+ message: `No lead event found for customer ${existingCustomer.id}.`,
+ });
+
+ return;
+ }
+
+ const clickData = clickEventSchemaTB
+ .omit({ timestamp: true })
+ .parse(leadEvent);
+
+ const eventId = nanoid(16);
+
+ await Promise.all([
+ prisma.commission.create({
+ data: {
+ id: createId({ prefix: "cm_" }),
+ eventId,
+ type: "sale",
+ programId: program.id,
+ partnerId: existingCustomer.link.partnerId,
+ linkId: existingCustomer.linkId,
+ customerId: existingCustomer.id,
+ amount: saleAmount,
+ earnings,
+ currency: "usd",
+ quantity: 1,
+ status: commission.approved ? "paid" : "pending",
+ invoiceId: commission.id.toString(), // this is not the actual invoice ID, but we use this to deduplicate the sales
+ createdAt: new Date(commission.created_at),
+ },
+ }),
+
+ saleAmount > 0 &&
+ recordSaleWithTimestamp({
+ ...clickData,
+ event_id: eventId,
+ event_name: "Invoice paid",
+ amount: saleAmount,
+ customer_id: existingCustomer.id,
+ payment_processor: "stripe",
+ currency: "usd",
+ metadata: JSON.stringify(commission),
+ timestamp: new Date(commission.created_at).toISOString(),
+ }),
+
+ // update link stats (if sale amount is greater than 0)
+ prisma.link.update({
+ where: {
+ id: existingCustomer.linkId,
+ },
+ data: {
+ ...(isFirstConversion({
+ customer: existingCustomer,
+ linkId: existingCustomer.linkId,
+ }) && {
+ conversions: {
+ increment: 1,
+ },
+ lastConversionAt: updateLinkStatsForImporter({
+ currentTimestamp: existingCustomer.link.lastConversionAt,
+ newTimestamp: new Date(commission.created_at),
+ }),
+ }),
+ ...(saleAmount > 0 && {
+ sales: {
+ increment: 1,
+ },
+ saleAmount: {
+ increment: saleAmount,
+ },
+ }),
+ },
+ }),
+
+ syncPartnerLinksStats({
+ partnerId: existingCustomer.link.partnerId,
+ programId: program.id,
+ eventType: "sale",
+ }),
+
+ // update customer stats (if sale amount is greater than 0)
+ saleAmount > 0 &&
+ prisma.customer.update({
+ where: {
+ id: existingCustomer.id,
+ },
+ data: {
+ sales: {
+ increment: 1,
+ },
+ saleAmount: {
+ increment: saleAmount,
+ },
+ firstSaleAt: existingCustomer.firstSaleAt
+ ? undefined
+ : new Date(commission.created_at),
+ },
+ }),
+ ]);
+
+ await syncTotalCommissions({
+ partnerId: existingCustomer.link.partnerId,
+ programId: program.id,
+ });
+}
diff --git a/apps/web/lib/tapfiliate/import-customers.ts b/apps/web/lib/tapfiliate/import-customers.ts
new file mode 100644
index 0000000000..45415bd6d8
--- /dev/null
+++ b/apps/web/lib/tapfiliate/import-customers.ts
@@ -0,0 +1,289 @@
+import { prisma } from "@/lib/prisma";
+import { chunk, nanoid } from "@dub/utils";
+import { Customer, Link, Project } from "@prisma/client";
+import { createId } from "../api/create-id";
+import { updateLinkStatsForImporter } from "../api/links/update-link-stats-for-importer";
+import { syncPartnerLinksStats } from "../api/partners/sync-partner-links-stats";
+import { recordClick, recordLeadWithTimestamp } from "../tinybird";
+import { logImportError } from "../tinybird/log-import-error";
+import { clickEventSchemaTB } from "../zod/schemas/clicks";
+import { TapfiliateApi } from "./api";
+import { TAPFILIATE_MAX_BATCHES, tapfiliateImporter } from "./importer";
+import { TapfiliateCustomer, TapfiliateImportPayload } from "./types";
+
+export async function importCustomers(payload: TapfiliateImportPayload) {
+ const { importId, programId, tapfiliateProgramId, page = 1 } = payload;
+
+ const program = await prisma.program.findUnique({
+ where: {
+ id: programId,
+ },
+ include: {
+ workspace: {
+ select: {
+ id: true,
+ plan: true,
+ stripeConnectId: true,
+ },
+ },
+ },
+ });
+
+ if (!program) {
+ console.error(`Program ${programId} not found.`);
+ return;
+ }
+
+ if (!program.domain || !program.url) {
+ console.error("Program domain or url not found", program.id);
+ return;
+ }
+
+ const { workspace } = program;
+
+ const { apiKey } = await tapfiliateImporter.getCredentials(workspace.id);
+
+ const tapfiliateApi = new TapfiliateApi({
+ apiKey,
+ });
+
+ let currentPage = page;
+ let hasMore = true;
+ let processedBatches = 0;
+
+ while (hasMore && processedBatches < TAPFILIATE_MAX_BATCHES) {
+ let customers = await tapfiliateApi.listCustomers({
+ programId: tapfiliateProgramId,
+ page: currentPage,
+ });
+
+ if (customers.length === 0) {
+ hasMore = false;
+ break;
+ }
+
+ // Filter customers by program and affiliate
+ customers = customers.filter(
+ (customer) =>
+ customer.program?.id === tapfiliateProgramId && customer.affiliate?.id,
+ );
+
+ if (customers.length > 0) {
+ // Map the Tapfiliate affiliate id -> the partner's referral link (link key = affiliate id)
+ const affiliateIds = [
+ ...new Set(customers.map((customer) => customer.affiliate!.id)),
+ ];
+
+ const links = await prisma.link.findMany({
+ where: {
+ domain: program.domain,
+ key: {
+ in: affiliateIds,
+ },
+ },
+ select: {
+ id: true,
+ key: true,
+ domain: true,
+ url: true,
+ partnerId: true,
+ programId: true,
+ lastLeadAt: true,
+ },
+ });
+
+ const affiliateIdToLink = new Map(links.map((link) => [link.key, link]));
+ const customerExternalIds = [
+ ...new Set(
+ customers
+ .map((customer) => customer.customer_id)
+ .filter((id): id is string => id !== null),
+ ),
+ ];
+
+ // Find the existing customers by their external customer_id
+ const existingCustomers = await prisma.customer.findMany({
+ where: {
+ projectId: workspace.id,
+ externalId: {
+ in: customerExternalIds,
+ },
+ },
+ select: {
+ id: true,
+ externalId: true,
+ },
+ });
+
+ // New customers to create
+ const newCustomers = customers.filter(
+ (customer) =>
+ !existingCustomers.some((c) => c.externalId === customer.customer_id),
+ );
+
+ if (newCustomers.length > 0) {
+ const customerChunks = chunk(newCustomers, 10);
+
+ for (const customerChunk of customerChunks) {
+ await Promise.all(
+ customerChunk.map((customer) =>
+ createCustomer({
+ workspace,
+ customer,
+ link: affiliateIdToLink.get(customer.affiliate!.id),
+ importId,
+ }),
+ ),
+ );
+ }
+ }
+ }
+
+ currentPage++;
+ processedBatches++;
+ }
+
+ await tapfiliateImporter.queue({
+ ...payload,
+ action: hasMore ? "import-customers" : "import-commissions",
+ page: hasMore ? currentPage : undefined,
+ });
+}
+
+async function createCustomer({
+ workspace,
+ customer,
+ link,
+ importId,
+}: {
+ workspace: Pick<Project, "id" | "stripeConnectId">;
+ customer: TapfiliateCustomer;
+ link?: Pick<
+ Link,
+ "id" | "key" | "domain" | "url" | "partnerId" | "programId" | "lastLeadAt"
+ >;
+ importId: string;
+}) {
+ const externalId = customer.customer_id;
+
+ const commonImportLogInputs = {
+ workspace_id: workspace.id,
+ import_id: importId,
+ source: "tapfiliate",
+ entity: "customer",
+ entity_id: externalId,
+ } as const;
+
+ if (!link) {
+ await logImportError({
+ ...commonImportLogInputs,
+ code: "LINK_NOT_FOUND",
+ message: `No link found for customer ${externalId} (affiliate ${customer.affiliate?.id}).`,
+ });
+
+ return;
+ }
+
+ const tapfiliateClickData = customer.click;
+ const clickedAt = new Date(
+ tapfiliateClickData?.created_at || customer.created_at,
+ );
+
+ const dummyRequest = new Request(link.url, {
+ headers: new Headers({
+ "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)",
+ "x-forwarded-for": "127.0.0.1",
+ "x-vercel-ip-country": "US",
+ "x-vercel-ip-country-region": "CA",
+ "x-vercel-ip-continent": "NA",
+ }),
+ ...(tapfiliateClickData?.referrer && {
+ referrer: tapfiliateClickData.referrer,
+ }),
+ });
+
+ const clickData = await recordClick({
+ req: dummyRequest,
+ clickId: nanoid(16),
+ workspaceId: workspace.id,
+ linkId: link.id,
+ domain: link.domain,
+ key: link.key,
+ url: link.url,
+ skipRatelimit: true,
+ timestamp: clickedAt.toISOString(),
+ });
+
+ const clickEvent = clickEventSchemaTB.parse({
+ ...clickData,
+ bot: 0,
+ qr: 0,
+ });
+
+ let createdCustomer: Customer | null = null;
+
+ try {
+ createdCustomer = await prisma.customer.create({
+ data: {
+ id: createId({ prefix: "cus_" }),
+ name: externalId,
+ externalId,
+ projectId: workspace.id,
+ projectConnectId: workspace.stripeConnectId,
+ clickId: clickEvent.click_id,
+ linkId: link.id,
+ programId: link.programId,
+ partnerId: link.partnerId,
+ country: clickEvent.country,
+ clickedAt,
+ createdAt: new Date(customer.created_at),
+ },
+ });
+ } catch (error) {
+ if (error.code === "P2002") {
+ console.warn(
+ `Customer with external ID ${externalId} already exists. Skipping...`,
+ );
+ } else {
+ console.error("Error creating customer", customer, error);
+ }
+
+ return;
+ }
+
+ await Promise.all([
+ recordLeadWithTimestamp({
+ ...clickEvent,
+ event_id: nanoid(16),
+ event_name: "Sign up",
+ customer_id: createdCustomer.id,
+ timestamp: new Date(customer.created_at).toISOString(),
+ }),
+
+ prisma.link.update({
+ where: {
+ id: link.id,
+ },
+ data: {
+ leads: {
+ increment: 1,
+ },
+ lastLeadAt: updateLinkStatsForImporter({
+ currentTimestamp: link.lastLeadAt,
+ newTimestamp: new Date(customer.created_at),
+ }),
+ },
+ }),
+
+ // partner links should always have a partnerId and programId, but we're doing this to make TS happy
+ ...(link.partnerId && link.programId
+ ? [
+ syncPartnerLinksStats({
+ partnerId: link.partnerId,
+ programId: link.programId,
+ eventType: "lead",
+ }),
+ ]
+ : []),
+ ]);
+}
diff --git a/apps/web/lib/tapfiliate/import-groups.ts b/apps/web/lib/tapfiliate/import-groups.ts
new file mode 100644
index 0000000000..7ac41571c9
--- /dev/null
+++ b/apps/web/lib/tapfiliate/import-groups.ts
@@ -0,0 +1,102 @@
+import { prisma } from "@/lib/prisma";
+import { RESOURCE_COLORS } from "@/ui/colors";
+import { getDomainWithoutWWW, randomValue } from "@dub/utils";
+import slugify from "@sindresorhus/slugify";
+import { createId } from "../api/create-id";
+import { DEFAULT_ADDITIONAL_PARTNER_LINKS } from "../zod/schemas/groups";
+import { TapfiliateApi } from "./api";
+import { tapfiliateImporter } from "./importer";
+import { TapfiliateImportPayload } from "./types";
+
+export async function importGroups(payload: TapfiliateImportPayload) {
+ const { programId } = payload;
+
+ const program = await prisma.program.findUniqueOrThrow({
+ where: {
+ id: programId,
+ },
+ select: {
+ workspaceId: true,
+ domain: true,
+ url: true,
+ defaultGroupId: true,
+ groups: {
+ select: {
+ id: true,
+ linkStructure: true,
+ holdingPeriodDays: true,
+ maxPartnerLinks: true,
+ },
+ },
+ },
+ });
+
+ if (!program.domain || !program.url) {
+ throw new Error("Program domain or URL is not set.");
+ }
+
+ const defaultGroup = program.groups.find(
+ (group) => group.id === program.defaultGroupId,
+ );
+
+ const { apiKey } = await tapfiliateImporter.getCredentials(
+ program.workspaceId,
+ );
+
+ const tapfiliateApi = new TapfiliateApi({
+ apiKey,
+ });
+
+ const groups = await tapfiliateApi.listGroups();
+
+ for (const group of groups) {
+ const slug = `tapfiliate-${slugify(group.id)}`;
+
+ await prisma.partnerGroup.upsert({
+ where: {
+ programId_slug: {
+ programId,
+ slug,
+ },
+ },
+ create: {
+ id: createId({ prefix: "grp_" }),
+ programId,
+ name: group.title,
+ slug,
+ color: randomValue(RESOURCE_COLORS),
+ additionalLinks: [
+ {
+ domain: getDomainWithoutWWW(program.url),
+ validationMode: "domain",
+ },
+ ],
+ ...(defaultGroup
+ ? {
+ linkStructure: defaultGroup.linkStructure,
+ holdingPeriodDays: defaultGroup.holdingPeriodDays,
+ maxPartnerLinks: defaultGroup.maxPartnerLinks,
+ }
+ : {
+ maxPartnerLinks: DEFAULT_ADDITIONAL_PARTNER_LINKS,
+ }),
+ partnerGroupDefaultLinks: {
+ create: {
+ id: createId({ prefix: "pgdl_" }),
+ programId,
+ domain: program.domain,
+ url: program.url,
+ },
+ },
+ },
+ update: {},
+ });
+
+ console.log(`Imported group ${group.title}.`);
+ }
+
+ await tapfiliateImporter.queue({
+ ...payload,
+ action: "import-partners",
+ });
+}
diff --git a/apps/web/lib/tapfiliate/import-partners.ts b/apps/web/lib/tapfiliate/import-partners.ts
new file mode 100644
index 0000000000..a4e1cce782
--- /dev/null
+++ b/apps/web/lib/tapfiliate/import-partners.ts
@@ -0,0 +1,277 @@
+import { prisma } from "@/lib/prisma";
+import { PartnerGroup, Program } from "@prisma/client";
+import slugify from "@sindresorhus/slugify";
+import { createId } from "../api/create-id";
+import { createLink } from "../api/links";
+import { generatePartnerLink } from "../api/partners/generate-partner-link";
+import { logImportError } from "../tinybird/log-import-error";
+import { WorkspaceProps } from "../types";
+import { DEFAULT_PARTNER_GROUP } from "../zod/schemas/groups";
+import { TapfiliateApi } from "./api";
+import { TAPFILIATE_MAX_BATCHES, tapfiliateImporter } from "./importer";
+import { TapfiliateImportPayload, TapfiliatePartner } from "./types";
+
+export async function importPartners(payload: TapfiliateImportPayload) {
+ const { importId, programId, userId, page = 1 } = payload;
+
+ const program = await prisma.program.findUnique({
+ where: {
+ id: programId,
+ },
+ include: {
+ groups: {
+ select: {
+ id: true,
+ slug: true,
+ clickRewardId: true,
+ leadRewardId: true,
+ saleRewardId: true,
+ discountId: true,
+ },
+ },
+ workspace: {
+ select: {
+ id: true,
+ plan: true,
+ },
+ },
+ },
+ });
+
+ if (!program) {
+ console.error(`Program ${programId} not found.`);
+ return;
+ }
+
+ if (!program.domain || !program.url) {
+ console.error("Program domain or url not found", program.id);
+ return;
+ }
+
+ const groupsBySlug = Object.fromEntries(
+ program.groups.map((group) => [group.slug, group]),
+ );
+
+ const defaultGroup = groupsBySlug[DEFAULT_PARTNER_GROUP.slug];
+
+ const workspace = program.workspace as WorkspaceProps;
+
+ if (!defaultGroup) {
+ console.error(`Default group not found for program ${programId}.`);
+ return;
+ }
+
+ const { apiKey } = await tapfiliateImporter.getCredentials(
+ program.workspace.id,
+ );
+
+ const tapfiliateApi = new TapfiliateApi({
+ apiKey,
+ });
+
+ let currentPage = page;
+ let hasMore = true;
+ let processedBatches = 0;
+
+ while (hasMore && processedBatches < TAPFILIATE_MAX_BATCHES) {
+ const affiliates = await tapfiliateApi.listPartners({
+ page: currentPage,
+ });
+
+ if (affiliates.length === 0) {
+ hasMore = false;
+ break;
+ }
+
+ const partnersPromise = await Promise.allSettled(
+ affiliates.map((affiliate) => {
+ let group = defaultGroup;
+
+ if (affiliate.affiliate_group_id) {
+ const groupSlug = `tapfiliate-${slugify(affiliate.affiliate_group_id)}`;
+ group = groupsBySlug[groupSlug] ?? defaultGroup;
+ }
+
+ return createPartnerAndLinks({
+ workspace,
+ program,
+ affiliate,
+ group,
+ userId,
+ importId,
+ });
+ }),
+ );
+
+ const partnerIds = partnersPromise
+ .filter(
+ (p): p is PromiseFulfilledResult<string> =>
+ p.status === "fulfilled" && p.value != null,
+ )
+ .map((p) => p.value);
+
+ if (partnerIds.length > 0) {
+ await tapfiliateImporter.trackImportedPartnerIds({
+ programId,
+ partnerIds,
+ });
+ }
+
+ currentPage++;
+ processedBatches++;
+ }
+
+ // After importing partners, clean up by deleting any groups that have no assigned partners
+ if (!hasMore) {
+ const groups = await prisma.partnerGroup.findMany({
+ where: {
+ programId,
+ slug: {
+ not: DEFAULT_PARTNER_GROUP.slug,
+ startsWith: "tapfiliate-",
+ },
+ partners: {
+ none: {},
+ },
+ },
+ select: {
+ id: true,
+ },
+ });
+
+ if (groups.length > 0) {
+ console.log(
+ `Found ${groups.length} Tapfiliate groups with no partners, deleting...`,
+ );
+
+ await prisma.partnerGroup.deleteMany({
+ where: {
+ id: {
+ in: groups.map(({ id }) => id),
+ },
+ },
+ });
+ }
+ }
+
+ await tapfiliateImporter.queue({
+ ...payload,
+ action: hasMore ? "import-partners" : "import-customers",
+ page: hasMore ? currentPage : undefined,
+ });
+}
+
+async function createPartnerAndLinks({
+ workspace,
+ program,
+ affiliate,
+ group,
+ userId,
+ importId,
+}: {
+ workspace: Pick<WorkspaceProps, "id" | "plan">;
+ program: Pick<
+ Program,
+ "id" | "workspaceId" | "domain" | "url" | "defaultFolderId"
+ >;
+ affiliate: TapfiliatePartner;
+ group: Pick<
+ PartnerGroup,
+ "id" | "discountId" | "clickRewardId" | "leadRewardId" | "saleRewardId"
+ >;
+ userId: string;
+ importId: string;
+}) {
+ if (!affiliate.email) {
+ await logImportError({
+ workspace_id: program.workspaceId,
+ import_id: importId,
+ source: "tapfiliate",
+ entity: "partner",
+ entity_id: affiliate.id,
+ code: "PARTNER_NOT_FOUND",
+ message: `Affiliate ${affiliate.id} not imported because it has no email.`,
+ });
+
+ return undefined;
+ }
+
+ const name = [affiliate.firstname, affiliate.lastname]
+ .filter(Boolean)
+ .join(" ");
+
+ const partner = await prisma.partner.upsert({
+ where: {
+ email: affiliate.email,
+ },
+ create: {
+ id: createId({ prefix: "pn_" }),
+ name: name || affiliate.email,
+ email: affiliate.email,
+ country: affiliate.address?.country?.code ?? null,
+ },
+ update: {},
+ });
+
+ const { links } = await prisma.programEnrollment.upsert({
+ where: {
+ partnerId_programId: {
+ partnerId: partner.id,
+ programId: program.id,
+ },
+ },
+ create: {
+ id: createId({ prefix: "pge_" }),
+ programId: program.id,
+ partnerId: partner.id,
+ status: "approved",
+ groupId: group.id,
+ clickRewardId: group.clickRewardId,
+ leadRewardId: group.leadRewardId,
+ saleRewardId: group.saleRewardId,
+ discountId: group.discountId,
+ },
+ update: {
+ status: "approved",
+ },
+ select: {
+ links: {
+ select: {
+ key: true,
+ },
+ },
+ },
+ });
+
+ if (links.length > 0 && links.some((link) => link.key === affiliate.id)) {
+ console.log(
+ `Partner ${partner.email} already has a link with key ${affiliate.id}, skipping...`,
+ );
+ return partner.id;
+ }
+
+ try {
+ const partnerLink = await generatePartnerLink({
+ workspace,
+ program,
+ partner: {
+ id: partner.id,
+ name: partner.name,
+ email: partner.email!,
+ },
+ link: {
+ domain: program.domain!,
+ url: program.url!,
+ key: affiliate.id,
+ },
+ userId,
+ });
+
+ await createLink(partnerLink);
+
+ return partner.id;
+ } catch (error) {
+ console.error("Error creating partner link", error, affiliate);
+ return partner.id;
+ }
+}
diff --git a/apps/web/lib/tapfiliate/importer.ts b/apps/web/lib/tapfiliate/importer.ts
new file mode 100644
index 0000000000..b202008322
--- /dev/null
+++ b/apps/web/lib/tapfiliate/importer.ts
@@ -0,0 +1,88 @@
+import { qstash } from "@/lib/cron";
+import { redis } from "@/lib/upstash";
+import { APP_DOMAIN_WITH_NGROK } from "@dub/utils";
+import * as z from "zod/v4";
+import { tapfiliateImportPayloadSchema } from "./schemas";
+import { TapfiliateCredentials } from "./types";
+
+export const TAPFILIATE_MAX_BATCHES = 10;
+export const CACHE_EXPIRY = 60 * 60 * 24;
+export const CACHE_KEY_PREFIX = "tapfiliate:import";
+export const PARTNER_IDS_KEY_PREFIX = "tapfiliate:import:partnerIds";
+
+class TapfiliateImporter {
+ async setCredentials(
+ workspaceId: string,
+ credentials: TapfiliateCredentials,
+ ) {
+ await redis.set(`${CACHE_KEY_PREFIX}:${workspaceId}`, credentials, {
+ ex: CACHE_EXPIRY,
+ });
+ }
+
+ async getCredentials(workspaceId: string) {
+ const credentials = await redis.get<TapfiliateCredentials>(
+ `${CACHE_KEY_PREFIX}:${workspaceId}`,
+ );
+
+ if (!credentials) {
+ throw new Error(
+ "Tapfiliate credentials not found. Please restart the import process.",
+ );
+ }
+
+ return credentials;
+ }
+
+ async deleteCredentials(workspaceId: string) {
+ return await redis.del(`${CACHE_KEY_PREFIX}:${workspaceId}`);
+ }
+
+ async queue(
+ body: z.infer<typeof tapfiliateImportPayloadSchema>,
+ options?: { delay?: number },
+ ) {
+ return await qstash.publishJSON({
+ url: `${APP_DOMAIN_WITH_NGROK}/api/cron/import/tapfiliate`,
+ body,
+ contentBasedDeduplication: true,
+ ...(options?.delay != null && { delay: options.delay }),
+ });
+ }
+
+ async trackImportedPartnerIds({
+ programId,
+ partnerIds,
+ }: {
+ programId: string;
+ partnerIds: string[];
+ }) {
+ if (!partnerIds || partnerIds.length === 0) {
+ return;
+ }
+
+ await redis.lpush(`${PARTNER_IDS_KEY_PREFIX}:${programId}`, ...partnerIds);
+ }
+
+ async listImportedPartnerIds({
+ programId,
+ start,
+ end,
+ }: {
+ programId: string;
+ start: number;
+ end: number;
+ }) {
+ return await redis.lrange(
+ `${PARTNER_IDS_KEY_PREFIX}:${programId}`,
+ start,
+ end,
+ );
+ }
+
+ async clearImportedPartnerIds(programId: string) {
+ return await redis.del(`${PARTNER_IDS_KEY_PREFIX}:${programId}`);
+ }
+}
+
+export const tapfiliateImporter = new TapfiliateImporter();
diff --git a/apps/web/lib/tapfiliate/schemas.ts b/apps/web/lib/tapfiliate/schemas.ts
new file mode 100644
index 0000000000..b6fea57afd
--- /dev/null
+++ b/apps/web/lib/tapfiliate/schemas.ts
@@ -0,0 +1,94 @@
+import * as z from "zod/v4";
+
+export const tapfiliateImportSteps = z.enum([
+ "import-groups",
+ "import-partners",
+ "import-customers",
+ "import-commissions",
+ "update-stripe-customers",
+ "cleanup-partners",
+]);
+
+export const tapfiliateImportPayloadSchema = z.object({
+ importId: z.string(),
+ userId: z.string(),
+ programId: z.string(),
+ tapfiliateProgramId: z.string(),
+ action: tapfiliateImportSteps,
+ page: z.number().optional(), // Tapfiliate pagination
+ startingAfter: z.string().optional(), // Dub pagination
+});
+
+export const tapfiliateGroupSchema = z.object({
+ id: z.string(),
+ title: z.string(),
+ affiliate_count: z.number(),
+});
+
+export const tapfiliateProgramSchema = z.object({
+ id: z.string(),
+ title: z.string(),
+ currency: z.string().nullish(),
+});
+
+export const tapfiliatePartnerSchema = z.object({
+ id: z.string(),
+ email: z.string(),
+ firstname: z.string().nullable(),
+ lastname: z.string().nullable(),
+ affiliate_group_id: z.string().nullable(),
+ address: z
+ .object({
+ country: z.object({
+ code: z.string(),
+ }),
+ })
+ .nullable(),
+});
+
+export const tapfiliateCustomerSchema = z.object({
+ id: z.string(),
+ customer_id: z.string().describe("External customer ID."),
+ created_at: z.string(),
+ click: z
+ .object({
+ created_at: z.string(),
+ referrer: z.string().nullable(),
+ landing_page: z.string().nullable(),
+ })
+ .nullable(),
+ program: tapfiliateProgramSchema
+ .pick({
+ id: true,
+ })
+ .nullable(),
+ affiliate: tapfiliatePartnerSchema
+ .pick({
+ id: true,
+ })
+ .nullable(),
+});
+
+export const tapfiliateCommissionSchema = z.object({
+ id: z.number(),
+ currency: z.string(),
+ created_at: z.string(),
+ approved: z.boolean().nullable(),
+ amount: z.number(),
+ conversion_sub_amount: z.number(),
+});
+
+export const tapfiliateConversionSchema = z.object({
+ id: z.number(),
+ program: tapfiliateProgramSchema
+ .pick({
+ id: true,
+ })
+ .nullable(),
+ customer: tapfiliateCustomerSchema
+ .pick({
+ customer_id: true,
+ })
+ .nullable(),
+ commissions: z.array(tapfiliateCommissionSchema).nullable(),
+});
diff --git a/apps/web/lib/tapfiliate/types.ts b/apps/web/lib/tapfiliate/types.ts
new file mode 100644
index 0000000000..84b87ca844
--- /dev/null
+++ b/apps/web/lib/tapfiliate/types.ts
@@ -0,0 +1,37 @@
+import * as z from "zod/v4";
+import {
+ tapfiliateCommissionSchema,
+ tapfiliateConversionSchema,
+ tapfiliateCustomerSchema,
+ tapfiliateGroupSchema,
+ tapfiliateImportPayloadSchema,
+ tapfiliatePartnerSchema,
+ tapfiliateProgramSchema,
+} from "./schemas";
+
+export interface TapfiliateCredentials {
+ apiKey: string;
+}
+
+export type TapfiliateImportPayload = z.infer<
+ typeof tapfiliateImportPayloadSchema
+>;
+
+export type TapfiliateProgram = z.infer<typeof tapfiliateProgramSchema>;
+
+export type TapfiliateGroup = z.infer<typeof tapfiliateGroupSchema>;
+
+export type TapfiliatePartner = z.infer<typeof tapfiliatePartnerSchema>;
+
+export type TapfiliateCustomer = z.infer<typeof tapfiliateCustomerSchema>;
+
+type TapfiliateCommission = z.infer<typeof tapfiliateCommissionSchema>;
+
+export type TapfiliateConversion = z.infer<typeof tapfiliateConversionSchema>;
+
+export type TapfiliateConversionWithCommission = Omit<
+ TapfiliateConversion,
+ "commissions"
+> & {
+ commission: TapfiliateCommission;
+};
diff --git a/apps/web/lib/tapfiliate/update-stripe-customers.ts b/apps/web/lib/tapfiliate/update-stripe-customers.ts
new file mode 100644
index 0000000000..dca4328478
--- /dev/null
+++ b/apps/web/lib/tapfiliate/update-stripe-customers.ts
@@ -0,0 +1,204 @@
+import { prisma } from "@/lib/prisma";
+import { Customer, Project } from "@prisma/client";
+import Stripe from "stripe";
+import * as z from "zod/v4";
+import { stripeAppClient } from "../stripe";
+import { logImportError } from "../tinybird/log-import-error";
+import { TAPFILIATE_MAX_BATCHES, tapfiliateImporter } from "./importer";
+import { TapfiliateImportPayload } from "./types";
+
+const CUSTOMERS_PER_BATCH = 20;
+
+const stripe = stripeAppClient({
+ ...(process.env.VERCEL_ENV && { mode: "live" }),
+});
+
+export async function updateStripeCustomers(payload: TapfiliateImportPayload) {
+ const { importId, programId } = payload;
+ let { startingAfter } = payload;
+
+ const { workspace } = await prisma.program.findUniqueOrThrow({
+ where: {
+ id: programId,
+ },
+ select: {
+ workspace: {
+ select: {
+ id: true,
+ slug: true,
+ stripeConnectId: true,
+ },
+ },
+ },
+ });
+
+ if (!workspace.stripeConnectId) {
+ console.error(
+ `Workspace ${workspace.id} has no stripeConnectId. Skipping...`,
+ );
+ return;
+ }
+
+ let hasMore = true;
+ let processedBatches = 0;
+
+ while (hasMore && processedBatches < TAPFILIATE_MAX_BATCHES) {
+ const customers = await prisma.customer.findMany({
+ where: {
+ projectId: workspace.id,
+ stripeCustomerId: null,
+ },
+ select: {
+ id: true,
+ name: true,
+ email: true,
+ externalId: true,
+ },
+ orderBy: {
+ id: "asc",
+ },
+ take: CUSTOMERS_PER_BATCH,
+ ...(startingAfter && {
+ skip: 1,
+ cursor: {
+ id: startingAfter,
+ },
+ }),
+ });
+
+ if (customers.length === 0) {
+ hasMore = false;
+ break;
+ }
+
+ await Promise.allSettled(
+ customers.map((customer) =>
+ searchStripeAndUpdateCustomer({
+ workspace,
+ customer,
+ importId,
+ }),
+ ),
+ );
+
+ await new Promise((resolve) => setTimeout(resolve, 2000));
+
+ processedBatches++;
+ startingAfter = customers[customers.length - 1].id;
+ }
+
+ await tapfiliateImporter.queue({
+ ...payload,
+ startingAfter: hasMore ? startingAfter : undefined,
+ action: hasMore ? "update-stripe-customers" : "cleanup-partners",
+ });
+}
+
+async function searchStripeAndUpdateCustomer({
+ workspace,
+ customer,
+ importId,
+}: {
+ workspace: Pick<Project, "id" | "slug" | "stripeConnectId">;
+ customer: Pick<Customer, "id" | "email" | "externalId">;
+ importId: string;
+}) {
+ if (!customer.externalId) {
+ console.log(`Customer ${customer.id} has no externalId. Skipping...`);
+ return;
+ }
+
+ const commonImportLogInputs = {
+ workspace_id: workspace.id,
+ import_id: importId,
+ source: "tapfiliate",
+ entity: "customer",
+ entity_id: customer.id,
+ } as const;
+
+ let stripeCustomer: Stripe.Customer | Stripe.DeletedCustomer | null = null;
+
+ const { success: isEmail } = z.email().safeParse(customer.externalId);
+
+ try {
+ // If externalId is a valid email
+ if (isEmail) {
+ const stripeCustomers = await stripe.customers.search(
+ {
+ query: `email:'${customer.externalId}'`,
+ expand: ["data.subscriptions"],
+ },
+ {
+ stripeAccount: workspace.stripeConnectId!,
+ },
+ );
+
+ // No customers found
+ if (stripeCustomers.data.length === 0) {
+ await logImportError({
+ ...commonImportLogInputs,
+ code: "STRIPE_CUSTOMER_NOT_FOUND",
+ message: `Stripe search returned no customer for ${customer.email}`,
+ });
+ return null;
+ }
+
+ // Single customer found
+ else if (stripeCustomers.data.length === 1) {
+ stripeCustomer = stripeCustomers.data[0];
+ }
+
+ // More than one customer found, look for the one with subscriptions
+ else {
+ const customerWithSubcription = stripeCustomers.data.find(
+ ({ subscriptions }) => subscriptions && subscriptions.data.length > 0,
+ );
+
+ if (!customerWithSubcription) {
+ await logImportError({
+ ...commonImportLogInputs,
+ code: "STRIPE_CUSTOMER_NOT_FOUND",
+ message: `Stripe search returned multiple customers for ${customer.email} for workspace ${workspace.slug} and none had subscriptions`,
+ });
+ return null;
+ }
+
+ stripeCustomer = customerWithSubcription;
+ }
+ }
+
+ // If externalId is a valid Stripe customer ID
+ else if (customer.externalId.startsWith("cus_")) {
+ stripeCustomer = await stripe.customers.retrieve(customer.externalId, {
+ stripeAccount: workspace.stripeConnectId!,
+ });
+ }
+ } catch (error) {
+ console.error("Error searching Stripe customer", error);
+ return null;
+ }
+
+ if (!stripeCustomer) {
+ await logImportError({
+ ...commonImportLogInputs,
+ code: "STRIPE_CUSTOMER_NOT_FOUND",
+ message: `Stripe search returned no customer for ${customer.externalId}`,
+ });
+ return null;
+ }
+
+ console.log(`Stripe customer found for ${customer.id}: ${stripeCustomer.id}`);
+
+ await prisma.customer.update({
+ where: {
+ id: customer.id,
+ },
+ data: {
+ stripeCustomerId: stripeCustomer.id,
+ },
+ });
+
+ console.log(
+ `Updated customer ${customer.id} with Stripe customer ID ${stripeCustomer.id}`,
+ );
+}
diff --git a/apps/web/lib/zod/schemas/import-error-log.ts b/apps/web/lib/zod/schemas/import-error-log.ts
index b84b292338..bde1e972d2 100644
--- a/apps/web/lib/zod/schemas/import-error-log.ts
+++ b/apps/web/lib/zod/schemas/import-error-log.ts
@@ -3,7 +3,13 @@ import * as z from "zod/v4";
export const importErrorLogSchema = z.object({
workspace_id: z.string(),
import_id: z.string(),
- source: z.enum(["rewardful", "tolt", "partnerstack", "firstpromoter"]),
+ source: z.enum([
+ "rewardful",
+ "tolt",
+ "partnerstack",
+ "firstpromoter",
+ "tapfiliate",
+ ]),
entity: z.enum(["partner", "link", "customer", "commission"]),
entity_id: z.string(),
code: z.enum([
diff --git a/apps/web/ui/modals/import-tapfiliate-modal.tsx b/apps/web/ui/modals/import-tapfiliate-modal.tsx
new file mode 100644
index 0000000000..58d38d89f3
--- /dev/null
+++ b/apps/web/ui/modals/import-tapfiliate-modal.tsx
@@ -0,0 +1,323 @@
+import { setTapfiliateTokenAction } from "@/lib/actions/partners/set-tapfiliate-token";
+import { startTapfiliateImportAction } from "@/lib/actions/partners/start-tapfiliate-import";
+import useWorkspace from "@/lib/swr/use-workspace";
+import { TapfiliateProgram } from "@/lib/tapfiliate/types";
+import {
+ Button,
+ Check2,
+ Logo,
+ Modal,
+ ScrollContainer,
+ useMediaQuery,
+ useRouterStuff,
+} from "@dub/ui";
+import { cn } from "@dub/utils";
+import { ArrowRight } from "lucide-react";
+import { AnimatePresence, motion } from "motion/react";
+import { useAction } from "next-safe-action/hooks";
+import { useRouter, useSearchParams } from "next/navigation";
+import {
+ Dispatch,
+ SetStateAction,
+ useCallback,
+ useEffect,
+ useMemo,
+ useState,
+} from "react";
+import { toast } from "sonner";
+import { MarkdownDescription } from "../shared/markdown-description";
+
+type Step = "set-token" | "select-program";
+
+function ImportTapfiliateModal({
+ showImportTapfiliateModal,
+ setShowImportTapfiliateModal,
+}: {
+ showImportTapfiliateModal: boolean;
+ setShowImportTapfiliateModal: Dispatch<SetStateAction<boolean>>;
+}) {
+ const searchParams = useSearchParams();
+ const { queryParams } = useRouterStuff();
+ const [step, setStep] = useState<Step>("set-token");
+ const [programs, setPrograms] = useState<TapfiliateProgram[]>([]);
+
+ useEffect(() => {
+ if (searchParams?.get("import") === "tapfiliate") {
+ setShowImportTapfiliateModal(true);
+ } else {
+ setShowImportTapfiliateModal(false);
+ }
+ }, [searchParams]);
+
+ // Reset the step and programs when the modal is closed
+ useEffect(() => {
+ if (!showImportTapfiliateModal) {
+ setStep("set-token");
+ setPrograms([]);
+ }
+ }, [showImportTapfiliateModal]);
+
+ return (
+ <Modal
+ showModal={showImportTapfiliateModal}
+ setShowModal={setShowImportTapfiliateModal}
+ onClose={() =>
+ queryParams({
+ del: "import",
+ })
+ }
+ >
+ <div className="flex flex-col items-center justify-center space-y-3 border-b border-neutral-200 px-4 py-8 sm:px-16">
+ <div className="flex items-center space-x-3 py-4">
+ <img
+ src="https://assets.dub.co/misc/icons/tapfiliate.svg"
+ alt="Tapfiliate logo"
+ className="h-10 w-10 rounded-full"
+ />
+ <ArrowRight className="h-5 w-5 text-neutral-600" />
+ <Logo />
+ </div>
+ <h3 className="text-lg font-medium">Import your Tapfiliate program</h3>
+ <MarkdownDescription className="text-center text-sm text-neutral-500">
+ [Migrate your existing Tapfiliate
+ program](https://dub.co/help/article/migrating-from-tapfiliate),
+ partners, and historical stats into Dub in just a few clicks.
+ </MarkdownDescription>
+ </div>
+
+ <div className="flex flex-col space-y-6 bg-neutral-50 px-4 py-8 text-left sm:px-16">
+ <AnimatePresence mode="wait">
+ {step === "set-token" ? (
+ <motion.div
+ key="token-form"
+ initial={{ opacity: 0, x: 20 }}
+ animate={{ opacity: 1, x: 0 }}
+ exit={{ opacity: 0, x: -20 }}
+ transition={{ duration: 0.2, ease: "easeInOut" }}
+ >
+ <TokenForm setStep={setStep} setPrograms={setPrograms} />
+ </motion.div>
+ ) : (
+ <motion.div
+ key="select-program"
+ initial={{ opacity: 0, x: 20 }}
+ animate={{ opacity: 1, x: 0 }}
+ exit={{ opacity: 0, x: -20 }}
+ transition={{ duration: 0.2, ease: "easeInOut" }}
+ >
+ <SelectProgram
+ programs={programs}
+ onClose={() => {
+ setShowImportTapfiliateModal(false);
+ queryParams({
+ del: "import",
+ });
+ }}
+ />
+ </motion.div>
+ )}
+ </AnimatePresence>
+ </div>
+ </Modal>
+ );
+}
+
+function TokenForm({
+ setStep,
+ setPrograms,
+}: {
+ setStep: Dispatch<SetStateAction<Step>>;
+ setPrograms: Dispatch<SetStateAction<TapfiliateProgram[]>>;
+}) {
+ const { isMobile } = useMediaQuery();
+ const { id: workspaceId } = useWorkspace();
+
+ const [apiKey, setApiKey] = useState("");
+
+ const { executeAsync, isPending } = useAction(setTapfiliateTokenAction, {
+ onSuccess: ({ data }) => {
+ if (data?.programs) {
+ setPrograms(data.programs);
+ setStep("select-program");
+ }
+ },
+ onError: ({ error }) => {
+ toast.error(error.serverError);
+ },
+ });
+
+ const onSubmit = async (e: React.FormEvent) => {
+ e.preventDefault();
+
+ if (!workspaceId || !apiKey) {
+ return;
+ }
+
+ await executeAsync({
+ workspaceId,
+ apiKey,
+ });
+ };
+
+ return (
+ <form onSubmit={onSubmit} className="flex flex-col space-y-4">
+ <div>
+ <label
+ htmlFor="apiKey"
+ className="block text-sm font-medium text-neutral-700"
+ >
+ Tapfiliate API Key
+ </label>
+ <input
+ type="password"
+ id="apiKey"
+ value={apiKey}
+ autoFocus={!isMobile}
+ onChange={(e) => setApiKey(e.target.value)}
+ className="mt-1 block w-full rounded-md border border-neutral-200 px-3 py-2 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm"
+ required
+ />
+ <p className="mt-1.5 text-xs text-neutral-500">
+ You can find your API key in your{" "}
+ <a
+ href="https://support.tapfiliate.com/en/articles/12793297-how-to-find-account-id-and-api-key"
+ target="_blank"
+ rel="noopener noreferrer"
+ className="text-blue-500 hover:text-blue-600"
+ >
+ Profile settings
+ </a>
+ </p>
+ </div>
+
+ <Button
+ text={isPending ? "Fetching programs..." : "Fetch programs"}
+ loading={isPending}
+ disabled={!apiKey}
+ />
+ </form>
+ );
+}
+
+function SelectProgram({
+ programs,
+ onClose,
+}: {
+ programs: TapfiliateProgram[];
+ onClose: () => void;
+}) {
+ const router = useRouter();
+ const { id: workspaceId, slug } = useWorkspace();
+
+ const [selectedProgramId, setSelectedProgramId] = useState<string | null>(
+ programs.length === 1 ? programs[0].id : null,
+ );
+
+ const { executeAsync, isPending } = useAction(startTapfiliateImportAction, {
+ onSuccess: () => {
+ onClose();
+ toast.success(
+ "Successfully added program to import queue! We will send you an email when your program has been fully imported.",
+ );
+ router.push(`/${slug}/program/partners`);
+ },
+ onError: ({ error }) => {
+ toast.error(error.serverError);
+ },
+ });
+
+ const onSubmit = async (e: React.FormEvent) => {
+ e.preventDefault();
+
+ if (!workspaceId || !selectedProgramId) {
+ return;
+ }
+
+ await executeAsync({
+ workspaceId,
+ tapfiliateProgramId: selectedProgramId,
+ });
+ };
+
+ return (
+ <form onSubmit={onSubmit} className="flex flex-col space-y-6">
+ <div className="space-y-2">
+ <label className="block text-sm font-medium text-neutral-700">
+ Choose a program to import
+ </label>
+
+ <ScrollContainer className="max-h-[280px] rounded-lg border border-neutral-200 bg-white">
+ <div className="flex w-full flex-col gap-1 p-1">
+ {programs.map((program) => {
+ const checked = selectedProgramId === program.id;
+
+ return (
+ <button
+ key={program.id}
+ type="button"
+ onClick={() => setSelectedProgramId(program.id)}
+ className={cn(
+ "flex cursor-pointer select-none items-center gap-3 rounded-md px-3 py-2.5 text-left text-sm text-neutral-700",
+ "hover:bg-neutral-100",
+ checked && "bg-neutral-100",
+ )}
+ >
+ <div
+ className={cn(
+ "border-border-emphasis flex size-4 shrink-0 items-center justify-center rounded-full border bg-white transition-colors duration-75",
+ checked && "border-neutral-900 bg-neutral-900",
+ )}
+ >
+ <Check2
+ className={cn(
+ "size-2.5 text-white transition-[transform,opacity] duration-75",
+ !checked && "scale-75 opacity-0",
+ )}
+ />
+ </div>
+ <div className="flex min-w-0 flex-1 flex-col">
+ <span className="min-w-0 truncate font-medium">
+ {program.title}
+ </span>
+ <span className="text-xs text-neutral-500">
+ {program.id}
+ </span>
+ </div>
+ </button>
+ );
+ })}
+ </div>
+ </ScrollContainer>
+ </div>
+
+ <Button
+ text="Import program"
+ disabled={!selectedProgramId}
+ loading={isPending}
+ className="w-full justify-center"
+ />
+ </form>
+ );
+}
+
+export function useImportTapfiliateModal() {
+ const [showImportTapfiliateModal, setShowImportTapfiliateModal] =
+ useState(false);
+
+ const ImportTapfiliateModalCallback = useCallback(() => {
+ return (
+ <ImportTapfiliateModal
+ showImportTapfiliateModal={showImportTapfiliateModal}
+ setShowImportTapfiliateModal={setShowImportTapfiliateModal}
+ />
+ );
+ }, [showImportTapfiliateModal, setShowImportTapfiliateModal]);
+
+ return useMemo(
+ () => ({
+ setShowImportTapfiliateModal,
+ ImportTapfiliateModal: ImportTapfiliateModalCallback,
+ }),
+ [setShowImportTapfiliateModal, ImportTapfiliateModalCallback],
+ );
+}
diff --git a/apps/web/ui/modals/modal-provider.tsx b/apps/web/ui/modals/modal-provider.tsx
index cacc89380d..169408d4d4 100644
--- a/apps/web/ui/modals/modal-provider.tsx
+++ b/apps/web/ui/modals/modal-provider.tsx
@@ -27,6 +27,7 @@ import { useAddEditTagModal } from "./add-edit-tag-modal";
import { useImportPartnerStackModal } from "./import-partnerstack-modal";
import { useImportRebrandlyModal } from "./import-rebrandly-modal";
import { useImportRewardfulModal } from "./import-rewardful-modal";
+import { useImportTapfiliateModal } from "./import-tapfiliate-modal";
import { useImportToltModal } from "./import-tolt-modal";
import { useLinkBuilder } from "./link-builder";
import { useProgramWelcomeModal } from "./program-welcome-modal";
@@ -44,6 +45,7 @@ export const ModalContext = createContext<{
setShowImportPartnerStackModal: Dispatch<SetStateAction<boolean>>;
setShowImportRewardfulModal: Dispatch<SetStateAction<boolean>>;
setShowImportToltModal: Dispatch<SetStateAction<boolean>>;
+ setShowImportTapfiliateModal: Dispatch<SetStateAction<boolean>>;
}>({
setShowAddWorkspaceModal: () => {},
setShowAddEditDomainModal: () => {},
@@ -56,6 +58,7 @@ export const ModalContext = createContext<{
setShowImportPartnerStackModal: () => {},
setShowImportRewardfulModal: () => {},
setShowImportToltModal: () => {},
+ setShowImportTapfiliateModal: () => {},
});
export function ModalProvider({ children }: { children: ReactNode }) {
@@ -109,6 +112,8 @@ function ModalProviderClient({ children }: { children: ReactNode }) {
const { setShowImportRewardfulModal, ImportRewardfulModal } =
useImportRewardfulModal();
const { setShowImportToltModal, ImportToltModal } = useImportToltModal();
+ const { setShowImportTapfiliateModal, ImportTapfiliateModal } =
+ useImportTapfiliateModal();
useEffect(() => {
setShowProgramWelcomeModal(searchParams.has("onboarded-program"));
@@ -194,6 +199,7 @@ function ModalProviderClient({ children }: { children: ReactNode }) {
setShowImportPartnerStackModal,
setShowImportRewardfulModal,
setShowImportToltModal,
+ setShowImportTapfiliateModal,
}}
>
<AddWorkspaceModal />
@@ -207,6 +213,7 @@ function ModalProviderClient({ children }: { children: ReactNode }) {
<ImportPartnerStackModal />
<ImportRewardfulModal />
<ImportToltModal />
+ <ImportTapfiliateModal />
<UpgradedModal />
<ProgramWelcomeModal />
{children}
diff --git a/packages/email/src/templates/program-imported.tsx b/packages/email/src/templates/program-imported.tsx
index 7b76be8ca0..4cc378779f 100644
--- a/packages/email/src/templates/program-imported.tsx
+++ b/packages/email/src/templates/program-imported.tsx
@@ -26,7 +26,12 @@ export default function ProgramImported({
importId = "1K1QFYS3W9CJTEJ325SQKWCHF",
}: {
email: string;
- provider: "Rewardful" | "Tolt" | "PartnerStack" | "FirstPromoter";
+ provider:
+ | "Rewardful"
+ | "Tolt"
+ | "PartnerStack"
+ | "FirstPromoter"
+ | "Tapfiliate";
workspace: {
slug: string;
};