Created
September 11, 2017 16:01
-
-
Save moltar/f2a9cbcae5e10b036bb6826be8fe83e8 to your computer and use it in GitHub Desktop.
PayKickStart IPN POST Checksum Verification in JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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