Table of Contents
- type:
- integration
- system:
- node.js
- name:
- Node.js HMAC Price Validation
- description:
- Node.js function for encrypting values for an add to cart link or form
- date:
- 2016-07-30
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 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, 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]; }); }