Skip to content

Commit

Permalink
v0.3.0 Refactoring pay method to accept strict sale JSON
Browse files Browse the repository at this point in the history
Old 'gatewa.pay' signature was removed.
The new one obligates user to save client before execute pay method
  • Loading branch information
rafaelverger committed Mar 20, 2014
1 parent b6bf513 commit e0053ff
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 110 deletions.
41 changes: 5 additions & 36 deletions lib/maxipago.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,30 +59,12 @@ exports.Gateway = (function(){
return xmlBuilder.buildObject({'api-request': data});
};

var buildSaleXML = function(mpClientId, creditCard, order){
var buildSaleXML = function(sale){
var data = {};
data.version = MP_API_VERSION;
data.verification = MP_AUTH;
data.order = {
sale: {
processorID: '1',
referenceNum: order.code,
billing: {
name: creditCard.fullname,
},
transactionDetail: {
payType: {
creditCard: creditCard
}
},
payment: {
currencyCode: 'BRL',
chargeTotal: order.total
},
saveOnFile: {
customerToken: mpClientId
}
}
sale: sale
};
delete data.order.sale.transactionDetail.payType.creditCard.fullname;
return xmlBuilder.buildObject({'transaction-request': data});
Expand All @@ -102,22 +84,9 @@ exports.Gateway = (function(){

}

Gateway.prototype.pay = function(client, order, creditCard, callback) {
var clientXML = buildAddCostumerXML(client),
gateway_cli = this.client;

gateway_cli.post(
MP_POST_API,
clientXML,
reqHandler(function(err, mp_err, data){
if (err) return callback(err);
if (mp_err) return callback(err, mp_err, data);

var cId = data.result.customerId;
saleXML = buildSaleXML(cId, creditCard, order);
gateway_cli.post(MP_POST_XML, saleXML, reqHandler(callback));
})
);
Gateway.prototype.pay = function(sale, callback) {
var saleXML = buildSaleXML(sale);
this.client.post(MP_POST_XML, saleXML, reqHandler(callback));
};

Gateway.prototype.saveClient = function(client, callback) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "maxipago",
"version": "0.2.0",
"version": "0.3.0",
"description": "A NodeJS lib for MaxiPago Gateway",
"author": {
"name": "Rafael Verger",
Expand Down
110 changes: 59 additions & 51 deletions test/gateway-pay-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,70 +2,78 @@ var util = require('./index'),
mpGateway = util.buildGateway();

exports.testPaySuccess = function(test){
var payData = util.buildPayData(true);
test.expect(16);

test.expect(15);
var client = util.basicClient();
mpGateway.saveClient(client, function(err, mp_err, data){
var cId = data.result.customerId,
sale = util.basicSale(cId, true);

mpGateway.pay(
payData.client,
payData.order,
payData.creditCard,
function(err, mp_err, data){
test.ok(!err);
test.ok(!mp_err);
mpGateway.pay(
sale,
function(err, mp_err, data){
test.ok(!err);
test.ok(!mp_err);

test.equal(data.authCode, '123456');
test.equal(data.referenceNum, payData.order.code);
test.equal(data.errorMessage, '');
test.equal(data.responseCode, '0');
test.equal(data.responseMessage, 'CAPTURED');
test.equal(data.avsResponseCode, 'YYY');
test.equal(data.cvvResponseCode, 'M');
test.equal(data.processorCode, 'A');
test.equal(data.processorMessage, 'APPROVED');
test.equal(data.authCode, '123456');
test.equal(data.referenceNum, sale.referenceNum);
test.equal(data.errorMessage, '');
test.equal(data.responseCode, '0');
test.equal(data.responseMessage, 'CAPTURED');
test.equal(data.avsResponseCode, 'YYY');
test.equal(data.cvvResponseCode, 'M');
test.equal(data.processorCode, 'A');
test.equal(data.processorMessage, 'APPROVED');

test.ok(data['save-on-file'].hasOwnProperty('token'));
test.ok(data.hasOwnProperty('save-on-file'));
test.ok(data['save-on-file'].hasOwnProperty('token'));

test.ok(data.hasOwnProperty('orderID'));
test.ok(data.hasOwnProperty('transactionID'));
test.ok(data.hasOwnProperty('transactionTimestamp'));
test.ok(data.hasOwnProperty('orderID'));
test.ok(data.hasOwnProperty('transactionID'));
test.ok(data.hasOwnProperty('transactionTimestamp'));

test.done();
}
);
test.done();
}
);
});
};

exports.testPayFailed = function(test){
var payData = util.buildPayData(false);

test.expect(16);

mpGateway.pay(
payData.client,
payData.order,
payData.creditCard,
function(err, mp_err, data){
test.ok(!err);
test.ok(!mp_err);
var client = util.basicClient();
mpGateway.saveClient(client, function(err, mp_err, data){
var cId = data.result.customerId,
sale = util.basicSale(cId, false);

mpGateway.pay(
sale,
function(err, mp_err, data){
test.ok(!err);
test.ok(!mp_err);

test.equal(data.authCode, '');
test.equal(data.referenceNum, payData.order.code);
test.equal(data.errorMessage, '');
test.equal(data.responseCode, '1');
test.equal(data.responseMessage, 'DECLINED');
test.equal(data.avsResponseCode, 'NNN');
test.equal(data.cvvResponseCode, 'N');
test.equal(data.processorCode, 'D');
test.equal(data.processorMessage, 'DECLINED');
test.equal(data.authCode, '');
test.equal(data.referenceNum, sale.referenceNum);
test.equal(data.errorMessage, '');
test.equal(data.responseCode, '1');
test.equal(data.responseMessage, 'DECLINED');
test.equal(data.avsResponseCode, 'NNN');
test.equal(data.cvvResponseCode, 'N');
test.equal(data.processorCode, 'D');
test.equal(data.processorMessage, 'DECLINED');

test.ok(!data['save-on-file'].hasOwnProperty('token'));
test.equal(data['save-on-file'].error, 'transaction failed. card on file not done.');
test.ok(!data['save-on-file'].hasOwnProperty('token'));
test.equal(
data['save-on-file'].error,
'transaction failed. card on file not done.'
);

test.ok(data.hasOwnProperty('orderID'));
test.ok(data.hasOwnProperty('transactionID'));
test.ok(data.hasOwnProperty('transactionTimestamp'));
test.ok(data.hasOwnProperty('orderID'));
test.ok(data.hasOwnProperty('transactionID'));
test.ok(data.hasOwnProperty('transactionTimestamp'));

test.done();
}
);
test.done();
}
);
});
};
50 changes: 28 additions & 22 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ var _buildGateway = function(){
return new maxipago.Gateway(testMerchantId, testMerchantKey, true);
},
_basicClient = function() {
var time = new Date().getTime();
var now = new Date().getTime();
return {
customerIdExt: time,
customerIdExt: now,
firstName: "Fulano",
lastName: "De Tal " + time
lastName: "De Tal " + now
};
},
_fullClient = function() {
Expand All @@ -27,30 +27,36 @@ var _buildGateway = function(){
client.sex = 'M';
return client;
},
_buildPayData = function(forValidSale) {
var time = new Date().getTime();
_basicSale = function(customerId, forValidSale) {
var now = new Date().getTime();
return {
client: {
firstName: "Fulano",
lastName: "De Tal " + time,
customerIdExt: time,
processorID: '1',
referenceNum: "PONumber-" + now,
billing: {
name: "Test Credit Card"
},
order: {
total: forValidSale ? "10.00" : "15.33",
code: "PONumber-" + time
transactionDetail: {
payType: {
creditCard: {
number: "4111111111111111",
expMonth: "12",
expYear: "2020",
cvvNumber: "999",
}
}
},
creditCard: {
number: "4111111111111111",
expMonth: "12",
expYear: "2020",
cvvNumber: "999",
fullname: "Test Credit Card"
payment: {
currencyCode: 'BRL',
chargeTotal: forValidSale ? "10.00" : "15.33"
},
saveOnFile: {
customerToken: customerId
}
};
}
;

exports.buildGateway = _buildGateway,
exports.buildPayData = _buildPayData,
exports.basicClient = _basicClient,
exports.fullClient = _fullClient
exports.buildGateway = _buildGateway;
exports.basicClient = _basicClient;
exports.fullClient = _fullClient;
exports.basicSale = _basicSale;

0 comments on commit e0053ff

Please sign in to comment.