Skip to content

Commit b6af1ee

Browse files
committed
Fix, support api defined by 42-cent-models
1 parent 54fb131 commit b6af1ee

File tree

4 files changed

+346
-269
lines changed

4 files changed

+346
-269
lines changed

lib/AuthorizeNetGateway.js

+118-102
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ var util = require('util');
33
var BaseGateway = require('42-cent-base').BaseGateway;
44
var mapKeys = require('42-cent-util').mapKeys;
55
var GatewayError = require('42-cent-base').GatewayError;
6-
var Promise = require('bluebird');
6+
var P = require('bluebird');
77
var request = require('request');
8-
var toJson = Promise.promisify(require('xml2js').parseString);
8+
var toJson = P.promisify(require('xml2js').parseString);
99
var xml2js = require('xml2js');
10-
var post = Promise.promisify(request.post);
10+
var post = P.promisify(request.post);
1111
var schemas = require('./schemas.js');
1212

1313
var billToSchema = schemas.billing;
@@ -50,34 +50,26 @@ function setRequest (service, rootNodeName, requestNode) {
5050

5151
requestObject[rootNodeName][requestNode.key] = requestNode.value;
5252

53-
//var objarray2 = xmlBuilder.buildObject(requestObject);
54-
//objarray(requestObject);
55-
56-
//objarray2[0][rootNodeName].unshift({
57-
// _attr: {
58-
// "xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
59-
// "xmlns:xsd": "http://www.w3.org/2001/XMLSchema",
60-
// "xmlns": "AnetApi/xml/v1/schema/AnetApiSchema.xsd"
61-
// }
62-
//});
63-
6453
return requestObject;
6554

6655
}
6756

