Skip to content

Instantly share code, notes, and snippets.

@dnetix
Created June 1, 2017 16:51
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dnetix/fea3868afe915229c7d140967e4d8519 to your computer and use it in GitHub Desktop.
Save dnetix/fea3868afe915229c7d140967e4d8519 to your computer and use it in GitHub Desktop.
NodeJS authentication generator required for PlacetoPay's Redirection (Web Checkout)
var crypto = require('crypto');
/**
* Generates authentication data in order to communicate with PlacetoPay Web Checkout service
*
* USAGE:
*
* var authGenerator = new RedirectionAuth("YOUR_LOGIN", "YOUR_TRANKEY");
* var auth = authGenerator.asObject();
*
* IMPORTANT
*
* If you need to make another request and you have already the object instanciated, please make sure to
* use the generate method BEFORE using the asObject again
*
* var auth = authGenerator.generate().asObject()
*
* @param login
* @param tranKey
* @constructor
*/
function RedirectionAuth(login, tranKey) {
var self = this;
var _nonce;
var _seed;
this.generate = function() {
_nonce = Math.random().toString(36).substring(7);
_seed = (new Date()).toISOString();
return self;
};
this.getRealNonce = function() {
return _nonce;
};
this.login = function() {
return login;
};
this.nonce = function() {
return new Buffer(_nonce).toString('base64');
};
this.seed = function() {
return _seed;
};
this.tranKey = function() {
return crypto.createHash('sha1').update(_nonce + seed + tranKey).digest('b64').toString('base64');
};
this.asObject = function() {
return {
"login": self.login(),
"tranKey": self.tranKey(),
"seed": self.seed(),
"nonce": self.nonce()
}
};
// For testing purposes
this.setSeed = function(seed) {
_seed = seed;
return self;
};
this.setNonce = function(nonce) {
_nonce = nonce;
return self;
};
this.generate();
}
module.exports = RedirectionAuth;
@ramonluis1987
Copy link

Line 50 should be

return crypto.createHash('sha1').update(_nonce+ _seed + tranKey).digest('b64').toString('base64');

change seed to _seed

@carboleda
Copy link

carboleda commented Oct 21, 2017

The seed format should be 'YYYY-MM-DDTHH:mm:ssZ' Line 29
_seed = (new Date()).toISOString();

I resolved it using moment (import momento before)
_seed = moment().format('YYYY-MM-DDTHH:mm:ssZ');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment