Skip to content
This repository was archived by the owner on Oct 1, 2019. It is now read-only.

Commit 42ca6ad

Browse files
committed
first commit
1 parent 4f56bb2 commit 42ca6ad

File tree

7 files changed

+224
-0
lines changed

7 files changed

+224
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea
2+
node_modules

.travis.yml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
language: node_js
2+
node_js:
3+
- '0.10'
4+
before_script:
5+
- npm install
6+
script:
7+
- npm test
8+
notifications:
9+
hipchat:
10+
rooms:
11+
secure: CXlLv+G1XdrSc2+v7cV/Keb6JRvS7enLQfgZweweuvEBeCYLDrlB8Ca+piQJpVGEcg4qhsDPRRjXEsaO6pvNm1q/6PzT5nYnYaLBp5jNKuIV4n8OPq2NMIIyYHxL3f86zKKikOL2mOZQscVYq6gjLXT6pSWPNjsM/n3lfGPtUDc=

config.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
username: 'demo',
3+
password: 'password'
4+
};

index.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
var Gateway = require('./lib/NMIGateway.js');
2+
module.exports = function (config) {
3+
return new Gateway(config);
4+
}

lib/NMIGateway.js

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
var utils = require('util');
2+
var assert = require('assert');
3+
var base = require('42-cent-base').BaseGateway;
4+
var Promise = require('bluebird');
5+
var request = require('request');
6+
var mapKeys = require('42-cent-base').mapKeys;
7+
var qs = require('query-string');
8+
var GatewayError = require('42-cent-base').GatewayError;
9+
10+
var schema = {
11+
amount: 'amount',
12+
creditCardNumber: 'ccnumber',
13+
cvv: 'cvv',
14+
customerFirstName: 'firstname',
15+
customerLastName: 'lastname',
16+
customerEmail: 'email',
17+
billingAddress: 'address1',
18+
billingCity: 'city',
19+
billingState: 'state',
20+
billingZip: 'zip',
21+
billingCountry: 'country',
22+
shippingFirstName: 'shipping_firstname',
23+
shippingLastName: 'shipping_lastname',
24+
shippingAddress: 'shipping_address1',
25+
shippingCity: 'shipping_city',
26+
shippingState: 'shipping_state',
27+
shippingZip: 'shipping_zip',
28+
shippingCountry: 'shipping_country'
29+
};
30+
31+
function NMIGateway(config) {
32+
33+
assert(config.username, 'username must be defined');
34+
assert(config.password, 'password must be defined');
35+
36+
this.endpoint = 'https://secure.nmi.com/api/transact.php';
37+
38+
utils._extend(this, config);
39+
}
40+
41+
42+
function postRequest(params, service) {
43+
44+
var post = Promise.promisify(request.post);
45+
46+
params.username = service.username;
47+
params.password = service.password;
48+
49+
return post(service.endpoint, {formData: params}).then(function (result) {
50+
return qs.parse('?' + result[1]);
51+
});
52+
}
53+
54+
utils.inherits(NMIGateway, base);
55+
56+
57+
NMIGateway.prototype.submitTransaction = function submitTransaction(order, creditCard, prospect, other) {
58+
59+
var params = {};
60+
61+
if (other) {
62+
console.log('other field is not supported');
63+
}
64+
65+
utils._extend(params, order);
66+
utils._extend(params, creditCard);
67+
utils._extend(params, prospect);
68+
69+
params = mapKeys(params, schema);
70+
71+
params.type = 'sale';
72+
params.ccexp = creditCard.expirationMonth.toString() + creditCard.expirationYear.toString();
73+
return postRequest(params, this).then(function (result) {
74+
75+
if (result.response !== '1') {
76+
throw new GatewayError(result.responsetext, result);
77+
}
78+
79+
return {
80+
transactionId: result.transactionid,
81+
_original: result,
82+
authCode: result.authcode
83+
}
84+
});
85+
86+
};
87+
88+
module.exports = NMIGateway;

package.json

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "42-cent-nmi",
3+
"version": "0.1.0",
4+
"description": "42-cent adaptor for nmi payment gateway",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "NODE_ENV='test' node ./node_modules/.bin/mocha ./test/ -R spec -t 35000"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "https://github.com/continuous-software/42-cent-nmi.git"
12+
},
13+
"keywords": [
14+
"nmi",
15+
"payment",
16+
"gateway",
17+
"42-cent"
18+
],
19+
"author": "Laurent Renard",
20+
"license": "MIT",
21+
"devDependencies": {
22+
"mocha": "^2.0.1"
23+
},
24+
"dependencies": {
25+
"42-cent-base": "^0.3.0",
26+
"bluebird": "^2.3.6",
27+
"query-string": "^1.0.0",
28+
"request": "^2.47.0",
29+
"require": "^2.4.17"
30+
}
31+
}

test/test.js

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
var config = require('../config.js');
2+
var factory = require('../index.js');
3+
var assert = require('assert');
4+
var GatewayError = require('42-cent-base').GatewayError;
5+
6+
describe('NMI adpator', function () {
7+
8+
var service;
9+
10+
beforeEach(function () {
11+
service = factory(config);
12+
});
13+
14+
it('should submit transaction ', function (done) {
15+
var order = {
16+
amount: Math.random() * 100 + 1
17+
};
18+
19+
var cc = {
20+
creditCardNumber: '4111111111111111',
21+
expirationYear: '17',
22+
expirationMonth: '01',
23+
cvv: '123'
24+
};
25+
var prospect = {
26+
customerFirstName: 'Ellen',
27+
customerLastName: 'Johson',
28+
billingAddress: '14 Main Street',
29+
billingCity: 'Pecan Springs',
30+
billingZip: '44628',
31+
billingState: 'TX',
32+
shippingFirstName: 'China',
33+
shippingLastName: 'Bayles',
34+
shippingAddress: '12 Main Street',
35+
shippingCity: 'Pecan Springs',
36+
shippingZip: '44628'
37+
};
38+
39+
service.submitTransaction(order, cc, prospect)
40+
.then(function (result) {
41+
assert(result.transactionId, 'transaction should be defined');
42+
assert(result.authCode, 'authCode should be defined');
43+
assert(result._original, '_original should be defined');
44+
done();
45+
});
46+
});
47+
48+
it('should reject the promise if gateway does not return appropriate result', function (done) {
49+
var order = {
50+
amount: Math.random() * 100 + 1
51+
};
52+
53+
var cc = {
54+
creditCardNumber: '666',
55+
expirationYear: '17',
56+
expirationMonth: '01',
57+
cvv: '123'
58+
};
59+
var prospect = {
60+
customerFirstName: 'Ellen',
61+
customerLastName: 'Johson',
62+
billingAddress: '14 Main Street',
63+
billingCity: 'Pecan Springs',
64+
billingZip: '44628',
65+
billingState: 'TX',
66+
shippingFirstName: 'China',
67+
shippingLastName: 'Bayles',
68+
shippingAddress: '12 Main Street',
69+
shippingCity: 'Pecan Springs',
70+
shippingZip: '44628'
71+
};
72+
73+
service.submitTransaction(order, cc, prospect)
74+
.then(function () {
75+
throw new Error('should not get here')
76+
}, function (err) {
77+
assert(err instanceof GatewayError);
78+
assert(err.message.indexOf('Invalid Credit Card Number') !== -1);
79+
assert(err._original, '_original should be defined');
80+
done();
81+
});
82+
});
83+
84+
});

0 commit comments

Comments
 (0)