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.

simplify the test

Kiran Ka12b6cba976f
diff --git a/apps/web/playwright/api/shopify/helpers.ts b/apps/web/playwright/api/shopify/helpers.ts
deleted file mode 100644
index d84499da3f..0000000000
--- a/apps/web/playwright/api/shopify/helpers.ts
+++ /dev/null
@@ -1,435 +0,0 @@
-import { createId } from "@/lib/api/create-id";
-import { shopifyCheckoutCache } from "@/lib/integrations/shopify/checkout-cache";
-import { prisma } from "@/lib/prisma";
-import { nanoid } from "@dub/utils";
-import { expect } from "@playwright/test";
-import { DiscountProvider, EventType, RewardStructure } from "@prisma/client";
-import { createHmac } from "crypto";
-import { randomName, randomPartnerEmail } from "../../utils";
-import { PLAYWRIGHT_API_BASE } from "../constants";
-import type { ApiClient } from "../fixtures";
-import { deletePartner } from "../partners/helpers";
-import { TEST_WORKSPACE } from "../setup-test-workspace";
-
-export const SHOPIFY_ORDER_AMOUNT_CENTS = 2250; // $22.50
-export const SHOPIFY_SALE_REWARD_PERCENT = 10;
-
-export function shopifyOrderPayload({
- checkoutToken,
- ...overrides
-}: {
- checkoutToken: string;
-} & Record<string, unknown>) {
- return {
- confirmation_number: nanoid(10).toUpperCase(),
- checkout_token: checkoutToken,
- customer: {
- id: randomName("pw_cus", 16),
- first_name: "Playwright",
- last_name: "Shopify",
- email: `pw.shopify.${nanoid(8)}@dub-internal-test.com`,
- },
- current_subtotal_price_set: {
- shop_money: {
- amount: (SHOPIFY_ORDER_AMOUNT_CENTS / 100).toFixed(2),
- currency_code: "USD",
- },
- },
- discount_codes: [],
- billing_address: {
- province: "California",
- country_code: "US",
- },
- ...overrides,
- };
-}
-
-function shopifyWebhookSignature(body: string) {
- const secret = process.env.SHOPIFY_WEBHOOK_SECRET;
- if (!secret) {
- return undefined;
- }
-
- return createHmac("sha256", secret).update(body, "utf8").digest("base64");
-}
-
-export async function postShopifyPixel({
- clickId,
- checkoutToken,
-}: {
- clickId: string | null;
- checkoutToken: string;
-}) {
- const response = await fetch(`${PLAYWRIGHT_API_BASE}/api/shopify/pixel`, {
- method: "POST",
- headers: {
- "Content-Type": "application/json",
- },
- body: JSON.stringify({
- clickId,
- checkoutToken,
- }),
- });
-
- return {
- status: response.status,
- data: await response.json(),
- };
-}
-
-export async function postShopifyOrdersPaidWebhook({
- checkoutToken,
- ...orderOverrides
-}: {
- checkoutToken: string;
-} & Record<string, unknown>) {
- const payload = shopifyOrderPayload({ checkoutToken, ...orderOverrides });
- const body = JSON.stringify(payload);
- const signature = shopifyWebhookSignature(body);
-
- const response = await fetch(
- `${PLAYWRIGHT_API_BASE}/api/shopify/integration/webhook`,
- {
- method: "POST",
- headers: {
- "Content-Type": "application/json",
- "x-shopify-topic": "orders/paid",
- "x-shopify-shop-domain": TEST_WORKSPACE.shopify.storeId,
- ...(signature && { "x-shopify-hmac-sha256": signature }),
- },
- body,
- },
- );
-
- return {
- status: response.status,
- data: await response.text(),
- payload,
- };
-}
-
-/** Pixel writes clickId via waitUntil — wait until Redis has it before the webhook. */
-export async function waitForShopifyCheckoutClickId({
- checkoutToken,
- clickId,
-}: {
- checkoutToken: string;
- clickId: string;
-}) {
- await expect
- .poll(
- async () => {
- const { clickId } = await shopifyCheckoutCache.get(checkoutToken);
- return clickId;
- },
- { timeout: 5_000 },
- )
- .toEqual(clickId);
-}
-
-export async function seedShopifyPartner({
- workspaceId,
- programId,
- groupId,
- withDiscountCode = false,
-}: {
- workspaceId: string;
- programId: string;
- groupId: string;
- withDiscountCode?: boolean;
-}) {
- const partnerId = createId({ prefix: "pn_" });
- const linkId = createId({ prefix: "link_" });
- const saleRewardId = createId({ prefix: "rw_" });
- const discountId = withDiscountCode
- ? createId({ prefix: "disc_" })
- : undefined;
- const code = withDiscountCode ? `PW${nanoid(8).toUpperCase()}` : undefined;
- const key = randomName("pw", 8);
-
- await prisma.partner.create({
- data: {
- id: partnerId,
- name: randomName("pw-shopify"),
- email: randomPartnerEmail(),
- username: randomName("pw", 12),
- },
- });
-
- await prisma.reward.create({
- data: {
- id: saleRewardId,
- programId,
- event: EventType.sale,
- type: RewardStructure.percentage,
- amountInPercentage: SHOPIFY_SALE_REWARD_PERCENT,
- maxDuration: 0,
- },
- });
-
- if (discountId) {
- await prisma.discount.create({
- data: {
- id: discountId,
- programId,
- amount: 10,
- type: RewardStructure.percentage,
- maxDuration: null,
- provider: DiscountProvider.custom,
- },
- });
- }
-
- await prisma.programEnrollment.create({
- data: {
- id: createId({ prefix: "pge_" }),
- partnerId,
- programId,
- groupId,
- status: "approved",
- saleRewardId,
- discountId,
- },
- });
-
- await prisma.link.create({
- data: {
- id: linkId,
- domain: TEST_WORKSPACE.program.domain,
- key,
- url: TEST_WORKSPACE.program.url,
- shortLink: `https://${TEST_WORKSPACE.program.domain}/${key}`,
- projectId: workspaceId,
- programId,
- partnerId,
- trackConversion: true,
- },
- });
-
- if (discountId && code) {
- await prisma.discountCode.create({
- data: {
- id: createId({ prefix: "dcode_" }),
- code,
- programId,
- partnerId,
- linkId,
- discountId,
- },
- });
- }
-
- return {
- partnerId,
- linkId,
- saleRewardId,
- discountId,
- code,
- domain: TEST_WORKSPACE.program.domain,
- key,
- };
-}
-
-export type SeededShopifyPartner = Awaited<
- ReturnType<typeof seedShopifyPartner>
->;
-
-export async function cleanupShopifyPartner({
- partnerId,
- saleRewardId,
- discountId,
-}: {
- partnerId: string | undefined;
- saleRewardId: string | undefined;
- discountId?: string | undefined;
-}) {
- if (partnerId) {
- await prisma.customer.deleteMany({ where: { partnerId } });
- }
-
- await deletePartner(partnerId);
-
- if (discountId) {
- await prisma.discount.delete({ where: { id: discountId } }).catch(() => {});
- }
-
- if (saleRewardId) {
- await prisma.reward.delete({ where: { id: saleRewardId } }).catch(() => {});
- }
-}
-
-/** Seeds a Shopify partner and always cleans up, even when the test body throws. */
-export async function withShopifyPartner<T>(
- {
- workspaceId,
- programId,
- groupId,
- withDiscountCode = false,
- }: {
- workspaceId: string;
- programId: string;
- groupId: string;
- withDiscountCode?: boolean;
- },
- run: (seeded: SeededShopifyPartner) => Promise<T>,
-): Promise<T> {
- const seeded = await seedShopifyPartner({
- workspaceId,
- programId,
- groupId,
- withDiscountCode,
- });
-
- try {
- return await run(seeded);
- } finally {
- await cleanupShopifyPartner(seeded);
- }
-}
-
-export async function waitForShopifyCustomerSale({
- workspaceId,
- externalId,
- amount = SHOPIFY_ORDER_AMOUNT_CENTS,
-}: {
- workspaceId: string;
- externalId: string;
- amount?: number;
-}) {
- await expect
- .poll(
- async () => {
- const customer = await prisma.customer.findUnique({
- where: {
- projectId_externalId: {
- projectId: workspaceId,
- externalId,
- },
- },
- });
-
- if (!customer) {
- return null;
- }
-
- return {
- id: customer.id,
- sales: customer.sales,
- saleAmount: Number(customer.saleAmount),
- };
- },
- { timeout: 30_000 },
- )
- .toEqual({
- id: expect.any(String),
- sales: 1,
- saleAmount: amount,
- });
-
- return prisma.customer.findUniqueOrThrow({
- where: {
- projectId_externalId: {
- projectId: workspaceId,
- externalId,
- },
- },
- });
-}
-
-export async function waitForShopifySaleCommission({
- partnerId,
- customerId,
- amount = SHOPIFY_ORDER_AMOUNT_CENTS,
- earnings = Math.round(
- (SHOPIFY_ORDER_AMOUNT_CENTS * SHOPIFY_SALE_REWARD_PERCENT) / 100,
- ),
-}: {
- partnerId: string;
- customerId: string;
- amount?: number;
- earnings?: number;
-}) {
- await expect
- .poll(
- async () => {
- const commission = await prisma.commission.findFirst({
- where: {
- partnerId,
- customerId,
- type: "sale",
- },
- });
-
- if (!commission) {
- return null;
- }
-
- return {
- amount: commission.amount,
- earnings: commission.earnings,
- type: commission.type,
- invoiceId: commission.invoiceId,
- };
- },
- { timeout: 30_000 },
- )
- .toEqual({
- amount,
- earnings,
- type: "sale",
- invoiceId: expect.any(String),
- });
-}
-
-export async function waitForShopifySaleEvent({
- api,
- customerId,
- amount = SHOPIFY_ORDER_AMOUNT_CENTS,
-}: {
- api: ApiClient;
- customerId: string;
- amount?: number;
-}) {
- await expect
- .poll(
- async () => {
- const { status, data } = await api.get<
- {
- event: string;
- eventName: string;
- sale: { amount: number; paymentProcessor: string };
- customer: { id: string };
- }[]
- >(
- `/api/events?${new URLSearchParams({
- event: "sales",
- interval: "24h",
- customerId,
- })}`,
- );
-
- if (status !== 200 || !Array.isArray(data) || data.length === 0) {
- return null;
- }
-
- const sale = data.find((event) => event.customer?.id === customerId);
- if (!sale) {
- return null;
- }
-
- return {
- event: sale.event,
- eventName: sale.eventName,
- amount: sale.sale.amount,
- paymentProcessor: sale.sale.paymentProcessor,
- };
- },
- { timeout: 30_000 },
- )
- .toEqual({
- event: "sale",
- eventName: "Purchase",
- amount,
- paymentProcessor: "shopify",
- });
-}
diff --git a/apps/web/playwright/api/shopify/orders.spec.ts b/apps/web/playwright/api/shopify/orders.spec.ts
deleted file mode 100644
index e0f140970a..0000000000
--- a/apps/web/playwright/api/shopify/orders.spec.ts
+++ /dev/null
@@ -1,292 +0,0 @@
-import { prisma } from "@/lib/prisma";
-import { expect } from "@playwright/test";
-import { randomName } from "../../utils";
-import { trackClick, trackLead } from "../conversions/helpers";
-import { test } from "../fixtures";
-import {
- postShopifyOrdersPaidWebhook,
- postShopifyPixel,
- waitForShopifyCheckoutClickId,
- waitForShopifyCustomerSale,
- waitForShopifySaleCommission,
- waitForShopifySaleEvent,
- withShopifyPartner,
-} from "./helpers";
-
-test.describe("Shopify orders/paid attribution", () => {
- test.describe.configure({ timeout: 90_000 });
-
- test("existing customer – records sale event and commission", async ({
- api,
- workspace,
- program,
- }) => {
- await withShopifyPartner(
- {
- workspaceId: workspace.id,
- programId: program.id,
- groupId: program.defaultGroupId,
- },
- async (seeded) => {
- const { clickId } = await trackClick({
- domain: seeded.domain,
- key: seeded.key,
- });
-
- const externalId = randomName("pw_cus", 16);
- await trackLead({
- clickId,
- customerExternalId: externalId,
- });
-
- const checkoutToken = randomName("pw_checkout", 24);
- const { status, data } = await postShopifyOrdersPaidWebhook({
- checkoutToken,
- customer: {
- id: externalId,
- first_name: "Playwright",
- last_name: "Shopify",
- email: `pw.shopify.${externalId}@dub-internal-test.com`,
- },
- });
-
- expect(status).toEqual(200);
- expect(data).toMatch(/Existing customer .+ found\. Order queued/);
-
- const customer = await waitForShopifyCustomerSale({
- workspaceId: workspace.id,
- externalId,
- });
-
- await waitForShopifySaleEvent({ api, customerId: customer.id });
- await waitForShopifySaleCommission({
- partnerId: seeded.partnerId,
- customerId: customer.id,
- });
- },
- );
- });
-
- test("partner discount code – records sale event and commission", async ({
- api,
- workspace,
- program,
- }) => {
- await withShopifyPartner(
- {
- workspaceId: workspace.id,
- programId: program.id,
- groupId: program.defaultGroupId,
- withDiscountCode: true,
- },
- async (seeded) => {
- const externalId = randomName("pw_cus", 16);
- const checkoutToken = randomName("pw_checkout", 24);
- const { status, data } = await postShopifyOrdersPaidWebhook({
- checkoutToken,
- customer: {
- id: externalId,
- first_name: "Playwright",
- last_name: "Shopify",
- email: `pw.shopify.${externalId}@dub-internal-test.com`,
- },
- discount_codes: [{ code: seeded.code }],
- });
-
- expect(status).toEqual(200);
- expect(data).toEqual(
- `[Shopify] Partner discount code ${seeded.code} found. Order queued for processing.`,
- );
-
- const customer = await waitForShopifyCustomerSale({
- workspaceId: workspace.id,
- externalId,
- });
-
- await waitForShopifySaleEvent({ api, customerId: customer.id });
- await waitForShopifySaleCommission({
- partnerId: seeded.partnerId,
- customerId: customer.id,
- });
- },
- );
- });
-
- test("new customer – webhook waits for pixel", async () => {
- const checkoutToken = randomName("pw_checkout", 24);
- const { status, data } = await postShopifyOrdersPaidWebhook({
- checkoutToken,
- });
-
- expect(status).toEqual(200);
- expect(data).toEqual("[Shopify] Waiting for pixel event to arrive...");
- });
-
- test("pixel first, then webhook – records sale event and commission", async ({
- api,
- workspace,
- program,
- }) => {
- await withShopifyPartner(
- {
- workspaceId: workspace.id,
- programId: program.id,
- groupId: program.defaultGroupId,
- },
- async (seeded) => {
- const { clickId } = await trackClick({
- domain: seeded.domain,
- key: seeded.key,
- });
-
- const checkoutToken = randomName("pw_checkout", 24);
- const externalId = randomName("pw_cus", 16);
-
- const pixel = await postShopifyPixel({ clickId, checkoutToken });
- expect(pixel).toEqual({ status: 200, data: "OK" });
-
- await waitForShopifyCheckoutClickId({ checkoutToken, clickId });
-
- const { status, data } = await postShopifyOrdersPaidWebhook({
- checkoutToken,
- customer: {
- id: externalId,
- first_name: "Playwright",
- last_name: "Shopify",
- email: `pw.shopify.${externalId}@dub-internal-test.com`,
- },
- });
-
- expect(status).toEqual(200);
- expect(data).toEqual(
- `[Shopify] Click ID ${clickId} found. Order queued for processing.`,
- );
-
- const customer = await waitForShopifyCustomerSale({
- workspaceId: workspace.id,
- externalId,
- });
-
- await waitForShopifySaleEvent({ api, customerId: customer.id });
- await waitForShopifySaleCommission({
- partnerId: seeded.partnerId,
- customerId: customer.id,
- });
- },
- );
- });
-
- test("webhook first, then pixel – records sale event and commission", async ({
- api,
- workspace,
- program,
- }) => {
- await withShopifyPartner(
- {
- workspaceId: workspace.id,
- programId: program.id,
- groupId: program.defaultGroupId,
- },
- async (seeded) => {
- const { clickId } = await trackClick({
- domain: seeded.domain,
- key: seeded.key,
- });
-
- const checkoutToken = randomName("pw_checkout", 24);
- const externalId = randomName("pw_cus", 16);
-
- const webhook = await postShopifyOrdersPaidWebhook({
- checkoutToken,
- customer: {
- id: externalId,
- first_name: "Playwright",
- last_name: "Shopify",
- email: `pw.shopify.${externalId}@dub-internal-test.com`,
- },
- });
- expect(webhook.status).toEqual(200);
- expect(webhook.data).toEqual(
- "[Shopify] Waiting for pixel event to arrive...",
- );
-
- const pixel = await postShopifyPixel({ clickId, checkoutToken });
- expect(pixel).toEqual({ status: 200, data: "OK" });
-
- const customer = await waitForShopifyCustomerSale({
- workspaceId: workspace.id,
- externalId,
- });
-
- await waitForShopifySaleEvent({ api, customerId: customer.id });
- await waitForShopifySaleCommission({
- partnerId: seeded.partnerId,
- customerId: customer.id,
- });
- },
- );
- });
-
- test("pixel and webhook arrive at the same time – records one sale and commission", async ({
- api,
- workspace,
- program,
- }) => {
- await withShopifyPartner(
- {
- workspaceId: workspace.id,
- programId: program.id,
- groupId: program.defaultGroupId,
- },
- async (seeded) => {
- const { clickId } = await trackClick({
- domain: seeded.domain,
- key: seeded.key,
- });
-
- const checkoutToken = randomName("pw_checkout", 24);
- const externalId = randomName("pw_cus", 16);
- const customer = {
- id: externalId,
- first_name: "Playwright",
- last_name: "Shopify",
- email: `pw.shopify.${externalId}@dub-internal-test.com`,
- };
-
- const [pixel, webhook] = await Promise.all([
- postShopifyPixel({ clickId, checkoutToken }),
- postShopifyOrdersPaidWebhook({ checkoutToken, customer }),
- ]);
-
- expect(pixel).toEqual({ status: 200, data: "OK" });
- expect(webhook.status).toEqual(200);
- expect([
- `[Shopify] Click ID ${clickId} found. Order queued for processing.`,
- "[Shopify] Waiting for pixel event to arrive...",
- ]).toContain(webhook.data);
-
- const createdCustomer = await waitForShopifyCustomerSale({
- workspaceId: workspace.id,
- externalId,
- });
-
- await waitForShopifySaleEvent({ api, customerId: createdCustomer.id });
- await waitForShopifySaleCommission({
- partnerId: seeded.partnerId,
- customerId: createdCustomer.id,
- });
-
- // Rendezvous claim + sale idempotency: exactly one sale for this checkout.
- expect(createdCustomer.sales).toEqual(1);
- const commissions = await prisma.commission.count({
- where: {
- partnerId: seeded.partnerId,
- customerId: createdCustomer.id,
- type: "sale",
- },
- });
- expect(commissions).toEqual(1);
- },
- );
- });
-});
diff --git a/apps/web/scripts/dev/simulate-shopify-conversion.ts b/apps/web/scripts/dev/simulate-shopify-conversion.ts
new file mode 100644
index 0000000000..e769e86c81
--- /dev/null
+++ b/apps/web/scripts/dev/simulate-shopify-conversion.ts
@@ -0,0 +1,111 @@
+import "dotenv-flow/config";
+
+import { nanoid } from "@dub/utils";
+import { createHmac } from "crypto";
+
+async function main() {
+ const clickId = "";
+ const checkoutToken = nanoid(10);
+ const storeId = "store.dub.co";
+
+ await trackPixel({
+ clickId,
+ checkoutToken,
+ });
+
+ await trackOrderPaid({
+ checkoutToken,
+ storeId,
+ });
+}
+
+async function trackPixel({
+ clickId,
+ checkoutToken,
+}: {
+ clickId: string;
+ checkoutToken: string;
+}) {
+ const response = await fetch("http://localhost:8888/api/shopify/pixel", {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify({
+ clickId,
+ checkoutToken,
+ }),
+ });
+
+ return response.json();
+}
+
+async function trackOrderPaid({
+ checkoutToken,
+ storeId,
+}: {
+ checkoutToken: string;
+ storeId: string;
+}) {
+ const payload = shopifyOrderPayload({ checkoutToken });
+ const body = JSON.stringify(payload);
+ const signature = shopifyWebhookSignature(body);
+
+ const response = await fetch(
+ "http://localhost:8888/api/shopify/integration/webhook",
+ {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ "x-shopify-topic": "orders/paid",
+ "x-shopify-shop-domain": storeId,
+ ...(signature && { "x-shopify-hmac-sha256": signature }),
+ },
+ body,
+ },
+ );
+
+ return response.json();
+}
+
+function shopifyWebhookSignature(body: string) {
+ const secret = process.env.SHOPIFY_WEBHOOK_SECRET;
+
+ if (!secret) {
+ return undefined;
+ }
+
+ return createHmac("sha256", secret).update(body, "utf8").digest("base64");
+}
+
+function shopifyOrderPayload({
+ checkoutToken,
+ ...overrides
+}: {
+ checkoutToken: string;
+} & Record<string, unknown>) {
+ return {
+ confirmation_number: nanoid(10),
+ checkout_token: checkoutToken,
+ customer: {
+ id: nanoid(10),
+ first_name: "John",
+ last_name: "Doe",
+ email: `john.doe.${nanoid(5)}@example.com`,
+ },
+ current_subtotal_price_set: {
+ shop_money: {
+ amount: 50,
+ currency_code: "USD",
+ },
+ },
+ discount_codes: [],
+ billing_address: {
+ province: "California",
+ country_code: "US",
+ },
+ ...overrides,
+ };
+}
+
+main();