Working with returned data in Nodejs after web hook verification

DemonSmooth
New Member
1 0 0

Figured out how to verify request is coming from Shopify with the following:

const app = express();

app.use('/webhooks', bodyParser.raw({ type: 'application/json' }));
app.use(bodyParser.json());

function isFromShopify(req) {
const hmac = req.get('X-Shopify-Hmac-Sha256')

const hash = crypto
.createHmac('sha256', process.env.SHOPIFY_API_WEBHOOK)
.update(req.body, 'utf8', 'hex')
.digest('base64')

if (hash === hmac) {
console.log('From Shopify!');
return true;
} else {
console.log('Not from Shopify!')
return false;
}
}

 

However, now I need to process data coming back, but when I console log it it's in hex.

 

app.post('/webhooks/orders/create', async (req, res) => {
  
  if (isFromShopify(req)) {
    console.log(req.body)
    res.sendStatus(200);
  } else {
    res.sendStatus(403);
  }

})

 

Returns

From Shopify!
From Shopify!
<Buffer 7b 22 69 64 22 3a 38 32 30 39 38 32 39 31 31 39 34 36 31 35 34 35 30 38 2c 22 65 6d 61 69 6c 22 3a 22 6a 6f 6e 40 64 6f 65 2e 63 61 22 2c 22 63 6c 6f ... 6501 more bytes>
From Shopify!
<Buffer 7b 22 69 64 22 3a 38 32 30 39 38 32 39 31 31 39 34 36 31 35 34 35 30 38 2c 22 65 6d 61 69 6c 22 3a 22 6a 6f 6e 40 64 6f 65 2e 63 61 22 2c 22 63 6c 6f ... 6501 more bytes>
From Shopify!
<Buffer 7b 22 69 64 22 3a 38 32 30 39 38 32 39 31 31 39 34 36 31 35 34 35 30 38 2c 22 65 6d 61 69 6c 22 3a 22 6a 6f 6e 40 64 6f 65 2e 63 61 22 2c 22 63 6c 6f ... 6501 more bytes>
From Shopify!
<Buffer 7b 22 69 64 22 3a 33 32 33 33 35 36 32 34 32 37 35 35 33 2c 22 65 6d 61 69 6c 22 3a 22 6a 7a 61 6d 62 72 61 6e 6f 40 6e 6f 74 63 68 2e 68 65 61 6c 74 ... 9894 more bytes>

 

Any suggestion on how to work with the data returned?

0 Likes