Skip to content

Commit 9f81e27

Browse files
committed
some fixes
1 parent e47cd21 commit 9f81e27

File tree

4 files changed

+23
-45
lines changed

4 files changed

+23
-45
lines changed

apps/web/app/api/cron/shopify/checkout-completed/route.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,13 @@ export async function POST(req: Request) {
127127
]);
128128
}
129129

130-
const amount = Number(parsedOrder.total_price);
130+
const eventId = nanoid(16);
131+
const amount = Number(parsedOrder.total_price) * 100;
131132
const currency = parsedOrder.currency;
132133
const invoiceId = parsedOrder.confirmation_number;
133-
134-
const eventId = nanoid(16);
135134
const paymentProcessor = "shopify";
136135

136+
137137
const [_sale, link, _project] = await Promise.all([
138138
// record sale
139139
recordSale({
@@ -145,9 +145,8 @@ export async function POST(req: Request) {
145145
amount,
146146
currency,
147147
invoice_id: invoiceId,
148-
metadata: JSON.stringify({
149-
parsedOrder,
150-
}),
148+
metadata: JSON.stringify(parsedOrder),
149+
151150
}),
152151

153152
// update link sales count

apps/web/app/api/shopify/webhook/customer-created.ts

Lines changed: 0 additions & 16 deletions
This file was deleted.

apps/web/app/api/shopify/webhook/route.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,20 @@
1-
import { parseRequestBody } from "@/lib/api/utils";
21
import { log } from "@dub/utils";
32
import { NextResponse } from "next/server";
4-
import { customerCreated } from "./customer-created";
53
import { orderPaid } from "./order-paid";
4+
import { verifyShopifySignature } from "./utils";
65

7-
// TODO:
8-
// Verify the webhook signature
9-
10-
const relevantTopics = new Set([
11-
"customers/create",
12-
"customers/update",
13-
"orders/paid",
14-
]);
6+
const relevantTopics = new Set(["orders/paid"]);
157

168
// POST /api/shopify/webhook – Listen to Shopify webhook events
179
export const POST = async (req: Request) => {
18-
const body = await parseRequestBody(req);
10+
const body = await req.json();
1911

2012
// Find the topic from the headers
2113
const headers = req.headers;
2214
const topic = headers.get("x-shopify-topic") || "";
15+
const signature = headers.get("x-shopify-hmac-sha256") || "";
16+
17+
await verifyShopifySignature({ body, signature });
2318

2419
if (!relevantTopics.has(topic)) {
2520
return new Response("Unsupported topic, skipping...", {
@@ -29,9 +24,6 @@ export const POST = async (req: Request) => {
2924

3025
try {
3126
switch (topic) {
32-
case "customers/create":
33-
await customerCreated(body);
34-
break;
3527
case "orders/paid":
3628
await orderPaid(body);
3729
break;
Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
import crypto from "crypto";
2-
import { NextRequest } from "next/server";
32

4-
export async function verifyWebhookSignature(req: NextRequest) {
5-
const signature = req.headers["x-shopify-hmac-sha256"];
6-
7-
const genSig = crypto
3+
export const verifyShopifySignature = async ({
4+
body,
5+
signature,
6+
}: {
7+
body: Record<string, unknown>;
8+
signature: string;
9+
}) => {
10+
const generatedSignature = crypto
811
.createHmac("sha256", `${process.env.SHOPIFY_WEBHOOK_SECRET}`)
9-
.update(JSON.stringify(req))
12+
.update(JSON.stringify(body))
1013
.digest("base64");
1114

1215
console.log({
13-
genSig,
16+
generatedSignature,
1417
signature,
1518
});
1619

17-
if (genSig !== signature) {
20+
if (generatedSignature !== signature) {
1821
throw new Error("Invalid webhook signature.");
1922
}
2023

2124
return true;
22-
}
25+
};

0 commit comments

Comments
 (0)