Skip to content

Commit

Permalink
Added debug logs, transbank needs those in order to pass certification
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianBravoA committed Oct 5, 2016
1 parent 28a0d98 commit 987bb4f
Showing 4 changed files with 41 additions and 17 deletions.
3 changes: 2 additions & 1 deletion lib/config/secrets.js
Original file line number Diff line number Diff line change
@@ -5,5 +5,6 @@ module.exports = {
ENVIRONMENT : process.env.TBK_ENVIRONMENT || 'INTEGRACION', //TBK debug mode as default
PRIVATE_KEY : process.env.TBK_PRIVATE_KEY || __dirname + '/certs/private_test_cert.pem',
PUBLIC_KEY : process.env.TBK_PUBLIC_KEY || __dirname + '/certs/public_test_cert.pem',
WEBPAY_KEY : process.env.TBK_WEBPAY_KEY || __dirname + '/certs/webpay_test_cert.pem'
WEBPAY_KEY : process.env.TBK_WEBPAY_KEY || __dirname + '/certs/webpay_test_cert.pem',
DEBUG_LOGS : process.env.TBK_DEBUG_LOG || false
};
25 changes: 16 additions & 9 deletions lib/security/WSSecurityCertTBK.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";

var optional = require("optional");
var ursa = optional('ursa');
var ursa = optional('ursa');
var fs = require('fs');
var path = require('path');
var ejs = require('ejs');
@@ -37,16 +37,17 @@ function generateId() {
return uuid.v4().replace(/-/gm, '');
}

function WSSecurityCertTBK(privatePEM, publicP12PEM, password, encoding) {
function WSSecurityCertTBK(privatePEM, publicP12PEM, password, encoding, debug) {
if (!ursa) {
throw new Error('Module ursa must be installed to use WSSecurityCertTBK');
}
this.privateKey = ursa.createPrivateKey(privatePEM, password, encoding);
this.publicP12PEM = publicP12PEM.toString().replace('-----BEGIN CERTIFICATE-----', '').replace('-----END CERTIFICATE-----', '').replace(/(\r\n|\n|\r)/gm, '');
this.debug = debug;
this.privateKey = ursa.createPrivateKey(privatePEM, password, encoding);
this.publicP12PEM = publicP12PEM.toString().replace('-----BEGIN CERTIFICATE-----', '').replace('-----END CERTIFICATE-----', '').replace(/(\r\n|\n|\r)/gm, '');

this.signer = new SignedXml();
this.signer.signingKey = this.privateKey.toPrivatePem();
this.x509Id = "x509-" + generateId();
this.signer = new SignedXml();
this.signer.signingKey = this.privateKey.toPrivatePem();
this.x509Id = "x509-" + generateId();

var _this = this;
pem.readCertificateInfo(publicP12PEM,
@@ -92,11 +93,17 @@ WSSecurityCertTBK.prototype.postProcess = function (xml) {
issuerName : this.issuer
});

var xmlWithSec = insertStr(secHeader, xml, xml.indexOf('</soap:Header>'));
var xmlWithSec = insertStr(secHeader, xml, xml.indexOf('</soap:Header>'));

this.signer.computeSignature(xmlWithSec);

return insertStr(this.signer.getSignatureXml(), xmlWithSec, xmlWithSec.indexOf('</wsse:Security>'));
var retXml = insertStr(this.signer.getSignatureXml(), xmlWithSec, xmlWithSec.indexOf('</wsse:Security>'));

if( this.debug ){
console.log("TBK INPUT XML > "+retXml);
}

return retXml;
};

module.exports = WSSecurityCertTBK;
25 changes: 20 additions & 5 deletions lib/tbk.js
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@ function TBK( config ){
this.privateCert = fs.readFileSync(this.config.PRIVATE_KEY);
this.publicCert = fs.readFileSync(this.config.PUBLIC_KEY);

var wsSecurity = new WSSecurityCertTBK(this.privateCert, this.publicCert, '', 'utf8');
var wsSecurity = new WSSecurityCertTBK(this.privateCert, this.publicCert, '', 'utf8', this.config.DEBUG_LOGS);

var options = {
ignoredNamespaces: {
@@ -37,13 +37,16 @@ function TBK( config ){
self.soapClient = client;
self.soapClient.setSecurity(wsSecurity);

console.log("TBK Client description > "+JSON.stringify(self.soapClient.describe()));
if( self.config.DEBUG_LOGS ){
console.log("TBK Client description > "+JSON.stringify(self.soapClient.describe()));
}
}
}
);
}

TBK.prototype.initTransaction = function(amount, buyOrder, sessionId, returnURL, finalURL, callback /*F(error, result)*/) {
var self = this;
if( !this.soapClient ){
return console.error("SOAP Client not ready yet");
}
@@ -62,10 +65,12 @@ TBK.prototype.initTransaction = function(amount, buyOrder, sessionId, returnURL
}
};

var _this = this;

this.soapClient.WSWebpayServiceImplService.WSWebpayServiceImplPort.initTransaction(initInput, function(error, result, raw){
if( !error && _this.verifySignature(raw) ){
if( self.config.DEBUG_LOGS ){
console.log("TBK OUTPUT XML > "+raw);
}

if( !error && self.verifySignature(raw) ){
callback(error, result);
}else{
callback({message : "Invalid signature or "+error}, result);
@@ -98,12 +103,17 @@ TBK.prototype.verifySignature = function(xml){
};

TBK.prototype.getTransactionResult = function(token, callback /* F(error, result)*/){
var self = this;
var data = {
"tokenInput" : token
};
var _this = this;

this.soapClient.WSWebpayServiceImplService.WSWebpayServiceImplPort.getTransactionResult(data, function(error, result, raw){
if( self.config.DEBUG_LOGS ){
console.log("TBK OUTPUT XML > "+raw);
}

if(!error && _this.verifySignature(raw)){
callback(error, result);
}else{
@@ -113,13 +123,18 @@ TBK.prototype.getTransactionResult = function(token, callback /* F(error, resul
};

TBK.prototype.acknowledgeTransaction = function(token, callback /* F(error, result)*/){
var self = this;
var data = {
"tokenInput" : token
};

var _this = this;

this.soapClient.WSWebpayServiceImplService.WSWebpayServiceImplPort.acknowledgeTransaction(data, function(error, result, raw){
if( self.config.DEBUG_LOGS ){
console.log("TBK OUTPUT XML > "+raw);
}

if(!error && _this.verifySignature(raw)){
callback(error, result);
}else{
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "transbank",
"version": "1.1.1",
"version": "1.1.2",
"description": "Transbank implementation for node.js (Not official)",
"main": "index.js",
"scripts": {
@@ -11,10 +11,11 @@
"tbk"
],
"author": "Fabián Bravo <fabian.bravo.abarca@gmail.com>",
"license": "ISC",
"license": "LGPL",
"dependencies": {
"pem": "^1.8.3",
"soap": "^0.16.0",
"ursa": "^0.9.4",
"xml": "^1.0.1",
"xml-crypto": "^0.8.4",
"xmldom": "^0.1.22"

0 comments on commit 987bb4f

Please sign in to comment.