File tree Expand file tree Collapse file tree 4 files changed +23
-45
lines changed
cron/shopify/checkout-completed Expand file tree Collapse file tree 4 files changed +23
-45
lines changed Original file line number Diff line number Diff 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
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 1- import { parseRequestBody } from "@/lib/api/utils" ;
21import { log } from "@dub/utils" ;
32import { NextResponse } from "next/server" ;
4- import { customerCreated } from "./customer-created" ;
53import { 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
179export 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 ;
Original file line number Diff line number Diff line change 11import 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+ } ;
You can’t perform that action at this time.
0 commit comments