68-
function sendXmlifiedRequest (service, request) {
69-
70-
var builder = new xml2js.Builder();
71-
72-
var xmlContent = builder.buildObject(request);
73-
74-
return post(service.endpoint, {
75-
headers: {
76-
'Content-Type': 'application/xml',
77-
'Content-Length': xmlContent.length
78-
},
79-
body: xmlContent
80-
});
57+
function sendXmlifiedRequest (service) {
58+
59+
return function (request) {
60+
return P.resolve()
61+
.then(function () {
62+
var builder = new xml2js.Builder();
63+
var xmlContent = builder.buildObject(request);
64+
return post(service.endpoint, {
65+
headers: {
66+
'Content-Type': 'application/xml',
67+
'Content-Length': xmlContent.length
68+
},
69+
body: xmlContent
70+
})
71+
});
72+
}
8173
}
8274

8375
function createJsonCallback (cb) {
@@ -97,12 +89,17 @@ function createJsonCallback (cb) {
9789
}
9890

9991
AuthorizeNetGateway.prototype.sendTransactionRequest = function setTransactionRequest (body, transactionCb) {
100-
var obj = setRequest(this, 'createTransactionRequest', {
101-
key: 'transactionRequest',
102-
value: body
103-
});
10492

105-
return sendXmlifiedRequest(this, obj)
93+
var service = this;
94+
95+
return P.resolve()
96+
.then(function () {
97+
return setRequest(service, 'createTransactionRequest', {
98+
key: 'transactionRequest',
99+
value: body
100+
});
101+
})
102+
.then(sendXmlifiedRequest(service))
106103
.spread(createJsonCallback(function (json) {
107104
if (json.createTransactionResponse) {
108105

@@ -288,7 +285,7 @@ AuthorizeNetGateway.prototype.refundTransaction = function refundTransaction (tr
288285
var opt = options || {};
289286

290287
if (!opt.expirationMonth || !opt.expirationYear) {
291-
return Promise.reject(new Error('expirationMonth and expirationYear must be provided in the options object'));
288+
return P.reject(new Error('expirationMonth and expirationYear must be provided in the options object'));
292289
}
293290

294291
//fetch missing info if required
@@ -299,7 +296,7 @@ AuthorizeNetGateway.prototype.refundTransaction = function refundTransaction (tr
299296
opt.creditCardNumber = opt.creditCardNumber || res.payment[0].creditCard[0].cardNumber[0];
300297
return opt;
301298
}) :
302-
Promise.resolve(opt);
299+
P.resolve(opt);
303300

304301

305302
return fullOptions.then(function (fullOpt) {
@@ -333,17 +330,21 @@ AuthorizeNetGateway.prototype.refundTransaction = function refundTransaction (tr
333330

334331
AuthorizeNetGateway.prototype.getTransactionList = function getTransactionList (batchId) {
335332

336-
var obj = setRequest(this, 'getTransactionListRequest', {
337-
key: 'batchId',
338-
value: batchId
339-
});
333+
var service = this;
340334

341-
return sendXmlifiedRequest(this, obj)
335+
return P.resolve()
336+
.then(function () {
337+
return setRequest(service, 'createTransactionRequest', {
338+
key: 'batchId',
339+
value: batchId
340+
});
341+
})
342+
.then(sendXmlifiedRequest(service))
342343
.spread(createJsonCallback(function (json) {
343344
var transactions;
344345
if (json.getTransactionListResponse) {
345346

346-
if (json.getTransactionListResponse.messages.resultCode === 'Error') {
347+
if (json.getTransactionListResponse.messages[0].resultCode[0] === 'Error') {
347348
throw new GatewayError('some error from the gateway', json.getTransactionListResponse);
348349
}
349350

@@ -359,20 +360,25 @@ AuthorizeNetGateway.prototype.getTransactionList = function getTransactionList (
359360
};
360361

361362
AuthorizeNetGateway.prototype.getTransactionDetails = function getTransactionDetails (transId) {
362-
var obj = setRequest(this, 'getTransactionDetailsRequest', {
363-
key: 'transId',
364-
value: transId
365-
});
366363

367-
return sendXmlifiedRequest(this, obj)
364+
var service = this;
365+
366+
return P.resolve()
367+
.then(function () {
368+
return setRequest(service, 'getTransactionDetailsRequest', {
369+
key: 'transId',
370+
value: transId
371+
});
372+
})
373+
.then(sendXmlifiedRequest(service))
368374
.spread(createJsonCallback(function (json) {
369375
if (json.getTransactionDetailsResponse) {
370376

371-
if (json.getTransactionDetailsResponse.messages.resultCode === 'Error') {
377+
if (json.getTransactionDetailsResponse.messages[0].resultCode[0] === 'Error') {
372378
throw new GatewayError('some error from the gateway', json.getTransactionDetailsResponse);
373379
}
374380

375-
return json.getTransactionDetailsResponse.transaction;
381+
return json.getTransactionDetailsResponse.transaction[0];
376382

377383
} else {
378384
throw new Error('Can not parse answer from gateway');
@@ -401,31 +407,37 @@ AuthorizeNetGateway.prototype.voidTransaction = function voidTransaction (transa
401407
*/
402408
AuthorizeNetGateway.prototype.createCustomerProfile = function (payment, billing, shipping, options) {
403409

404-
options = options || {};
410+
var service = this;
405411

406-
var body = {
407-
merchantCustomerId: options.merchantCustomerId || '',
408-
description: options.description || '',
409-
email: billing.billingEmailAddress,
410-
paymentProfiles: {
411-
payment: {
412-
creditCard: {
413-
cardNumber: payment.creditCardNumber,
414-
expirationDate: [payment.expirationYear.toString(), payment.expirationMonth.toString()].join('-'),
415-
cardCode: payment.cvv2
412+
return P.resolve()
413+
.then(function () {
414+
options = options || {};
415+
416+
var body = {
417+
merchantCustomerId: options.merchantCustomerId || '',
418+
description: options.description || '',
419+
email: billing.billingEmailAddress,
420+
paymentProfiles: {
421+
payment: {
422+
creditCard: {
423+
cardNumber: payment.creditCardNumber,
424+
expirationDate: [payment.expirationYear.toString(), payment.expirationMonth.toString()].join('-'),
425+
cardCode: payment.cvv2
426+
}
427+
}
416428
}
417-
}
418-
}
419-
};
429+
};
420430

421-
var obj = setRequest(this, 'createCustomerProfileRequest', {
422-
key: 'profile',
423-
value: body
424-
});
431+
var obj = setRequest(service, 'createCustomerProfileRequest', {
432+
key: 'profile',
433+
value: body
434+
});
425435

426-
obj.createCustomerProfileRequest.validationMode = this.testMode ? 'testMode' : 'none';
436+
obj.createCustomerProfileRequest.validationMode = this.testMode ? 'testMode' : 'none';
427437

428-
return sendXmlifiedRequest(this, obj)
438+
return obj;
439+
})
440+
.then(sendXmlifiedRequest(service))
429441
.spread(createJsonCallback(function (json) {
430442

431443
var errors;
@@ -499,9 +511,12 @@ AuthorizeNetGateway.prototype.chargeCustomer = function (order, prospect, other)
499511
*/
500512
AuthorizeNetGateway.prototype.getCustomerProfile = function getCustomerProfile (profileId) {
501513

502-
var req = setRequest(this, 'getCustomerProfileRequest', {key: 'customerProfileId', value: profileId});
503-
504-
return sendXmlifiedRequest(this, req)
514+
var service = this;
515+
return P.resolve()
516+
.then(function () {
517+
return setRequest(service, 'getCustomerProfileRequest', {key: 'customerProfileId', value: profileId});
518+
})
519+
.then(sendXmlifiedRequest(service))
505520
.spread(createJsonCallback(function (json) {
506521
if (json.getCustomerProfileResponse) {
507522

@@ -551,38 +566,41 @@ AuthorizeNetGateway.prototype.createSubscription = function createSubscription (
551566
return yearString + '-' + monthString + '-' + dayString;
552567
}
553568

554-
var expirationYear = cc.expirationYear.toString().length === 4 ? cc.expirationYear.toString().substr(-2) : cc.expirationYear.toString();
555-
var expirationMonth = cc.expirationMonth.toString().length === 2 ? cc.expirationMonth.toString() : '0' + cc.expirationMonth.toString();
556-
var startDate = new Date(subscriptionPlan.startingDate);
557-
558-
var body = {
559-
paymentSchedule: {
560-
interval: {
561-
length: subscriptionPlan.periodLength || 1,
562-
unit: subscriptionPlan.periodUnit || 'months'
563-
},
564-
startDate: formatString(startDate),
565-
totalOccurrences: subscriptionPlan.iterationCount,
566-
trialOccurrences: subscriptionPlan.trialCount || 0
567-
},
568-
amount: subscriptionPlan.amount,
569-
trialAmount: subscriptionPlan.trialAmount || 0,
570-
payment: {
571-
creditCard: {
572-
cardNumber: cc.creditCardNumber,
573-
expirationDate: expirationMonth + expirationYear,
574-
cardCode: cc.cvv
575-
}
576-
},
577-
billTo: mapKeys(prospect, billToSchema)
578-
};
569+
var service = this;
579570

580-
var obj = setRequest(this, 'ARBCreateSubscriptionRequest', {
581-
key: 'subscription',
582-
value: body
583-
});
571+
return P.resolve()
572+
.then(function () {
573+
var expirationYear = cc.expirationYear.toString().length === 4 ? cc.expirationYear.toString().substr(-2) : cc.expirationYear.toString();
574+
var expirationMonth = cc.expirationMonth.toString().length === 2 ? cc.expirationMonth.toString() : '0' + cc.expirationMonth.toString();
575+
var startDate = new Date(subscriptionPlan.startingDate);
576+
var body = {
577+
paymentSchedule: {
578+
interval: {
579+
length: subscriptionPlan.periodLength || 1,
580+
unit: subscriptionPlan.periodUnit || 'months'
581+
},
582+
startDate: formatString(startDate),
583+
totalOccurrences: subscriptionPlan.iterationCount,
584+
trialOccurrences: subscriptionPlan.trialCount || 0
585+
},
586+
amount: subscriptionPlan.amount,
587+
trialAmount: subscriptionPlan.trialAmount || 0,
588+
payment: {
589+
creditCard: {
590+
cardNumber: cc.creditCardNumber,
591+
expirationDate: expirationMonth + expirationYear,
592+
cardCode: cc.cvv2
593+
}
594+
},
595+
billTo: mapKeys(prospect, billToSchema)
596+
};
584597

585-
return sendXmlifiedRequest(this, obj)
598+
return setRequest(service, 'ARBCreateSubscriptionRequest', {
599+
key: 'subscription',
600+
value: body
601+
});
602+
})
603+
.then(sendXmlifiedRequest(service))
586604
.spread(createJsonCallback(function (json) {
587605
if (json.ARBCreateSubscriptionResponse) {
588606

@@ -605,8 +623,6 @@ AuthorizeNetGateway.prototype.createSubscription = function createSubscription (
605623
} else {
606624
throw new Error('Can not parse the answer from the gateway');
607625
}
608-
609-
610626
}));
611627
};
612628

lib/schemas.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
module.exports = {
22
billing: {
3-
customerFirstName: 'firstName',
4-
customerLastName: 'lastName',
5-
billingAddress: 'address',
3+
billingFirstName: 'firstName',
4+
billingLastName: 'lastName',
5+
billingAddress1: 'address',
66
billingCity: 'city',
77
billingState: 'state',
8-
billingZip: 'zip',
8+
billingPostalCode: 'zip',
99
billingCountry: 'country'
1010
},
1111
shipping: {
1212
shippingFirstName: 'firstName',
1313
shippingLastName: 'lastName',
14-
shippingAddress: 'address',
14+
shippingAddress1: 'address',
1515
shippingCity: 'city',
1616
shippingState: 'state',
17-
shippingZip: 'zip',
17+
shippingPostalCode: 'zip',
1818
shippingCountry: 'country'
1919
}
2020
};

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "authorize-net",
3-
"version": "1.0.5",
3+
"version": "1.0.6",
44
"description": "Node.js SDK for Authorize.net paymenet gateway.",
55
"main": "index.js",
66
"scripts": {
@@ -26,7 +26,8 @@
2626
"xml2js": "^0.4.6"
2727
},
2828
"devDependencies": {
29-
"42-cent-model": "^1.0.0",
29+
"42-cent-model": "^2.0.0",
30+
"casual": "^1.4.7",
3031
"mocha": "^1.21.4"
3132
}
3233
}

0 commit comments

Comments
 (0)