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.

handle mandate webhook

Kiran K428359c57abe
diff --git a/apps/web/app/api/shopify/integration/webhook/customers-data-request.ts b/apps/web/app/api/shopify/integration/webhook/customers-data-request.ts
new file mode 100644
index 0000000000..0fb2e84e4f
--- /dev/null
+++ b/apps/web/app/api/shopify/integration/webhook/customers-data-request.ts
@@ -0,0 +1,34 @@
+import { waitUntil } from "@vercel/functions";
+import { sendEmail } from "emails";
+import { z } from "zod";
+
+const schema = z.object({
+ shop_domain: z.string(),
+ orders_requested: z.array(z.number()),
+ customer: z.object({
+ id: z.number(),
+ email: z.string(),
+ phone: z.string(),
+ }),
+});
+
+export async function customersDataRequest({ body }: { body: any }) {
+ const {
+ customer,
+ shop_domain: shopDomain,
+ orders_requested: ordersRequested,
+ } = schema.parse(body);
+
+ waitUntil(
+ sendEmail({
+ email: "[email protected]",
+ from: "Steven Tey <[email protected]>",
+ subject: "[Shopify] - Customer Data Request received",
+ text: `Customer Data Request received for shop: ${shopDomain}.
+ Customer ID: ${customer.id},
+ Email: ${customer.email},
+ Phone: ${customer.phone},
+ Orders Requested: ${ordersRequested.join(", ")}`,
+ }),
+ );
+}
diff --git a/apps/web/app/api/shopify/integration/webhook/customers-redact.ts b/apps/web/app/api/shopify/integration/webhook/customers-redact.ts
new file mode 100644
index 0000000000..511766e725
--- /dev/null
+++ b/apps/web/app/api/shopify/integration/webhook/customers-redact.ts
@@ -0,0 +1,34 @@
+import { waitUntil } from "@vercel/functions";
+import { sendEmail } from "emails";
+import { z } from "zod";
+
+const schema = z.object({
+ shop_domain: z.string(),
+ orders_to_redact: z.array(z.number()),
+ customer: z.object({
+ id: z.number(),
+ email: z.string(),
+ phone: z.string(),
+ }),
+});
+
+export async function customersRedact({ body }: { body: any }) {
+ const {
+ customer,
+ shop_domain: shopDomain,
+ orders_to_redact: ordersToRedact,
+ } = schema.parse(body);
+
+ waitUntil(
+ sendEmail({
+ email: "[email protected]",
+ from: "Steven Tey <[email protected]>",
+ subject: "[Shopify] - Customer Redacted request received",
+ text: `Customer Redacted request received for shop: ${shopDomain}.
+ Customer ID: ${customer.id},
+ Email: ${customer.email},
+ Phone: ${customer.phone},
+ Orders to Redact: ${ordersToRedact.join(", ")}`,
+ }),
+ );
+}
diff --git a/apps/web/app/api/shopify/integration/webhook/order-paid.ts b/apps/web/app/api/shopify/integration/webhook/order-paid.ts
index 580b1b1bc6..8385478d1e 100644
--- a/apps/web/app/api/shopify/integration/webhook/order-paid.ts
+++ b/apps/web/app/api/shopify/integration/webhook/order-paid.ts
@@ -2,20 +2,17 @@ import { qstash } from "@/lib/cron";
import { redis } from "@/lib/upstash";
import { APP_DOMAIN_WITH_NGROK } from "@dub/utils";
-// TODO:
-// Instead of checkout_token, maybe use order_number or checkout_id
-
export async function orderPaid({
- order,
+ body,
workspaceId,
}: {
- order: any;
+ body: any;
workspaceId: string;
}) {
- const checkoutToken = order.checkout_token;
+ const checkoutToken = body.checkout_token;
await redis.hset(`shopify:checkout:${checkoutToken}`, {
- order,
+ order: body,
});
await qstash.publishJSON({
diff --git a/apps/web/app/api/shopify/integration/webhook/route.ts b/apps/web/app/api/shopify/integration/webhook/route.ts
index f08f4198f4..40b57af1d8 100644
--- a/apps/web/app/api/shopify/integration/webhook/route.ts
+++ b/apps/web/app/api/shopify/integration/webhook/route.ts
@@ -1,10 +1,20 @@
import { prisma } from "@dub/prisma";
import { log } from "@dub/utils";
+import crypto from "crypto";
import { NextResponse } from "next/server";
+import { customersDataRequest } from "./customers-data-request";
+import { customersRedact } from "./customers-redact";
import { orderPaid } from "./order-paid";
-import { verifyShopifySignature } from "./utils";
+import { shopRedact } from "./shop-redact";
-const relevantTopics = new Set(["orders/paid"]);
+const relevantTopics = new Set([
+ "orders/paid",
+
+ // Mandatory compliance webhooks
+ "customers/data_request",
+ "customers/redact",
+ "shop/redact",
+]);
// POST /api/shopify/integration/webhook – Listen to Shopify webhook events
export const POST = async (req: Request) => {
@@ -12,17 +22,29 @@ export const POST = async (req: Request) => {
const headers = req.headers;
const topic = headers.get("x-shopify-topic") || "";
const signature = headers.get("x-shopify-hmac-sha256") || "";
- const shop = headers.get("x-shopify-shop-domain") || "";
+ const shopDomain = headers.get("x-shopify-shop-domain") || "";
- await verifyShopifySignature({ body, signature });
+ // Verify signature
+ const generatedSignature = crypto
+ .createHmac("sha256", `${process.env.SHOPIFY_WEBHOOK_SECRET}`)
+ .update(JSON.stringify(body))
+ .digest("base64");
+ if (generatedSignature !== signature) {
+ return new Response(`[Shopify] Invalid signature. Skipping...`, {
+ status: 401,
+ });
+ }
+
+ // Check if topic is relevant
if (!relevantTopics.has(topic)) {
return new Response(`[Shopify] Unsupported topic: ${topic}. Skipping...`);
}
+ // Find workspace
const workspace = await prisma.project.findUnique({
where: {
- shopifyStoreId: shop,
+ shopifyStoreId: shopDomain,
},
select: {
id: true,
@@ -31,7 +53,7 @@ export const POST = async (req: Request) => {
if (!workspace) {
return new Response(
- `[Shopify] Workspace not found for shop: ${shop}. Skipping...`,
+ `[Shopify] Workspace not found for shop: ${shopDomain}. Skipping...`,
);
}
@@ -39,10 +61,25 @@ export const POST = async (req: Request) => {
switch (topic) {
case "orders/paid":
await orderPaid({
- order: body,
+ body,
workspaceId: workspace.id,
});
break;
+ case "customers/data_request":
+ await customersDataRequest({
+ body,
+ });
+ break;
+ case "customers/redact":
+ await customersRedact({
+ body,
+ });
+ break;
+ case "shop/redact":
+ await shopRedact({
+ body,
+ });
+ break;
}
} catch (error) {
await log({
diff --git a/apps/web/app/api/shopify/integration/webhook/shop-redact.ts b/apps/web/app/api/shopify/integration/webhook/shop-redact.ts
new file mode 100644
index 0000000000..d4fb50b131
--- /dev/null
+++ b/apps/web/app/api/shopify/integration/webhook/shop-redact.ts
@@ -0,0 +1,20 @@
+import { waitUntil } from "@vercel/functions";
+import { sendEmail } from "emails";
+import { z } from "zod";
+
+const schema = z.object({
+ shop_domain: z.string(),
+});
+
+export async function shopRedact({ body }: { body: any }) {
+ const { shop_domain: shopDomain } = schema.parse(body);
+
+ waitUntil(
+ sendEmail({
+ email: "[email protected]",
+ from: "Steven Tey <[email protected]>",
+ subject: "[Shopify] - Shop Redacted request received",
+ text: `Shop Redacted request received for shop: ${shopDomain}`,
+ }),
+ );
+}
diff --git a/apps/web/app/api/shopify/integration/webhook/utils.ts b/apps/web/app/api/shopify/integration/webhook/utils.ts
deleted file mode 100644
index 2212aad314..0000000000
--- a/apps/web/app/api/shopify/integration/webhook/utils.ts
+++ /dev/null
@@ -1,18 +0,0 @@
-import crypto from "crypto";
-
-export const verifyShopifySignature = async ({
- body,
- signature,
-}: {
- body: Record<string, unknown>;
- signature: string;
-}) => {
- const generatedSignature = crypto
- .createHmac("sha256", `${process.env.SHOPIFY_WEBHOOK_SECRET}`)
- .update(JSON.stringify(body))
- .digest("base64");
-
- if (generatedSignature !== signature) {
- throw new Error("Invalid webhook signature.");
- }
-};