Skip to content

Instantly share code, notes, and snippets.

@moltar
Created September 11, 2017 16:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moltar/f2a9cbcae5e10b036bb6826be8fe83e8 to your computer and use it in GitHub Desktop.
Save moltar/f2a9cbcae5e10b036bb6826be8fe83e8 to your computer and use it in GitHub Desktop.
PayKickStart IPN POST Checksum Verification in JavaScript
function verifyChecksum (params, secret) {
const valuesArray = Object
.keys(params)
.filter((key) => key && params[key] && key !== 'verification_code')
.map((key) => params[key])
const valuesObject = {}
for (let i = 0; i < valuesArray.length; i++) {
// stringify indexes for ASCII sort
valuesObject[i.toString()] = valuesArray[i]
}
const string = Object
.keys(valuesObject)
.sort()
// now access the array by actual numerical index
.map((key) => valuesArray[parseInt(key)])
.join('|')
const checksum = sha1(string, secret)
log.info('Checksum string %s == %s: %s', checksum, params.verification_code, string)
return checksum === params.verification_code
}
function sha1 (string, secret) {
const hmac = crypto.createHmac('sha1', secret)
return hmac.update(Buffer.from(string, 'utf-8')).digest('hex')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment