import { checkWebhookHmacValidity , createRawBody } from 'shopify-hmac-validation' ; import validateWebhook from '../../../../_utils/validateWebhook' ; import { NextApiRequest , NextApiResponse } from 'next' ; export default async ( req , res ) => { if ( req . method !== 'POST' ) { return res . status ( 429 ) . json ( { body : 'Unauthorized access' } ) ; } const hmac = req . headers [ 'x-shopify-hmac-sha256' ] ; const shop = req . headers [ 'x-shopify-shop-domain' ] ; if ( ! shop || ! hmac ) { return res . status ( 429 ) . json ( { body : 'Request could not be completed' } ) ; } if ( validateWebhook ( req , hmac ) ) { console . log ( 'webhook Valid' ) ; return res . status ( 200 ) . json ( { body : `This app does not store customer data on ${ shop }` } ) ; } else { console . error ( { error : true , req : { headers : req . headers , body : req . body , shop } } ) ; return res . status ( 429 ) . json ( { body : 'Request is not validated by HMAC' } ) ; } } ;