The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
update webhooks
Kiran K95813bcea178
diff --git a/apps/web/app/api/cron/shopify/order-paid/route.ts b/apps/web/app/api/cron/shopify/order-paid/route.tsindex 9969d15568..0be70737f8 100644--- a/apps/web/app/api/cron/shopify/order-paid/route.ts+++ b/apps/web/app/api/cron/shopify/order-paid/route.ts@@ -1,6 +1,6 @@import { DubApiError, handleAndReturnErrorResponse } from "@/lib/api/errors";import { notifyPartnerSale } from "@/lib/api/partners/notify-partner-sale";-import { createSaleData } from "@/lib/api/sales/sale";+import { createSaleData } from "@/lib/api/sales/create-sale-data";import { createId } from "@/lib/api/utils";import { verifyQstashSignature } from "@/lib/cron/verify-qstash";import { generateRandomName } from "@/lib/names";@@ -270,33 +270,39 @@ const createSale = async ({// for program linksif (link.programId) {- const { program, partner } =+ const { program, partnerId, commissionAmount } =await prisma.programEnrollment.findUniqueOrThrow({where: {linkId,},select: {program: true,- partner: {- select: {- id: true,- },- },+ partnerId: true,+ commissionAmount: true,},});const saleRecord = createSaleData({- customerId,- linkId,- clickId,- invoiceId,- eventId,- paymentProcessor,- amount,- currency,- partnerId: partner.id,program,- metadata: order,+ partner: {+ id: partnerId,+ commissionAmount,+ },+ customer: {+ id: customerId,+ linkId,+ clickId,+ },+ sale: {+ amount,+ currency,+ invoiceId,+ eventId,+ paymentProcessor,+ },+ metadata: {+ ...order,+ },});await Promise.allSettled([@@ -306,7 +312,7 @@ const createSale = async ({notifyPartnerSale({partner: {- id: partner.id,+ id: partnerId,referralLink: link.shortLink,},program,diff --git a/apps/web/app/api/shopify/integration/webhook/app-uninstalled.ts b/apps/web/app/api/shopify/integration/webhook/app-uninstalled.tsnew file mode 100644index 0000000000..c7d95a8bc6--- /dev/null+++ b/apps/web/app/api/shopify/integration/webhook/app-uninstalled.ts@@ -0,0 +1,17 @@+import { redis } from "@/lib/upstash";+import { prisma } from "@dub/prisma";++export async function appUninstalled({ shopDomain }: { shopDomain: string }) {+ await Promise.all([+ prisma.project.update({+ where: {+ shopifyStoreId: shopDomain,+ },+ data: {+ shopifyStoreId: null,+ },+ }),++ redis.del(`shopify:shop:${shopDomain}`),+ ]);+}diff --git a/apps/web/app/api/shopify/integration/webhook/order-paid.ts b/apps/web/app/api/shopify/integration/webhook/order-paid.tsindex 8385478d1e..9c94a00394 100644--- a/apps/web/app/api/shopify/integration/webhook/order-paid.ts+++ b/apps/web/app/api/shopify/integration/webhook/order-paid.ts@@ -1,6 +1,11 @@import { qstash } from "@/lib/cron";import { redis } from "@/lib/upstash";import { APP_DOMAIN_WITH_NGROK } from "@dub/utils";+import { z } from "zod";++const schema = z.object({+ checkout_token: z.string(),+});export async function orderPaid({body,@@ -9,7 +14,7 @@ export async function orderPaid({body: any;workspaceId: string;}) {- const checkoutToken = body.checkout_token;+ const { checkout_token: checkoutToken } = schema.parse(body);await redis.hset(`shopify:checkout:${checkoutToken}`, {order: body,diff --git a/apps/web/app/api/shopify/integration/webhook/route.ts b/apps/web/app/api/shopify/integration/webhook/route.tsindex 40b57af1d8..a230d9c2d4 100644--- a/apps/web/app/api/shopify/integration/webhook/route.ts+++ b/apps/web/app/api/shopify/integration/webhook/route.ts@@ -2,6 +2,7 @@ import { prisma } from "@dub/prisma";import { log } from "@dub/utils";import crypto from "crypto";import { NextResponse } from "next/server";+import { appUninstalled } from "./app-uninstalled";import { customersDataRequest } from "./customers-data-request";import { customersRedact } from "./customers-redact";import { orderPaid } from "./order-paid";@@ -11,6 +12,7 @@ const relevantTopics = new Set(["orders/paid",// Mandatory compliance webhooks+ "app/uninstalled","customers/data_request","customers/redact","shop/redact",@@ -18,16 +20,15 @@ const relevantTopics = new Set([// POST /api/shopify/integration/webhook – Listen to Shopify webhook eventsexport const POST = async (req: Request) => {- const body = await req.json();+ const data = await req.text();const headers = req.headers;const topic = headers.get("x-shopify-topic") || "";const signature = headers.get("x-shopify-hmac-sha256") || "";- const shopDomain = headers.get("x-shopify-shop-domain") || "";// Verify signatureconst generatedSignature = crypto.createHmac("sha256", `${process.env.SHOPIFY_WEBHOOK_SECRET}`)- .update(JSON.stringify(body))+ .update(data, "utf8").digest("base64");if (generatedSignature !== signature) {@@ -41,6 +42,9 @@ export const POST = async (req: Request) => {return new Response(`[Shopify] Unsupported topic: ${topic}. Skipping...`);}+ const body = JSON.parse(data);+ const shopDomain = headers.get("x-shopify-shop-domain") || "";+// Find workspaceconst workspace = await prisma.project.findUnique({where: {@@ -80,6 +84,11 @@ export const POST = async (req: Request) => {body,});break;+ case "app/uninstalled":+ await appUninstalled({+ shopDomain,+ });+ break;}} catch (error) {await log({