The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
some fixes
Kiran K9f81e273ead7
diff --git a/apps/web/app/api/cron/shopify/checkout-completed/route.ts b/apps/web/app/api/cron/shopify/checkout-completed/route.tsindex bc4c471f91..9d0ed9649d 100644--- a/apps/web/app/api/cron/shopify/checkout-completed/route.ts+++ b/apps/web/app/api/cron/shopify/checkout-completed/route.ts@@ -127,13 +127,13 @@ export async function POST(req: Request) {]);}- const amount = Number(parsedOrder.total_price);+ const eventId = nanoid(16);+ const amount = Number(parsedOrder.total_price) * 100;const currency = parsedOrder.currency;const invoiceId = parsedOrder.confirmation_number;-- const eventId = nanoid(16);const paymentProcessor = "shopify";+const [_sale, link, _project] = await Promise.all([// record salerecordSale({@@ -145,9 +145,8 @@ export async function POST(req: Request) {amount,currency,invoice_id: invoiceId,- metadata: JSON.stringify({- parsedOrder,- }),+ metadata: JSON.stringify(parsedOrder),+}),// update link sales countdiff --git a/apps/web/app/api/shopify/webhook/customer-created.ts b/apps/web/app/api/shopify/webhook/customer-created.tsdeleted file mode 100644index a285d6d762..0000000000--- a/apps/web/app/api/shopify/webhook/customer-created.ts+++ /dev/null@@ -1,16 +0,0 @@-import { generateRandomName } from "@/lib/names";-import { prisma } from "@dub/prisma";--// TODO: We probably don't need this event--export async function customerCreated(event: any) {- console.log("customerCreated", event);-- await prisma.customer.create({- data: {- projectId: "cl7pj5kq4006835rbjlt2ofka",- externalId: event.id.toString(),- name: generateRandomName(),- },- });-}diff --git a/apps/web/app/api/shopify/webhook/route.ts b/apps/web/app/api/shopify/webhook/route.tsindex 57b158f015..c6c9dc786e 100644--- a/apps/web/app/api/shopify/webhook/route.ts+++ b/apps/web/app/api/shopify/webhook/route.ts@@ -1,25 +1,20 @@-import { parseRequestBody } from "@/lib/api/utils";import { log } from "@dub/utils";import { NextResponse } from "next/server";-import { customerCreated } from "./customer-created";import { orderPaid } from "./order-paid";+import { verifyShopifySignature } from "./utils";-// TODO:-// Verify the webhook signature--const relevantTopics = new Set([- "customers/create",- "customers/update",- "orders/paid",-]);+const relevantTopics = new Set(["orders/paid"]);// POST /api/shopify/webhook – Listen to Shopify webhook eventsexport const POST = async (req: Request) => {- const body = await parseRequestBody(req);+ const body = await req.json();// Find the topic from the headersconst headers = req.headers;const topic = headers.get("x-shopify-topic") || "";+ const signature = headers.get("x-shopify-hmac-sha256") || "";++ await verifyShopifySignature({ body, signature });if (!relevantTopics.has(topic)) {return new Response("Unsupported topic, skipping...", {@@ -29,9 +24,6 @@ export const POST = async (req: Request) => {try {switch (topic) {- case "customers/create":- await customerCreated(body);- break;case "orders/paid":await orderPaid(body);break;diff --git a/apps/web/app/api/shopify/webhook/utils.ts b/apps/web/app/api/shopify/webhook/utils.tsindex 0747f3897d..e1902d6957 100644--- a/apps/web/app/api/shopify/webhook/utils.ts+++ b/apps/web/app/api/shopify/webhook/utils.ts@@ -1,22 +1,25 @@import crypto from "crypto";-import { NextRequest } from "next/server";-export async function verifyWebhookSignature(req: NextRequest) {- const signature = req.headers["x-shopify-hmac-sha256"];-- const genSig = 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(req))+ .update(JSON.stringify(body)).digest("base64");console.log({- genSig,+ generatedSignature,signature,});- if (genSig !== signature) {+ if (generatedSignature !== signature) {throw new Error("Invalid webhook signature.");}return true;-}+};