---- dataentry integration ---- type : integration # do not change this line supports-foxycart-version-from : # Minimum required FoxyCart version for this to work supports-foxycart-version-to : # Last FoxyCart version that supports this (leave empty if unknown) systems : node.js # the system(s) that the integration is for, separated with commas if more than one name : Node.js HMAC Price Validation # the name of the integration description : Node.js function for encrypting values for an add to cart link or form tags_tags : node, javascript # tags, separated by commas. don't include the "system" here. date_dt : 2016-07-30 # the date in YYYY-MM-DD format version : # if you have a version for your code, enter it here developer_url : # if you'd like a link back to your site, stick it here ---- ====== Node.js HMAC Price Validation ====== //**Please note:**// The code on this page is submitted by members of the FoxyCart community, and may not verified by FoxyCart.com LLC in any way, shape, or form. Please double check the code before installing. If you need help with it please [[http://forum.foxycart.com/|post in our forum]], but if we cannot offer assistance (due to unfamiliarity with this particular system or language) we apologize in advance. ===== Description ===== colinskow on the FoxyCart forums has developed a Node.js version of the simple add to cart link and form validation, [[static:redirect:price_validation|as detailed on this page]]. ===== Code ===== var createHmac = require('crypto').createHmac; module.exports = function hmacValidate(secret, key, value, productCode, parentCode = '', encodeValue = false) { let hmac = createHmac('sha256', secret); let toEncode = escapeHtml(productCode + parentCode + key + value); let label = encodeValue ? value : key; hmac.update(toEncode); let result = label + '||' + hmac.digest('hex').toLowerCase(); if(value === '--OPEN--') { result += '||open'; } return result; }; function escapeHtml(text) { var map = { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }; return text.replace(/[&<>"']/g, function(m) { return map[m]; }); } ===== Reference ===== [[https://forum.foxycart.com/discussion/10795/hmac-product-verification-with-node-js|Original forum post]]